Did things

This commit is contained in:
Thomas 2022-01-09 16:32:54 +01:00
parent 09bce989d2
commit f2822815dd
No known key found for this signature in database
GPG Key ID: E538821A6CDFDAD7
8 changed files with 34 additions and 54 deletions

View File

@ -50,7 +50,7 @@ private:
ConfigData confData;
/*!
* @brief
* @brief ScoresManager : utility to handle score saving and signing
*/
ScoresManager sm;

View File

@ -141,7 +141,7 @@ public:
* @param[]
* @fn
*/
void drawSprite(const nsGui::Sprite& sprite, const Position& pos) const;
void drawSprite(const nsGui::Sprite& sprite, const Position& pos = Position(0, 0)) const;
/*!
* @brief
@ -162,16 +162,6 @@ public:
* @param[]
* @fn
*/
void drawGameBackground() const;
/*!
* @brief
* @param[]
* @fn
*/
void drawMenuBackground() const;
// TODO remove because unused ?
void displayText(const Position& pos, const string& text, const nsGraphics::RGBAcolor& color = nsGraphics::KWhite) const;
/*!
@ -182,8 +172,7 @@ public:
void drawFPS(unsigned fps) const;
PlayMode showInitialMenu();
PlayMode showDeathMenu();
string nameMenu(playerID pID);
bool showDeathMenu();
unsigned getScreenHeight() const;
unsigned getScreenWidth() const;

View File

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

View File

@ -69,11 +69,6 @@ void PixelManager::drawPlayer(unsigned x, unsigned width, const nsGraphics::RGBA
window << Triangle(Position(15+width+x,720-PLAYER_HEIGHT/2), Position(15+width*2+x,720-PLAYER_HEIGHT/2), Position(15+width+x,720-PLAYER_HEIGHT*0.9), color);
}
void PixelManager::askPlayerNameMenu(playerID pID, string& name) {
cout << "ask for player " << (pID+1) << endl;
name = nameMenu(pID);
}
void PixelManager::drawMissile(const Position& baseVector, unsigned width, const nsGraphics::RGBAcolor& color) const {
window << Rectangle(baseVector, baseVector + Position(width, width * PROJ_LENGTH_FACTOR), color);
}
@ -88,14 +83,6 @@ void PixelManager::drawSprite(const Sprite& sprite, const Position& pos) const {
sprite.draw(window);
}
void PixelManager::drawGameBackground() const {
gameBackground.draw(window);
}
void PixelManager::drawMenuBackground() const {
menuBackground.draw(window);
}
unsigned PixelManager::getScreenHeight() const {
return window.getWindowSize().getY();
}

View File

@ -28,7 +28,7 @@ void PixelManager::displayButton(const Position& baseVector,const string& text,n
void PixelManager::displayMenu(const Position& pos, Menu& currentMenu){
startFrame();
drawMenuBackground();
drawSprite(menuBackground);
drawSprite(logo,Position(100,50));
displayText(Position(1150,700), "version 1.0.0");
size_t margin = 0;
@ -74,15 +74,16 @@ PlayMode PixelManager::showInitialMenu(){
}
}
}
exit(0);
}
string PixelManager::nameMenu(playerID pID){
string name (6,'A');
void PixelManager::askPlayerNameMenu(playerID pID, string& name) {
name = string(6, 'A');
size_t currentSelected = 0 ;
chrono::milliseconds waitTime = chrono::milliseconds(100);
while (window.isOpen()){
startFrame();
drawMenuBackground();
drawSprite(menuBackground);
displayText(Position(600,100),"Nom du joueur "+ string(1,pID));
for (size_t i = 0; i < name.size(); ++i){
displayText(Position(600+30*i,200),string(1,name[i]),(i == currentSelected) ? nsGraphics::KRed : nsGraphics::KWhite);
@ -115,14 +116,19 @@ string PixelManager::nameMenu(playerID pID){
// select option
else if (window.isPressed({13, false}))
{
this_thread::sleep_for(waitTime);
return name;
window.resetKey({13, false});
return;
}
}
}
exit(0);
}
PlayMode PixelManager::showDeathMenu() {
vector<string> entries {"main menu","exit"};
/**
*
* @return true if the player plays again
*/
bool PixelManager::showDeathMenu() {
vector<string> entries {"retry","main menu"};
Menu death {entries,0,nsGraphics::KRed,nsGraphics::KWhite};
const unsigned xOffset = getScreenHeight() / 2 ;
const unsigned yOffset = getScreenWidth() / 2 - 90;
@ -144,14 +150,13 @@ PlayMode PixelManager::showDeathMenu() {
else if (window.isPressed({13, false})){
switch(death.currentValue){
case 0:
return PlayMode::NONE;
return true;
case 1:
return PlayMode::EXIT;
default:
return PlayMode::EXIT;
return false;
}
}
}
exit(0);
}

View File

@ -16,7 +16,7 @@
* The more important stuff must be drawn last
*/
void Game::displayAll(unsigned fps) const {
pm.drawGameBackground();
pm.drawSprite(pm.gameBackground);
for (unsigned i = 0; i < this->grid.size(); ++i){
for (unsigned j = 0; j < this->grid[i].size(); ++j){
Position vec(

View File

@ -39,15 +39,13 @@ bool Game::updateColumns(){
}
void Game::handleScoreSaving(){
cout << players[0].score << endl; // will remove
string pName;
pm.askPlayerNameMenu(0, pName);
sm.inputScore(pName, players[0].score);
pm.askPlayerNameMenu(PLAYER1, pName);
sm.inputScore(move(pName), players[0].score);
if(playMode==PlayMode::TWO_LOCAL){
string pName2;
pm.askPlayerNameMenu(1, pName2);
sm.inputScore(pName, players[1].score);
pm.askPlayerNameMenu(PLAYER2, pName2);
sm.inputScore(move(pName2), players[1].score);
}
sm.writeFile();
}
@ -65,7 +63,7 @@ void Game::managedGames() {
enterGameLoop(); // will read the playMode
handleScoreSaving();
cout << "END OF GAME" << endl;
playMode = pm.showDeathMenu();
if(!pm.showDeathMenu())playMode = PlayMode::NONE;
}
}
}

View File

@ -81,10 +81,12 @@ void ScoresManager::writeFile() const {
ofs << hasher(str+SECRET_KEY) << endl << str;
}
#define SCORE_LIMIT 10
/**
* Insertion sort, probably the most efficient here
*/
void ScoresManager::inputScore(const string& name, unsigned score) {
void ScoresManager::inputScore(string&& name, unsigned score) {
auto ite = scores.begin();
while(ite!=scores.end()){
if(score > ite->score) {
@ -94,12 +96,11 @@ void ScoresManager::inputScore(const string& name, unsigned score) {
++ite;
}
if(ite==scores.end())scores.emplace(ite, name, score);
if(scores.size()==6)scores.pop_back();
if(scores.size()>SCORE_LIMIT)scores.erase(scores.begin()+SCORE_LIMIT);
}
// Not sure if I should use move semantics
ScoreLink::ScoreLink(const string& name, unsigned int score) {
ScoreLink::ScoreLink(string&& name, unsigned int score) {
this->name = name;
this->score = score;
}