From 14fef65b95beb5258a8c486e4380656240098f5d Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 26 Dec 2021 15:34:32 +0100 Subject: [PATCH] just a save commit bc I know I'm going to mess everything --- README | 3 + headers/config.h | 6 ++ headers/game.h | 20 +++--- headers/{drawEngine.h => pixelManager.h} | 6 +- headers/pos.h | 11 --- headers/position.h | 11 +++ headers/utils.h | 17 +++-- lib_headers/mingl/graphics/vec2d.h | 36 +++++----- src/game_basics.cpp | 9 ++- src/game_managers.cpp | 88 ++++++++++++++++-------- src/main.cpp | 1 + src/{drawEngine.cpp => pixelManager.cpp} | 18 +++-- src/{pos.cpp => position.cpp} | 4 +- src/utils.cpp | 6 ++ 14 files changed, 149 insertions(+), 87 deletions(-) rename headers/{drawEngine.h => pixelManager.h} (83%) delete mode 100644 headers/pos.h create mode 100644 headers/position.h rename src/{drawEngine.cpp => pixelManager.cpp} (84%) rename src/{pos.cpp => position.cpp} (52%) create mode 100644 src/utils.cpp diff --git a/README b/README index e383112..b9a2049 100644 --- a/README +++ b/README @@ -5,3 +5,6 @@ Nommage en anglais Pas de fonctions de +100 lignes écran : constante 1280x720 + + +Reminder : les missiles sont tirés par les envahisseurs, et les torpilles par le joueur \ No newline at end of file diff --git a/headers/config.h b/headers/config.h index 8da5375..ce664a1 100644 --- a/headers/config.h +++ b/headers/config.h @@ -13,6 +13,12 @@ public: unsigned alien_size; unsigned distance; + unsigned missile_width; + unsigned missile_length; // auto defined from width + unsigned torpedo_width; + unsigned torpedo_length; // auto defined from width + unsigned player_width; + bool loadConfig(); }; diff --git a/headers/game.h b/headers/game.h index c12d9dd..1ad40ec 100644 --- a/headers/game.h +++ b/headers/game.h @@ -1,32 +1,36 @@ -#ifndef GUARD_GAME -#define GUARD_GAME +#ifndef GUARD_GAME_H +#define GUARD_GAME_H #include #include "mingl/mingl.h" -#include "drawEngine.h" +#include "pixelManager.h" #include "config.h" #include "utils.h" -#include "pos.h" +#include "position.h" using namespace std; class Game { private: - DrawEngine drawer; + PixelManager pm; Config conf; unsigned baseX; unsigned baseY; aliensGrid grid; - vector missiles; - vector torpedos; - pos player; + vector missiles; + vector torpedos; + unsigned playerX; void managePlayer(); bool manageInvaders(); void display(); + + // collision thingies unsigned manageAllCollisions(); void remCollidingProjectiles(); void moveMissiles(); void moveTorpedos(); + bool checkMissilesAndPlayer(); + bool checkTorpedosAndInvaders(); public: // in case someone wants to mess with the code, here's a minimal API, costs nothing to us Game(); diff --git a/headers/drawEngine.h b/headers/pixelManager.h similarity index 83% rename from headers/drawEngine.h rename to headers/pixelManager.h index e849389..4054bea 100644 --- a/headers/drawEngine.h +++ b/headers/pixelManager.h @@ -7,16 +7,18 @@ #include "mingl/shape/rectangle.h" #include "mingl/shape/circle.h" -class DrawEngine{ +class PixelManager{ public: MinGL window; - DrawEngine(); + PixelManager(); void dessinerInvader1(const nsGraphics::Vec2D& baseVector); void dessinerInvader2(const nsGraphics::Vec2D& baseVector); void dessinerInvader3(const nsGraphics::Vec2D& baseVector); unsigned showInitialMenu(); unsigned showDeathMenu(); + unsigned getScreenHeight(); + unsigned getScreenWidth(); }; diff --git a/headers/pos.h b/headers/pos.h deleted file mode 100644 index a6532f7..0000000 --- a/headers/pos.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef SPACE_POS_H -#define SPACE_POS_H - -#include - -class pos : public nsGraphics::Vec2D { -public: - bool isColliding(pos& p, unsigned rx, unsigned ry); -}; - -#endif //SPACE_POS_H diff --git a/headers/position.h b/headers/position.h new file mode 100644 index 0000000..35126db --- /dev/null +++ b/headers/position.h @@ -0,0 +1,11 @@ +#ifndef SPACE_POSITION_H +#define SPACE_POSITION_H + +#include + +class position : public nsGraphics::Vec2D { +public: + bool isColliding(position& p, unsigned rx, unsigned ry); +}; + +#endif //SPACE_POSITION_H diff --git a/headers/utils.h b/headers/utils.h index 4dba7cb..b396f82 100644 --- a/headers/utils.h +++ b/headers/utils.h @@ -1,18 +1,27 @@ -#ifndef SPACE_STRUCTS -#define SPACE_STRUCTS +#ifndef SPACE_STRUCTS_H +#define SPACE_STRUCTS_H #include #include +#include "position.h" +// hardcoded values #define PLAYER_WINS 1 #define INVADERS_WINS 2 +#define PLAYER_HEIGHT 100 +#define PLAYER_HEIGHT 100 +#define PROJ_LENGTH_FACTOR 2 using namespace std; -#define PLAYER_HEIGHT 100 - typedef unsigned Alien; typedef vector aliensLine; typedef vector aliensGrid; +typedef position missile; +typedef position torpedo; + +// didn't want to use position because of the semantic with x and y +bool lineCollideCheck(unsigned start1, unsigned end1, unsigned start2, unsigned end2); + #endif \ No newline at end of file diff --git a/lib_headers/mingl/graphics/vec2d.h b/lib_headers/mingl/graphics/vec2d.h index c3b7739..e7cb51c 100755 --- a/lib_headers/mingl/graphics/vec2d.h +++ b/lib_headers/mingl/graphics/vec2d.h @@ -37,7 +37,7 @@ public: /** * @brief Constructeur de recopie pour la classe Vec2D * @param[in] pos : Vec2D a copier - * @fn Vec2D(const Vec2D& pos); + * @fn Vec2D(const Vec2D& position); */ Vec2D(const Vec2D& pos); @@ -56,21 +56,21 @@ public: /** * @brief Opérateur d'addition * @param[in] pos : Vecteur a additionner - * @fn Vec2D operator+(const Vec2D& pos) const; + * @fn Vec2D operator+(const Vec2D& position) const; */ Vec2D operator+(const Vec2D& pos) const; /** * @brief Opérateur de soustraction * @param[in] pos : Vecteur a soustraire - * @fn Vec2D operator-(const Vec2D& pos) const; + * @fn Vec2D operator-(const Vec2D& position) const; */ Vec2D operator-(const Vec2D& pos) const; /** * @brief Opérateur de multiplication * @param[in] pos : Vecteur a multiplier - * @fn Vec2D operator*(const Vec2D& pos) const; + * @fn Vec2D operator*(const Vec2D& position) const; */ Vec2D operator*(const Vec2D& pos) const; @@ -84,7 +84,7 @@ public: /** * @brief Opérateur de division * @param[in] pos : Vecteur a diviser - * @fn Vec2D operator/(const Vec2D& pos) const; + * @fn Vec2D operator/(const Vec2D& position) const; */ Vec2D operator/(const Vec2D& pos) const; @@ -98,21 +98,21 @@ public: /** * @brief Opérateur modulo * @param[in] pos : Vecteur avec lequel faire un modulo - * @fn Vec2D operator%(const Vec2D& pos) const; + * @fn Vec2D operator%(const Vec2D& position) const; */ Vec2D operator%(const Vec2D& pos) const; /** * @brief Opérateur d'égalité * @param[in] pos : Vecteur avec lequel vérifier l'égalité - * @fn bool operator==(const Vec2D& pos) const; + * @fn bool operator==(const Vec2D& position) const; */ bool operator==(const Vec2D& pos) const; /** * @brief Opérateur d'inégalité * @param[in] pos : Vecteur avec lequel vérifier l'inégalité - * @fn bool operator!=(const Vec2D& pos) const; + * @fn bool operator!=(const Vec2D& position) const; */ bool operator!=(const Vec2D& pos) const; @@ -120,7 +120,7 @@ public: * @brief Opérateur de stricte infériorité * (Vérifie la stricte infériorité de la magnitude des deux vecteurs) * @param[in] pos : Vecteur avec lequel vérifier la stricte infériorité - * @fn bool operator<(const Vec2D& pos) const; + * @fn bool operator<(const Vec2D& position) const; */ bool operator<(const Vec2D& pos) const; @@ -128,7 +128,7 @@ public: * @brief Opérateur de stricte supériorité * (Vérifie la stricte supériorité de la magnitude des deux vecteurs) * @param[in] pos : Vecteur avec lequel vérifier la stricte supériorité - * @fn bool operator>(const Vec2D& pos) const; + * @fn bool operator>(const Vec2D& position) const; */ bool operator>(const Vec2D& pos) const; @@ -136,7 +136,7 @@ public: * @brief Opérateur d'infériorité * (Vérifie l'infériorité de la magnitude des deux vecteurs) * @param[in] pos : Vecteur avec lequel vérifier l'infériorité - * @fn bool operator<=(const Vec2D& pos) const; + * @fn bool operator<=(const Vec2D& position) const; */ bool operator<=(const Vec2D& pos) const; @@ -144,49 +144,49 @@ public: * @brief Opérateur de supériorité * (Vérifie la supériorité de la magnitude des deux vecteurs) * @param[in] pos : Vecteur avec lequel vérifier la supériorité - * @fn bool operator>=(const Vec2D& pos) const; + * @fn bool operator>=(const Vec2D& position) const; */ bool operator>=(const Vec2D& pos) const; /** * @brief Opérateur d'assignement * @param[in] pos : Vecteur source - * @fn Vec2D& operator=(const Vec2D& pos); + * @fn Vec2D& operator=(const Vec2D& position); */ Vec2D& operator=(const Vec2D& pos); /** * @brief Opérateur d'addition avec assignement * @param[in] pos : Vecteur avec lequel additionner le vecteur actuel - * @fn Vec2D& operator+=(const Vec2D& pos); + * @fn Vec2D& operator+=(const Vec2D& position); */ Vec2D& operator+=(const Vec2D& pos); /** * @brief Opérateur de soustraction avec assignement * @param[in] pos : Vecteur avec lequel soustraire le vecteur actuel - * @fn Vec2D& operator-=(const Vec2D& pos); + * @fn Vec2D& operator-=(const Vec2D& position); */ Vec2D& operator-=(const Vec2D& pos); /** * @brief Opérateur de multiplication avec assignement * @param[in] pos : Vecteur avec lequel multiplier le vecteur actuel - * @fn Vec2D& operator*=(const Vec2D& pos); + * @fn Vec2D& operator*=(const Vec2D& position); */ Vec2D& operator*=(const Vec2D& pos); /** * @brief Opérateur de division avec assignement * @param[in] pos : Vecteur avec lequel diviser le vecteur actuel - * @fn Vec2D& operator/=(const Vec2D& pos); + * @fn Vec2D& operator/=(const Vec2D& position); */ Vec2D& operator/=(const Vec2D& pos); /** * @brief Opérateur modulo avec assignement * @param[in] pos : Vecteur avec lequel faire un modulo sur le vecteur actuel - * @fn Vec2D& operator%=(const Vec2D& pos); + * @fn Vec2D& operator%=(const Vec2D& position); */ Vec2D& operator%=(const Vec2D& pos); diff --git a/src/game_basics.cpp b/src/game_basics.cpp index 38009bb..e9f7c8c 100644 --- a/src/game_basics.cpp +++ b/src/game_basics.cpp @@ -17,7 +17,7 @@ void Game::managedGames() { } void Game::initialMenuHandler(){ - switch(drawer.showInitialMenu()){ + switch(pm.showInitialMenu()){ case 0:{ // play return; } @@ -26,7 +26,7 @@ void Game::initialMenuHandler(){ } void Game::deathMenuHandler(){ - switch(drawer.showDeathMenu()){ + switch(pm.showDeathMenu()){ case 0:{ // play return; } @@ -41,8 +41,7 @@ void Game::deathMenuHandler(){ unsigned Game::playGame(){ // returns when game is finished // INIT grid = conf.grid; // will copy the vector - player.setX() - player.setY() + playerX = 0; // GAMELOOP while(true){ @@ -60,7 +59,7 @@ unsigned Game::playGame(){ // returns when game is finished void Game::display() { for (unsigned i = 0; i < this->grid.size(); ++i){ for (unsigned j = 0; j < this->grid[0].size(); ++j){ - drawer.dessinerInvader1(nsGraphics::Vec2D( + pm.dessinerInvader1(nsGraphics::Vec2D( baseX+j*conf.alien_size+j*conf.distance, baseY+i*conf.alien_size+i*conf.distance )); diff --git a/src/game_managers.cpp b/src/game_managers.cpp index 03c897e..84d7f5a 100644 --- a/src/game_managers.cpp +++ b/src/game_managers.cpp @@ -1,4 +1,8 @@ #include "game.h" + +#define MISSILE_SPEED 5 +#define TORPEDO_SPEED MISSILE_SPEED + /** Makes the player play once */ void Game::managePlayer(){ @@ -14,63 +18,87 @@ bool Game::manageInvaders(){ } -/** makes projectile smove, and manage collisions between everything +/** makes projectiles move, and manage collisions between everything * * @return 1 if the invaders are all dead, 2 if the player is dead, else 0 */ - - void Game::remCollidingProjectiles(){ - auto i = missiles.begin(); - auto j = torpedos.begin(); + auto miss = missiles.begin(); + auto tor = torpedos.begin(); - while(i!=missiles.end()){ - bool flag = true; - while(j!=torpedos.end()){ - if(*i==*j){ - missiles.erase(i); - torpedos.erase(j); - flag = false; + while(miss != missiles.end()){ + bool wasColling = false; + while(tor != torpedos.end()){ + + // missiles can't be right under torpedos, so that must means they are colliding in Y + if(miss->getY()+conf.missile_lengthgetY()){ + + } + if(lineCollideCheck( // now check if they collide in X + miss->getX(), miss->getX()+conf.missile_width, + tor->getX(), tor->getX()+conf.torpedo_width)){ + missiles.erase(miss); + torpedos.erase(tor); + wasColling = true; break; } - ++j; + ++tor; } - if(flag)++i; + /* if it was colling, it was removed and his position is now replaced by the next. + * Else, go to the next + */ + if(!wasColling)++miss; } } void Game::moveMissiles() { - auto i = missiles.begin(); - while (i != missiles.end()) { - if (i->getY() == 0)missiles.erase(i); - else if (PLAYER_HEIGHT(me, *i))return INVADERS_WINS; + auto miss = missiles.begin(); + while (miss != missiles.end()) { + if (miss->getY() >= pm.getScreenHeight())missiles.erase(miss); else { - ++(i->line); - ++i; + miss->setY(miss->getY()+MISSILE_SPEED); + ++miss; } } } void Game::moveTorpedos() { - auto i = torpedos.begin(); - while (i != torpedos.end()) { - i->setY(i->getY()-1); - if (i->getY() == NB_LIGNES)torpedo.erase(i); - else if (contains(game.invader, *i))return PLAYER_WINS; - else ++i; + auto tor = torpedos.begin(); + while (tor != torpedos.end()) { + if (tor->getY() <= 0)torpedos.erase(tor); // TODO maybe wait for it to be 100% offscreen + else{ + tor->setY(tor->getY() - TORPEDO_SPEED); + ++tor; + } } } +bool Game::checkMissilesAndPlayer() { + for(missile& miss : missiles){ + if(miss.getY()<=PLAYER_HEIGHT){ // colliding on Y + if(lineCollideCheck( // now check collision on X + miss.getX(), miss.getX()+conf.missile_width, + playerX, playerX+conf.player_width)){ + return true; + } + } + } + return false; +} + +bool Game::checkTorpedosAndInvaders() { + return false; +} + unsigned Game::manageAllCollisions() { moveMissiles(); - if(checkMissilesAndPlayer())return INVADERS_WINS; - remCollidingProjectiles(); - moveTorpedos(); - remCollidingProjectiles(); + if(checkMissilesAndPlayer())return INVADERS_WINS; + if(checkTorpedosAndInvaders())return PLAYER_WINS; + return 0; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index d3f3733..33ebaf7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,5 +5,6 @@ using namespace std; int main(){ Game g; g.managedGames(); + return 0; } \ No newline at end of file diff --git a/src/drawEngine.cpp b/src/pixelManager.cpp similarity index 84% rename from src/drawEngine.cpp rename to src/pixelManager.cpp index d95291a..73906ca 100644 --- a/src/drawEngine.cpp +++ b/src/pixelManager.cpp @@ -1,13 +1,13 @@ -#include "drawEngine.h" +#include "pixelManager.h" #define WININIT window("space invader du turfu ma gueule", nsGraphics::Vec2D(1280, 720), nsGraphics::Vec2D(128, 128), nsGraphics::KBlack) -DrawEngine::DrawEngine() : WININIT { +PixelManager::PixelManager() : WININIT { window.initGlut(); window.initGraphic(); } -void DrawEngine::dessinerInvader1(const nsGraphics::Vec2D& baseVector){ +void PixelManager::dessinerInvader1(const nsGraphics::Vec2D& baseVector){ window << nsShape::Circle(nsGraphics::Vec2D(50, 50)+baseVector, 50, nsGraphics::KGray); window << nsShape::Circle(nsGraphics::Vec2D(60, 50)+baseVector, 50, nsGraphics::KGray); window << nsShape::Triangle(nsGraphics::Vec2D(35, 50)+baseVector, nsGraphics::Vec2D(15, 25)+baseVector, nsGraphics::Vec2D(15, 75)+baseVector, nsGraphics::KBlack); @@ -18,7 +18,7 @@ void DrawEngine::dessinerInvader1(const nsGraphics::Vec2D& baseVector){ } -void DrawEngine::dessinerInvader2(const nsGraphics::Vec2D& baseVector){ +void PixelManager::dessinerInvader2(const nsGraphics::Vec2D& baseVector){ window << nsShape::Circle(nsGraphics::Vec2D(50, 50)+baseVector, 50, nsGraphics::KRed); window << nsShape::Circle(nsGraphics::Vec2D(60, 50)+baseVector, 50, nsGraphics::KRed); window << nsShape::Rectangle(nsGraphics::Vec2D(25, 30)+baseVector, nsGraphics::Vec2D(45, 40)+baseVector, nsGraphics::KBlack); @@ -26,7 +26,7 @@ void DrawEngine::dessinerInvader2(const nsGraphics::Vec2D& baseVector){ window << nsShape::Rectangle(nsGraphics::Vec2D(35, 65)+baseVector, nsGraphics::Vec2D(75, 72)+baseVector, nsGraphics::KBlack); } -void DrawEngine::dessinerInvader3(const nsGraphics::Vec2D& baseVector){ +void PixelManager::dessinerInvader3(const nsGraphics::Vec2D& baseVector){ window << nsShape::Circle(nsGraphics::Vec2D(60, 50)+baseVector, 50, nsGraphics::KGreen); window << nsShape::Circle(nsGraphics::Vec2D(50, 50)+baseVector, 50, nsGraphics::KGreen); window << nsShape::Circle(nsGraphics::Vec2D(35, 35)+baseVector, 10, nsGraphics::KBlack); @@ -35,10 +35,14 @@ void DrawEngine::dessinerInvader3(const nsGraphics::Vec2D& baseVector){ } -unsigned DrawEngine::showInitialMenu(){ +unsigned PixelManager::showInitialMenu(){ return 0; } -unsigned DrawEngine::showDeathMenu() { +unsigned PixelManager::showDeathMenu() { + return 0; +} + +unsigned PixelManager::getScreenHeight() { return 0; } \ No newline at end of file diff --git a/src/pos.cpp b/src/position.cpp similarity index 52% rename from src/pos.cpp rename to src/position.cpp index b2207bd..b3ca584 100644 --- a/src/pos.cpp +++ b/src/position.cpp @@ -1,6 +1,6 @@ -#include "pos.h" +#include "position.h" #include -bool pos::isColliding(pos& p, unsigned rx, unsigned ry) { +bool position::isColliding(position& p, unsigned rx, unsigned ry) { return nsGraphics::Vec2D::isColliding(p, p+nsGraphics::Vec2D(rx, ry)); } \ No newline at end of file diff --git a/src/utils.cpp b/src/utils.cpp new file mode 100644 index 0000000..b39d7ae --- /dev/null +++ b/src/utils.cpp @@ -0,0 +1,6 @@ +#include "utils.h" + +bool lineCollideCheck(unsigned start1, unsigned end1, unsigned start2, unsigned end2){ + return start1 < end2 != start2 < end1; + // if true, are colliding. I like truth tables +} \ No newline at end of file