-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
928 lines (764 loc) · 27.4 KB
/
Copy pathGame.cpp
File metadata and controls
928 lines (764 loc) · 27.4 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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
#include "Characters.h"
#include "Items.h"
#include "Game.h"
#include <iostream>
#include <conio.h>
#include <ctime>
#include <Windows.h>
using std::cout;
using std::endl;
int NUMBER_OF_ROWS = 30;
int NUMBER_OF_COLUMNS = 25;
int Game::numberOfEnemies = 0;
int Game::numberOfChests = 0;
PlayerCharacter* playerCharacter = nullptr;
Enemy* enemies[10] = {};
Treasure* coins[15] = {};
Treasure* treasureChests[10] = {};
PressurePlate* pressurePlates[10] = {};
Item* walls[250] = {};
/// <summary>
/// Constructor
/// </summary>
Game::Game()
{
}
/// <summary>
/// Deconstructor
/// </summary>
Game::~Game()
{
delete playerCharacter;
for (Item* wall : walls)
{
delete wall;
}
for (Treasure* chest : treasureChests)
{
delete chest;
}
for (Treasure* coin : coins)
{
delete coin;
}
for (Enemy* enemy : enemies)
{
delete enemy;
}
for (Item* pressurePlate : pressurePlates)
{
delete pressurePlate;
}
}
/// <summary>
/// Sets whether the game is stil running
/// </summary>
/// <param name="gameRunning"></param>
void Game::setIsGameRunning(bool gameRunning) { isGameRunning = gameRunning; }
/// <summary>
/// Retrieves whether the game is running
/// </summary>
/// <returns></returns>
bool Game::getIsGameRunning() const { return isGameRunning; }
/// <summary>
/// Sets whether the game is to be replayed
/// </summary>
/// <param name="isReplaying"></param>
void Game::setReplay(bool isReplaying) { replay = isReplaying; }
/// <summary>
/// Retrieves whether the game is going to be replayed.
/// </summary>
/// <returns></returns>
bool Game::getReplay() const { return replay; }
/// <summary>
/// Retrieves the current level
/// </summary>
/// <returns></returns>
int Game::getCurrentLevel() const { return currentLevel; }
/// <summary>
/// Increases the current level
/// </summary>
void Game::increaseCurrentLevel() { currentLevel++; }
/// <summary>
/// Retrieves the number of enemies within the level to display at the top of the screen
/// </summary>
/// <returns></returns>
int Game::getNumberOfEnemies() { return numberOfEnemies; }
/// <summary>
/// Set the number of enemies within the level to display at the top of the screen
/// </summary>
/// <param name="amountToAdd"></param>
void Game::setNumberOfEnemies(int amountToAdd) { numberOfEnemies += amountToAdd; }
/// <summary>
/// Retrieves the number of treasure chests within the level to display at the top of the screen
/// </summary>
/// <returns></returns>
int Game::getNumberOfChests() { return numberOfChests; }
/// <summary>
/// Set the number of treasure chests within the level to display at the top of the screen
/// </summary>
/// <param name="amountToAdd"></param>
void Game::setNumberOfChests(int amountToAdd) { numberOfChests += amountToAdd; }
/// <summary>
/// Gets the Player's input to move the PC or exit the game
/// </summary>
/// <returns></returns>
int Game::getPlayerInput()
{
int keycode = 0;
cout << endl << "WASD - To Move | Esc - To Exit";
do
{
keycode = toupper(_getch()); // Gets the player's input
} while (keycode != 27 && keycode != 87 && keycode != 83 && keycode != 65 && keycode != 68); // While not A, W, S, D or Escape
if (keycode == 27) // Determines whether the player chooses to exit the game
{
isGameRunning = false;
}
return keycode;
}
/// <summary>
/// Displays the Intro Menu
/// </summary>
void Game::displayIntroMenu() const
{
int keycode = 0;
system("cls"); // Clears the console
cout << "\033[33m"; // Sets the colour of text/characters to yellow
cout << R"(
---|--- |----\ |----- /-----\ |----- | | |-----\ |-----
| | | | | | |___ | | | | |
| |_____/ |--- |-----| | | | |_____/ |---
| | \ | | | | | | | \ |
| | | |_____ | | _____| |_____| | | |_____
| | | | |\ | ---|--- |----- |----\
| | | | | \ | | | | |
|-----| | | | \ | | |--- |_____/
| | | | | \ | | | | \
| | |_____| | \| | |_____ | |
)";
cout << endl << endl << "\t\t\t\t Created by Dennis Cullip-Cross" << endl;
cout << endl << "\t\t- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -" << endl;
cout << endl << "\t\t\t\t Collect All the Treasure" << endl;
cout << endl << "\t\t\t\t Avoid All the Enemies" << endl << endl << endl;
cout << "\033[97m"; // Sets the colour of text/characters to bright white
cout << endl << "\t\t\t\t Player Character (^)" << endl;
cout << "\033[31m"; // Sets the colour of text/characters to red
cout << endl << "\t\t\t\t Enemies (W)" << endl;
cout << "\033[34m"; // Sets the colour of text/characters to blue
cout << endl << "\t\t\t\t Pressure Plates (P)" << endl;
cout << "\033[93m"; // Sets the colour of text/characters to bright yellow
cout << endl << "\t\t\t\t Coins (C)" << endl;
cout << endl << "\t\t\t\t Treasure Chests (T)" << endl << endl << endl;
cout << "\033[33m"; // Sets the colour of text/characters to yellow
cout << endl << endl << "\t\t\t\t Press ENTER to play" << endl;
do
{
keycode = toupper(_getch()); // Gets the player's input
} while (keycode != 13); // While the ENTER key has not been pressed
cout << "\033[0m"; // Resets the colour of text/characters displayed on the console
}
void Game::displayLevelMessage() const
{
const int delay = 3000;
cout << "\033[33m"; // Sets the colour of text/characters to yellow
system("cls");
switch (currentLevel)
{
case 0:
// Outputing LEVEL ONE to the console (ASCII Art)
cout << R"(
| |----- \ / |----- | /------\ |\ | |-----
| | \ / | | | | | \ | |
| |--- \ / |-- | | | | \ | |---
| | \ / | | | | | \ | |
|____ |_____ \/ |_____ |____ \________/ | \| |_____ )" << endl;
break;
case 1:
// Outputting LEVEL TWO to the console (ASCII Art)
cout << R"(
| |----- \ / |----- | -----|----- | | /--------\
| | \ / | | | | | | |
| |--- \ / |--- | | | | | |
| | \ / | | | | | | | |
|____ |_____ \/ |_____ |____ | |___|___| \________/ )" << endl;
break;
case 2:
// Outputting LEVEL THREE to the console (ASCII Art)
cout << R"(
| |----- \ / |----- | -----|----- | | |-----\ |----- |-----
| | \ / | | | | | | | | |
| |--- \ / |--- | | |-----| |---\--/ |--- |---
| | \ / | | | | | | \ | |
|____ |_____ \/ |_____ |____ | | | | | |_____ |_____ )" << endl;
break;
default:
break;
}
cout << "\033[0m"; // Resets the colour of text/characters displayed on the console
Sleep(3000); // Starts a three second delay
system("cls"); // Clears the console
}
/// <summary>
/// Creates the in-game entities
/// </summary>
void Game::generateObjects()
{
playerCharacter = new PlayerCharacter; // Creates the player character (PC)
for (int i = 0; i < (sizeof(enemies) / sizeof(enemies[0])); i++)
{
enemies[i] = new Enemy(i); // Creates a new enemy
}
for (int i = 0; i < (sizeof(coins) / sizeof(coins[0])); i++)
{
coins[i] = new Treasure('c', 1, 0, 0); // Creates a new coin
}
for (int i = 0; i < (sizeof(treasureChests) / sizeof(treasureChests[0])); i++)
{
treasureChests[i] = new Treasure('T', 50, 0, 0); // Creates a new treasure chest
}
for (int i = 0; i < (sizeof(pressurePlates) / sizeof(pressurePlates[0])); i++)
{
pressurePlates[i] = new PressurePlate('P', i, 0, 0); // Creates a new pressure plate
}
for (int i = 0; i < (sizeof(walls)) / sizeof(walls[0]); i++)
{
walls[i] = new Item('*', 0, 0); // Creates a new wall
}
}
/// <summary>
/// Resets the state of the game
/// </summary>
void Game::resetObjects()
{
// Reset the player character
playerCharacter->setHealth(-playerCharacter->getHealth() + 100); // Resets the player character's health to 100
playerCharacter->setXCoordinate(-playerCharacter->getXCoordinate()); // Resets the player character's x-coordinate to 0
playerCharacter->setYCoordinate(-playerCharacter->getYCoordinate()); // Resets the player character's y-coordinate to 0
setNumberOfChests(-numberOfChests); // Resets the number of treasure chests
setNumberOfEnemies(-numberOfEnemies); // Resets the number of enemies
// Deactivates and resets the walls
for (Item* wall : walls)
{
wall->deactivateItem();
wall->setXCoordinate(-wall->getXCoordinate()); // Resets the wall's x-coordinate to 0
wall->setYCoordinate(-wall->getYCoordinate()); // Resets the wall's y-coordinate to 0
}
// Deactivates resets the coins
for (Treasure* coin : coins)
{
coin->deactivateItem();
coin->setXCoordinate(-coin->getXCoordinate()); // Resets the coin's x-coordinate to 0
coin->setYCoordinate(-coin->getYCoordinate()); // Resets the coin's y-coordinate to 0
}
// Deactivates and resets the treasure chests
for (Treasure* chest : treasureChests)
{
chest->deactivateItem();
chest->setXCoordinate(-chest->getXCoordinate()); // Resets the chest's x-coordinate to 0
chest->setYCoordinate(-chest->getYCoordinate()); // Resets the chest's y-coordinate to 0
}
// Deactivates and resets the enemies
for (Enemy* enemy : enemies)
{
enemy->setHealth(-enemy->getHealth());
enemy->setXCoordinate(-enemy->getXCoordinate()); // Resets the enemy's x-coordinate to 0
enemy->setYCoordinate(-enemy->getYCoordinate()); // Resets the enemy's y-coordinate to 0
}
// Deactivates and resets the pressure plates
for (PressurePlate* pressurePlate : pressurePlates)
{
pressurePlate->deactivateItem();
pressurePlate->setXCoordinate(-pressurePlate->getXCoordinate()); // Resets the pressure plate's x-coordinate to 0
pressurePlate->setYCoordinate(-pressurePlate->getYCoordinate()); // Resets the pressure plate's y-coordinate to 0
}
}
/// <summary>
/// Loads the map for the current level
/// </summary>
void Game::loadMap()
{
for (int row = 0; row < NUMBER_OF_ROWS; row++) // Loops through each row on the gameboard
{
for (int column = 0; column < NUMBER_OF_COLUMNS; column++) // Loops through each column on the gameboard
{
int tile = map[currentLevel][row][column];
switch (tile)
{
case 1:
playerCharacter->setXCoordinate(column); // Updates the PC's x-coordinate
playerCharacter->setYCoordinate(row); // Updates the PC's y-coordinate
break;
case 2:
for (Item* wall : walls)
{
if (!wall->getIsInteractable())
{
wall->activateItem();
wall->setXCoordinate(column); // Set the wall's x-coordinate
wall->setYCoordinate(row); // Set the wall's y-coordinate
break;
}
}
break;
case 3:
for (Enemy* enemy : enemies)
{
if (enemy->getHealth() <= 0.0f)
{
enemy->setHealth(50.0f);
enemy->setXCoordinate(column); // Set the enemy's initial x-coordinate
enemy->setYCoordinate(row); // Set the enemy's initial y-coordinate
setNumberOfEnemies(1); // Increases the enemy counter
break;
}
}
break;
case 4:
for (PressurePlate* pressurePlate : pressurePlates)
{
if (!pressurePlate->getIsInteractable())
{
pressurePlate->setXCoordinate(column); // Set the pressure plates's x-coordinate
pressurePlate->setYCoordinate(row); // Set the pressure plates's y-coordinate
pressurePlate->activateItem();
break;
}
}
break;
case 5:
for (Treasure* chest : treasureChests)
{
if (!chest->getIsInteractable())
{
chest->activateItem();
chest->setXCoordinate(column); // Set chest's x-coordinate
chest->setYCoordinate(row); // Set chest's y-coordinate
setNumberOfChests(1); // Increase the chest counter
break;
}
}
break;
default:
break;
}
}
}
generateCoins(); // Generate the coins in the level
displayLevelMessage(); // Displays a message indicating the current level
}
/// <summary>
/// Generates the coins in each level randomly
/// </summary>
void Game::generateCoins()
{
int numberOfCoinsToSpawn = 0;
srand(static_cast<unsigned int>(time(0)));
switch (currentLevel)
{
case 0: // Level 1
numberOfCoinsToSpawn = 5;
break;
case 1: // Level 2
numberOfCoinsToSpawn = 7;
break;
case 2: // Level 3
numberOfCoinsToSpawn = 10;
break;
}
for (int i = 0; i < numberOfCoinsToSpawn;)
{
int xPosition = rand() % NUMBER_OF_COLUMNS; // Generates random x-coordinate
int yPosition = rand() % NUMBER_OF_ROWS; // Generates random y-coordinate
if (map[currentLevel][yPosition][xPosition] == 0) // Is the generated position avaliable?
{
map[currentLevel][yPosition][xPosition] = 7; // Marks the coordinate as occupied
coins[i]->activateItem();
coins[i]->setXCoordinate(xPosition); // Sets the x-coordinate of the coin
coins[i]->setYCoordinate(yPosition); // Sets the y-coordinate of the coin
i++;
}
}
}
/// <summary>
/// Displays the current state of the gameboard
/// </summary>
void Game::displayBoard() const
{
system("cls"); // Clears the console
cout << "\033[33m";
cout << " Level: " << currentLevel + 1 << endl;
cout << endl << " PC Health: " << playerCharacter->getHealth() << " | "; // Display's the PC's current health
cout << "Enemies: " << getNumberOfEnemies() << " | "; // Displays the current number of enemies
cout << "Chests Remaining: " << getNumberOfChests() << " | "; // Displays the current number of chests
cout << "Coins Collected: " << playerCharacter->getNumberOfCoins() << endl << endl; // Displays the number of coins collected
char symbol = ' ';
for (int row = 0; row < NUMBER_OF_ROWS; row++)
{
for (int column = 0; column < NUMBER_OF_COLUMNS; column++)
{
symbol = ' ';
cout << "\033[0m";
for (Item* wall : walls)
{
if (wall->getIsInteractable() && wall->getXCoordinate() == column && wall->getYCoordinate() == row) // Is the entity a wall?
{
cout << "\033[97m"; // Sets the colour of text/characters to bright white
symbol = wall->getSymbol(); // Output wall symbol to the console.
}
}
for (Enemy* enemy : enemies)
{
if (enemy->getHealth() > 0 && enemy->getXCoordinate() == column && enemy->getYCoordinate() == row) // Is the entity an enemy?
{
cout << "\033[91m"; // Sets the colour of text/characters to bright red
symbol = enemy->getSymbol(); // Output enemy symbol to the console.
}
}
for (Item* treasure : treasureChests)
{
if (treasure->getIsInteractable() && treasure->getXCoordinate() == column && treasure->getYCoordinate() == row) // Is the entity a treasure chest?
{
cout << "\033[93m"; // Sets the colour of text/characters to bright yellow
symbol = treasure->getSymbol(); // Output treasure chest symbol to the console.
}
}
for (Item* coin : coins)
{
if (coin->getIsInteractable() && coin->getXCoordinate() == column && coin->getYCoordinate() == row) // Is the entity a coin?
{
cout << "\033[93m"; // Sets the colour of text/characters to bright yellow
symbol = coin->getSymbol(); // Output coin symbol to the console.
}
}
for (PressurePlate* pressurePlate : pressurePlates)
{
if (pressurePlate->getIsInteractable() && pressurePlate->getXCoordinate() == column && pressurePlate->getYCoordinate() == row) // Is the entity a pressure plate?
{
cout << "\033[34m"; // Sets the colour of text/characters to blue
symbol = pressurePlate->getSymbol(); // Output pressure plate symbol to the console.
}
}
if (playerCharacter->getHealth() > 0 && playerCharacter->getXCoordinate() == column && playerCharacter->getYCoordinate() == row) // Is the entity the player character (PC)?
{
cout << "\033[97m"; // Sets the colour of text/characters to bright white
symbol = playerCharacter->getSymbol(); // Output player character symbol to the console.
}
cout << ' ' << symbol << ' ';
}
cout << endl;
}
}
/// <summary>
/// Checks if the PC is overlapping with an in-game entity
/// </summary>
/// <param name="previousXPosition"></param>
/// <param name="previousYPosition"></param>
void Game::checkForPcCollision(int previousXPosition, int previousYPosition)
{
for (Enemy* enemy : enemies)
{
if (enemy->getXCoordinate() == playerCharacter->getXCoordinate() && enemy->getYCoordinate() == playerCharacter->getYCoordinate() && enemy->getHealth() > 0) // Is PC colliding with an enemy?
{
fightEnemy(enemy->getID()); // Start the combat sequence
}
}
for (Treasure* coin : coins)
{
if (coin->getIsInteractable() && coin->getXCoordinate() == playerCharacter->getXCoordinate() && coin->getYCoordinate() == playerCharacter->getYCoordinate()) // Is PC colliding with a coin?
{
playerCharacter->setNumberOfCoins(coin->getValue()); // Increases the coins collected counter at the top of the screen
coin->deactivateItem(); // Deactivate the coin
}
}
for (Treasure* chest : treasureChests)
{
if (chest->getIsInteractable() && chest->getXCoordinate() == playerCharacter->getXCoordinate() && chest->getYCoordinate() == playerCharacter->getYCoordinate()) // Is PC colliding with a chest?
{
setNumberOfChests(-1); // Decreases the number of treasure chests remaining
chest->deactivateItem(); // Deactivate the chest
}
}
for (PressurePlate* pressurePlate : pressurePlates)
{
if (pressurePlate->getIsInteractable() && pressurePlate->getXCoordinate() == playerCharacter->getXCoordinate() && pressurePlate->getYCoordinate() == playerCharacter->getYCoordinate()) // Is PC colliding with a pressure plate?
{
openDoor(pressurePlate->getXCoordinate(), pressurePlate->getYCoordinate()); // Opens the locked door
pressurePlate->deactivateItem(); // Deactivates the pressure plate
}
}
for (Item* wall : walls)
{
if (wall->getIsInteractable() && wall->getXCoordinate() == playerCharacter->getXCoordinate() && wall->getYCoordinate() == playerCharacter->getYCoordinate()) // Is PC hitting a wall?
{
playerCharacter->setXCoordinate(-playerCharacter->getXCoordinate() + previousXPosition); // Return PC to previous x-coordinate (x position)
playerCharacter->setYCoordinate(-playerCharacter->getYCoordinate() + previousYPosition); // Return PC to previous y-coordinate (y position)
}
}
}
/// <summary>
/// Moves the enemies within the level
/// </summary>
void Game::moveEnemies()
{
for (Enemy* enemy : enemies)
{
int previousXPosition = enemy->getXCoordinate();
int previousYPosition = enemy->getYCoordinate();
enemy->moveEnemy(playerCharacter->getXCoordinate(), playerCharacter->getYCoordinate()); // Moves the enemy
for (Item* wall : walls)
{
if (wall->getIsInteractable() && wall->getXCoordinate() == enemy->getXCoordinate() && wall->getYCoordinate() == enemy->getYCoordinate()) // Is enemy hitting a wall?
{
enemy->setXCoordinate(-enemy->getXCoordinate() + previousXPosition); // Return enemy to previous x-coordinate (x position)
enemy->setYCoordinate(-enemy->getYCoordinate() + previousYPosition); // Return enemy to previous y-coordinate (y position)
}
}
for (Enemy* otherEnemy : enemies)
{
if (enemy->getID() != otherEnemy->getID()) // Are enemy and otherEnemy different entities?
{
if (otherEnemy->getHealth() > 0 && otherEnemy->getXCoordinate() == enemy->getXCoordinate() && otherEnemy->getYCoordinate() == enemy->getYCoordinate())
{
enemy->setXCoordinate(-enemy->getXCoordinate() + previousXPosition); // Return enemy to previous x-coordinate (x position)
enemy->setYCoordinate(-enemy->getYCoordinate() + previousYPosition); // Return enemy to previous y-coordinate (y position)
}
}
}
}
}
/// <summary>
/// Activates the pressure plate and opens the corresponding door
/// </summary>
/// <param name="xPosition"></param>
/// <param name="yPosition"></param>
void Game::openDoor(int xPosition, int yPosition)
{
int deactivateDoorXPosition = -1;
int deactivateDoorYPosition = -1;
switch (currentLevel)
{
case 0: // Level 1
if (xPosition == 8 && yPosition == 25) // Is PC standing on pressure plate?
{
deactivateDoorXPosition = 8;
deactivateDoorYPosition = 6;
}
else if (xPosition == 6 && yPosition == 1) // Is PC standing on pressure plate?
{
deactivateDoorXPosition = 19;
deactivateDoorYPosition = 3;
}
else if (xPosition == 22 && yPosition == 1) // Is PC standing on pressure plate?
{
deactivateDoorXPosition = 11;
deactivateDoorYPosition = 16;
}
break;
case 1: // Level 2
if (xPosition == 2 && yPosition == 3) // Is PC standing on pressure plate?
{
deactivateDoorXPosition = 5;
deactivateDoorYPosition = 15;
}
else if (xPosition == 7 && yPosition == 26) // Is PC standing on pressure plate?
{
deactivateDoorXPosition = 10;
deactivateDoorYPosition = 23;
}
else if (xPosition == 13 && yPosition == 3) // Is PC standing on pressure plate?
{
deactivateDoorXPosition = 15;
deactivateDoorYPosition = 11;
}
else if (xPosition == 17 && yPosition == 7) // Is PC standing on pressure plate?
{
deactivateDoorXPosition = 20;
deactivateDoorYPosition = 22;
}
break;
case 2: // Level 3
if (xPosition == 4 && yPosition == 23) // Is PC standing on pressure plate?
{
deactivateDoorXPosition = 16;
deactivateDoorYPosition = 15;
}
else if (xPosition == 22 && yPosition == 28) // Is PC standing on pressure plate?
{
deactivateDoorXPosition = 8;
deactivateDoorYPosition = 8;
}
else if (xPosition == 10 && yPosition == 1) // Is PC standing on pressure plate?
{
deactivateDoorXPosition = 2;
deactivateDoorYPosition = 19;
}
else if (xPosition == 17 && yPosition == 18) // Is PC standing on pressure plate?
{
deactivateDoorXPosition = 10;
deactivateDoorYPosition = 14;
}
break;
}
for (Item* wall : walls)
{
if (wall->getXCoordinate() == deactivateDoorXPosition && wall->getYCoordinate() == deactivateDoorYPosition) // Unlocks the correct door
{
wall->deactivateItem(); // Unlocks the entrance
}
}
}
/// <summary>
/// The PvE combat system
/// </summary>
/// <param name="enemyIndex"></param>
void Game::fightEnemy(int enemyIndex)
{
const unsigned int delay = 3000;
while (enemies[enemyIndex]->getHealth() > 0 && playerCharacter->getHealth() > 0)
{
system("cls"); // Clears the console
cout << "PC Health: " << playerCharacter->getHealth() << endl; // Displays the PC's current health
cout << "Enemy " << enemyIndex << " Health: " << enemies[enemyIndex]->getHealth() << endl; // Displays the enemy's current health
char playerInput = ' ';
while (playerInput != '1' && playerInput != '2' && playerInput != '3')
{
cout << endl << endl << endl << "Attack (1) || DEFEND (2) || GIVE UP (3)" << endl;
playerInput = _getch();
}
switch (playerInput)
{
case '1': // PC attacks the enemy
cout << "PC and the opponent attacked one another!" << endl;
enemies[enemyIndex]->setHealth(-playerCharacter->getDamage()); // Redeuces the enemy's health
if (enemies[enemyIndex]->getHealth() > 0)
{
playerCharacter->setHealth(-enemies[enemyIndex]->getDamage()); // Redeuces the PC's health
}
break;
case '2': // PC blocks the incoming attack
cout << "PC blocked the opponent's attack!" << endl;
playerCharacter->setHealth(-enemies[enemyIndex]->getDamage() * 0.5f); // Redeuces the PC's health
break;
case '3': // Player gives up
cout << "PC gave up..." << endl;
playerCharacter->setHealth(-playerCharacter->getHealth()); // Reduce's the PC's health to 0
break;
default:
break;
}
if (enemies[enemyIndex]->getHealth() <= 0 && playerCharacter->getHealth() > 0) // Is the enemy defeated and is the PC alive?
{
cout << "The PC was victorious!" << endl;
setNumberOfEnemies(-1); // Reduces the enemy counter at the top of the screen
}
else if(playerCharacter->getHealth() <= 0)
{
cout << "YOU LOST..." << endl;
displayPlayerLostScreen(); // Display the Player Lost Screen
break;
}
Sleep(delay); // Causes a three second delay
}
}
/// <summary>
/// Displays the player defeat/lost menu
/// </summary>
void Game::displayPlayerLostScreen()
{
char playerInput = ' ';
system("cls"); // Clears the console
cout << "You Lost..." << endl << "Play again (y/n)" << endl;
while (playerInput != 'y' && playerInput != 'n')
{
playerInput = tolower(_getch()); // Gets the player's input
}
if (playerInput == 'y')
{
replay = true;
isGameRunning = true;
playerCharacter->setNumberOfCoins(-playerCharacter->getNumberOfCoins()); // Resets the number of coins the PC has gathered to 0
currentLevel = 0; // Sets the current level to level one
resetObjects(); // Resets the state of each in-game entity
loadMap(); // Loads the map data for level one
}
else if(playerInput == 'n')
{
replay = false;
isGameRunning = false;
}
}
/// <summary>
/// Displays the player victory/win menu
/// </summary>
void Game::displayPlayerWonScreen()
{
char playerInput = ' ';
system("cls"); // Clears the console
cout << "You Won!" << endl << "Play again (y/n)" << endl;
while (playerInput != 'y' && playerInput != 'n')
{
playerInput = tolower(_getch()); // Gets the player's input
}
if (playerInput == 'y')
{
replay = true;
isGameRunning = true;
playerCharacter->setNumberOfCoins(-playerCharacter->getNumberOfCoins()); // Resets the number of coins the PC has gathered to 0
currentLevel = 0; // Sets the current level to level one
resetObjects(); // Resets the state of each in-game entity
loadMap(); // Loads the map data for level one
}
else if (playerInput == 'n')
{
replay = false;
isGameRunning = false;
}
}
/// <summary>
/// Checks if the player has won or lost
/// </summary>
void Game::checkWin()
{
if ((numberOfChests <= 0 && currentLevel == 0) || (numberOfChests <= 0 && currentLevel == 1))
{
currentLevel++;
resetObjects(); // Resets the state of each in-game entity
loadMap(); // Loads the map data for the next level
}
else if (numberOfChests <= 0 && currentLevel == 2)
{
displayPlayerWonScreen();
playerCharacter->setNumberOfCoins(-playerCharacter->getNumberOfCoins()); // Resets the number of coins the PC has gathered to 0
currentLevel = 0;
}
}
/// <summary>
/// Main gameplay loop
/// </summary>
void Game::updateGame()
{
displayIntroMenu(); // Displays the Intro Menu
generateObjects(); // Generates in-game entities
resetObjects(); // Resets the state of each in-game entity
loadMap(); // Loads the map data for level one
while (replay)
{
while (isGameRunning)
{
displayBoard(); // Displays the updated game board.
isGameRunning = playerCharacter->getHealth() > 0 ? true : false; // Checks whether the PC is still alive.
if (!isGameRunning)
{
return;
}
int previousXPosition = playerCharacter->getXCoordinate(); // Stores the PC's x-coordinate before moving
int previousYPosition = playerCharacter->getYCoordinate(); // Stores the PC's y-coordinate before moving
playerCharacter->movePC(getPlayerInput()); // Allows the pc to move using the WASD keys
moveEnemies(); // Moves the enemies
checkForPcCollision(previousXPosition, previousYPosition); // Checks if the player character is overlapping with another object
checkWin(); // Checks whether the PC has won the game
}
return;
}
}