-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOptions.cpp
327 lines (296 loc) · 9.77 KB
/
GameOptions.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include "GameOptions.h"
#include "Menu.h"
#include "Styles.h"
GameOptions::GameOptions(StateManager& stateManager)
: StateScreen(stateManager)
{
using namespace sf;
cursorMap = 0;
cursorMode = 0;
text.setFont(assetManager->getFont("game font"));
text.setFillColor(Color::White);
dropOnBeatGameButton.setPosition(Vector2f(200, 300));
dropOnBeatGameButton.setSize(Vector2f(500, 100));
dropOnBeatGameButton.setHighlightColor(TRStyles::btnHLColor);
dropOnBeatGameButton.setBaseColor(TRStyles::btnBaseColor);
limitedGameButton.setPosition(Vector2f(200, 450));
limitedGameButton.setSize(Vector2f(500, 100));
limitedGameButton.setHighlightColor(TRStyles::btnHLColor);
limitedGameButton.setBaseColor(TRStyles::btnBaseColor);
endlessGameButton.setPosition(Vector2f(200, 600));
endlessGameButton.setSize(Vector2f(500, 100));
endlessGameButton.setHighlightColor(TRStyles::btnHLColor);
endlessGameButton.setBaseColor(TRStyles::btnBaseColor);
sprintGameButton.setPosition(Vector2f(200, 750));
sprintGameButton.setSize(Vector2f(500, 100));
sprintGameButton.setHighlightColor(TRStyles::btnHLColor);
sprintGameButton.setBaseColor(TRStyles::btnBaseColor);
startButton.setPosition(Vector2f(2048 / 2 - 100, 1000));
startButton.setSize(Vector2f(200, 100));
startButton.setHighlightColor(TRStyles::btnHLColor);
startButton.setBaseColor(TRStyles::btnBaseColor);
std::string path = "BeatMaps";
for (const auto& entry : fs::directory_iterator(path))
{
maps.push_back(entry.path());
}
selectMap(0);
}
GameOptions::~GameOptions()
{
}
void GameOptions::tick(const float & dt, sf::RenderWindow& window)
{
}
void GameOptions::render(sf::RenderWindow& window)
{
using namespace sf;
using namespace std;
RenderTexture mapsTexture;
mapsTexture.create(1000, 550);
mapsTexture.clear(Color::Transparent);
mapsTexture.display();
text.setFillColor(Color::White);
text.setCharacterSize(120);
text.setString("Game Options");
text.setPosition(1024 - text.getLocalBounds().width / 2, 50);
window.draw(text);
dropOnBeatGameButton.setHighlight(cursorMode == 0);
limitedGameButton.setHighlight(cursorMode == 1);
endlessGameButton.setHighlight(cursorMode == 2);
sprintGameButton.setHighlight(cursorMode == 3);
window.draw(dropOnBeatGameButton); // drop blocks on the beat receives bonus
window.draw(limitedGameButton); // get the highest score in 2 min
text.setFillColor(limitedGameButton.isHighlighted() ? Color(0, 186, 211) : Color::White);
text.setCharacterSize(30);
text.setString("Highscore: " + to_string(GameSettings::getInstance()->getHighscores()->limit));
text.setPosition(200, 450 - 40);
window.draw(text);
window.draw(endlessGameButton); // just play
text.setFillColor(endlessGameButton.isHighlighted() ? Color(0, 186, 211) : Color::White);
text.setCharacterSize(30);
text.setString("Highscore: " + to_string(GameSettings::getInstance()->getHighscores()->endless));
text.setPosition(200, 600 - 40);
window.draw(text);
window.draw(sprintGameButton); // sprint
text.setFillColor(sprintGameButton.isHighlighted() ? Color(0, 186, 211) : Color::White);
text.setCharacterSize(30);
text.setString("Best time: " + (GameSettings::getInstance()->getHighscores()->sprintTime != INT_MAX ? getTimeFormat(GameSettings::getInstance()->getHighscores()->sprintTime) : "N/A"));
text.setPosition(200, 750 - 40);
window.draw(text);
int size = maps.size();
static int starterCounter = -size * 200; // temporary solution. it's ugly in code
for (int i = 0; i < size; ++i)
{
if (starterCounter < 0)
{
int tempCursorMap = clamp( - (starterCounter - 100) / 200, 0, (int)maps.size() - 1);
drawGameModeOption(mapsTexture, maps[i].filename().string(), 0, i * 200 + starterCounter + 200, tempCursorMap == i);
starterCounter+=5;
}
else
{
drawGameModeOption(mapsTexture, maps[i].filename().string(), 0, i * 200 + mapRenderOffset + 200, cursorMap == i);
}
}
Sprite sprite(mapsTexture.getTexture());
sprite.setPosition(2048 - 1000, 300);
window.draw(sprite);
window.draw(startButton);
if (mouseInBox(window, 20, 20, 40, 40)) // back button
{
window.draw(assetManager->getDrawable("back button hl"));
}
else
{
window.draw(assetManager->getDrawable("back button"));
}
}
void GameOptions::keyEvent(const float & dt, sf::Event event)
{
using namespace sf;
using namespace std;
if (event.type != Event::KeyPressed) return;
switch (event.key.code)
{
case Keyboard::Key::Escape:
previewMusic.stop();
stateManager.addState(std::unique_ptr<StateScreen>(new Menu(stateManager)));
break;
case Keyboard::Key::Return:
if (!choosingMap) // if player is not choosing map (choosing mode), enter allow user to choose map
{
choosingMap = true;
return;
}
previewMusic.stop();
startGame();
break;
case Keyboard::Key::Up:
if (choosingMap)
{
selectMap(clamp(cursorMap - 1, 0, (int)maps.size() - 1));
mapRenderOffset = -cursorMap * 200;
}
else
{
cursorMode = clamp(cursorMode - 1, 0, modeCount - 1);
}
break;
case Keyboard::Key::Down:
if (choosingMap)
{
selectMap(cursorMap = clamp(cursorMap + 1, 0, (int)maps.size() - 1));
mapRenderOffset = -cursorMap * 200;
}
else
{
cursorMode = clamp(cursorMode + 1, 0, modeCount - 1);
}
break;
case Keyboard::Key::Right:
if (!choosingMap)
{
choosingMap = true;
}
break;
case Keyboard::Key::Left:
if (choosingMap)
{
choosingMap = false;
}
}
}
void GameOptions::mouseEvent(const float & dt, sf::RenderWindow& window, sf::Event event)
{
using namespace sf;
using namespace std;
static bool isPressed = false;
static Vector2f pressedPosition = Vector2f(0,0);
if (event.type == Event::MouseMoved)
{
startButton.setHighlight(startButton.mouseInButton(window));
}
else if (event.type == Event::MouseButtonPressed && startButton.mouseInButton(window))
{
previewMusic.stop();
startGame();
}
else if (event.type == Event::MouseButtonPressed && dropOnBeatGameButton.mouseInButton(window))
{
cursorMode = 0;
}
else if (event.type == Event::MouseButtonPressed && limitedGameButton.mouseInButton(window))
{
cursorMode = 1;
}
else if (event.type == Event::MouseButtonPressed && endlessGameButton.mouseInButton(window))
{
cursorMode = 2;
}
else if (event.type == Event::MouseButtonPressed && sprintGameButton.mouseInButton(window))
{
cursorMode = 3;
}
else if (event.type == Event::MouseButtonPressed && mouseInBox(window, 2048 - 1000, 300, 1000, 700))
{
isPressed = true;
Vector2i pixelPos = Mouse::getPosition(window);
Vector2f mouseViewPos = window.mapPixelToCoords(pixelPos);
pressedPosition = Vector2f(mouseViewPos.x, mouseViewPos.y);
prevMapRenderOffset = mapRenderOffset;
}
else if (event.type == Event::MouseButtonPressed && mouseInBox(window, 20, 20, 40, 40)) // back button
{
previewMusic.stop();
stateManager.addState(std::unique_ptr<StateScreen>(new Menu(stateManager)));
}
else if (isPressed && Mouse::isButtonPressed(Mouse::Left))
{
Vector2i pixelPos = Mouse::getPosition(window);
Vector2f mouseViewPos = window.mapPixelToCoords(pixelPos);
mapRenderOffset = prevMapRenderOffset;
mapRenderOffset += mouseViewPos.y - pressedPosition.y;
selectMap(clamp(-(mapRenderOffset - 100) / 200, 0, (int)maps.size() - 1));
}
else if (event.type == Event::MouseButtonReleased)
{
isPressed = false;
mapRenderOffset = -cursorMap * 200;
prevMapRenderOffset = mapRenderOffset;
}
}
void GameOptions::mouseScrollEvent(const float& dt, sf::RenderWindow& window, sf::Event event)
{
if (event.type == sf::Event::MouseWheelScrolled && mouseInBox(window, 2048 - 1000, 300, 1000, 550))
{
selectMap(std::clamp(cursorMap - (int) event.mouseWheelScroll.delta, 0, (int)maps.size() - 1));
mapRenderOffset = -cursorMap * 200;
prevMapRenderOffset = mapRenderOffset;
}
}
void GameOptions::drawGameModeOption(sf::RenderTexture& window, std::string gameMode, int x, int y, bool isHighlight)
{
using namespace sf;
using namespace std;
if (isHighlight)
{
RectangleShape rect(Vector2f(window.getSize().x, 150));
rect.setPosition(x, y - 38);
rect.setFillColor(Color(0, 186, 211));
window.draw(rect);
text.setFillColor(Color::Black);
}
else
{
text.setFillColor(Color::White);
}
text.setPosition(x, y);
text.setCharacterSize(60);
text.setString(gameMode);
window.draw(text);
text.setFillColor(isHighlight ? Color(50, 50, 50) : Color(200, 200, 200));
text.setPosition(x, y - 30);
text.setCharacterSize(30);
std::map<std::string, int> thresholds = GameSettings::getInstance()->getHighscores()->dropToBeatThreshold;
auto highscores = GameSettings::getInstance()->getHighscores()->dropToBeatHS;
int highscore = highscores.find(gameMode) != highscores.end() ? highscores.at(gameMode) : 0;
int threshold = thresholds.find(gameMode) != thresholds.end() ? thresholds.at(gameMode) : 1;
text.setString("Rank: " + getRank(highscore, threshold) + "\t HS : " + to_string(highscore));
window.draw(text);
}
void GameOptions::selectMap(int mapIndex)
{
using namespace std;
cursorMap = mapIndex;
fs::path audioPath = maps[mapIndex];
audioPath.append(audioPath.filename().string());
audioPath = AssetManager::getAudioFilePathExtension(audioPath);
std::cout << audioPath.string() << endl;
previewMusic.openFromFile(audioPath.string());
previewMusic.play();
}
void GameOptions::startGame()
{
GameBase* gamePtr;
switch (cursorMode)
{
case 0:
gamePtr = new DropToTheBeatGame(stateManager, fs::absolute(maps[cursorMap]).string());
break;
case 1:
gamePtr = new LimitedTimeGame(stateManager, fs::absolute(maps[cursorMap]).string());
break;
case 2:
gamePtr = new EndlessGame(stateManager, fs::absolute(maps[cursorMap]).string());
break;
case 3:
gamePtr = new SprintGame(stateManager, fs::absolute(maps[cursorMap]).string());
break;
default:
gamePtr = new DropToTheBeatGame(stateManager, fs::absolute(maps[cursorMap]).string());
break;
}
stateManager.addState(std::unique_ptr<StateScreen>(gamePtr));
gamePtr->start();
}