From 576179218ccf0a4354736a4505ad23c92d3318a4 Mon Sep 17 00:00:00 2001 From: Fries Date: Sun, 17 Mar 2024 21:14:36 -0700 Subject: [PATCH] add some working collision --- src/engine/engine.cc | 4 ++ src/engine/engine.hh | 1 + src/engine/entity.cc | 25 ++++++++++ src/engine/entity.hh | 11 ++++- src/flappyBird.cc | 9 ++-- src/flappyBird/Bird.cc | 37 +++++++++++++- src/flappyBird/Bird.hh | 5 +- src/flappyBird/FlappyBird.cc | 93 ++++++++++++++++++++++++++---------- src/flappyBird/FlappyBird.hh | 4 ++ src/flappyBird/MainMenu.cc | 12 +++++ src/flappyBird/MainMenu.hh | 13 +++++ src/flappyBird/Pipes.cc | 34 +++++++++---- src/flappyBird/meson.build | 3 +- src/gl.cc | 9 ++-- src/hello.cc | 13 ++--- src/meson.build | 2 +- 16 files changed, 213 insertions(+), 62 deletions(-) create mode 100644 src/flappyBird/MainMenu.cc create mode 100644 src/flappyBird/MainMenu.hh diff --git a/src/engine/engine.cc b/src/engine/engine.cc index 280fc67..6e89bbd 100644 --- a/src/engine/engine.cc +++ b/src/engine/engine.cc @@ -99,3 +99,7 @@ void Engine::Vector3::operator/=(float amount) { } 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}; +} diff --git a/src/engine/engine.hh b/src/engine/engine.hh index 80bc45b..2caa9a5 100644 --- a/src/engine/engine.hh +++ b/src/engine/engine.hh @@ -51,6 +51,7 @@ class Engine { struct Vector3 { Vector3 operator/(float amount); void operator/=(float amount); + Vector3 operator+(Vector3 vector); float x; float y; float z; diff --git a/src/engine/entity.cc b/src/engine/entity.cc index f709960..df7020c 100644 --- a/src/engine/entity.cc +++ b/src/engine/entity.cc @@ -54,3 +54,28 @@ void Entity::updateChildrenEntity(Entity* entity) { 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; +} diff --git a/src/engine/entity.hh b/src/engine/entity.hh index 3e39d3e..8b9faa8 100644 --- a/src/engine/entity.hh +++ b/src/engine/entity.hh @@ -6,11 +6,10 @@ class Entity { Entity(Engine* engie, Entity* parent) { engine = engie; this->parent = parent; + updatePosition(Engine::Vector3::zero); }; virtual ~Entity(); Engine* engine; - Engine::Vector3 position = {0, 0, 0}; - Engine::Vector3 scale = {1, 1, 1}; Entity* parent = nullptr; std::vector children = {}; virtual void draw(){}; @@ -18,8 +17,16 @@ class Entity { bool destroyed = false; static void drawEntity(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: + Engine::Vector3 position = Engine::Vector3::zero; + Engine::Vector3 globalPosition = Engine::Vector3::zero; + Engine::Vector3 scale = {1, 1, 1}; void DestroyChildren(); static void drawChildrenEntity(Entity* entity); static void updateChildrenEntity(Entity* entity); diff --git a/src/flappyBird.cc b/src/flappyBird.cc index 5058a90..644cd76 100644 --- a/src/flappyBird.cc +++ b/src/flappyBird.cc @@ -1,9 +1,8 @@ #include "flappyBird/FlappyBird.hh" int main() { - FlappyBird* engine = new FlappyBird; - engine->initializeEngine(); - engine->initializeController(); - engine->initializeGameLoop(); - delete engine; + FlappyBird engine; + engine.initializeEngine(); + engine.initializeController(); + engine.initializeGameLoop(); } diff --git a/src/flappyBird/Bird.cc b/src/flappyBird/Bird.cc index 2d551b7..49aa1fc 100644 --- a/src/flappyBird/Bird.cc +++ b/src/flappyBird/Bird.cc @@ -1,5 +1,7 @@ #include "Bird.hh" +#include + void Bird::draw() { Engine::Vector3 one = {-1.0, -1.0, 0.0}; Engine::Vector3 two = {0.0, 1.0, 0.0}; @@ -18,6 +20,30 @@ void Bird::draw() { } 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(); engine->pressButton(Controller::A, [&]() { if (verticalSpeed < 0) { @@ -27,7 +53,14 @@ void Bird::update() { verticalSpeed = std::clamp(verticalSpeed + JUMP_CONST, 0.0f, 1.0f); }); - position.y = - std::clamp(position.y + (verticalSpeed * deltaTime), -1.0f, 1.0f); + Engine::Vector3 pos = getPosition(); + + pos.y = std::clamp(pos.y + (verticalSpeed * deltaTime), -1.0f, 1.0f); + updatePosition(pos); verticalSpeed -= FALLING_CONST * deltaTime; } + +Bird::Bird(Engine* engie, Entity* parent, PipesContainer* pipes) + : Entity(engie, parent) { + this->pipes = pipes; +} diff --git a/src/flappyBird/Bird.hh b/src/flappyBird/Bird.hh index 17e4f1e..9c83e06 100644 --- a/src/flappyBird/Bird.hh +++ b/src/flappyBird/Bird.hh @@ -1,16 +1,19 @@ #ifndef BIRD_HH #define BIRD_HH #include +#include "Pipes.hh" class Bird : public Entity { public: void draw() override; void update() override; - Bird(Engine* engie, Entity* parent) : Entity(engie, parent) {} + Bird(Engine* engie, Entity* parent, PipesContainer* pipes); + bool isHit = false; private: static constexpr float FALLING_CONST = 0.65; static constexpr float JUMP_CONST = 0.5; float verticalSpeed = JUMP_CONST; + PipesContainer *pipes = nullptr; }; #endif diff --git a/src/flappyBird/FlappyBird.cc b/src/flappyBird/FlappyBird.cc index 00016d7..5e35ca9 100644 --- a/src/flappyBird/FlappyBird.cc +++ b/src/flappyBird/FlappyBird.cc @@ -1,28 +1,16 @@ #include "FlappyBird.hh" + #include +#include "MainMenu.hh" + void FlappyBird::gameLoop() { - if (!thread) { - thread = new std::thread(&FlappyBird::spawnPipes, this); + if (mainMenu->active) { + mainMenuLoop(); + return; } - 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(); + + mainGameLoop(); } 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() { - 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); + 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() { - threadRunning = false; delete bird; delete pipesContainer; - thread->detach(); + delete mainMenu; + threadRunning = false; + if (thread != nullptr) thread->detach(); delete thread; } diff --git a/src/flappyBird/FlappyBird.hh b/src/flappyBird/FlappyBird.hh index 58fa057..9a569b0 100644 --- a/src/flappyBird/FlappyBird.hh +++ b/src/flappyBird/FlappyBird.hh @@ -5,14 +5,18 @@ #include #include "Bird.hh" #include "Pipes.hh" +#include "MainMenu.hh" class FlappyBird : public Engine { void gameLoop() override; void spawnPipes(); + void mainGameLoop(); + void mainMenuLoop(); public: Bird* bird; PipesContainer* pipesContainer; + MainMenu* mainMenu; std::thread* thread = nullptr; bool threadRunning; bool addNewPipe; diff --git a/src/flappyBird/MainMenu.cc b/src/flappyBird/MainMenu.cc new file mode 100644 index 0000000..2278482 --- /dev/null +++ b/src/flappyBird/MainMenu.cc @@ -0,0 +1,12 @@ +#include "MainMenu.hh" +#include +#include + +void MainMenu::draw() { + glClear(GL_COLOR_BUFFER_BIT); +} +void MainMenu::update() { + engine->pressButton(Controller::A, [&]() { + active = false; + }); +} diff --git a/src/flappyBird/MainMenu.hh b/src/flappyBird/MainMenu.hh new file mode 100644 index 0000000..67205f2 --- /dev/null +++ b/src/flappyBird/MainMenu.hh @@ -0,0 +1,13 @@ +#ifndef MAIN_MENU_HH +#define MAIN_MENU_HH + +#include +class MainMenu : public Entity { + void draw() override; + void update() override; + + public: + MainMenu(Engine* engie, Entity* parent) : Entity(engie, parent) {}; + bool active; +}; +#endif diff --git a/src/flappyBird/Pipes.cc b/src/flappyBird/Pipes.cc index 3b9b809..56d5796 100644 --- a/src/flappyBird/Pipes.cc +++ b/src/flappyBird/Pipes.cc @@ -24,13 +24,23 @@ Pipes::Pipes(Engine* engie, Entity* parent) : Entity(engie, parent) { Pipe* pipe1 = new Pipe(engine, this); Pipe* pipe2 = new Pipe(engine, this); - pipe1->scale.x = 0.1; - pipe1->position.y = 1.0f; - pipe1->pipeLength = pipeLength + pipeOffset; + Engine::Vector3 pipe1Scale = pipe1->getScale(); + Engine::Vector3 pipe1Position = pipe1->getPosition(); - pipe2->scale.x = 0.1; - pipe2->position.y = -1.0f; - pipe2->pipeLength = pipeLength - pipeOffset; + Engine::Vector3 pipe2Scale = pipe2->getScale(); + Engine::Vector3 pipe2Position = pipe2->getPosition(); + + 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(pipe2); @@ -39,16 +49,22 @@ Pipes::Pipes(Engine* engie, Entity* parent) : Entity(engie, parent) { } void Pipes::update() { + Engine::Vector3 position = this->getPosition(); position.x -= 0.5f * engine->getDeltaTime(); if (position.x < -2) { destroyed = true; } + this->updatePosition(position); } Pipes* Pipes::CreatePipes(Engine* engie, Entity* parent) { Pipes* pipes = new Pipes(engie, parent); - pipes->scale.x = 0.1f; - pipes->scale.y = 0.1f; - pipes->position.x = 1.25f; + Engine::Vector3 pipesScale = pipes->getScale(); + Engine::Vector3 pipesPosition = pipes->getPosition(); + pipesScale.x = 0.1f; + pipesScale.y = 0.1f; + pipesPosition.x = 1.25f; + pipes->updateScale(pipesScale); + pipes->updatePosition(pipesPosition); return pipes; } diff --git a/src/flappyBird/meson.build b/src/flappyBird/meson.build index 37ae5dd..bbc3d39 100644 --- a/src/flappyBird/meson.build +++ b/src/flappyBird/meson.build @@ -1,7 +1,8 @@ flappy_sources = [ 'Bird.cc', 'FlappyBird.cc', - 'Pipes.cc' + 'Pipes.cc', + 'MainMenu.cc' ] flappy_bird = static_library('flappyBird', flappy_sources, dependencies: deps, include_directories: incdirs) diff --git a/src/gl.cc b/src/gl.cc index d6c01b4..8662c4a 100644 --- a/src/gl.cc +++ b/src/gl.cc @@ -1,9 +1,8 @@ #include "gl/Gl.hh" int main() { - Glcc* engine = new Glcc; - engine->initializeEngine(); - engine->initializeController(); - engine->initializeGameLoop(); - delete engine; + Glcc engine; + engine.initializeEngine(); + engine.initializeController(); + engine.initializeGameLoop(); } diff --git a/src/hello.cc b/src/hello.cc index 5dd53d0..760efb7 100644 --- a/src/hello.cc +++ b/src/hello.cc @@ -1,14 +1,7 @@ -#ifdef _arch_dreamcast -#include -#else -#include -#endif - #include "hello/Hello.hh" int main(int argc, char *argv[]) { - Hello* engine = new Hello; - engine->initializeEngine(); - engine->initializeGameLoop(); - delete engine; + Hello engine; + engine.initializeEngine(); + engine.initializeGameLoop(); } diff --git a/src/meson.build b/src/meson.build index fc52ba6..f682f45 100644 --- a/src/meson.build +++ b/src/meson.build @@ -40,7 +40,7 @@ if host_machine.system() != 'dreamcast' deps += dependency('glfw3', fallback: ['glfw', 'glfw_dep'], required: true) if host_machine.system() == 'windows' deps += cc.find_library('glu32', required: true) - elif + else deps += dependency('glu', required: true) endif endif