-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameState.cpp
262 lines (236 loc) · 7.54 KB
/
GameState.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "GameState.h"
void GameState::loadGameStateConfig()
{
currentAction = GameAction::Playing;
background.setTexture(game->textureManager.getTexture("labyrinthBackground"));
sf::Vector2f position = sf::Vector2f(game->window.getSize());
guiView.setSize(position);
gameView.setSize(position);
position *= 0.5f;
guiView.setCenter(position);
gameView.setCenter(position);
float buttonWidth = 192;
float buttonHeight = 32;
guiSystem.emplace("menu", Gui(sf::Vector2f(buttonWidth, buttonHeight), 4, false, game->styleSheets.at("text2"),
{
std::make_pair("Main menu", "exit_to_menu"),
std::make_pair("Exit", "exit_game"),
}));
guiSystem.at("menu").setPosition(game->window.getSize().x - buttonWidth / 2, buttonHeight / 2);
guiSystem.at("menu").setOrigin(buttonWidth / 2, buttonHeight / 2);
guiSystem.at("menu").hide();
float finalWidth = 1000;
float finalHeight = 48;
guiSystem.emplace("final", Gui(sf::Vector2f(finalWidth, finalHeight), 0, false, game->styleSheets.at("final_credits"),
{
std::make_pair("The least number of steps was: ", "null"),
std::make_pair("You did it with ", "null"),
std::make_pair(" steps, congratulations.", "null"),
std::make_pair("Your score: ", "null")
}));
guiSystem.at("final").setOrigin(finalWidth / 2, finalHeight / 2);
guiSystem.at("final").hide();
}
void GameState::addScore(std::pair<std::string, unsigned int> newScore)
{
//Load highscores from file and create array of scores.
std::fstream file(game->scoresFilename);
std::array<std::pair<std::string, unsigned int>, 10> highScores;
for (unsigned int index = 0; index < game->highScoresNumber; index++)
file >> highScores[index].first >> highScores[index].second;
file.close();
int index = game->highScoresNumber - 1;
//If new score is less than lowest high score then return.
if (newScore.second <= highScores[index].second)
return;
//If new score should be last on the highscores list.
else if (newScore.second <= highScores[index - 1].second)
{
highScores[index] = newScore;
}
else
{
//Check where new score should be put in.
while ((index > 0) && (newScore.second > highScores[index - 1].second))
{
highScores[index] = highScores[index - 1];
--index;
}
highScores[index] = newScore;
}
//Save to file.
file.open(game->scoresFilename);
for (auto score : highScores)
file << score.first << "\t" << score.second << "\n";
file.close();
}
void GameState::draw(const float deltaTime)
{
//Get position of player in window coordinates.
auto truePosition = player.truePosition();
//Center views on player coordinates.
gameView.setCenter(truePosition);
guiView.setCenter(truePosition);
game->window.setView(gameView);
game->window.clear(sf::Color::Black);
//Draw labyrinth and player.
labyrinth.draw(game->window, deltaTime);
player.draw(game->window, deltaTime);
game->window.setView(guiView);
guiSystem.at("menu").setPosition(truePosition.x - game->window.getSize().x / 2 + guiSystem.at("menu").getOrigin().x,
truePosition.y - game->window.getSize().y / 2 + guiSystem.at("menu").getOrigin().y);
guiSystem.at("final").setPosition(truePosition.x, truePosition.y - 3.0f * guiSystem.at("final").getOrigin().y);
//Draw GUIs.
for (auto& gui : guiSystem)
{
game->window.draw(gui.second);
}
}
void GameState::update(const float deltaTime)
{
switch(currentAction)
{
case(GameAction::Playing):
//If player reaches the end.
if (player.position == labyrinth.exit)
{
currentAction = GameAction::Final;
std::string text;
text = guiSystem.at("final").entries[0].text.getString();
text += std::to_string(labyrinth.shortestPathLength);
guiSystem.at("final").entries[0].text.setString(text);
text = guiSystem.at("final").entries[2].text.getString();
text.insert(0, std::to_string(player.stepsCount));
guiSystem.at("final").entries[2].text.setString(text);
text = guiSystem.at("final").entries[3].text.getString();
unsigned int score = static_cast<unsigned int>(static_cast<float>(labyrinth.shortestPathLength / static_cast<float>(player.stepsCount))
* static_cast<float>(labyrinth.width * labyrinth.height));
text += std::to_string(score);
guiSystem.at("final").entries[3].text.setString(text);
guiSystem.at("menu").show();
guiSystem.at("final").show();
addScore(std::make_pair(playerName, score));
}
player.update(deltaTime);
break;
case(GameAction::Paused) :
break;
case(GameAction::Final) :
break;
}
}
void GameState::handleInput()
{
sf::Event event;
sf::Vector2f mousePosition = game->window.mapPixelToCoords(sf::Mouse::getPosition(game->window), this->gameView);
while (game->window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
{
game->closeGame();
break;
}
case sf::Event::MouseMoved:
{
guiSystem.at("menu").highlight(guiSystem.at("menu").getEntry(mousePosition));
break;
}
case sf::Event::MouseButtonPressed:
{
if (event.mouseButton.button == sf::Mouse::Left)
{
std::string message = guiSystem.at("menu").activate(mousePosition);
if (message == "exit_to_menu")
{
game->popState();
}
else if (message == "exit_game")
{
game->closeGame();
}
}
guiSystem.at("menu").highlight(-1);
break;
}
case sf::Event::KeyPressed:
{
if (currentAction == GameAction::Playing)
{
switch (event.key.code)
{
case (sf::Keyboard::Up) :
player.move(Directions::Up);
break;
case (sf::Keyboard::Right) :
player.move(Directions::Right);
break;
case (sf::Keyboard::Down) :
player.move(Directions::Down);
break;
case (sf::Keyboard::Left) :
player.move(Directions::Left);
break;
case (sf::Keyboard::Escape) :
currentAction = GameAction::Paused;
guiSystem.at("menu").show();
break;
}
}
else if(currentAction == GameAction::Paused)
{
switch (event.key.code)
{
case (sf::Keyboard::Escape) :
{
guiSystem.at("menu").hide();
currentAction = GameAction::Playing;
break;
}
}
}
}
default:
break;
}
}
}
void GameState::saveGame(std::string filename)
{
std::ofstream file(filename, std::ios::out);
file << playerName << "\t" << player.position.x << "\t" << player.position.y << "\t" << player.stepsCount << "\n";
file << labyrinth.width << "\t" << labyrinth.height << "\n";
for (unsigned int row = 0; row < labyrinth.height; row++)
{
for (unsigned int column = 0; column < labyrinth.width; column++)
{
file << static_cast<unsigned int>(labyrinth.at(row, column).tileType) << " " << labyrinth.at(row, column).orientation << " ";
}
file << "\n";
}
}
GameState::GameState(std::shared_ptr<GameManager> game, std::string playerName, unsigned int width, unsigned int height)
: labyrinth(Labyrinth(width, height, game->textureManager, game->tileAtlas)), player({ height / 2, width/ 2 },
game->textureManager, std::make_shared<Labyrinth>(labyrinth))
{
this->game = game;
this->playerName = playerName;
loadGameStateConfig();
}
GameState::GameState(std::shared_ptr<GameManager> game, std::string filename)
: labyrinth(filename, game->textureManager, game->tileAtlas)
{
this->game = game;
std::ifstream file(filename, std::ios::in);
unsigned int playerX, playerY, stepsCount;
file >> playerName >> playerX >> playerY >> stepsCount;
player = Player({ playerX, playerY }, game->textureManager, std::make_shared<Labyrinth>(labyrinth));
player.stepsCount = stepsCount;
loadGameStateConfig();
}
GameState::~GameState()
{
if (currentAction != GameAction::Final)
saveGame(game->saveFilename);
}