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
- 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 ?
- 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 <> ?

View File

@ -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;

View File

@ -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);
};

View File

@ -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<Score> scores;
void inputScore(const string& name, unsigned points);
vector<ScoreLink> scores;
void inputScore(const string& name, unsigned score);
void readFile();
void writeFile();
};

View File

@ -28,7 +28,7 @@ public:
};
typedef vector<InvadersColumn> 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);

View File

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

View File

@ -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() {

View File

@ -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

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);
}
void PixelManager::askPlayerNameMenu(string& name){
void PixelManager::askPlayerNameMenu(playerID pID, string& name){
cout << "ask for player " << (pID+1) << endl;
name = "Thomas";
}

View File

@ -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;
}