grafika_komputerowa/grk/project/Enemy.h

101 lines
2.5 KiB
C++

#include "glm.hpp"
#include "ext.hpp"
#include "src/Render_Utils.h"
#include "Bullet.h"
#include "./GameEntity.h"
#include "Spaceship.h"
#pragma once
class Enemy : public GameEntity
{
private:
std::list<Bullet*> bullets;
float lastShootTime = 0.f;
float shootInterval = 0.3f;
glm::mat4 initialModelMatrix;
public:
glm::mat4 modelMatrix;
float aggroRange;
Enemy(float currHp,float initialHp, glm::mat4 initialModelMatrix, float initialDmg, float initialAggroRange)
:
aggroRange(initialAggroRange),
modelMatrix(initialModelMatrix),
initialModelMatrix(initialModelMatrix),
GameEntity(currHp, initialHp, initialDmg)
{}
virtual ~Enemy() = default;
virtual void attack(const glm::vec3& shipPos, float time) {
if (isInAggroRange(shipPos)) {
requestShoot(time);
}
}
virtual bool isAlive() const {
if (this->currentHP <= 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, std::vector<GameEntity*>& gameEntities) {
for (auto it = bullets.begin(); it != bullets.end();) {
Bullet* bullet = *it;
if (bullet->shouldBeDestroyed(time, program, gameEntities, dmg)) {
delete bullet;
it = bullets.erase(it);
}
else {
it++;
}
}
}
glm::vec3 getPosition() const override
{
return modelMatrix[3];
}
glm::mat4 getModelMatrix() override
{
return modelMatrix;
}
void respawn() override {
this->currentHP = this->maxHP;
this->modelMatrix = this->initialModelMatrix;
return;
}
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]));
glm::vec3 bulletDirection = glm::normalize(glm::vec3(spaceship->getModelMatrix()[3]) - glm::vec3(modelMatrix[3]));
//bulletDirection += glm::linearRand(glm::vec3(-0.01f), glm::vec3(0.1f)); // Modyfikacja kierunku o losowy szum
this->bullets.push_back(Bullet::createSimpleBullet(bulletDirection, this->modelMatrix[3], time));
this->lastShootTime = time;
}
};