Add constant enemies respawn when half of them is defeated

This commit is contained in:
s473577 2024-02-08 00:23:07 +01:00
parent 6737f14356
commit 23b1f0c124
3 changed files with 81 additions and 57 deletions

View File

@ -12,17 +12,20 @@ private:
std::list<Bullet*> bullets;
float lastShootTime = 0.f;
float shootInterval = 0.3f;
glm::mat4 initialModelMatrix;
float initAggroRange;
public:
glm::mat4 modelMatrix;
float aggroRange;
glm::mat4 initialModelMatrix;
Enemy(float currHp,float initialHp, glm::mat4 initialModelMatrix, float initialDmg, float initialAggroRange)
:
aggroRange(initialAggroRange),
modelMatrix(initialModelMatrix),
initialModelMatrix(initialModelMatrix),
initAggroRange(initialAggroRange),
GameEntity(currHp, initialHp, initialDmg)
{}
@ -75,6 +78,8 @@ public:
void respawn() override {
this->currentHP = this->maxHP;
this->modelMatrix = this->initialModelMatrix;
this->aggroRange = this->initAggroRange;
this->dmg = this->initDMG;
return;
}
private:

View File

@ -7,9 +7,10 @@ public:
float currentHP;
float dmg;
float maxHP;
float initDMG;
GameEntity(float currentHP, float maxHP, float initialDmg)
: currentHP(currentHP), maxHP(maxHP), dmg(initialDmg)
: currentHP(currentHP), maxHP(maxHP), dmg(initialDmg),initDMG(initialDmg)
{
}

View File

@ -95,6 +95,17 @@ Spaceship* spaceship = Spaceship::getInstance();
void createSolarSystem(glm::vec3 sunPos, GLuint sunTexId,float sunScale, float* planetSizes, int numberOfPlanets, float planetsDistance, float planetSpeedCoef);
Core::SpriteRenderer* spriteRenderer;
const int ENEMY_COUNT = 20;
std::vector<glm::vec3> referencePoints = {
glm::vec3(0, 2, 0),
glm::vec3(150, 5, 0),
glm::vec3(-20, -30, 50),
glm::vec3(100, 20, -50),
glm::vec3(0, 52, 200),
glm::vec3(150, 55, 200),
glm::vec3(-20, 20, 250),
glm::vec3(100, 70, 150)
};
void updateDeltaTime(float time) {
if (lastTime < 0) {
lastTime = time;
@ -139,8 +150,67 @@ void renderHUD(GLFWwindow* window) {
programSpriteBar);
glEnable(GL_DEPTH_TEST);
}
bool isFarFromReferencePoints(float x, float y, float z, const std::vector<glm::vec3>& referencePoints) {
float minDistance = 10.0f;
glm::vec3 currentPoint(x, y, z);
auto checkDistance = [currentPoint, minDistance](const glm::vec3& referencePoint) {
return glm::distance(currentPoint, referencePoint) > minDistance;
};
for (const auto& referencePoint : referencePoints) {
if (!checkDistance(referencePoint)) {
return false;
}
}
return true;
}
glm::mat4 generateRandomMatrix(const glm::mat4& startMatrix) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<float> disX(-30.0f, 180.0f);
std::uniform_real_distribution<float> disY(-50.0f, 60.0f);
std::uniform_real_distribution<float> disZ(-30.0f, 210.0f);
float randomX, randomY, randomZ;
do {
randomX = disX(gen);
randomY = disY(gen);
randomZ = disZ(gen);
} while (!isFarFromReferencePoints(randomX, randomY, randomZ, referencePoints));
glm::mat4 randomModelMatrix = glm::translate(startMatrix, glm::vec3(randomX, randomY, randomZ));
return randomModelMatrix;
}
void renderEnemies() {
int counter = 0;
glUseProgram(program);
for (const auto& enemy : enemies) {
if (enemy->isAlive()) {
enemy->attack(spaceship->spaceshipPos, glfwGetTime());
enemy->renderBullets(glfwGetTime(), program, gameEntities);
}
else {
counter++;
}
}
if (counter >= ENEMY_COUNT / 2) {
for (const auto& enemy : enemies) {
if (!enemy->isAlive()) {
enemy->initialModelMatrix = generateRandomMatrix(enemy->getModelMatrix());
enemy->respawn();
enemy->aggroRange *= 1.1;
enemy->dmg *= 1.2;
}
}
counter = 0;
}
glUseProgram(programSpriteBar);
for (const auto& enemy : enemies) {
if (enemy->isAlive()) {
@ -163,15 +233,6 @@ void renderEnemies() {
}
}
glUseProgram(program);
for (const auto& enemy : enemies) {
if (enemy->isAlive()) {
enemy->attack(spaceship->spaceshipPos, glfwGetTime());
enemy->renderBullets(glfwGetTime(), program, gameEntities);
}
}
}
TextureTuple getRandomPlanetTexture() {
int textureIndex = rand() % planetTextures.size();
@ -318,58 +379,15 @@ void createSolarSystem(glm::vec3 sunPos, GLuint sunTexId, float sunScale, float*
planets.push_back(planet);
}
}
bool isFarFromReferencePoints(float x, float y, float z, const std::vector<glm::vec3>& referencePoints) {
float minDistance = 10.0f;
glm::vec3 currentPoint(x, y, z);
auto checkDistance = [currentPoint, minDistance](const glm::vec3& referencePoint) {
return glm::distance(currentPoint, referencePoint) > minDistance;
};
for (const auto& referencePoint : referencePoints) {
if (!checkDistance(referencePoint)) {
return false;
}
}
return true;
}
void createEnemies() {
//enemies.push_back(new Enemy(100.0f,100.0f, glm::mat4(1.0f), 1.0f, 5.0f));
//enemies.push_back(new Enemy(100.0f,100.0f, glm::translate(glm::mat4(1.0f) , glm::vec3(1.f,1.f,1.f)), 1.0f, 5.0f));
//enemies.push_back(new Enemy(100.0f,100.0f, glm::translate(glm::mat4(1.0f), glm::vec3(-1.f, 2.f, -0.9f)), 1.0f, 5.0f));
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<float> disX(-30.0f, 180.0f);
std::uniform_real_distribution<float> disY(-50.0f, 60.0f);
std::uniform_real_distribution<float> disZ(-30.0f, 210.0f);
std::vector<glm::vec3> referencePoints = {
glm::vec3(0, 2, 0),
glm::vec3(150, 5, 0),
glm::vec3(-20, -30, 50),
glm::vec3(100, 20, -50),
glm::vec3(0, 52, 200),
glm::vec3(150, 55, 200),
glm::vec3(-20, 20, 250),
glm::vec3(100, 70, 150)
};
for (int i = 0; i < 20; ++i) {
float randomX, randomY, randomZ;
do {
randomX = disX(gen);
randomY = disY(gen);
randomZ = disZ(gen);
} while (!isFarFromReferencePoints(randomX, randomY, randomZ, referencePoints));
glm::mat4 randomModelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(randomX, randomY, randomZ));
for (int i = 0; i < ENEMY_COUNT; ++i) {
glm::mat4 randomModelMatrix = generateRandomMatrix(glm::mat4(1.0f));
enemies.push_back(new Enemy(100.0f, 100.0f, randomModelMatrix, 1.0f, 8.0f));
}