add some working collision
This commit is contained in:
parent
63565fc7f1
commit
576179218c
16 changed files with 213 additions and 62 deletions
|
@ -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};
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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<Entity*> 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);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include "Bird.hh"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -1,16 +1,19 @@
|
|||
#ifndef BIRD_HH
|
||||
#define BIRD_HH
|
||||
#include <engine/entity.hh>
|
||||
#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
|
||||
|
|
|
@ -1,28 +1,16 @@
|
|||
#include "FlappyBird.hh"
|
||||
|
||||
#include <thread>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
|
|
@ -5,14 +5,18 @@
|
|||
#include <engine/entity.hh>
|
||||
#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;
|
||||
|
|
12
src/flappyBird/MainMenu.cc
Normal file
12
src/flappyBird/MainMenu.cc
Normal 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;
|
||||
});
|
||||
}
|
13
src/flappyBird/MainMenu.hh
Normal file
13
src/flappyBird/MainMenu.hh
Normal 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
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
13
src/hello.cc
13
src/hello.cc
|
@ -1,14 +1,7 @@
|
|||
#ifdef _arch_dreamcast
|
||||
#include <GL/gl.h>
|
||||
#else
|
||||
#include <GLFW/glfw3.h>
|
||||
#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();
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue