diff --git a/src/include/ripscreen.h b/src/include/ripscreen.h index afcd314b19a064c387688a6920dcd7b4d0467b8b..af3497a9be5b57452e8367c192f4675c7abcee5d 100644 --- a/src/include/ripscreen.h +++ b/src/include/ripscreen.h @@ -2,6 +2,7 @@ #include "uistate.h" #include "playerchar.h" #include "../libtcod/include/libtcod.hpp" +#include "level.h" #include <vector> #include <string> #include <sstream> @@ -10,7 +11,7 @@ struct ScoreItem; class RIPScreen : public UIState { public: - RIPScreen(PlayerChar*); + RIPScreen(PlayerChar*, Level*, std::string); virtual void draw(TCODConsole*); virtual UIState* handleInput(TCOD_key_t); private: diff --git a/src/playstate.cpp b/src/playstate.cpp index 217973ac0c025273d3fe6c9a9026f4ffc0c0cee5..5f38bcc677a6893f7ead4658f9ac298b623cc0c2 100644 --- a/src/playstate.cpp +++ b/src/playstate.cpp @@ -53,7 +53,7 @@ class QuitPrompt : public Prompt { virtual Transition handleInput(TCOD_key_t key) { if (key.c == 'y' || key.c == 'Y') { - return Transition(NULL, new RIPScreen(player)); + return Transition(NULL, new RIPScreen(player, level, "retired")); } if (key.c == 'n' || key.c == 'N') { return Transition(NULL, NULL); diff --git a/src/ripscreen.cpp b/src/ripscreen.cpp index c872e02c9332e187fa999401781a9c1c018616a1..1dd8b346c5721e121b6eb1962935dbed7a551def 100644 --- a/src/ripscreen.cpp +++ b/src/ripscreen.cpp @@ -6,6 +6,7 @@ #include <fstream> #include <sstream> #include <exception> +#include <algorithm> struct ScoreItem { ScoreItem(int gold, int depth, std::string name, std::string death) @@ -48,10 +49,15 @@ struct ScoreItem { } return true; } + + bool operator<(const ScoreItem& other) const { + return gold < other.gold; + } }; -RIPScreen::RIPScreen(PlayerChar* player) +RIPScreen::RIPScreen(PlayerChar* player, + Level* level, std::string reason) : player(player) { std::ifstream scoreFile(SCORE_FILE); @@ -68,23 +74,26 @@ RIPScreen::RIPScreen(PlayerChar* player) } else { std::cerr << "could not open " << SCORE_FILE << "\n"; } - scores.push_back(ScoreItem(player->getGold(), -1, player->getName(), "unknown")); + scores.push_back(ScoreItem(player->getGold(), level->getDepth(), player->getName(), reason)); // create if not exist, otherwise append std::ofstream outStream(SCORE_FILE, std::ios::app | std::ios::out); if (outStream.is_open()) { outStream << scores.back().encode() << "\n"; } outStream.close(); + delete level; + std::sort(scores.begin(), scores.end()); + std::reverse(scores.begin(), scores.end()); } void RIPScreen::draw(TCODConsole* con) { - int i = 0; + int y = 1; for (ScoreItem item : scores) { - con->print(0, i, (std::to_string(item.gold) + ":" + item.name + " " + item.death + con->print(0, y, (std::to_string(item.gold) + ":" + item.name + " " + item.death + " on level " + std::to_string(item.depth)).c_str()); - i++; + y++; } - con->print(10, 10, std::string("rip").c_str()); + con->print(0, 0, std::string("rip").c_str()); } UIState* RIPScreen::handleInput(TCOD_key_t key) { diff --git a/src/scores.txt b/src/scores.txt index 66fcc16d8337830cf5b6ab1234447119632661ce..204dae1b6ec02a4bd83e8ffa45ac386b157d1d4f 100644 --- a/src/scores.txt +++ b/src/scores.txt @@ -1,5 +1,2 @@ -0,-1,quark,unknown, -0,-1,qwerty,unknown -0,-1,brucethebold,unknown -0,-1,arglebargle,unknown 1350,8,robert the mediocre,was killed by a grue +0,0,jordy,retired