add some working collision

This commit is contained in:
Fries 2024-03-17 21:14:36 -07:00
parent 63565fc7f1
commit 576179218c
16 changed files with 213 additions and 62 deletions

View file

@ -99,3 +99,7 @@ void Engine::Vector3::operator/=(float amount) {
} }
float Engine::getDeltaTime() const { return deltaTime; } float Engine::getDeltaTime() const { return deltaTime; }
Engine::Vector3 Engine::Vector3::operator+(Engine::Vector3 vector) {
return {this->x + vector.x, this->y + vector.y, this->z + vector.z};
}

View file

@ -51,6 +51,7 @@ class Engine {
struct Vector3 { struct Vector3 {
Vector3 operator/(float amount); Vector3 operator/(float amount);
void operator/=(float amount); void operator/=(float amount);
Vector3 operator+(Vector3 vector);
float x; float x;
float y; float y;
float z; float z;

View file

@ -54,3 +54,28 @@ void Entity::updateChildrenEntity(Entity* entity) {
updateChildrenEntity(childEntity); updateChildrenEntity(childEntity);
} }
} }
Engine::Vector3 Entity::getPosition() const {
return position;
}
Engine::Vector3 Entity::getGlobalPosition() const {
return globalPosition;
}
void Entity::updatePosition(Engine::Vector3 position) {
this->position = position;
if (this->parent != nullptr) {
globalPosition = this->position + this->parent->globalPosition;
} else {
globalPosition = position;
}
}
Engine::Vector3 Entity::getScale() const {
return scale;
}
void Entity::updateScale(Engine::Vector3 scale) {
this->scale = scale;
}

View file

