-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
380 lines (340 loc) · 11.3 KB
/
Game.cpp
File metadata and controls
380 lines (340 loc) · 11.3 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#include "class.h"
#include "game.h"
void Game::Start(void)
{
if(_gameState != Uninitialized)
{
return;
}
_mainWindow.Create(VideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BIT),"Stick Man Rumble",Style::Fullscreen);
_mainWindow.UseVerticalSync(false);
_mainWindow.SetFramerateLimit(FPS);
_mainWindow.ShowMouseCursor(false);
_gameState = Game::ShowingSplash;
loadFonts();
while(!IsExiting())
{
GameLoop();
}
_mainWindow.Close();
}
bool Game::IsExiting()
{
if(_gameState == Game::Exiting)
{
return true;
}
else
{
return false;
}
}
void Game::ShowSplashScreen()
{
SplashScreen(); //Load the splashscreen and return only when a key is pressed or mouse is clicked
}
void Game::ShowMenu()
{
MainMenu mainMenu;
mainMenu.SetMenu();
_mainWindow.SetFramerateLimit(120);
MainMenu::MenuResult result = mainMenu.Show();
switch(result)
{
case MainMenu::Exit:
{
_gameState = Game::Exiting;
Sounds::PlaySound(Sounds::MenuButton);
break;
}
case MainMenu::Play:
{
_gameState = Game::Playing;
Sounds::PlaySound(Sounds::MenuButton);
for (int i=0; i<4; i++)
_button[i]= false;
player1.initiatePlayer();
world1.SetWorld();
Sounds::PlayingGame=true;
Game::sound1.Launch(); //Launches a thread to play the sounds
gameClock.Reset();
break;
}
case MainMenu::Score:
{
_gameState = Game::ShowingScores;
Sounds::PlaySound(Sounds::MenuButton);
break;
}
default:
{
break;
}
}
Game::_mainWindow.SetFramerateLimit(Game::FPS);
}
void Game::displayGameOverMessage()
{
int gamePlayedTime = gameClock.GetElapsedTime();
gameClock.Reset();
_mainWindow.Clear();
Image tempImage;
tempImage.LoadFromFile("gameScreen.jpg");
Sprite tempSprite(tempImage);
sf::String messageTitle ("Game Over !!!", messageFont, 75);
sf::String messageString ("Press any key to see the highscores", messageFont, 25);
sf::Shape messageBox;
messageBox.AddPoint(SCREEN_WIDTH / 2 - 1, SCREEN_HEIGHT / 2 - 1, Color(0, 0, 0, 200), Color(255, 255, 255));
messageBox.AddPoint(SCREEN_WIDTH / 2 - 1, SCREEN_HEIGHT / 2 + 1, Color(0, 0, 0, 200), Color(255, 255, 255));
messageBox.AddPoint(SCREEN_WIDTH / 2 + 1, SCREEN_HEIGHT / 2 - 1, Color(0, 0, 0, 200), Color(255, 255, 255));
messageBox.AddPoint(SCREEN_WIDTH / 2 + 1, SCREEN_HEIGHT / 2 + 1, Color(0, 0, 0, 200), Color(255, 255, 255));
for (int i=0; i < FPS; i++)
{
messageBox.SetPointPosition(0, SCREEN_WIDTH / 2 - (SCREEN_WIDTH / 2 * i/FPS), SCREEN_HEIGHT / 2 - (SCREEN_HEIGHT / 2 * i/FPS));
messageBox.SetPointPosition(1, SCREEN_WIDTH / 2 - (SCREEN_WIDTH / 2 * i/FPS), SCREEN_HEIGHT / 2 + (SCREEN_HEIGHT / 2 * i/FPS));
messageBox.SetPointPosition(3, SCREEN_WIDTH / 2 + (SCREEN_WIDTH / 2 * i/FPS), SCREEN_HEIGHT / 2 - (SCREEN_HEIGHT / 2 * i/FPS));
messageBox.SetPointPosition(2, SCREEN_WIDTH / 2 + (SCREEN_WIDTH / 2 * i/FPS), SCREEN_HEIGHT / 2 + (SCREEN_HEIGHT / 2 * i/FPS));
_mainWindow.Draw(tempSprite);
_mainWindow.Draw(messageBox);
_mainWindow.Display();
}
messageTitle.SetPosition(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4);
messageString.SetPosition(SCREEN_WIDTH / 4, 3 * SCREEN_HEIGHT / 4);
_mainWindow.Draw(messageTitle);
_mainWindow.Draw(messageString);
_mainWindow.Display();
remove("gameScreen.jpg");
pauseGame();
}
void Game::handle_event(Event & currentEvent)
{
if(currentEvent.Type == Event::KeyPressed)
{
switch(currentEvent.Key.Code)
{
case Key::Escape:
{
Sounds::PlayingGame=false;
Sounds::PlaySound(Sounds::EscapeToMenu);
_gameState=ShowingMenu;
break;
}
case Key::F1:
{
Sounds::PlaySound(Sounds::Camera);
Image Screen = _mainWindow.Capture();
Screen.SaveToFile("screenshot.jpg");
break;
}
case Key::P:
{
gamePaused = true;
pauseGame();
break;
}
case Key::Left:
{
_button[Key_Left]=true;
break;
}
case Key::Right:
{
_button[Key_Right]=true;
break;
}
case Key::Up:
{
_button[Key_Up]=true;
break;
}
case Key::Down:
{
_button[Key_Down]=true;
break;
}
default:
{
break;
}
} //Switch
} //If condition
else if(currentEvent.Type == Event::KeyReleased)
{
switch(currentEvent.Key.Code)
{
case Key::Left:
{
_button[Key_Left]=false;
break;
}
case Key::Right:
{
_button[Key_Right]=false;
break;
}
case Key::Up:
{
_button[Key_Up]=false;
break;
}
case Key::Down:
{
_button[Key_Down]=false;
break;
}
default:
{
break;
}
} //Switch
}// Else if
allKeyUp = true;
for (int i = 0; i<4; i++)
{
if (_button[i] == true)
{
allKeyUp = false;
break;
}
}
}
void Game::pauseGame()
{
Event keyEvent;
bool keyPressed = false;
while (!keyPressed)
{
_mainWindow.GetEvent(keyEvent);
if (keyEvent.Type == Event::KeyPressed and !gamePaused)
keyPressed = true;
if (keyEvent.Type == Event::KeyPressed and gamePaused)
if (keyEvent.Key.Code == Key::P)
{
keyPressed = true;
gamePaused = false;
}
if (keyEvent.Key.Code == Key::Escape)
{
_gameState=ShowingMenu;
}
}
}
void Game::GameLoop(void)
{
while(_mainWindow.IsOpened())
{
player1.getPlayerInstance();
Event currentEvent;
while(true)
{
_mainWindow.GetEvent(currentEvent);
switch(_gameState)
{
case Game::ShowingScores:
{
Highscore::displayScore();
break;
}
case Game::ShowingMenu:
{
ShowMenu();
break;
}
case Game::ShowingSplash:
{
ShowSplashScreen();
break;
}
case Game::Playing:
{
float Time = gameClock.GetElapsedTime();
int frameRate = 1 /_mainWindow.GetFrameTime();
string tempX, tempY;
Player::Direction tempCollision = player1.collisionDirection();
itemsDisplayed = 0;
player1.getPlayerInstance();
world1.drawWorld();
player1.Draw(_mainWindow);
world1.drawWorldOverlay();
HUD::displayHUDBackground();
healthHUD.displayHUD();
scoreHUD.displayHUD();
player1.displayText("FPS", (double)frameRate);
player1.displayText("Previous State", player1.previousState);
player1.displayText("Current State", player1.playerState);
player1.displayText("Next State", player1.nextState);
player1.displayText("Direction", player1.playerDirection);
player1.displayText("Player Position", player1.position);
player1.displayText("World Index", (double)world1.player_column());
{
tempX = player1.numberToString((int)world1.column1[world1.player_column()].xPosition),
tempY = player1.numberToString((int)world1.column1[world1.player_column()].yPosition);
}
player1.displayText("Player Column (x,y)", "(" + tempX + "," + tempY + ")");
{
tempX = player1.numberToString((int)player1._sprite.GetPosition().x);
tempY = player1.numberToString((int)player1._sprite.GetPosition().y);
}
player1.displayText("Player (x,y)", "(" + tempX + "," + tempY + ")");
player1.displayText("Player Y Velocity", player1.yVelocity);
player1.displayText("Player X Velocity", (double)player1.xVelocity);
player1.displayText("wallOnLeft", player1.leftWall);
player1.displayText("wallOnRight", player1.rightWall);
player1.displayText("wallOnBottom", player1.bottomWall);
player1.displayText("MaxFrameNumber", (double)player1.maxFrameNumber);
player1.displayText("CurrentFrameNumber", (double)player1.currentFrameNumber);
player1.displayText("PausedFrameCount", (double)player1.pausedFrameCount);
player1.displayText("Collision with barrier", player1.collisionDirection(world1.barrier._sprite));
player1.displayText("Score", (double)(int)player1.playerScore);
player1.displayText("Health", (double)(int)player1.playerHealth);
player1.displayText("Time", Time);
_mainWindow.Display(); //Don't delete
handle_event(currentEvent);
player1.updatePlayer(currentEvent);
world1.updateWorld();
if (player1.playerHealth == 0 and player1.playerState == Player::DFalling and player1.currentFrameNumber == 8)
{
_mainWindow.Display(); //Don't delete
Sounds::PlayingGame=false;
play_time=gameClock.GetElapsedTime();
_gameState = ShowingPostGame;
}
break;
}
case Game::Exiting:
{
return;
}
case Game::ShowingPostGame:
{
Image Screen = _mainWindow.Capture();
Screen.SaveToFile("gameScreen.jpg");
displayGameOverMessage();
Highscore::saveScore(static_cast<int>(player1.playerScore),static_cast<int>(play_time));
Highscore::displayScore();
_gameState = ShowingMenu;
break;
}
default:
{
break;
}
}
}
}
}
void Game::loadFonts(void)
{
if (!HUDFont.LoadFromFile("data/fonts/HUDFont.ttf")
or
!messageFont.LoadFromFile("data/fonts/messageFont.ttf")
or
!highScoreFont.LoadFromFile("data/fonts/highScoreFont.ttf")
or
!infoFont.LoadFromFile("data/fonts/infoFont.ttf")
or
!debugFont.LoadFromFile("data/fonts/debugFont.ttf"))
{
exit(0);
}
}