grafika_komputerowa/grk/project/Enemy.h

109 lines
2.7 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;
float initAggroRange;
2024-01-22 00:45:08 +01:00
public:
glm::mat4 modelMatrix;
float aggroRange;
glm::mat4 initialModelMatrix;
2024-01-22 00:45:08 +01:00
2024-02-03 20:20:01 +01:00
Enemy(float currHp,float initialHp, glm::mat4 initialModelMatrix, float initialDmg, float initialAggroRange)
:
aggroRange(initialAggroRange),
modelMatrix(initialModelMatrix),
2024-02-07 23:44:53 +01:00
initialModelMatrix(initialModelMatrix),
initAggroRange(initialAggroRange),
2024-02-03 20:20:01 +01:00
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-02-07 23:44:53 +01:00
void respawn() override {
this->currentHP = this->maxHP;
this->modelMatrix = this->initialModelMatrix;
this->aggroRange = this->initAggroRange;
this->dmg = this->initDMG;
2024-02-07 23:44:53 +01:00
return;
}
2024-02-08 04:44:22 +01:00
void heal() override {
this->currentHP = this->currentHP + 5.0f;
}
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();
2024-02-07 23:44:53 +01:00
//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]));
2024-02-03 21:50:55 +01:00
//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));
2024-01-22 00:45:08 +01:00
this->lastShootTime = time;
}
};