@ -6,11 +6,10 @@ class Entity {
Entity(Engine* engie, Entity* parent) { Entity(Engine* engie, Entity* parent) {
engine = engie; engine = engie;
this->parent = parent; this->parent = parent;
updatePosition(Engine::Vector3::zero);
}; };
virtual ~Entity(); virtual ~Entity();
Engine* engine; Engine* engine;
Engine::Vector3 position = {0, 0, 0};
Engine::Vector3 scale = {1, 1, 1};
Entity* parent = nullptr; Entity* parent = nullptr;
std::vector<Entity*> children = {}; std::vector<Entity*> children = {};
virtual void draw(){}; virtual void draw(){};
@ -18,8 +17,16 @@ class Entity {
bool destroyed = false; bool destroyed = false;
static void drawEntity(Entity* entity); static void drawEntity(Entity* entity);
static void updateEntity(Entity* entity); static void updateEntity(Entity* entity);
Engine::Vector3 getPosition() const;
Engine::Vector3 getGlobalPosition() const;
void updatePosition(Engine::Vector3 position);
Engine::Vector3 getScale() const;
void updateScale(Engine::Vector3 scale);
private: private:
Engine::Vector3 position = Engine::Vector3::zero;
Engine::Vector3 globalPosition = Engine::Vector3::zero;
Engine::Vector3 scale = {1, 1, 1};
void DestroyChildren(); void DestroyChildren();
static void drawChildrenEntity(Entity* entity); static void drawChildrenEntity(Entity* entity);
static void updateChildrenEntity(Entity* entity); static void updateChildrenEntity(Entity* entity);

View file

@ -1,9 +1,8 @@
#include "flappyBird/FlappyBird.hh" #include "flappyBird/FlappyBird.hh"
int main() { int main() {
FlappyBird* engine = new FlappyBird; FlappyBird engine;
engine->initializeEngine(); engine.initializeEngine();
engine->initializeController(); engine.initializeController();
engine->initializeGameLoop(); engine.initializeGameLoop();
delete engine;
} }

View file

@ -1,5 +1,7 @@
#include "Bird.hh" #include "Bird.hh"
#include <cstdio>
void Bird::draw() { void Bird::draw() {
Engine::Vector3 one = {-1.0, -1.0, 0.0}; Engine::Vector3 one = {-1.0, -1.0, 0.0};
Engine::Vector3 two = {0.0, 1.0, 0.0}; Engine::Vector3 two = {0.0, 1.0, 0.0};
@ -18,6 +20,30 @@ void Bird::draw() {
} }
void Bird::update() { void Bird::update() {
if (pipes != nullptr) {
for (int i = 0; i < pipes->children.size(); i++) {
if (getGlobalPosition().x >=
pipes->children[i]->getGlobalPosition().x - 0.1f &&
getGlobalPosition().x <=
pipes->children[i]->getGlobalPosition().x + 0.15f) {
Pipes* pipe = (Pipes*)pipes->children[i];
float topPipeBottomPosition = 1 - ((Pipe*)(pipe->children[0]))->pipeLength;
float bottomPipeTopPosition = -1 + ((Pipe*)(pipe->children[1]))->pipeLength;
bool betweenPipes = getGlobalPosition().y < topPipeBottomPosition && getGlobalPosition().y > bottomPipeTopPosition;
if(betweenPipes) {
printf("between pipes\n");
} else {
printf("colliding: %f\n",
pipes->children[i]->getGlobalPosition().x);
isHit = true;
}
}
}
}
float deltaTime = engine->getDeltaTime(); float deltaTime = engine->getDeltaTime();
engine->pressButton(Controller::A, [&]() { engine->pressButton(Controller::A, [&]() {
if (verticalSpeed < 0) { if (verticalSpeed < 0) {
@ -27,7 +53,14 @@ void Bird::update() {
verticalSpeed = std::clamp(verticalSpeed + JUMP_CONST, 0.0f, 1.0f); verticalSpeed = std::clamp(verticalSpeed + JUMP_CONST, 0.0f, 1.0f);
}); });
position.y = Engine::Vector3 pos = getPosition();
std::clamp(position.y + (verticalSpeed * deltaTime), -1.0f, 1.0f);
pos.y = std::clamp(pos.y + (verticalSpeed * deltaTime), -1.0f, 1.0f);
updatePosition(pos);
verticalSpeed -= FALLING_CONST * deltaTime; verticalSpeed -= FALLING_CONST * deltaTime;
} }
Bird::Bird(Engine* engie, Entity* parent, PipesContainer* pipes)
: Entity(engie, parent) {
this->pipes = pipes;
}

View file

@ -1,16 +1,19 @@
#ifndef BIRD_HH #ifndef BIRD_HH
#define BIRD_HH #define BIRD_HH
#include <engine/entity.hh> #include <engine/entity.hh>
#include "Pipes.hh"
class Bird : public Entity { class Bird : public Entity {
public: public:
void draw() override; void draw() override;
void update() override; void update() override;
Bird(Engine* engie, Entity* parent) : Entity(engie, parent) {} Bird(Engine* engie, Entity* parent, PipesContainer* pipes);
bool isHit = false;
private: private:
static constexpr float FALLING_CONST = 0.65; static constexpr float FALLING_CONST = 0.65;
static constexpr float JUMP_CONST = 0.5; static constexpr float JUMP_CONST = 0.5;
float verticalSpeed = JUMP_CONST; float verticalSpeed = JUMP_CONST;
PipesContainer *pipes = nullptr;
}; };
#endif #endif

View file

@ -1,28 +1,16 @@
#include "FlappyBird.hh" #include "FlappyBird.hh"
#include <thread> #include <thread>
#include "MainMenu.hh"
void FlappyBird::gameLoop() { void FlappyBird::gameLoop() {
if (!thread) { if (mainMenu->active) {
thread = new std::thread(&FlappyBird::spawnPipes, this); mainMenuLoop();
return;
} }
for (size_t i = 0; i < pipesContainer->children.size(); i++) {
if (pipesContainer->children[i]->destroyed) { mainGameLoop();
delete pipesContainer->children[i];
pipesContainer->children.erase(pipesContainer->children.begin() + i);
}
}
if (addNewPipe) {
pipesContainer->children.push_back(
Pipes::CreatePipes(this, pipesContainer));
addNewPipe = false;
}
glClear(GL_COLOR_BUFFER_BIT);
controller->PollController();
Entity::updateEntity(this->bird);
Entity::updateEntity(this->pipesContainer);
Entity::drawEntity(this->bird);
Entity::drawEntity(this->pipesContainer);
SwapBuffers();
} }
void FlappyBird::spawnPipes() { void FlappyBird::spawnPipes() {
@ -39,18 +27,71 @@ void FlappyBird::spawnPipes() {
} }
} }
void FlappyBird::mainGameLoop() {
if (bird->isHit) {
mainMenu->active = true;
bird->isHit = false;
SwapBuffers();
return;
}
if (!thread) {
thread = new std::thread(&FlappyBird::spawnPipes, this);
}
for (size_t i = 0; i < pipesContainer->children.size(); i++) {
if (pipesContainer->children[i]->destroyed) {
// delete pipesContainer->children[i];
pipesContainer->children.erase(pipesContainer->children.begin() +
i);
}
}
if (addNewPipe) {
pipesContainer->children.push_back(
Pipes::CreatePipes(this, pipesContainer));
addNewPipe = false;
}
glClear(GL_COLOR_BUFFER_BIT);
controller->PollController();
Entity::updateEntity(this->bird);
Entity::updateEntity(this->pipesContainer);
Entity::drawEntity(this->bird);
Entity::drawEntity(this->pipesContainer);
SwapBuffers();
}
void FlappyBird::mainMenuLoop() {
if (thread) {
threadRunning = false;
thread->detach();
delete thread;
thread = nullptr;
pipesContainer->children.clear();
}
controller->PollController();
Entity::updateEntity(this->mainMenu);
Entity::drawEntity(this->mainMenu);
SwapBuffers();
}
FlappyBird::FlappyBird() { FlappyBird::FlappyBird() {
bird = new Bird(this, nullptr);
bird->scale.x = 0.1;
bird->scale.y = 0.1;
bird->position.x = -0.75;
pipesContainer = new PipesContainer(this, nullptr); pipesContainer = new PipesContainer(this, nullptr);
bird = new Bird(this, nullptr, pipesContainer);
Vector3 scale = bird->getScale();
Vector3 position = bird->getPosition();
scale.x = 0.1;
scale.y = 0.1;
position.x = -0.75;
bird->updateScale(scale);
bird->updatePosition(position);
mainMenu = new MainMenu(this, nullptr);
mainMenu->active = true;
} }
FlappyBird::~FlappyBird() { FlappyBird::~FlappyBird() {
threadRunning = false;
delete bird; delete bird;
delete pipesContainer; delete pipesContainer;
thread->detach(); delete mainMenu;
threadRunning = false;
if (thread != nullptr) thread->detach();
delete thread; delete thread;
} }

View file

@ -5,14 +5,18 @@
#include <engine/entity.hh> #include <engine/entity.hh>
#include "Bird.hh" #include "Bird.hh"
#include "Pipes.hh" #include "Pipes.hh"
#include "MainMenu.hh"
class FlappyBird : public Engine { class FlappyBird : public Engine {
void gameLoop() override; void gameLoop() override;
void spawnPipes(); void spawnPipes();
void mainGameLoop();
void mainMenuLoop();
public: public:
Bird* bird; Bird* bird;
PipesContainer* pipesContainer; PipesContainer* pipesContainer;
MainMenu* mainMenu;
std::thread* thread = nullptr; std::thread* thread = nullptr;
bool threadRunning; bool threadRunning;
bool addNewPipe; bool addNewPipe;

View file

@ -0,0 +1,12 @@
#include "MainMenu.hh"
#include <engine/controller/controller.hh>
#include <cstdio>
void MainMenu::draw() {
glClear(GL_COLOR_BUFFER_BIT);
}
void MainMenu::update() {
engine->pressButton(Controller::A, [&]() {
active = false;
});
}

View file

@ -0,0 +1,13 @@
#ifndef MAIN_MENU_HH
#define MAIN_MENU_HH
#include <engine/entity.hh>
class MainMenu : public Entity {
void draw() override;
void update() override;
public:
MainMenu(Engine* engie, Entity* parent) : Entity(engie, parent) {};
bool active;
};
#endif

View file

@ -24,13 +24,23 @@ Pipes::Pipes(Engine* engie, Entity* parent) : Entity(engie, parent) {
Pipe* pipe1 = new Pipe(engine, this); Pipe* pipe1 = new Pipe(engine, this);
Pipe* pipe2 = new Pipe(engine, this); Pipe* pipe2 = new Pipe(engine, this);
pipe1->scale.x = 0.1; Engine::Vector3 pipe1Scale = pipe1->getScale();
pipe1->position.y = 1.0f; Engine::Vector3 pipe1Position = pipe1->getPosition();
pipe1->pipeLength = pipeLength + pipeOffset;
pipe2->scale.x = 0.1; Engine::Vector3 pipe2Scale = pipe2->getScale();
pipe2->position.y = -1.0f; Engine::Vector3 pipe2Position = pipe2->getPosition();
pipe2->pipeLength = pipeLength - pipeOffset;
pipe1Scale.x = 0.1;
pipe1Position.y = 1.0f;
pipe1->pipeLength = std::abs(pipeLength + pipeOffset);
pipe1->updateScale(pipe1Scale);
pipe1->updatePosition(pipe1Position);
pipe2Scale.x = 0.1;
pipe2Position.y = -1.0f;
pipe2->pipeLength = std::abs(pipeLength - pipeOffset);
pipe2->updateScale(pipe2Scale);
pipe2->updatePosition(pipe2Position);
children.push_back(pipe1); children.push_back(pipe1);
children.push_back(pipe2); children.push_back(pipe2);
@ -39,16 +49,22 @@ Pipes::Pipes(Engine* engie, Entity* parent) : Entity(engie, parent) {
} }
void Pipes::update() { void Pipes::update() {
Engine::Vector3 position = this->getPosition();
position.x -= 0.5f * engine->getDeltaTime(); position.x -= 0.5f * engine->getDeltaTime();
if (position.x < -2) { if (position.x < -2) {
destroyed = true; destroyed = true;
} }
this->updatePosition(position);
} }
Pipes* Pipes::CreatePipes(Engine* engie, Entity* parent) { Pipes* Pipes::CreatePipes(Engine* engie, Entity* parent) {
Pipes* pipes = new Pipes(engie, parent); Pipes* pipes = new Pipes(engie, parent);
pipes->scale.x = 0.1f; Engine::Vector3 pipesScale = pipes->getScale();
pipes->scale.y = 0.1f; Engine::Vector3 pipesPosition = pipes->getPosition();
pipes->position.x = 1.25f; pipesScale.x = 0.1f;
pipesScale.y = 0.1f;
pipesPosition.x = 1.25f;
pipes->updateScale(pipesScale);
pipes->updatePosition(pipesPosition);
return pipes; return pipes;
} }

View file

@ -1,7 +1,8 @@
flappy_sources = [ flappy_sources = [
'Bird.cc', 'Bird.cc',
'FlappyBird.cc', 'FlappyBird.cc',
'Pipes.cc' 'Pipes.cc',
'MainMenu.cc'
] ]
flappy_bird = static_library('flappyBird', flappy_sources, dependencies: deps, include_directories: incdirs) flappy_bird = static_library('flappyBird', flappy_sources, dependencies: deps, include_directories: incdirs)

View file

@ -1,9 +1,8 @@
#include "gl/Gl.hh" #include "gl/Gl.hh"
int main() { int main() {
Glcc* engine = new Glcc; Glcc engine;
engine->initializeEngine(); engine.initializeEngine();
engine->initializeController(); engine.initializeController();
engine->initializeGameLoop(); engine.initializeGameLoop();
delete engine;
} }

View file

@ -1,14 +1,7 @@
#ifdef _arch_dreamcast
#include <GL/gl.h>
#else
#include <GLFW/glfw3.h>
#endif
#include "hello/Hello.hh" #include "hello/Hello.hh"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
Hello* engine = new Hello; Hello engine;
engine->initializeEngine(); engine.initializeEngine();
engine->initializeGameLoop(); engine.initializeGameLoop();
delete engine;
} }

View file

@ -40,7 +40,7 @@ if host_machine.system() != 'dreamcast'
deps += dependency('glfw3', fallback: ['glfw', 'glfw_dep'], required: true) deps += dependency('glfw3', fallback: ['glfw', 'glfw_dep'], required: true)
if host_machine.system() == 'windows' if host_machine.system() == 'windows'
deps += cc.find_library('glu32', required: true) deps += cc.find_library('glu32', required: true)
elif else
deps += dependency('glu', required: true) deps += dependency('glu', required: true)
endif endif
endif endif