Merge pull request 'enemies_placement' (#19) from enemies_placement into master

Reviewed-on: #19
This commit is contained in:
Mateusz Kantorski 2024-02-08 00:44:07 +01:00
commit 630438b1bc
5 changed files with 125 additions and 21 deletions

View File

@ -62,7 +62,6 @@ public:
for (const auto& entity : gameEntities) {
glm::mat4 entityModelMatrix = entity->getModelMatrix();
// Sprawdź kolizję AABB między pociskiem a obiektem z wektora gameEntities
if (checkAABBCollision(bulletModelMatrix, entityModelMatrix)) {
entity->applyDamage(attackerDmg);
return true;
@ -72,7 +71,7 @@ public:
return false;
}
bool checkAABBCollision(const glm::mat4& obj1ModelMatrix, const glm::mat4& obj2ModelMatrix) {
// Pobierz rozmiary obiektów z ich macierzy modelu
glm::vec3 obj1Min = glm::vec3(obj1ModelMatrix * glm::vec4(-0.5f, -0.5f, -0.5f, 1.0f));
glm::vec3 obj1Max = glm::vec3(obj1ModelMatrix * glm::vec4(0.5f, 0.5f, 0.5f, 1.0f));

View File

@ -12,15 +12,20 @@ private:
std::list<Bullet*> bullets;
float lastShootTime = 0.f;
float shootInterval = 0.3f;
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)
{}
@ -70,7 +75,13 @@ public:
{
return modelMatrix;
}
void respawn() override {
this->currentHP = this->maxHP;
this->modelMatrix = this->initialModelMatrix;
this->aggroRange = this->initAggroRange;
this->dmg = this->initDMG;
return;
}
private:
void requestShoot(float time) {
if (canShoot(time)) {
@ -84,7 +95,8 @@ private:
void shoot(float time) {
Spaceship* spaceship = Spaceship::getInstance();
glm::vec3 bulletDirection = glm::normalize(spaceship->spaceshipPos - glm::vec3(modelMatrix[3]));
//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;

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)
{
}
@ -19,5 +20,12 @@ public:
};
virtual glm::vec3 getPosition() const = 0;
virtual glm::mat4 getModelMatrix() = 0;
virtual bool isAlive() {
if (this->currentHP <= 0) {
return false;
}
return true;
};
virtual void respawn() = 0;
};

View File

@ -41,7 +41,7 @@ public:
float turbo = 1.0f;
float turboMAX = 1.0f;
glm::vec3 spaceshipPos /*= glm::vec3(0.065808f, 1.250000f, -2.189549f)*/;
glm::vec3 spaceshipPos = glm::vec3(4.065808f, 10.250000f, -20.189549f);
glm::vec3 spaceshipDir = glm::vec3(-0.490263f, 0.000000f, 0.871578f);
glm::vec3 spotlightPos = glm::vec3(0, 0, 0);
@ -225,7 +225,7 @@ public:
if (w == GLFW_PRESS) {
if (shiftState == GLFW_PRESS) {
turbo = glm::max(0.0f, turbo - 0.004f * deltaTime * 60);
turbo = glm::max(0.0f, turbo - 0.003f * deltaTime * 60);
if (turbo == 0.0f) {
shiftState = GLFW_RELEASE;
moveSpeed = 0.05f * deltaTime * 60;
@ -386,4 +386,12 @@ public:
return cameraMatrix;
}
void respawn() override {
this->currentHP = this->maxHP;
this->turbo = this->turboMAX;
this->spaceshipPos = glm::vec3(4.065808f, 10.250000f, -20.189549f);
return;
}
};

View File

@ -25,6 +25,8 @@
#include "../ParticleSystem.h"
#include "Camera.h"
#include <random>
#ifndef EX_9_1_HPP
#define EX_9_1_HPP
@ -95,6 +97,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 +152,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 +235,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();
@ -260,6 +323,13 @@ void renderScene(GLFWwindow* window)
glm::mat4 viewMatrix = Core::createViewMatrix(spaceship->cameraPos, spaceship->cameraDir, cameraUp);
spaceship->renderParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time);
if(!spaceship->isAlive()){
spaceship->respawn();
for (const auto& enemy : enemies) {
enemy->respawn();
}
}
renderHUD(window);
renderEnemies();
@ -338,12 +408,19 @@ void createSolarSystem(glm::vec3 sunPos, GLuint sunTexId, float sunScale, float*
planets.push_back(planet);
}
}
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));
//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));
enemies.push_back(new Enemy(100.0f, 100.0f, glm::translate(glm::translate(glm::mat4(1.0f), spaceship->getPosition()), glm::vec3(1.f, 1.f, 2.f)), 1.0f, 5.0f));
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));
}
//obiekty do ktorych bedzie sprawdzana kolizja dla pociskow enemy
gameEntities.push_back(spaceship);