#include "glm.hpp" #include "ext.hpp" #include "src/Render_Utils.h" #include "Bullet.h" #include "Spaceship.h" #pragma once class Enemy { private: std::list bullets; float lastShootTime = 0.f; float shootInterval = 0.3f; public: glm::mat4 modelMatrix; int hp; int dmg; float aggroRange; Enemy(int initialHp, glm::mat4 initialModelMatrix, int initialDmg, float initialAggroRange) : hp(initialHp), modelMatrix(initialModelMatrix), dmg(initialDmg), aggroRange(initialAggroRange) {} virtual ~Enemy() = default; virtual void attack(const glm::vec3& shipPos, float time) { if (isInAggroRange(shipPos)) { requestShoot(time); } } virtual bool isAlive() const { if (this->hp < 0) { return false; } return true; } virtual bool isInAggroRange(const glm::vec3& shipPos) const { float distanceFromShip = glm::length(shipPos - glm::vec3(this->modelMatrix[3])); if (distanceFromShip > this->aggroRange) { return false; } return true; } void renderBullets(float time, GLuint program) { for (auto it = bullets.begin(); it != bullets.end();) { Bullet* bullet = *it; if (bullet->shouldBeDestroyed(time, program)) { delete bullet; it = bullets.erase(it); } else { it++; } } } private: void requestShoot(float time) { if (canShoot(time)) { shoot(time); } } bool canShoot(float time) { return this->lastShootTime == 0 || time - this->lastShootTime > this->shootInterval; } void shoot(float time) { glm::vec3 spaceshipPos = Spaceship::getInstance()->spaceshipPos; // Przekształć pozycję statku do przestrzeni lokalnej obiektu Enemy glm::vec3 localSpaceshipPos = glm::inverse(glm::mat3(this->modelMatrix)) * (spaceshipPos - glm::vec3(this->modelMatrix[3])); // Uzyskaj kierunek od obecnej pozycji obiektu Enemy do pozycji statku glm::vec3 localBulletDirection = glm::normalize(localSpaceshipPos); // Przekształć kierunek strzału do przestrzeni świata, biorąc pod uwagę macierz modelu glm::vec3 worldBulletDirection = glm::mat3(this->modelMatrix) * localBulletDirection; // Dodaj nowy pocisk do listy pocisków this->bullets.push_back(Bullet::createSimpleBullet(worldBulletDirection, this->modelMatrix[3], time)); this->lastShootTime = time; } };