grafika_komputerowa/grk/project/Bullet.h

93 lines
3.2 KiB
C
Raw Normal View History

2024-01-16 22:15:26 +01:00
#include "glm.hpp"
#include "src/Shader_Loader.h"
2024-02-03 20:20:01 +01:00
#include <vector>
#include "GameEntity.h"
2024-01-16 22:15:26 +01:00
#pragma once
class Bullet
{
private:
float speed;
float lifetime;
glm::vec3 directionNormalized;
glm::vec3 startPosition;
float birthTime;
float scale;
//GLuint program;
Core::RenderContext renderContext;
float getAge(float time) {
return time - birthTime;
}
Bullet(float speed, float lifetime, glm::vec3 directionNormalized, glm::vec3 startPosition, float birthTime, Core::RenderContext renderContext, float scale){
this->speed = speed;
this->lifetime = lifetime;
this->directionNormalized = directionNormalized;
this->startPosition = startPosition;
this->birthTime = birthTime;
this->renderContext = renderContext;
this->scale = scale;
}
public:
static Bullet* createSimpleBullet(glm::vec3 directionNormalized, glm::vec3 startPosition, float birthTime) {
static bool simpleRenderContextLoaded;
static Core::RenderContext simpleRenderContext;
if (!simpleRenderContextLoaded) {
simpleRenderContextLoaded = true;
Core::loadModelToContext("./models/sphere.obj", simpleRenderContext);
}
return new Bullet(10, 10, directionNormalized, startPosition, birthTime, simpleRenderContext, 0.01f);
}
2024-02-03 20:20:01 +01:00
bool shouldBeDestroyed(float time, GLuint program, std::vector<GameEntity*>& gameEntities, float attackerDmg) {
2024-01-16 22:15:26 +01:00
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);
2024-02-03 20:20:01 +01:00
//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<77> kolizj<7A> AABB mi<6D>dzy pociskiem a obiektem z wektora gameEntities
if (checkAABBCollision(bulletModelMatrix, entityModelMatrix)) {
entity->applyDamage(attackerDmg);
return true;
}
}
2024-01-16 22:15:26 +01:00
return false;
}
2024-02-03 20:20:01 +01:00
bool checkAABBCollision(const glm::mat4& obj1ModelMatrix, const glm::mat4& obj2ModelMatrix) {
// Pobierz rozmiary obiekt<6B>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<77> kolizj<7A> wzd<7A>u<EFBFBD> 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<73>puje tylko wtedy, gdy zachodzi kolizja na wszystkich trzech osiach
return collisionX && collisionY && collisionZ;
}
2024-01-16 22:15:26 +01:00
};