-
Notifications
You must be signed in to change notification settings - Fork 0
/
SprintGame.cpp
96 lines (84 loc) · 2.26 KB
/
SprintGame.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
#include "SprintGame.h"
SprintGame::SprintGame(StateManager &stateManager, std::string folderPath) : GameBase(stateManager, folderPath)
{
timer = sf::Clock();
}
SprintGame::~SprintGame()
{
}
void SprintGame::tick(const float & dt, sf::RenderWindow& window)
{
if (!isGameOver && linesCleared >= 40)
{
gameOver();
}
}
void SprintGame::gameOver()
{
GameBase::gameOver();
if (linesCleared >= 40)
{
currSprintTime = timer.getElapsedTime().asMilliseconds();
highscores->sprintTime = std::min(highscores->sprintTime, currSprintTime);
GameSettings::getInstance()->saveHighscores();
}
}
void SprintGame::restart()
{
GameBase::restart();
timer = sf::Clock();
}
void SprintGame::keyEvent(const float & dt, sf::Event event)
{
GameBase::keyEvent(dt, event);
}
void SprintGame::mouseEvent(const float & dt, sf::RenderWindow& window, sf::Event event)
{
GameBase::mouseEvent(dt, window, event);
}
void SprintGame::render(sf::RenderWindow& window)
{
using namespace sf;
using namespace std;
GameBase::render(window);
text.setFillColor(Color::White);
text.setCharacterSize(70);
text.setString("Lines left: " + to_string(40 - linesCleared));
text.setPosition(100, 650);
window.draw(text);
if (isGameOver)
{
GameBase::renderGameOver(window);
if (linesCleared >= 40) // success
{
Int32 tleft = highscores->sprintTime;
bool isPB = currSprintTime == tleft;
text.setFillColor(Color::Green);
text.setCharacterSize(100);
text.setString(isPB ? "Personal Best" : "Success");
text.setPosition(1024 - text.getLocalBounds().width / 2, 576 - 300);
window.draw(text);
text.setFillColor(isPB ? Color::Green : Color::White);
text.setCharacterSize(70);
text.setString(getTimeFormat(currSprintTime));
text.setPosition(1024 - text.getLocalBounds().width / 2, 576 - 200);
window.draw(text);
}
else
{
text.setFillColor(Color::Red);
text.setCharacterSize(100);
text.setString("Failed");
text.setPosition(1024 - text.getLocalBounds().width / 2, 576 - 250);
window.draw(text);
}
}
else
{
Int32 tleft = timer.getElapsedTime().asMilliseconds();
text.setCharacterSize(70);
text.setString(to_string(tleft / 1000/60) + ":"+to_string(tleft / 1000%60) + ":" + to_string((tleft % 1000)/10));
text.setPosition(300, 550);
window.draw(text);
}
}