-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighScoresState.cpp
162 lines (137 loc) · 4.26 KB
/
HighScoresState.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "HighScoresState.h"
void HighScoresState::loadHighScores(std::string filename)
{
std::ifstream file(filename);
for (unsigned int index = 0; index < game->highScoresNumber; index++)
{
file >> highScores[index].first >> highScores[index].second;
}
}
void HighScoresState::saveHighScores(std::string filename)
{
std::ofstream file(filename);
for (auto score : highScores)
{
file << score.first << "\t" << score.second << "\n";
}
}
void HighScoresState::resetHighScores()
{
//Default random high scores.
highScores = {
std::make_pair("Caterina" , 100),
std::make_pair("Reggie" , 90),
std::make_pair("Xiomara" , 80),
std::make_pair("Donnell" , 70),
std::make_pair("Han" , 60),
std::make_pair("Suzan" , 50),
std::make_pair("Bettyann" , 40),
std::make_pair("Julian" , 30),
std::make_pair("Michiko" , 20),
std::make_pair("Eleonor" , 10)
};
saveHighScores(scoresFilename);
refreshHighScores();
}
void HighScoresState::refreshHighScores()
{
unsigned int index = 0;
for (auto highScore : highScores)
{
guiSystem.at("names").entries[index].text.setString(highScore.first);
guiSystem.at("scores").entries[index++].text.setString(std::to_string(highScore.second));
}
}
void HighScoresState::draw(const float dt)
{
game->window.setView(view);
game->window.clear(sf::Color::Black);
game->window.draw(background);
for (auto gui : guiSystem)
game->window.draw(gui.second);
}
void HighScoresState::update(const float dt)
{
}
void HighScoresState::handleInput()
{
sf::Event event;
sf::Vector2f mousePosition = game->window.mapPixelToCoords(sf::Mouse::getPosition(game->window), view);
while (game->window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
{
game->closeGame();
break;
}
case sf::Event::MouseMoved:
{
guiSystem.at("buttons").highlight(guiSystem.at("buttons").getEntry(mousePosition));
break;
}
case sf::Event::MouseButtonPressed:
{
if (event.mouseButton.button == sf::Mouse::Left)
{
std::string message = guiSystem.at("buttons").activate(mousePosition);
if (message == "reset_scores")
{
resetHighScores();
}
else if (message == "main_menu")
{
game->popState();
}
}
break;
}
default:
break;
}
}
}
HighScoresState::HighScoresState(std::shared_ptr<GameManager> game, std::string highScoresFilename) :
scoresFilename(highScoresFilename)
{
this->game = game;
loadHighScores(scoresFilename);
background.setTexture(game->textureManager.getTexture("menuBackground"));
sf::Vector2f position = static_cast<sf::Vector2f>(game->window.getSize());
view.setSize(position);
position *= 0.5f;
view.setCenter(position);
float buttonWidth = 192;
float buttonHeight = 32;
float highScoreHeight = 32;
float nameWidth = 256;
float scoreWidth = 64;
guiSystem.emplace("buttons", Gui(sf::Vector2f(buttonWidth, buttonHeight), 4, false, game->styleSheets.at("text2"),
{
std::make_pair("Back", "main_menu"),
std::make_pair("Reset scores", "reset_scores")
}));
guiSystem.at("buttons").setPosition(game->window.getSize().x - buttonWidth / 2, buttonHeight / 2);
guiSystem.at("buttons").setOrigin(buttonWidth / 2, buttonHeight /2);
guiSystem.at("buttons").show();
std::vector < std::pair<std::string, std::string>> namesVector;
std::vector < std::pair<std::string, std::string>> scoresVector;
for (auto highScore : highScores)
{
namesVector.emplace_back(std::make_pair(highScore.first, "null"));
scoresVector.emplace_back(std::make_pair(std::to_string(highScore.second), "null"));
}
guiSystem.emplace("names", Gui(sf::Vector2f(nameWidth, highScoreHeight), 4, false, game->styleSheets.at("text2"), namesVector));
guiSystem.emplace("scores", Gui(sf::Vector2f(scoreWidth, highScoreHeight), 4, false, game->styleSheets.at("text2"), scoresVector));
guiSystem.at("names").setPosition(game->window.getSize().x / 2 - scoreWidth / 2, 9 * highScoreHeight / 2);
guiSystem.at("names").setOrigin(nameWidth / 2, highScoreHeight / 2);
guiSystem.at("names").show();
guiSystem.at("scores").setPosition(game->window.getSize().x / 2 + nameWidth / 2, 9 * highScoreHeight / 2);
guiSystem.at("scores").setOrigin(scoreWidth / 2, highScoreHeight / 2);
guiSystem.at("scores").show();
}
HighScoresState::~HighScoresState()
{
saveHighScores(scoresFilename);
}