health_bar #11

Merged
s473577 merged 4 commits from health_bar into master 2024-02-03 21:51:30 +01:00
8 changed files with 169 additions and 49 deletions
Showing only changes of commit 1f09c40bb5 - Show all commits

View File

@ -1,5 +1,7 @@
#include "glm.hpp"
#include "src/Shader_Loader.h"
#include <vector>
#include "GameEntity.h"
#pragma once
class Bullet
@ -39,14 +41,52 @@ public:
return new Bullet(10, 10, directionNormalized, startPosition, birthTime, simpleRenderContext, 0.01f);
}
bool shouldBeDestroyed(float time, GLuint program) {
bool shouldBeDestroyed(float time, GLuint program, std::vector<GameEntity*>& gameEntities, float attackerDmg) {
float age = getAge(time);
if (age > lifetime) {
return true;
}
glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), startPosition) * glm::translate(directionNormalized * speed * age) * glm::scale(glm::vec3(scale));
Core::drawObjectPBR(renderContext, modelMatrix, glm::vec3(1.f, 0.f, 0.f), 0.3, 0, program);
//std::cout << "x: " << modelMatrix[3].x << std::endl;
//std::cout << "y: " << modelMatrix[3].y << std::endl;
//std::cout << "z: " << modelMatrix[3].z << std::endl;
if (checkCollisionWithGameEntities(gameEntities, modelMatrix, attackerDmg)) {
return true;
}
return false;
}
bool checkCollisionWithGameEntities(std::vector<GameEntity*>& gameEntities, glm::mat4 bulletModelMatrix, float attackerDmg) {
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;
}
}
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));
glm::vec3 obj2Min = glm::vec3(obj2ModelMatrix * glm::vec4(-0.5f, -0.5f, -0.5f, 1.0f));
glm::vec3 obj2Max = glm::vec3(obj2ModelMatrix * glm::vec4(0.5f, 0.5f, 0.5f, 1.0f));
// SprawdŸ kolizjê wzd³u¿ trzech osi (x, y, z)
bool collisionX = obj1Max.x >= obj2Min.x && obj1Min.x <= obj2Max.x;
bool collisionY = obj1Max.y >= obj2Min.y && obj1Min.y <= obj2Max.y;
bool collisionZ = obj1Max.z >= obj2Min.z && obj1Min.z <= obj2Max.z;
// Kolizja wystêpuje tylko wtedy, gdy zachodzi kolizja na wszystkich trzech osiach
return collisionX && collisionY && collisionZ;
}
};

View File

