Pacman Torben Petré
Loading...
Searching...
No Matches
Score.h
Go to the documentation of this file.
1#ifndef PACMAN_SCORE_H
2#define PACMAN_SCORE_H
3
4#include <fstream>
5#include <utility>
6#include <vector>
7
8#include "Observer.h"
9
10
11namespace logic {
12
13 constexpr int SCORE_DECAY = 10; // 10 points per second
14 constexpr int GHOST_POINTS = 200;
15 constexpr int LEVEL_CLEAR_POINTS = 500;
16 constexpr int BASE_COIN_POINTS = 10;
17
18
24 struct ScoreEntry {
25 std::string username;
26 int score;
27
31 ScoreEntry() : username(""), score(0) {}
32
39 ScoreEntry(std::string username, const int score) : username(std::move(username)), score(score) {}
40
41
47 bool operator<(const ScoreEntry& other) const {
48 return score > other.score;
49 }
50 };
51
52
53 class Score final : public Observer, public Subject {
54 public:
59 Score();
60
64 ~Score() override;
65
66
72 [[nodiscard]] std::vector<std::unique_ptr<ScoreEntry>>& getHighscores();
73
82 [[nodiscard]] int getScore() const;
83
88 [[nodiscard]] const ScoreEntry& getLastScore() const;
89
90
95 void setUser(const std::string& username);
96
102 void update(double dt);
103
108 void addScoreEntry();
109
113 void write() const;
114
115 private:
116
120 void createHighscoresFile() const;
121
126 void update(Events event) override;
127
128 std::string filename = "highscores.txt";
129 std::vector<std::unique_ptr<ScoreEntry>> highscores;
130 std::string username;
131 ScoreEntry lastScore{};
132
133 // While the World is restarting scores should not be decaying.
134 bool paused = false;
135 int score;
136
137 double timeLastCoin = 0;
138 double accumulator = 0;
139
140 // Since ghost eating points are exponential when done in a row, I keep
141 // track of the current amount.
142 int ghostPoints = 0;
143 };
144}
145
146#endif //PACMAN_SCORE_H
Definition Observer.h:16
Score()
Creates new Score object, and loads entries from highscores.txt file.
Definition Score.cpp:11
int getScore() const
Returns the current score.
Definition Score.cpp:54
void update(double dt)
Updates time since last coin collection and time accumulator, te determine score decay.
Definition Score.cpp:97
const ScoreEntry & getLastScore() const
Returns the last achieved score as a Highscore object.
Definition Score.cpp:58
~Score() override
Overrides destructor behaviour to call Score::write;.
Definition Score.cpp:45
void setUser(const std::string &username)
Sets the user to whom the next ScoreEntry will be attributed.
Definition Score.cpp:63
void write() const
Writes the ScoreEntry objects to the highscores.txt file.
Definition Score.cpp:81
std::vector< std::unique_ptr< ScoreEntry > > & getHighscores()
Returns a reference to the vector holding all known Highscore objects.
Definition Score.cpp:50
void addScoreEntry()
Creates a new score entry for the current user with the current score.
Definition Score.cpp:68
Definition Observer.h:32
Definition Difficulty.h:6
Events
Definition Observer.h:14
constexpr int GHOST_POINTS
Definition Score.h:14
constexpr int SCORE_DECAY
Definition Score.h:13
constexpr int LEVEL_CLEAR_POINTS
Definition Score.h:15
constexpr int BASE_COIN_POINTS
Definition Score.h:16
Definition Score.h:24
bool operator<(const ScoreEntry &other) const
Accesses the score member variables and compares them.
Definition Score.h:47
ScoreEntry(std::string username, const int score)
Constructs a new ScoreEntry object, populated with the provided values.
Definition Score.h:39
std::string username
Definition Score.h:25
int score
Definition Score.h:26
ScoreEntry()
Default constructor initiates an empty object.
Definition Score.h:31