grafika_komputerowa/grk/project/Enemy.h

96 lines
2.3 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;
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<74><61> pozycj<63> 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<74><61> kierunek strza<7A>u do przestrzeni <20>wiata, bior<6F>c pod uwag<61> macierz modelu
glm::vec3 worldBulletDirection = glm::mat3(this->modelMatrix) * localBulletDirection;
// Dodaj nowy pocisk do listy pocisk<73>w
this->bullets.push_back(Bullet::createSimpleBullet(worldBulletDirection, this->modelMatrix[3], time));
this->lastShootTime = time;
}
};