implemented save

This commit is contained in:
Thomas 2022-01-03 17:49:31 +01:00
parent 3beb1a4d77
commit 00aebd652e
No known key found for this signature in database
GPG Key ID: E538821A6CDFDAD7
10 changed files with 51 additions and 40 deletions

View File

@ -1,9 +1,12 @@
Questions que je (Thomas Rubini) voudrais poser 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 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 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-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 ? - Est-ce mon implémentation du multithreading 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 ?)
- Que pensez-vous de la sémantique de déplacement, plutot que la référence constante ? - 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 <> ?

View File

@ -9,6 +9,7 @@
#include "playMode.h" #include "playMode.h"
#include "configData.h" #include "configData.h"
#include "projectiles.h" #include "projectiles.h"
#include "scoresManager.h"
using namespace std; using namespace std;
@ -17,6 +18,7 @@ private:
MinGL window; MinGL window;
PixelManager pm; PixelManager pm;
ConfigData confData; ConfigData confData;
ScoresManager sm;
Position basePos; Position basePos;
InvadersGrid grid; InvadersGrid grid;

View File

@ -8,6 +8,7 @@
#include "mingl/shape/rectangle.h" #include "mingl/shape/rectangle.h"
#include "mingl/shape/circle.h" #include "mingl/shape/circle.h"
#include "mingl/gui/sprite.h" #include "mingl/gui/sprite.h"
#include "utils.h"
using namespace std; using namespace std;
@ -33,7 +34,7 @@ public:
void startFrame(); void startFrame();
void endFrame(); void endFrame();
void askPlayerNameMenu(string& name); void askPlayerNameMenu(playerID pID, string& name);
}; };

View File

@ -7,17 +7,17 @@
using namespace std; using namespace std;
struct Score{ struct ScoreLink{
string name; string name;
unsigned points; unsigned score;
Score() = default; ScoreLink() = default;
Score(const string& name, unsigned points); ScoreLink(const string& name, unsigned score);
}; };
class ScoresManager { class ScoresManager {
public: public:
vector<Score> scores; vector<ScoreLink> scores;
void inputScore(const string& name, unsigned points); void inputScore(const string& name, unsigned score);
void readFile(); void readFile();
void writeFile(); void writeFile();
}; };

View File

@ -28,7 +28,7 @@ public:
}; };
typedef vector<InvadersColumn> InvadersGrid; typedef vector<InvadersColumn> InvadersGrid;
typedef nsGraphics::Vec2D Position; 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 // didn't want to use Position because of the semantic with x and y
bool areLinesColliding(unsigned start1, unsigned end1, unsigned start2, unsigned end2); bool areLinesColliding(unsigned start1, unsigned end1, unsigned start2, unsigned end2);

View File

@ -1,6 +1,5 @@
13363418835389185581 1722516557529414056
thomas2,2 Thomas,1000
thomas2,2 Thomas,0
thomas2,2 Thomas,0
thomas2,2 Thomas,0
thomas2,2

View File

@ -10,6 +10,7 @@ Game::Game() : WININIT, pm(window) {
if(!reloadConfig()){ if(!reloadConfig()){
throw runtime_error("Initial config loading failed. Please check the error above and fix the configuration"); throw runtime_error("Initial config loading failed. Please check the error above and fix the configuration");
} }
sm.readFile();
} }
void Game::updateColumns(){ void Game::updateColumns(){
@ -27,10 +28,14 @@ void Game::handleScoreSaving(){
cout << players[0].score << endl; // will remove cout << players[0].score << endl; // will remove
string pName; string pName;
pm.askPlayerNameMenu(pName); pm.askPlayerNameMenu(0, pName);
sm.inputScore(pName, players[0].score);
if(playMode==PlayMode::TWO_LOCAL){ if(playMode==PlayMode::TWO_LOCAL){
string pName2;
pm.askPlayerNameMenu(1, pName2);
sm.inputScore(pName, players[1].score);
} }
sm.writeFile();
} }
void Game::managedGames() { void Game::managedGames() {

View File

@ -1,21 +1,21 @@
#include "game.h" #include "game.h"
#define ISPRESSED(ID, X) window.isPressed({confData.playerDefs[ID].keys.X, false}) #define ISPRESSED(ID, X) window.isPressed({confData.playerDefs[ID].keys.X, false})
void Game::manageOnePlayer(unsigned id){ void Game::manageOnePlayer(playerID pID){
if (ISPRESSED(id, left)){ if (ISPRESSED(pID, left)){
if(players[id].x < confData.playersSpeed) players[id].x = 0; if(players[pID].x < confData.playersSpeed) players[pID].x = 0;
else players[id].x -= confData.playersSpeed; else players[pID].x -= confData.playersSpeed;
} }
if (ISPRESSED(id, right)){ if (ISPRESSED(pID, right)){
if(players[id].x + confData.playersWidth + confData.playersSpeed >= pm.getScreenWidth()) players[id].x = pm.getScreenWidth() - confData.playersWidth - 1; if(players[pID].x + confData.playersWidth + confData.playersSpeed >= pm.getScreenWidth()) players[pID].x = pm.getScreenWidth() - confData.playersWidth - 1;
else players[id].x += confData.playersSpeed; else players[pID].x += confData.playersSpeed;
} }
if(players[id].fireCooldown==0) { if(players[pID].fireCooldown==0) {
if (ISPRESSED(id, shoot)) { if (ISPRESSED(pID, shoot)) {
torpedos.emplace_back(players[id].x + confData.playersWidth / 2, pm.getScreenHeight() - PLAYER_HEIGHT, id); torpedos.emplace_back(players[pID].x + confData.playersWidth / 2, pm.getScreenHeight() - PLAYER_HEIGHT, pID);
players[id].fireCooldown = confData.playersFireCooldown; players[pID].fireCooldown = confData.playersFireCooldown;
} }
}else --players[id].fireCooldown; }else --players[pID].fireCooldown;
} }
/** Makes the players play once /** Makes the players play once

View File

@ -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); 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"; name = "Thomas";
} }

View File

@ -61,10 +61,10 @@ void ScoresManager::writeFile() {
ofstream ofs(SCORE_FILE); ofstream ofs(SCORE_FILE);
string str; // this one must be counted in the hash too 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(sc.name);
str.append(","); str.append(",");
str.append(to_string(sc.points)); str.append(to_string(sc.score));
str.append("\n"); str.append("\n");
} }
@ -74,22 +74,22 @@ void ScoresManager::writeFile() {
/** /**
* Insertion sort, probably the most efficient here * 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(); auto ite = scores.begin();
while(ite!=scores.end()){ while(ite!=scores.end()){
if(points>ite->points) { if(score > ite->score) {
scores.emplace(ite, name, points); scores.emplace(ite, name, score);
break; break;
} }
++ite; ++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); if(scores.size()==6)scores.resize(5);
} }
// Not sure if I should use move semantics // 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->name = name;
this->points = points; this->score = score;
} }