-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLimitedTimeGame.cpp
87 lines (76 loc) · 2.07 KB
/
LimitedTimeGame.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
#include "LimitedTimeGame.h"
#include "Utils.h"
//TODO: speed depends on the song speed. the song speed up after every round
LimitedTimeGame::LimitedTimeGame(StateManager& stateManager, std::string folderPath) : GameBase(stateManager, folderPath)
{
clock = sf::Clock();
}
LimitedTimeGame::~LimitedTimeGame()
{
}
void LimitedTimeGame::tick(const float & dt, sf::RenderWindow& window)
{
using namespace sf;
if (isGameOver) return;
GameBase::tick(dt, window);
frameCount++;
// restart song if not game over
if (!isGameOver && sound.getStatus() == SoundSource::Status::Stopped)
{
sound.play();
}
if (clock.getElapsedTime().asSeconds() >= 120)
{
sound.stop();
isGameOver = true;
}
}
void LimitedTimeGame::gameOver()
{
GameBase::gameOver();
highscores->limit = std::max(highscores->limit, score);
GameSettings::getInstance()->saveHighscores();
}
void LimitedTimeGame::keyEvent(const float & dt, sf::Event event)
{
using namespace sf;
if (event.type != Event::KeyPressed && event.type != Event::KeyReleased) return;
GameBase::keyEvent(dt, event);
if (event.key.code == Keyboard::R)
{
clock = Clock();
}
}
void LimitedTimeGame::mouseEvent(const float & dt, sf::RenderWindow& window, sf::Event event)
{
using namespace sf;
if (isGameOver && Mouse::isButtonPressed(Mouse::Left) && mouseInBox(window, 1024 - 150, 576 - 60 - 20, 300, 60)) // RESTART button
{
clock = Clock();
}
GameBase::mouseEvent(dt, window, event);
}
void LimitedTimeGame::render(sf::RenderWindow& window)
{
using namespace sf;
using namespace std;
GameBase::render(window);
Int32 tleft = 120000-clock.getElapsedTime().asMilliseconds();
text.setCharacterSize(50);
text.setString("Time left ");
text.setPosition(300, 500);
window.draw(text);
text.setCharacterSize(70);
text.setString(getTimeFormat(tleft));
text.setPosition(300, 550);
window.draw(text);
if (isGameOver)
{
GameBase::renderGameOver(window);
text.setCharacterSize(80);
text.setFillColor(Color::White);
text.setString(to_string(score));
text.setPosition(1024 - text.getLocalBounds().width / 2, 576 - 250);
window.draw(text);
}
}