-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.cpp
261 lines (223 loc) · 7.6 KB
/
menu.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
#include "menu.h"
#include "input.h"
#include <iostream>
#include <iomanip>
#include "audio.h"
#include "entityManager.h"
#define USESPRITES 1
bool _justClosed = false;
bool _justOpened = false;
Menu::Menu(Player* player) {
menu = nullptr;
curSelection = 0;
this->player = player;
setVisible(true);
//if (titleTexture.loadFromFile("assets/images/ui/Title.png")) {
// titleSprite.setTexture(titleTexture);
//}
title.setFont(Resources::get().font);
title.setCharacterSize(110U);
title.setColor(TITLE_COLOR);
title.setString("DOWNTOWN BAZOOKA");
instructions.setFont(Resources::get().font);
instructions.setColor(SELECTED_COLOR);
instructions.setString(
"WASD : move\n"
"Rclick/E : shoot\n"
"Lclick : rocket jump\n"
"Space : jump, select\n"
"ESC : menu, quit\n"
"---------------------------\n"
"TAB : pause\n"
"F1 : show FPS\n"
"Q : toggle flymode\n"
"1 : toggle blur\n"
"2 : toggle mipmaps\n"
"3 : toggle physics debug\n"
"4 : toggle terrain debug\n"
"5 : randomize world seed\n"
"0 : recompile shaders");
instructions.setScale(sf::Vector2f(1.5f, 1.5f));
instructions.setPosition(5.0f, 50.0f);
healthBar.setFillColor(sf::Color(180, 255, 0, 255));
fpsText.setFont(Resources::get().font);
fpsText.setColor(sf::Color(255, 0, 0));
fpsText.setScale(2.0f, 2.0f);
fpsText.setPosition(0.0f, -10.0f);
deadText.setFont(Resources::get().font);
deadText.setScale(6.0f, 6.0f);
overlay.setFillColor(sf::Color(0, 0, 0, 150));
overlay.setPosition(sf::Vector2f());
// this should give the width in pixels of the text so we can use that to center it
// but too bad it crashes the game so hard lol whyyyyyy???
// some sort of weird access violation where we arent reseting states right??? bugged pos???
//sf::FloatRect rect = title.getLocalBounds();
}
Menu::~Menu() {
}
void Menu::draw(sf::RenderWindow& window, bool showFPS) {
int width = window.getSize().x;
int height = window.getSize().y;
glEnable(GL_DEPTH_TEST);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.1f);
if (showFPS) {
window.draw(fpsText);
}
// if main menu is not being shown then draw game UI stuff
if (!visible) {
if (player->isDead()) {
overlay.setSize(sf::Vector2f(sf::Vector2i(width, height)));
glDepthMask(GL_FALSE);
window.draw(overlay);
glDepthMask(GL_TRUE);
deadText.setString("GAME OVER!");
deadText.setColor(sf::Color(255, 0, 0));
deadText.setPosition(width / 2.0f - 500.0f, height / 5.0f);
window.draw(deadText);
} else {
healthBar.setPosition(0.0f, static_cast<float>(height - HEALTH_BAR_HEIGHT));
float x = static_cast<float>(width * player->getHealth() / player->getMaxHealth());
float y = static_cast<float>(HEALTH_BAR_HEIGHT);
healthBar.setSize(sf::Vector2f(x, y));
window.draw(healthBar);
Boss* boss = EntityManagerInstance->getBoss();
if (boss != nullptr) {
if (boss->getHealthPercentage() <= 0.0f) {
deadText.setString("YOU WIN!");
deadText.setColor(sf::Color(0, 255, 0));
deadText.setPosition(width / 2.0f - 500.0f, height / 5.0f);
window.draw(deadText);
} else if (boss->isVulnerable()) {
bossHealthBar.setFillColor(sf::Color(255, 0, 0, 255));
} else {
bossHealthBar.setFillColor(sf::Color(30, 30, 30, 255));
}
bossHealthBar.setPosition(0.0f, 0.0f);
float x = static_cast<float>(width * boss->getHealthPercentage());
float y = static_cast<float>(HEALTH_BAR_HEIGHT);
bossHealthBar.setSize(sf::Vector2f(x, y));
window.draw(bossHealthBar);
}
}
return;
}
overlay.setSize(sf::Vector2f(sf::Vector2i(width, height)));
glDepthMask(GL_FALSE);
window.draw(overlay);
glDepthMask(GL_TRUE);
if (showingInstructions) {
window.draw(instructions);
} else {
//titleSprite.setPosition(width / 2.0f - 500.0f, height / 5.0f);
//window.draw(titleSprite);
title.setPosition(sf::Vector2f(width / 2.0f - 530.0f, height * 0.05f));
window.draw(title);
if (menu == nullptr) {
setVisible(true);
}
for (int i = 0; i < MAX_NUMBER_OF_ITEMS; i++) {
menu[i]->draw(window, sf::Vector2f(width / 2.0f, height / (MAX_NUMBER_OF_ITEMS + 1)*(1.0f + i)));
//window.draw(menu[i]);
}
}
glDisable(GL_ALPHA_TEST);
}
int circularClamp(int n, int min, int max) {
if (n < min) {
return max;
} else if (n > max) {
return min;
}
return n;
}
void Menu::move(bool up) {
AudioInstance->playSound(Resources::get().menuMoveSound);
menu[curSelection]->SetIsSelected(false);
curSelection += up ? -1 : 1;
curSelection = circularClamp(curSelection, 0, MAX_NUMBER_OF_ITEMS - 1);
menu[curSelection]->SetIsSelected(true);
}
bool Menu::update(GLfloat delta) {
updateFpsText(delta);
_justClosed = false;
_justOpened = false;
if (Input::justPressed(sf::Keyboard::Escape)) {
if (visible) {
if (showingInstructions) {
showingInstructions = false;
} else {
return false;
}
} else {
visible = true;
_justOpened = true;
}
}
if (!visible) {
return true;
}
if (Input::justPressed(sf::Keyboard::W) || Input::justPressed(sf::Keyboard::Up)) {
move(true);
}
if (Input::justPressed(sf::Keyboard::S) || Input::justPressed(sf::Keyboard::Down)) {
move(false);
}
if (Input::justPressed(sf::Keyboard::Return) || Input::justPressed(sf::Keyboard::Space)) {
switch (curSelection) {
case 0:
AudioInstance->playSound(Resources::get().menuSelectSound);
setVisible(false);
_justClosed = true;
break;
case 1:
AudioInstance->playSound(Resources::get().menuSelectSound);
showingInstructions = true;
break;
case 2:
return false;
}
}
return true;
}
void Menu::setVisible(bool visible) {
if (visible && menu == nullptr) {
menu = new TextOption*[MAX_NUMBER_OF_ITEMS];
menu[0] = new TextOption("Play");
menu[1] = new TextOption("Instructions");
menu[2] = new TextOption("Quit");
menu[0]->SetIsSelected(true);
} else if (!visible && menu != nullptr) {
for (int i = 0; i < MAX_NUMBER_OF_ITEMS; i++)
delete menu[i];
delete[] menu;
menu = nullptr;
}
this->visible = visible;
}
// calculates a buffers the fps
void Menu::updateFpsText(float delta) {
if (delta < 0.005f) {
delta = 0.005f;
}
float fps = 1.0f / delta;
fpsValues.push(fps);
totalFpsQueueValue += fps;
while (fpsValues.size() > 30) {
totalFpsQueueValue -= fpsValues.front();
fpsValues.pop();
}
float avgFps = totalFpsQueueValue / fpsValues.size();
std::stringstream ss;
ss << std::fixed << std::setprecision(1) << avgFps;
fpsText.setString(ss.str());
}
bool Menu::getVisible() const {
return visible;
}
bool Menu::justClosed() {
return _justClosed;
}
bool Menu::justOpened() {
return _justOpened;
}