@ -2,10 +2,11 @@
#include "ext.hpp"
#include "src/Render_Utils.h"
#include "Bullet.h"
#include "./GameEntity.h"
#include "Spaceship.h"
#pragma once
class Enemy
class Enemy : public GameEntity
{
private:
std::list<Bullet*> bullets;
@ -14,13 +15,13 @@ private:
public:
glm::mat4 modelMatrix;
int hp;
int initialHp;
int dmg;
float aggroRange;
Enemy(int currHp,int initialHp, glm::mat4 initialModelMatrix, int initialDmg, float initialAggroRange)
: hp(currHp), initialHp(initialHp), modelMatrix(initialModelMatrix), dmg(initialDmg), aggroRange(initialAggroRange)
Enemy(float currHp,float initialHp, glm::mat4 initialModelMatrix, float initialDmg, float initialAggroRange)
:
aggroRange(initialAggroRange),
modelMatrix(initialModelMatrix),
GameEntity(currHp, initialHp, initialDmg)
{}
@ -34,7 +35,7 @@ public:
}
virtual bool isAlive() const {
if (this->hp < 0) {
if (this->currentHP <= 0) {
return false;
}
return true;
@ -48,11 +49,11 @@ public:
return true;
}
void renderBullets(float time, GLuint program) {
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)) {
if (bullet->shouldBeDestroyed(time, program, gameEntities, dmg)) {
delete bullet;
it = bullets.erase(it);
}
@ -61,6 +62,15 @@ public:
}
}
}
glm::vec3 getPosition() const override
{
return modelMatrix[3];
}
glm::mat4 getModelMatrix() override
{
return modelMatrix;
}
private:
void requestShoot(float time) {
if (canShoot(time)) {
@ -75,6 +85,7 @@ private:
void shoot(float time) {
Spaceship* spaceship = Spaceship::getInstance();
glm::vec3 bulletDirection = glm::normalize(spaceship->spaceshipPos - glm::vec3(modelMatrix[3]));
this->bullets.push_back(Bullet::createSimpleBullet(bulletDirection, this->modelMatrix[3], time));
this->lastShootTime = time;
}

23
grk/project/GameEntity.h Normal file
View File

@ -0,0 +1,23 @@
#include "glm.hpp"
#pragma once
class GameEntity
{
public:
float currentHP;
float dmg;
float maxHP;
GameEntity(float currentHP, float maxHP, float initialDmg)
: currentHP(currentHP), maxHP(maxHP), dmg(initialDmg)
{
}
virtual void applyDamage(float attackerDmg) {
currentHP = currentHP - attackerDmg;
};
virtual glm::vec3 getPosition() const = 0;
virtual glm::mat4 getModelMatrix() = 0;
};

View File

@ -4,10 +4,11 @@
#include <list>
#include "Bullet.h"
#include "ParticleSystem.h"
#include "GameEntity.h"
#pragma once
class Spaceship
class Spaceship : public GameEntity
{
private:
std::list<Bullet*> bullets;
@ -16,8 +17,9 @@ private:
ParticleSystem* leftParticle;
ParticleSystem* rightParticle;
// Prywatny konstruktor, aby zapobiec zewnêtrznemu tworzeniu instancji
Spaceship() {
// Prywatny konstruktor, aby zapobiec zewn<77>trznemu tworzeniu instancji
Spaceship() : GameEntity(100.0f, 100.0f, 10.0f)
{
}
@ -164,11 +166,11 @@ public:
return cameraMatrix;
}
void renderBullets(float time, GLuint program) {
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)) {
if (bullet->shouldBeDestroyed(time, program, gameEntities, dmg)) {
delete bullet;
it = bullets.erase(it);
}
@ -194,4 +196,11 @@ public:
bullets.push_back(Bullet::createSimpleBullet(cameraDir, spaceshipPos + perpendicularVector, time));
lastShootTime = time;
}
glm::vec3 getPosition() const override {
return spaceshipPos;
}
glm::mat4 getModelMatrix() override {
return calculateModelMatrix();
}
};

View File

@ -29,6 +29,7 @@
<ItemGroup>
<ClInclude Include="Bullet.h" />
<ClInclude Include="Enemy.h" />
<ClInclude Include="GameEntity.h" />
<ClInclude Include="GameObject.h" />
<ClInclude Include="GameUtils.h" />
<ClInclude Include="ParticleSystem.h" />

View File

@ -128,6 +128,9 @@
<ClInclude Include="ParticleSystem.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="GameEntity.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="shaders\shader_8_sun.vert">

View File

@ -70,7 +70,7 @@ Sun* sun;
GLuint VAO,VBO;
std::vector<Enemy*> enemies;
std::vector<GameEntity*> gameEntities;
float exposition = 1.f;
@ -94,13 +94,21 @@ void updateDeltaTime(float time) {
if (deltaTime > 0.1) deltaTime = 0.1;
lastTime = time;
}
void renderEnemies() {
glDisable(GL_DEPTH_TEST);
void renderHUD() {
glUseProgram(programSpriteBar);
glDisable(GL_DEPTH_TEST);
glm::mat4 spaceshipModelMatrix = spaceship->calculateModelMatrix();
glm::mat4 healthBarPosition = glm::translate(spaceshipModelMatrix, glm::vec3(12.0f, -9.0f, 0.0f));
spriteRenderer->DrawHUDBar(glm::vec3(0.0f, 1.0f, 0.0f), glm::scale(healthBarPosition, glm::vec3(9.0f, 1.1f, 1.0f)), 1.f, programSpriteBar);
spriteRenderer->DrawHUDBar(
glm::vec3(0.0f, 1.0f, 0.0f),
glm::scale(healthBarPosition, glm::vec3(9.0f, 1.1f, 1.0f)),
spaceship->currentHP / spaceship->maxHP,
programSpriteBar);
glEnable(GL_DEPTH_TEST);
}
void renderEnemies() {
glUseProgram(programSpriteBar);
for (const auto& enemy : enemies) {
if (enemy->isAlive()) {
spriteRenderer->DrawSpriteBar(
@ -110,12 +118,13 @@ void renderEnemies() {
),
glm::vec3(1.0f, 0.2f, 1.0f)
),
static_cast<float>(enemy->hp / enemy->initialHp),
enemy->currentHP / enemy->maxHP,
programSpriteBar);
}
}
glUseProgram(programSprite);
for (const auto& enemy : enemies) {
if (enemy->isAlive()) {
spriteRenderer->DrawSprite(texture::spriteTexture, enemy->modelMatrix, programSprite);
}
@ -125,7 +134,7 @@ void renderEnemies() {
for (const auto& enemy : enemies) {
if (enemy->isAlive()) {
enemy->attack(spaceship->spaceshipPos, glfwGetTime());
enemy->renderBullets(glfwGetTime(), program);
enemy->renderBullets(glfwGetTime(), program, gameEntities);
}
}
@ -174,9 +183,8 @@ void renderScene(GLFWwindow* window)
glUseProgram(program);
spaceship->renderBullets(glfwGetTime(), program);
std::vector<GameEntity*> gameEntities(enemies.begin(), enemies.end());
spaceship->renderBullets(glfwGetTime(), program, gameEntities);
drawObjectPBR(models::sphereContext,
glm::translate(pointlightPos) * glm::scale(glm::vec3(0.1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(4.f, 0, 0)) * glm::eulerAngleY(time) * glm::translate(glm::vec3(1.f, 0, 0)) * glm::scale(glm::vec3(0.1f)),
@ -190,6 +198,7 @@ void renderScene(GLFWwindow* window)
texture::spaceshipTexture,
spaceship->roughness, spaceship->metallic, programTex
);
<<<<<<< HEAD
<<<<<<< HEAD
//Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 0);
@ -219,6 +228,9 @@ void renderScene(GLFWwindow* window)
glm::mat4 viewMatrix = Core::createViewMatrix(spaceship->cameraPos, spaceship->cameraDir, cameraUp);
spaceship->renderParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time);
=======
=======
renderHUD();
>>>>>>> 9586849 (Add collision detection and taking dmg)
renderEnemies();
//drawObjectPBR(sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0, program);
@ -268,9 +280,12 @@ void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int
void createEnemies() {
enemies.push_back(new Enemy(100,100, glm::mat4(1.0f), 20, 5.0f));
enemies.push_back(new Enemy(100,100, glm::translate(glm::mat4(1.0f) , glm::vec3(1.f,1.f,1.f)), 15, 5.0f));
enemies.push_back(new Enemy(100,100, glm::translate(glm::mat4(1.0f), glm::vec3(-1.f, 2.f, -0.9f)), 25, 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));
//obiekty do ktorych bedzie sprawdzana kolizja dla pociskow enemy
gameEntities.push_back(spaceship);
}

View File

@ -61,19 +61,16 @@ GLuint programSun;
GLuint programTest;
GLuint programSprite;
GLuint programCubemap;
<<<<<<< HEAD
GLuint programParticle;
GLuint programTex;
=======
GLuint programSpriteBar;
>>>>>>> 6ccdec5 (Add drawing health bars for enemies)
std::list<Planet*> planets;
Sun* sun;
GLuint VAO,VBO;
std::vector<Enemy*> enemies;
std::vector<GameEntity*> gameEntities;
float exposition = 1.f;
@ -97,7 +94,20 @@ void updateDeltaTime(float time) {
if (deltaTime > 0.1) deltaTime = 0.1;
lastTime = time;
}
void renderHUD() {
glUseProgram(programSpriteBar);
glDisable(GL_DEPTH_TEST);
glm::mat4 spaceshipModelMatrix = spaceship->calculateModelMatrix();
glm::mat4 healthBarPosition = glm::translate(spaceshipModelMatrix, glm::vec3(12.0f, -9.0f, 0.0f));
spriteRenderer->DrawHUDBar(
glm::vec3(0.0f, 1.0f, 0.0f),
glm::scale(healthBarPosition, glm::vec3(9.0f, 1.1f, 1.0f)),
spaceship->currentHP / spaceship->maxHP,
programSpriteBar);
glEnable(GL_DEPTH_TEST);
}
void renderEnemies() {
glUseProgram(programSpriteBar);
for (const auto& enemy : enemies) {
if (enemy->isAlive()) {
@ -108,24 +118,13 @@ void renderEnemies() {
),
glm::vec3(1.0f, 0.2f, 1.0f)
),
static_cast<float>(enemy->hp / enemy->initialHp),
enemy->currentHP / enemy->maxHP,
programSpriteBar);
}
<<<<<<< HEAD
TextureTuple getRandomPlanetTexture() {
int textureIndex = rand() % planetTextures.size();
TextureTuple selectedTextures = planetTextures[textureIndex];
//GLuint textureID = selectedTextures.textureID;
//GLuint normalMapID = selectedTextures.normalMapID;
return planetTextures[textureIndex];
}
=======
}
glUseProgram(programSprite);
for (const auto& enemy : enemies) {
if (enemy->isAlive()) {
spriteRenderer->DrawSprite(texture::spriteTexture, enemy->modelMatrix, programSprite);
}
@ -135,13 +134,20 @@ TextureTuple getRandomPlanetTexture() {
for (const auto& enemy : enemies) {
if (enemy->isAlive()) {
enemy->attack(spaceship->spaceshipPos, glfwGetTime());
enemy->renderBullets(glfwGetTime(), program);
enemy->renderBullets(glfwGetTime(), program, gameEntities);
}
}
}
>>>>>>> 6ccdec5 (Add drawing health bars for enemies)
TextureTuple getRandomPlanetTexture() {
int textureIndex = rand() % planetTextures.size();
TextureTuple selectedTextures = planetTextures[textureIndex];
//GLuint textureID = selectedTextures.textureID;
//GLuint normalMapID = selectedTextures.normalMapID;
return planetTextures[textureIndex];
}
void renderShadowapSun() {
float time = glfwGetTime();
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
@ -174,10 +180,15 @@ void renderScene(GLFWwindow* window)
for (Planet* p : planets) {
p->draw(time, programTex);
}
<<<<<<< HEAD
glUseProgram(program);
spaceship->renderBullets(glfwGetTime(), program);
=======
std::vector<GameEntity*> gameEntities(enemies.begin(), enemies.end());
spaceship->renderBullets(glfwGetTime(), program, gameEntities);
>>>>>>> 9586849 (Add collision detection and taking dmg)
@ -193,6 +204,7 @@ void renderScene(GLFWwindow* window)
texture::spaceshipTexture,
spaceship->roughness, spaceship->metallic, programTex
);
<<<<<<< HEAD
<<<<<<< HEAD
//Core::SetActiveTexture(texture::earthTexture, "textureSampler", programTex, 0);
@ -222,6 +234,9 @@ void renderScene(GLFWwindow* window)
glm::mat4 viewMatrix = Core::createViewMatrix(spaceship->cameraPos, spaceship->cameraDir, cameraUp);
spaceship->renderParticles(programParticle, viewMatrix, Core::createPerspectiveMatrix(), time);
=======
=======
renderHUD();
>>>>>>> 9586849 (Add collision detection and taking dmg)
renderEnemies();
//drawObjectPBR(sphereContext, glm::translate(pointlightPos) * glm::scale(glm::vec3(1)) * glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(10.f, 0, 0)) * glm::scale(glm::vec3(0.3f)), glm::vec3(0.2, 0.7, 0.3), 0.3, 0.0, program);
@ -271,9 +286,12 @@ void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int
void createEnemies() {
enemies.push_back(new Enemy(100,100, glm::mat4(1.0f), 20, 5.0f));
enemies.push_back(new Enemy(100,100, glm::translate(glm::mat4(1.0f) , glm::vec3(1.f,1.f,1.f)), 15, 5.0f));
enemies.push_back(new Enemy(100,100, glm::translate(glm::mat4(1.0f), glm::vec3(-1.f, 2.f, -0.9f)), 25, 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));
//obiekty do ktorych bedzie sprawdzana kolizja dla pociskow enemy
gameEntities.push_back(spaceship);
}