#include "pch.h" #include "Enemy.h" #include "Game.h" #include "Texture.h" const float Enemy::m_ClipWidth{ 80.f }; const float Enemy::m_ClipHeight{ 80.f }; const Vector2f Enemy::m_GravityAcceleration{ 0, -981.0f * 2 }; Enemy::Enemy(int x, int y) : Enemy(Point2f{ x * Tile::m_Size, y * Tile::m_Size }) { } Enemy::Enemy(const Point2f& pos) : m_Hitbox{ pos.x, pos.y, 0, 0 } , m_SrcRect{ 0, 0, m_ClipWidth, m_ClipHeight } , m_Velocity{ } , m_Angle{ } , m_SpriteRect{ 0, 0, 80, 80 } , m_Direction{ Direction::RIGHT } , m_AnimFirstFrameId{ 0 } , m_AnimLength{ 10 } , m_AnimLoop{ true } , m_NrFramesPerSec{ 20 } , m_AnimTime{ } , m_AnimFrame{ } , m_Lives{ 1 } { } int Enemy::GetLives() const { return m_Lives; } Enemy::~Enemy() { m_pTexture = nullptr; } std::vector<Point2f> Enemy::GetTopVertices() const { std::vector<Point2f> topVertices{ Point2f{ m_Hitbox.left, m_Hitbox.bottom + m_Hitbox.height }, Point2f{ m_Hitbox.left + m_Hitbox.width, m_Hitbox.bottom + m_Hitbox.height } }; return topVertices; } void Enemy::Draw() const { if (Game::DrawDebug) { //draw hitbox utils::SetColor(Color4f{ 1, 0, 0, 0.5f }); utils::FillRect(m_Hitbox); } Rectf srcRect{ (m_ClipWidth * (m_AnimFirstFrameId % 11)) + (m_ClipWidth * m_AnimFrame), m_ClipHeight + (m_ClipHeight * (m_AnimFirstFrameId / 11)), m_ClipWidth, m_ClipHeight }; Rectf destRect{ -m_SpriteRect.width / 2, 0, m_SpriteRect.width, m_SpriteRect.height }; glPushMatrix(); glTranslatef(m_SpriteRect.left - destRect.left, m_SpriteRect.bottom, 0); if (m_Direction == Direction::LEFT) { glScalef(-1, 1, 1); } //draw enemy m_pTexture->Draw(destRect, srcRect); glPopMatrix(); } void Enemy::CenterInTile() { m_Hitbox.left += (Tile::m_Size - m_Hitbox.width) / 2; } Rectf& Enemy::GetHitbox() { return m_Hitbox; }