grafika_komputerowa/grk/project/Enemy.h

94 lines
2.1 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"
2024-02-03 20:20:01 +01:00
#include "./GameEntity.h"
2024-01-22 00:45:08 +01:00
#include "Spaceship.h"
#pragma once
2024-02-03 20:20:01 +01:00
class Enemy : public GameEntity
2024-01-22 00:45:08 +01:00
{
private:
std::list<Bullet*> bullets;
float lastShootTime = 0.f;
float shootInterval = 0.3f;
public:
glm::mat4 modelMatrix;
float aggroRange;
2024-02-03 20:20:01 +01:00
Enemy(float currHp,float initialHp, glm::mat4 initialModelMatrix, float initialDmg, float initialAggroRange)
:
aggroRange(initialAggroRange),
modelMatrix(initialModelMatrix),
GameEntity(currHp, initialHp, initialDmg)
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 {
2024-02-03 20:20:01 +01:00
if (this->currentHP <= 0) {
2024-01-22 00:45:08 +01:00
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;
}
2024-02-03 20:20:01 +01:00
void renderBullets(float time, GLuint program, std::vector<GameEntity*>& gameEntities) {
2024-01-22 00:45:08 +01:00
for (auto it = bullets.begin(); it != bullets.end();) {
Bullet* bullet = *it;
2024-02-03 20:20:01 +01:00
if (bullet->shouldBeDestroyed(time, program, gameEntities, dmg)) {
2024-01-22 00:45:08 +01:00
delete bullet;
it = bullets.erase(it);
}
else {
it++;
}
}
}
2024-02-03 20:20:01 +01:00
glm::vec3 getPosition() const override
{
return modelMatrix[3];
}
glm::mat4 getModelMatrix() override
{
return modelMatrix;
}
2024-01-22 00:45:08 +01:00
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]));
2024-02-03 20:20:01 +01:00
this->bullets.push_back(Bullet::createSimpleBullet(bulletDirection, this->modelMatrix[3], time));
2024-01-22 00:45:08 +01:00
this->lastShootTime = time;
}
};