From 00aebd652ea47170d5480df9258b39986fbb1f5b Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 3 Jan 2022 17:49:31 +0100 Subject: [PATCH] implemented save --- QUESTIONS.md | 9 ++++++--- headers/game.h | 2 ++ headers/pixelManager.h | 3 ++- headers/scoresManager.h | 12 ++++++------ headers/utils.h | 2 +- scores.kus | 11 +++++------ src/gameBasics.cpp | 9 +++++++-- src/gameManagers.cpp | 24 ++++++++++++------------ src/pixelManager.cpp | 3 ++- src/scoresManager.cpp | 16 ++++++++-------- 10 files changed, 51 insertions(+), 40 deletions(-) diff --git a/QUESTIONS.md b/QUESTIONS.md index 704c164..f4424dd 100644 --- a/QUESTIONS.md +++ b/QUESTIONS.md @@ -1,9 +1,12 @@ Questions que je (Thomas Rubini) voudrais poser - Est-ce que vous préférez l'implémentation orienté objet ou orienté macro pour lire la config ? Pourquoi ? -- Est-ce que vouloir faire des structures optimisées (pas de redondance de mémoire) est une bonne chose, ou pas importa,t ? - Est-ce que traduire les chars A B et C (identifiants des types d'aliens) tirés de la config en valeurs d'enum est une bonne chose, pas important, ou contre-productif ? - Est-ce que mon implémentation du réseau est bonne ? -- Est-on obligé d'utiliser size_t quand on sait que la taille du vecteur ne dépassera jamais concrètement la taille d'un int (cas précis : taille de 100 maximum, est-on obligé d'utiliser size_t de 8 bytes ?) - Est-ce mon implémentation du multithreading est bonne ? -- Que pensez-vous de la sémantique de déplacement, plutot que la référence constante ? \ No newline at end of file + +- Est-on obligé d'utiliser size_t quand on sait que la taille du vecteur ne dépassera jamais concrètement la taille d'un int (cas précis : taille de 100 maximum, est-on obligé d'utiliser size_t de 8 bytes ?) +- Que pensez-vous de la sémantique de déplacement, plutot que la référence constante ? +- Est-ce qu'on doit forcément utiliser const pour des valeurs primitives (int, float...) qu'on ne touche pas en paramètres de fonction ? +- Est-ce que vouloir faire des structures optimisées (pas de redondance de mémoire) est une bonne chose, ou pas importa,t ? +- Pour import MinGL, il vaut mieux utiliser "" ou <> ? \ No newline at end of file diff --git a/headers/game.h b/headers/game.h index f25e5f2..58b8229 100644 --- a/headers/game.h +++ b/headers/game.h @@ -9,6 +9,7 @@ #include "playMode.h" #include "configData.h" #include "projectiles.h" +#include "scoresManager.h" using namespace std; @@ -17,6 +18,7 @@ private: MinGL window; PixelManager pm; ConfigData confData; + ScoresManager sm; Position basePos; InvadersGrid grid; diff --git a/headers/pixelManager.h b/headers/pixelManager.h index 571270b..1337caa 100644 --- a/headers/pixelManager.h +++ b/headers/pixelManager.h @@ -8,6 +8,7 @@ #include "mingl/shape/rectangle.h" #include "mingl/shape/circle.h" #include "mingl/gui/sprite.h" +#include "utils.h" using namespace std; @@ -33,7 +34,7 @@ public: void startFrame(); void endFrame(); - void askPlayerNameMenu(string& name); + void askPlayerNameMenu(playerID pID, string& name); }; diff --git a/headers/scoresManager.h b/headers/scoresManager.h index f09b043..1fd4582 100644 --- a/headers/scoresManager.h +++ b/headers/scoresManager.h @@ -7,17 +7,17 @@ using namespace std; -struct Score{ +struct ScoreLink{ string name; - unsigned points; - Score() = default; - Score(const string& name, unsigned points); + unsigned score; + ScoreLink() = default; + ScoreLink(const string& name, unsigned score); }; class ScoresManager { public: - vector scores; - void inputScore(const string& name, unsigned points); + vector scores; + void inputScore(const string& name, unsigned score); void readFile(); void writeFile(); }; diff --git a/headers/utils.h b/headers/utils.h index a7ab523..a944976 100644 --- a/headers/utils.h +++ b/headers/utils.h @@ -28,7 +28,7 @@ public: }; typedef vector InvadersGrid; typedef nsGraphics::Vec2D Position; -typedef unsigned playerID; +typedef unsigned playerID; // 0 for player 1, 1 for player 2 // didn't want to use Position because of the semantic with x and y bool areLinesColliding(unsigned start1, unsigned end1, unsigned start2, unsigned end2); diff --git a/scores.kus b/scores.kus index 296ef97..019cf10 100644 --- a/scores.kus +++ b/scores.kus @@ -1,6 +1,5 @@ -13363418835389185581 -thomas2,2 -thomas2,2 -thomas2,2 -thomas2,2 -thomas2,2 +1722516557529414056 +Thomas,1000 +Thomas,0 +Thomas,0 +Thomas,0 diff --git a/src/gameBasics.cpp b/src/gameBasics.cpp index 0cb8a8b..b8b1ac3 100644 --- a/src/gameBasics.cpp +++ b/src/gameBasics.cpp @@ -10,6 +10,7 @@ Game::Game() : WININIT, pm(window) { if(!reloadConfig()){ throw runtime_error("Initial config loading failed. Please check the error above and fix the configuration"); } + sm.readFile(); } void Game::updateColumns(){ @@ -27,10 +28,14 @@ void Game::handleScoreSaving(){ cout << players[0].score << endl; // will remove string pName; - pm.askPlayerNameMenu(pName); + pm.askPlayerNameMenu(0, pName); + sm.inputScore(pName, players[0].score); if(playMode==PlayMode::TWO_LOCAL){ - + string pName2; + pm.askPlayerNameMenu(1, pName2); + sm.inputScore(pName, players[1].score); } + sm.writeFile(); } void Game::managedGames() { diff --git a/src/gameManagers.cpp b/src/gameManagers.cpp index 4e1994e..1fc5b56 100644 --- a/src/gameManagers.cpp +++ b/src/gameManagers.cpp @@ -1,21 +1,21 @@ #include "game.h" #define ISPRESSED(ID, X) window.isPressed({confData.playerDefs[ID].keys.X, false}) -void Game::manageOnePlayer(unsigned id){ - if (ISPRESSED(id, left)){ - if(players[id].x < confData.playersSpeed) players[id].x = 0; - else players[id].x -= confData.playersSpeed; +void Game::manageOnePlayer(playerID pID){ + if (ISPRESSED(pID, left)){ + if(players[pID].x < confData.playersSpeed) players[pID].x = 0; + else players[pID].x -= confData.playersSpeed; } - if (ISPRESSED(id, right)){ - if(players[id].x + confData.playersWidth + confData.playersSpeed >= pm.getScreenWidth()) players[id].x = pm.getScreenWidth() - confData.playersWidth - 1; - else players[id].x += confData.playersSpeed; + if (ISPRESSED(pID, right)){ + if(players[pID].x + confData.playersWidth + confData.playersSpeed >= pm.getScreenWidth()) players[pID].x = pm.getScreenWidth() - confData.playersWidth - 1; + else players[pID].x += confData.playersSpeed; } - if(players[id].fireCooldown==0) { - if (ISPRESSED(id, shoot)) { - torpedos.emplace_back(players[id].x + confData.playersWidth / 2, pm.getScreenHeight() - PLAYER_HEIGHT, id); - players[id].fireCooldown = confData.playersFireCooldown; + if(players[pID].fireCooldown==0) { + if (ISPRESSED(pID, shoot)) { + torpedos.emplace_back(players[pID].x + confData.playersWidth / 2, pm.getScreenHeight() - PLAYER_HEIGHT, pID); + players[pID].fireCooldown = confData.playersFireCooldown; } - }else --players[id].fireCooldown; + }else --players[pID].fireCooldown; } /** Makes the players play once diff --git a/src/pixelManager.cpp b/src/pixelManager.cpp index b557465..a5b9788 100644 --- a/src/pixelManager.cpp +++ b/src/pixelManager.cpp @@ -44,7 +44,8 @@ void PixelManager::drawPlayer(const nsGraphics::Vec2D& baseVector, unsigned widt window << nsShape::Triangle(nsGraphics::Vec2D(15+width,720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(15+width*2,720-PLAYER_HEIGHT/2)+baseVector, nsGraphics::Vec2D(15+width,720-PLAYER_HEIGHT*0.9)+baseVector, color); } -void PixelManager::askPlayerNameMenu(string& name){ +void PixelManager::askPlayerNameMenu(playerID pID, string& name){ + cout << "ask for player " << (pID+1) << endl; name = "Thomas"; } diff --git a/src/scoresManager.cpp b/src/scoresManager.cpp index 1cae3e8..4e7f9d0 100644 --- a/src/scoresManager.cpp +++ b/src/scoresManager.cpp @@ -61,10 +61,10 @@ void ScoresManager::writeFile() { ofstream ofs(SCORE_FILE); string str; // this one must be counted in the hash too - for(Score& sc : scores){ + for(ScoreLink& sc : scores){ str.append(sc.name); str.append(","); - str.append(to_string(sc.points)); + str.append(to_string(sc.score)); str.append("\n"); } @@ -74,22 +74,22 @@ void ScoresManager::writeFile() { /** * Insertion sort, probably the most efficient here */ -void ScoresManager::inputScore(const string& name, unsigned points) { +void ScoresManager::inputScore(const string& name, unsigned score) { auto ite = scores.begin(); while(ite!=scores.end()){ - if(points>ite->points) { - scores.emplace(ite, name, points); + if(score > ite->score) { + scores.emplace(ite, name, score); break; } ++ite; } - if(ite==scores.end())scores.emplace(ite, name, points); + if(ite==scores.end())scores.emplace(ite, name, score); if(scores.size()==6)scores.resize(5); } // Not sure if I should use move semantics -Score::Score(const string& name, unsigned int points) { +ScoreLink::ScoreLink(const string& name, unsigned int score) { this->name = name; - this->points = points; + this->score = score; }