-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.cpp
More file actions
647 lines (535 loc) · 20 KB
/
Game.cpp
File metadata and controls
647 lines (535 loc) · 20 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#include "stdafx.h"
#include "SplashScreen.h"
#include "Game.h"
#include "FileInput.h"
#include "SFMLSoundProvider.h"
#include "ServiceLocator.h"
#include "HighScore.h"
#include "ImageCache.h"
//Unused Default Constructor
Game::Game()
{}
//Initialized Game, Checks to see if GameLoop should be entered, and if so, calls GameLoop()
void Game::Start()
{
//If the Game is not unitialized, there is already a Game::Start() called
if (m_GameState != dlb::Uninitialized)
{
return;
}
//Initializes m_MainWindow with the const variables initialized in Game.h
m_MainWindow.Create(sf::VideoMode(SCREENWIDTH, SCREENHEIGHT, BITSPERPIXEL), "Maze Game");
m_MainWindow.SetFramerateLimit(30); //Cap FPS to 30
//Create and Register SFMLSoundProvider with a Service Locator
SFMLSoundProvider soundProvider;
//Image Cache
ImageCache imageCache;
//Use Service Locator to Play a Sound or Song anywhere in code
ServiceLocator::RegisterServiceLocator(&soundProvider);
//Use Service Locator to return an image from the image cache
ServiceLocator::RegisterServiceLocator(&imageCache);
//Begin playing background music
//Comment out for debugging if it gets annoying.
ServiceLocator::GetAudio()->PlaySong("Sounds/background.ogg", true);
ShowSplashScreen(); //show splashscreen
if (IsExiting() == true)
return;
//Load the font for score
if (!m_scoreFont.LoadFromFile("Fonts/AntsyPants.ttf"))
{
Error("Couldn't load m_scoreFont at START");
}
//Set Score and the sf::String variable for the score
m_scoreInt = 0;
m_scoreText.SetFont(m_scoreFont);
m_scoreText.SetCenter(0.f, 0.f);
m_scoreText.SetPosition(32.f, 448.f);
m_scoreText.SetColor(sf::Color(255, 0, 0));
m_scoreText.SetSize(22.f);
do //Place to go back to if Restarting and not Exiting
{
InitializeRoom(); //Setup and Load objects for Current Room
while (!IsExiting() && !IsRestarting())
{
#ifdef DEBUG
std::cout << 1.f / m_MainWindow.GetFrameTime() << std::endl;
#endif
GameLoop(); //main Game Loop. Enter if neither restarting nor exiting
}
if (IsRestarting()) //If Restarting, Clear everything and prepare for new game
{
m_MainWindow.Clear(sf::Color(255, 0, 0));
m_MainWindow.Display();
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
m_currentRoom = dlb::room1;
m_scoreInt = 0;
}
}while (IsRestarting() && !IsExiting());
m_MainWindow.Close(); //Close Window
}
//Create a new object and add to GameObjectManager for each entry in the Info object
bool Game::Calculate(Info& info)
{
//objectIterator, xIterator, yIterator are members in Game.h
if (*objectIterator == "Wall")
{
Wall* wall = new Wall(*xIterator, *yIterator);
m_GameObjectManager.Add(wall->GetId(), wall);
}
else if (*objectIterator == "Player")
{
Player* player = new Player(*xIterator, *yIterator);
m_GameObjectManager.Add(player->GetId(), player);
}
else if (*objectIterator == "Goal")
{
Goal* goal = new Goal(*xIterator, *yIterator);
m_GameObjectManager.Add(goal->GetId(), goal);
}
else if (*objectIterator == "Diamond")
{
Diamond* diamond = new Diamond(*xIterator, *yIterator);
m_GameObjectManager.Add(diamond->GetId(), diamond);
}
else if (*objectIterator == "Door")
{
Door* door = new Door(*xIterator, *yIterator);
m_GameObjectManager.Add(door->GetId(), door);
}
else if (*objectIterator == "WallHorizontal")
{
WallHorizontal* wallHorizontal = new WallHorizontal(*xIterator, *yIterator);
m_GameObjectManager.Add(wallHorizontal->GetId(), wallHorizontal);
}
else if (*objectIterator == "WallVertical")
{
WallVertical* wallVertical = new WallVertical(*xIterator, *yIterator);
m_GameObjectManager.Add(wallVertical->GetId(), wallVertical);
}
else
{
Error("Calculate() could not find " + *objectIterator + ". Continue?");
return false;
}
//Set next object in Info
++objectIterator;
++xIterator;
++yIterator;
return true;
}
//MainGameLoop
void Game::GameLoop()
{
sf::Event currentEvent;
m_MainWindow.GetEvent(currentEvent);
switch (m_GameState) //Case for each GameState
{
case dlb::Playing:
//m_MainWindow.Clear(sf::Color(192, 192, 192, 255));
m_background.Draw(m_MainWindow); //Draw Background
m_GameObjectManager.UpdateAll(); //Update each object's Logic
m_GameObjectManager.DrawAll(m_MainWindow); //Draw each object
SetScoreText(); //Set sf::String for score to current score
m_MainWindow.Draw(m_scoreText);//Draw Score
m_MainWindow.Display(); //Display Window
//If Window Closed, Ensure destructors are called (possibly superfluous) and set m_GameState to Exiting
if(currentEvent.Type == sf::Event::Closed)
{
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
m_GameState = dlb::Exiting;
}
//If Escape pressed, Ensure destructors are called (possibly superfluous) and set m_GameState to Exiting
if(currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::Escape)
{
ShowGameMenu();
}
//If 'p' pressed, pause game for 15 seconds
if(currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::P)
{
//TODO Make pausable and unpausable
sf::Sleep(55.f);
}
//If 'g' pressed, Set State to Restarting - Restart Game
if (currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::G)
m_GameState = dlb::Restarting;
//If 'r' pressed, Restart Room
if (currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::R)
{
//Clear everything. Reset to original state
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
//Reinitialize everything, set's m_GameState to Playing
InitializeRoom();
//Score stays the same!
m_scoreInt -= 25;
}
#ifdef DEBUG //In Debug Mode, Numpad will allow to go to different rooms.
//MAKE SURE YOU KNOW WHAT YOU ARE DOING, THERE IS NO SAFEGAURD TO ENSURE THAT THE ROOM EXISTS
if (currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::Numpad1)
{
m_currentRoom = static_cast<dlb::CurrentRoom>(1); //Room One
//Clear everything. Reset to original state
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
//Reinitialize everything, set's m_GameState to Playing
InitializeRoom();
}
if (currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::Numpad2)
{
m_currentRoom = static_cast<dlb::CurrentRoom>(2); //Room Two
//Clear everything. Reset to original state
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
//Reinitialize everything, set's m_GameState to Playing
InitializeRoom();
}
if (currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::Numpad3)
{
m_currentRoom = static_cast<dlb::CurrentRoom>(3); //Room Three
//Clear everything. Reset to original state
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
//Reinitialize everything, set's m_GameState to Playing
InitializeRoom();
}
if (currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::Numpad4)
{
m_currentRoom = static_cast<dlb::CurrentRoom>(4); //Room Four
//Clear everything. Reset to original state
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
//Reinitialize everything, set's m_GameState to Playing
InitializeRoom();
}
if (currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::Numpad5)
{
m_currentRoom = static_cast<dlb::CurrentRoom>(5); //Room Five
//Clear everything. Reset to original state
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
//Reinitialize everything, set's m_GameState to Playing
InitializeRoom();
}
if (currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::Numpad6)
{
m_currentRoom = static_cast<dlb::CurrentRoom>(6); //Room Six
//Clear everything. Reset to original state
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
//Reinitialize everything, set's m_GameState to Playing
InitializeRoom();
}
if (currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::Numpad7)
{
m_currentRoom = static_cast<dlb::CurrentRoom>(7); //Room Seven
//Clear everything. Reset to original state
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
//Reinitialize everything, set's m_GameState to Playing
InitializeRoom();
}
if (currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::Numpad8)
{
m_currentRoom = static_cast<dlb::CurrentRoom>(8); //Room Eight
//Clear everything. Reset to original state
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
//Reinitialize everything, set's m_GameState to Playing
InitializeRoom();
}
if (currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::Numpad9)
{
m_currentRoom = static_cast<dlb::CurrentRoom>(9); //Room Nine
//Clear everything. Reset to original state
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
//Reinitialize everything, set's m_GameState to Playing
InitializeRoom();
}
if (currentEvent.Type == sf::Event::KeyPressed && currentEvent.Key.Code == sf::Key::Numpad0)
{
m_currentRoom = static_cast<dlb::CurrentRoom>(10); //Room Ten
//Clear everything. Reset to original state
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
//Reinitialize everything, set's m_GameState to Playing
InitializeRoom();
}
#endif
break;
//Room Complete, Rooms to go. Clear and Init() new room
case dlb::RoomWon:
{ //need braces so can initialize variables in switch statement
//Set currentRoom to next room
int room = m_currentRoom;
++room;
m_currentRoom = static_cast<dlb::CurrentRoom>(room);
//make sure room exists
if (m_currentRoom == dlb::End)
{
m_GameState = dlb::Restarting;
break;
}
//Clear
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
//Init
InitializeRoom();
//GameOn!!! ;)
m_GameState = dlb::Playing;
}
break;
//Code for Restarting done in Game::Start at end of do-loop
case dlb::Restarting:
break;
//Game Won
case dlb::Won:
//New Window to Display that one has won.
sf::RenderWindow Win(sf::VideoMode(300, 60, 32), "Win");
//Messae to Display
sf::String winMsg("You Win, Press Any Key", sf::Font::GetDefaultFont(), 20.f);
winMsg.SetPosition(10.f, 20.f);
Win.Draw(winMsg);
Win.Display();
//Loop to process input. If any key is pressed. Close Window
bool running = true;
while (running == true)
{
sf::Event event;
while(Win.GetEvent(event))
{
if (event.Type == sf::Event::KeyPressed)
{
running = false;
break;
}
}
}
sf::Sleep(1.f);
//Display High Score table
ShowHighScore();
break;
}
}
//Return GameObjectManager
GameObjectManager& Game::GetGameObjectManager()
{
return m_GameObjectManager;
}
//Return MainWindow
sf::RenderWindow& Game::GetMainWindow()
{
return m_MainWindow;
}
//Return MainWindow Height
int Game::GetMainWindowHeight()
{
return m_MainWindow.GetHeight();
}
//Return MainWindow Width
int Game::GetMainWindowWidth()
{
return m_MainWindow.GetWidth();
}
//Returns Current sf::Input - used in Player.cpp to see if key is down
const sf::Input& Game::GetInput()
{
return m_MainWindow.GetInput();
}
//Returns current status of m_GameState
const dlb::GameState& Game::GetGameState()
{
return m_GameState;
}
//Returns true if m_GameState == Exiting
bool Game::IsExiting()
{
if (m_GameState == dlb::Exiting)
return true;
else
return false;
}
//Returns true if m_GameState == Restarting
bool Game::IsRestarting()
{
if (m_GameState == dlb::Restarting)
return true;
else
return false;
}
//Init current Room
bool Game::InitializeRoom()
{
#ifdef DEBUG
std::cout << "Initializing Room" << std::endl;
#endif
bool good;
FileInput setup; //New FileInput object
if (setup.SetRoom(m_currentRoom)) //SetCurrent Room
{//If successful ->
Info info = setup.GetInfo(); //Create Info object and lood information into it from file
m_background.Init(info.backgroundFilename); //Initialize background with filename from info
//Set iterators (defined in Game.h) to Info object vector's beginning
objectIterator = info.m_object.begin();
xIterator = info.m_x.begin();
yIterator = info.m_y.begin();
//Keeps track of what line the information is from
int count = 1;
//While there is still more information
while (objectIterator != info.m_object.end())
{
good = Calculate(info); //Create new Object and add to GameObjectManager
if (!good) //If couldn't create new object
{
Error("Failed on initialization of: " + dlb::ConvertIntegerToString(count));
return false; //Couldn't Initialize
}
++count;
}
setup.Close(); //Close FileInput
m_GameState = dlb::Playing; //GameOn!
return true; //Successful
}
else //Couldn't Initialize Room
{
return false;
}
}
//Show SplashScreen
void Game::ShowSplashScreen()
{
SplashScreen splash;
splash.Show(m_MainWindow);
if (!IsExiting())
m_GameState = dlb::Playing;
}
//Set Score. If relative, add score to m_score, else set m_scoreInt to score
void Game::SetScore(bool relative, int score)
{
if (relative == true)
m_scoreInt += score;
else
m_scoreInt = score;
}
//Return score
int Game::GetScore()
{
return m_scoreInt;
}
//Set the sf::String m_Score to current Score
void Game::SetScoreText()
{
m_scoreText.SetText("Score: " + dlb::ConvertIntegerToString(m_scoreInt));
}
//Set m_GameState
void Game::SetGameState(dlb::GameState state)
{
m_GameState = state;
}
//Returns m_currentRoom
const dlb::CurrentRoom Game::GetCurrentRoom()
{
return m_currentRoom;
}
//Create HighScore object with m_scoreInt and then Display High Score Table. Set m_GameState to Restarting
void Game::ShowHighScore()
{
HighScore highscore(m_scoreInt);
highscore.ShowHighScore();
m_GameState = dlb::Restarting;
}
//Display Error Message, Escape or Window Close exits; Return or Space restart game
void Game::Error(std::string errorMessage)
{
m_background.Draw(m_MainWindow);
sf::String string(errorMessage);
string.SetPosition(240.f, 160.f);
string.SetColor(sf::Color::Red);
m_MainWindow.Draw(string);
bool waiting = true;
sf::Event Event;
while (waiting == true)
{
while (m_MainWindow.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
{
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
m_GameState = dlb::Exiting;
waiting = false;
}
//If Escape pressed, Ensure destructors are called (possibly superfluous) and set m_GameState to Exiting
if(Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::Escape)
{
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
m_GameState = dlb::Exiting;
waiting = false;
}
/*if (Event.Type == sf::Event::KeyPressed && (Event.Key.Code == sf::Key::Space || Event.Key.Code == sf::Key::Return))
{
GotoRoom(1)
}*/
if (Event.Type == sf::Event::KeyPressed)
{
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
m_GameState = dlb::Exiting;
waiting = false;
}
}
}
}
//Set the Room to be played
void Game::GotoRoom(int roomCode)
{
m_currentRoom = static_cast<dlb::CurrentRoom>(roomCode);
//Clear everything. Reset to original state
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
//Reinitialize everything, set's m_GameState to Playing
InitializeRoom();
}
void Game::ShowGameMenu()
{
//TODO finish
//Need to finish the GUI objects and then combine them all into a menu.
//ImageCache may need to go. It may be extraneous and unnecessary. Though it is a good learning experience
/*
m_GameState = dlb::MainMenu;
GameMenu gameMenu;
int bMenu = gameMenu.Start(); //returns 1 for gameOn, 2 to SplashScreen, 0 to exit and close
//TODO check whether gameOn or exit. if exit, free memory and close window
switch (bMenu)
{
case 0: //Exit the Game
m_GameObjectManager.ClearObjectManager();
m_background.ClearBackground();
m_GameState = dlb::Exiting;
break;
case 1: //gameOn
m_GameState = dlb::Playing;
break;
case 2: //Go to SplashScreen
ShowSplashScreen();
if (m_GameState != dlb::Exiting)
m_GameState = dlb::Playing;
break;
}
*/
}
//static variables
sf::RenderWindow Game::m_MainWindow;
GameObjectManager Game::m_GameObjectManager;
dlb::GameState Game::m_GameState = dlb::Uninitialized;
dlb::CurrentRoom Game::m_currentRoom = dlb::room1;
std::vector<std::string>::const_iterator Game::objectIterator;
std::vector<float>::const_iterator Game::xIterator;
std::vector<float>::const_iterator Game::yIterator;
sf::Font Game::m_scoreFont;
int Game::m_scoreInt;
sf::String Game::m_scoreText;
Background Game::m_background;