-
Notifications
You must be signed in to change notification settings - Fork 0
/
FinishState.cpp
199 lines (168 loc) · 7.48 KB
/
FinishState.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
#include "FinishState.hpp"
#include "DEFINITIONS.hpp"
#include "MainMenuState.hpp"
#include "Levels/Level1.hpp"
#include "Levels/Level2.hpp"
#include "Levels/Level3.hpp"
#include <fstream>
namespace engine{
//Constructor of FinishState
FinishState::FinishState(GameDataRef data, const float& tijd, const int& levelNumber):
_data(data),
tijd(tijd),
lastLevel(levelNumber)
{}
//Run on initialization of state
void FinishState::Init() {
//background Initializer
_data->assets.LoadTexture("FinishState Background", FINISH_BACKGROUND_FILEPATH);
_background.setTexture(_data->assets.GetTexture("FinishState Background"));
_background.setScale(SCREEN_WIDTH / _background.getGlobalBounds().width,
SCREEN_HEIGHT / _background.getGlobalBounds().height);
InitView.reset(sf::FloatRect{0, 0, SCREEN_WIDTH, SCREEN_HEIGHT});
_data->renderWindow.setView(InitView);
//play_next button Initializer
_data->assets.LoadTexture("FinishStateNextLevelButton", FINISH_NEXT_LEVEL_BUTTON_FILEPATH);
_data->assets.LoadTexture("FinishStateNextLevelButtonHover", FINISH_NEXT_LEVEL_BUTTON_HOVER_FILEPATH);
_play_next_level.setTexture(_data->assets.GetTexture("FinishStateNextLevelButton"));
_play_next_level.setPosition(SCREEN_WIDTH / 2 - _play_next_level.getGlobalBounds().width / 2, 200);
//menu button Initializer
_data->assets.LoadTexture("FinishStateMainMenuButton", FINISH_MAIN_MENU_PLAY_BUTTON_FILEPATH);
_data->assets.LoadTexture("FinishStateMainMenuButtonHover", FINISH_MAIN_MENU_PLAY_BUTTON_HOVER_FILEPATH);
_menu.setTexture(_data->assets.GetTexture("FinishStateMainMenuButton"));
_menu.setPosition(SCREEN_WIDTH / 2 - _menu.getGlobalBounds().width / 2, 350);
//quit button Initializer
_data->assets.LoadTexture("FinishStateQuitButton", FINISH_QUIT_BUTTON_FILEPATH);
_data->assets.LoadTexture("FinishStateQuitButtonHover", FINISH_QUIT_BUTTON_HOVER_FILEPATH);
_quit.setTexture(_data->assets.GetTexture("MainMenuQuitButton"));
_quit.setPosition(SCREEN_WIDTH / 2 - _quit.getGlobalBounds().width / 2, 500);
//tijdText Initializer
tijdText.setFont(_data->assets.GetFont("RussoOneFont"));
tijdText.setFillColor(sf::Color::Black);
tijdText.setCharacterSize(90);
//bestTimeText Initializer
bestTimeText.setFont(_data->assets.GetFont("RussoOneFont"));
bestTimeText.setFillColor(sf::Color::Black);
bestTimeText.setCharacterSize(90);
//Make string for high score filename
std::string file = HIGHSCORE_FILENAME;
file += std::to_string(lastLevel);
file += ".txt";
//Read high score file
std::ifstream readFile;
readFile.open(file);
if (readFile.is_open()) {
while (!readFile.eof()) {
readFile >> _bestTime;
}
}
readFile.close();
//Dont draw if the previous best time is the default number on creation
if(_bestTime == 10000000000.0){
previousBestText = false;
}
//Write file
if (tijd < _bestTime) {
//tijdText
std::string time = std::to_string(tijd);
time = time.substr(0, time.size() - 4);
tijdText.setString("New best Time! " + time);
tijdText.setPosition(SCREEN_WIDTH / 2 - tijdText.getGlobalBounds().width / 2 - 10, 10);
//bestTimeText
std::string bestTime = std::to_string(_bestTime);
bestTime = bestTime.substr(0, bestTime.size() - 4);
bestTimeText.setString("Previous best time: " + bestTime);
bestTimeText.setPosition(SCREEN_WIDTH / 2 - tijdText.getGlobalBounds().width / 2 - 90, 100);
//Update _bestTime and write to file
_bestTime = tijd;
std::ofstream writeFile;
writeFile.open(file);
if (writeFile.is_open()) {
writeFile << _bestTime;
}
writeFile.close();
} else {
//tijdText update string and position
std::string time = std::to_string(tijd);
time = time.substr(0, time.size() - 4);
tijdText.setString("Your time: " + time);
tijdText.setPosition(SCREEN_WIDTH / 2 - tijdText.getGlobalBounds().width / 2 - 10, 10);
//bestTimeText update string and position
std::string bestTime = std::to_string(_bestTime);
bestTime = bestTime.substr(0, bestTime.size() - 4);
bestTimeText.setString("Best time: " + bestTime);
bestTimeText.setPosition(SCREEN_WIDTH / 2 - tijdText.getGlobalBounds().width / 2 - 10, 100);
}
}
void FinishState::HandleInput() {
sf::Event event;
//Check for windows x button
while(_data->renderWindow.pollEvent(event)){
if(sf::Event::Closed == event.type){
_data->renderWindow.close();
}
}
//Change texture if hovered over quit button
if(_data->input.HoverOverButton(_quit, _data->renderWindow) && _data->renderWindow.hasFocus() &&!_hoverQuit){
_hoverQuit = true;
_quit.setTexture(_data->assets.GetTexture("FinishStateQuitButtonHover"));
}
else if(!_data->input.HoverOverButton(_quit, _data->renderWindow) && _data->renderWindow.hasFocus() &&_hoverQuit){
_hoverQuit = false;
_quit.setTexture(_data->assets.GetTexture("FinishStateQuitButton"));
}
//Change texture if hovered over play_next button
if(_data->input.HoverOverButton(_play_next_level, _data->renderWindow) && _data->renderWindow.hasFocus() &&!_hoverNextLevel){
_hoverNextLevel = true;
_play_next_level.setTexture(_data->assets.GetTexture("FinishStateNextLevelButtonHover"));
}
else if(!_data->input.HoverOverButton(_play_next_level, _data->renderWindow) && _data->renderWindow.hasFocus() &&_hoverNextLevel){
_hoverNextLevel = false;
_play_next_level.setTexture(_data->assets.GetTexture("FinishStateNextLevelButton"));
}
//Change texture if hovered over menu button
if(_data->input.HoverOverButton(_menu, _data->renderWindow) && _data->renderWindow.hasFocus() &&!_hoverMainMenu){
_hoverMainMenu = true;
_menu.setTexture(_data->assets.GetTexture("FinishStateMainMenuButtonHover"));
}
else if(!_data->input.HoverOverButton(_menu, _data->renderWindow) && _data->renderWindow.hasFocus() &&_hoverMainMenu){
_hoverMainMenu = false;
_menu.setTexture(_data->assets.GetTexture("FinishStateMainMenuButton"));
}
//Check for press play_next button
if(_data->input.IsSpriteClicked(_play_next_level, sf::Mouse::Left, _data->renderWindow) && _data->renderWindow.hasFocus()) {
if(lastLevel == 1){
_data->sound._clickButtonSound.play();
_data->machine.AddState(StateRef(new Level2(_data)), true);
}else if(lastLevel == 2){
_data->sound._clickButtonSound.play();
_data->machine.AddState(StateRef(new Level3(_data)), true);
}
}
//Check for press menu button
if(_data->input.IsSpriteClicked(_menu, sf::Mouse::Left, _data->renderWindow) && _data->renderWindow.hasFocus()){
_data->sound._clickButtonSound.play();
_data->machine.AddState( StateRef( new MainMenuState(_data)), true);
_data->machine.clean_states();
}
//Check for press quit button
if(_data->input.IsSpriteClicked(_quit, sf::Mouse::Left, _data->renderWindow) && _data->renderWindow.hasFocus()){
_data->sound._clickButtonSound.play();
_data->renderWindow.close();
}
}
void FinishState::Draw(float dt) {
_data->renderWindow.clear();
_data->renderWindow.draw(_background);
_data->renderWindow.draw(_quit);
if(lastLevel < LEVEL_AMOUNT){
_data->renderWindow.draw(_play_next_level);
}
_data->renderWindow.draw(_menu);
_data->renderWindow.draw(tijdText);
if(previousBestText){
_data->renderWindow.draw(bestTimeText);
}
_data->renderWindow.display();
}
}