94 lines
3.3 KiB
C++
94 lines
3.3 KiB
C++
#include "glm.hpp"
|
||
#include "src/Shader_Loader.h"
|
||
#include <vector>
|
||
#include "GameEntity.h"
|
||
|
||
#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);
|
||
}
|
||
|
||
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();
|
||
|
||
if (checkAABBCollision(bulletModelMatrix, entityModelMatrix)) {
|
||
entity->applyDamage(attackerDmg);
|
||
return true;
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
bool checkAABBCollision(const glm::mat4& obj1ModelMatrix, const glm::mat4& obj2ModelMatrix) {
|
||
|
||
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));*/
|
||
glm::vec3 obj2Min = glm::vec3(obj2ModelMatrix * glm::vec4(-0.6f, -0.6f, -0.6f, 1.0f));
|
||
glm::vec3 obj2Max = glm::vec3(obj2ModelMatrix * glm::vec4(0.6f, 0.6f, 0.6f, 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;
|
||
}
|
||
|
||
};
|
||
|