grafika_komputerowa/grk/project/Enemy.h

83 lines
1.8 KiB
C
Raw Normal View History

2024-01-22 00:45:08 +01:00
#include "glm.hpp"
#include "ext.hpp"
#include "src/Render_Utils.h"
#include "Bullet.h"
#include "Spaceship.h"
#pragma once
class Enemy
{
private:
std::list<Bullet*> bullets;
float lastShootTime = 0.f;
float shootInterval = 0.3f;
public:
glm::mat4 modelMatrix;
int hp;
2024-02-01 15:11:52 +01:00
int initialHp;
2024-01-22 00:45:08 +01:00
int dmg;
float aggroRange;
2024-02-01 15:11:52 +01:00
Enemy(int currHp,int initialHp, glm::mat4 initialModelMatrix, int initialDmg, float initialAggroRange)
: hp(currHp), initialHp(initialHp), modelMatrix(initialModelMatrix), dmg(initialDmg), aggroRange(initialAggroRange)
2024-01-22 00:45:08 +01:00
{}
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) {
Spaceship* spaceship = Spaceship::getInstance();
glm::vec3 bulletDirection = glm::normalize(spaceship->spaceshipPos - glm::vec3(modelMatrix[3]));
this->bullets.push_back(Bullet::createSimpleBullet(bulletDirection, this->modelMatrix[3], time));
2024-01-22 00:45:08 +01:00
this->lastShootTime = time;
}
};