-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
855 lines (785 loc) · 32.1 KB
/
main.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
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
#include "Dependencies.h"
#include <string>
#include <sstream>
#include <cstdlib>
/********************************************************
* main -- Part of the game that handles game loop and
* game mechanics like collision and user input
*
* Author: Nathaniel Clark, Sebastian Mark
*
* Purpose: Makes game window and uses game assets to build
* a functional game.
*
* Run the program with all needed files and then play the game
********************************************************/
//Score, window size, and the number of lives for the player.
static int score = 0;
static const float VIEW_WITDH = 1920.f;
static const float VIEW_HEIGHT = 1080.f;
static int lives = 3;
//Booleans to handle conditionals for movement and game pausing and lose/win conditions
bool wKeyReleased = true;
bool aKeyReleased = true;
bool sKeyReleased = true;
bool dKeyReleased = true;
bool endGame = false;
bool pause = false;
bool game_menu_toggle = true;
bool win = false;
//Game objects for window, view, and overlays for game states
sf::RenderWindow window(sf::VideoMode(VIEW_WITDH, VIEW_HEIGHT), "Dungeon Quest!", sf::Style::Resize | sf::Style::Close);
sf::View view(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
sf::RectangleShape game_pause;
sf::RectangleShape game_menu;
sf::RectangleShape game_win_lose_menu;
//Text that is displayed in the game
sf::Event event;
sf::Text scoreText;
sf::Font scoreFont;
sf::Text pause_screen_text;
sf::Text number_of_lives_text;
// Text for beginning instructions
sf::Text game_menu_text;
// Text for the win/lose screen
sf::Text game_win_lose_text;
//sprites to display at the start of the game to describe who is what is the game.
sf::Texture hero_menu_texture;
sf::Sprite hero_main_screen_sprite;
sf::Texture enemy1_menu_texture;
sf::Sprite enemy1_main_screen_sprite;
sf::Texture fireball_menu_texture;
sf::Sprite fireball_main_screen_sprite;
sf::Texture fireball_skull_menu_texture;
sf::Sprite fireball_skull_main_screen_sprite;
//Game objects like sprites, windows, players, Enemies, Animations, sounds, and the level.
Music gameMusic("../Assets/VillageConsort-KevinMacLeod.ogg");
Character character("../Assets/Character_animation/Knight.png", sf::IntRect( 0, 0, 16, 16), "../Assets/step.wav");
Fireball playerFireball("../Assets/Fireball.png", "../Assets/FireballShoot.wav", "../Assets/FireHitWall.wav");
Enemy enemy1("../Assets/Character_animation/Vampire.png", sf::IntRect(0, 0, 16, 16));
Enemy enemy2("../Assets/Character_animation/FlyingSkull.png", sf::IntRect(0, 0, 16, 16));
Enemy enemy3("../Assets/Character_animation/FlyingSkull.png", sf::IntRect(0, 0, 16, 16));
Fireball enemyFireBall1("../Assets/Fireball.png", "../Assets/FireballShoot.wav", "../Assets/FireHitWall.wav");
Fireball enemyFireBall2("../Assets/Fireball.png", "../Assets/FireballShoot.wav", "../Assets/FireHitWall.wav");
Fireball enemyFireBall3("../Assets/Fireball.png", "../Assets/FireballShoot.wav", "../Assets/FireHitWall.wav");
Fireball enemyFireBall4("../Assets/Fireball.png", "../Assets/FireballShoot.wav", "../Assets/FireHitWall.wav");
Sound hurtSound("../Assets/EnemyDie.wav");
Animation enemy1Animation(*enemy1.getSprite());
Animation enemy2Animation(*enemy2.getSprite());
Animation enemy3Animation(*enemy3.getSprite());
Animation characterAnimation(*character.getSprite());
Animation fireballAnimation(*playerFireball.getSprite());
Animation enemyFireBall1Animation(*enemyFireBall1.getSprite());
Animation enemyFireBall2Animation(*enemyFireBall2.getSprite());
Animation enemyFireBall3Animation(*enemyFireBall3.getSprite());
Animation enemyFireBall4Animation(*enemyFireBall4.getSprite());
Level level("../Assets/Dungeon_Tileset.png");
/********************************************************
* checkCollision - function that checks if two objects are in contact
*
* Parameters
* sprite1 - an sprite object
* sprite2 - another sprite object that we see is touching sprite1
*
* Return - true if sprites are touching, else false
********************************************************/
bool checkCollision(sf::Sprite* sprite1, sf::Sprite* sprite2){
if (sprite1->getGlobalBounds().intersects(sprite2->getGlobalBounds())){
return true;
}
return false;
}
/********************************************************
* enemies_init - function that initializes assets relating to enemies, like beginning states and animation
*
* Parameters - none
*
* Return - void
********************************************************/
void enemies_init(){
//initial enemies states
enemy1.setPosition(1573.0f, 605.0f);
enemy1.setScale(7.f, 7.f);
enemy1.setCurrentTile(93);
enemy2.set_Alive(false);
enemy3.set_Alive(false);
//Animations for the enemies and enemy fireballs
enemy1Animation.addFrame({sf::IntRect(0, 0, 16, 16), 3});
enemy1Animation.addFrame({sf::IntRect(16, 0, 16, 16), 3});
enemy1Animation.addFrame({sf::IntRect(32, 0, 16, 16), 3});
enemy1Animation.addFrame({sf::IntRect(48, 0, 16, 16), 3});
enemy2Animation.addFrame({sf::IntRect(0, 0, 16, 16), 3});
enemy2Animation.addFrame({sf::IntRect(16, 0, 16, 16), 3});
enemy2Animation.addFrame({sf::IntRect(32, 0, 16, 16), 3});
enemy2Animation.addFrame({sf::IntRect(48, 0, 16, 16), 3});
enemy3Animation.addFrame({sf::IntRect(0, 0, 16, 16), 3});
enemy3Animation.addFrame({sf::IntRect(16, 0, 16, 16), 3});
enemy3Animation.addFrame({sf::IntRect(32, 0, 16, 16), 3});
enemy3Animation.addFrame({sf::IntRect(48, 0, 16, 16), 3});
enemyFireBall1Animation.addFrame({sf::IntRect(0, 0, 16, 16), 3});
enemyFireBall1Animation.addFrame({sf::IntRect(16, 0, 16, 16), 3});
enemyFireBall1Animation.addFrame({sf::IntRect(32, 0, 16, 16), 3});
enemyFireBall1Animation.addFrame({sf::IntRect(48, 0, 16, 16), 3});
enemyFireBall1Animation.update(0.5);
enemyFireBall1.getSprite()->setScale(7.5, 7.5);
enemyFireBall2Animation.addFrame({sf::IntRect(0, 0, 16, 16), 3});
enemyFireBall2Animation.addFrame({sf::IntRect(16, 0, 16, 16), 3});
enemyFireBall2Animation.addFrame({sf::IntRect(32, 0, 16, 16), 3});
enemyFireBall2Animation.addFrame({sf::IntRect(48, 0, 16, 16), 3});
enemyFireBall2Animation.update(0.5);
enemyFireBall2.getSprite()->setScale(7.5, 7.5);
enemyFireBall3Animation.addFrame({sf::IntRect(0, 0, 16, 16), 3});
enemyFireBall3Animation.addFrame({sf::IntRect(16, 0, 16, 16), 3});
enemyFireBall3Animation.addFrame({sf::IntRect(32, 0, 16, 16), 3});
enemyFireBall3Animation.addFrame({sf::IntRect(48, 0, 16, 16), 3});
enemyFireBall3Animation.update(0.5);
enemyFireBall3.getSprite()->setScale(7.5, 7.5);
enemyFireBall4Animation.addFrame({sf::IntRect(0, 0, 16, 16), 3});
enemyFireBall4Animation.addFrame({sf::IntRect(16, 0, 16, 16), 3});
enemyFireBall4Animation.addFrame({sf::IntRect(32, 0, 16, 16), 3});
enemyFireBall4Animation.addFrame({sf::IntRect(48, 0, 16, 16), 3});
enemyFireBall4Animation.update(0.5);
enemyFireBall4.getSprite()->setScale(7.5, 7.5);
}
/********************************************************
* hero_attributes - function to initialize player attributes and animations
*
* Parameters - none
*
* Return - void
********************************************************/
void hero_attributes(){
//players initial state and animation
character.setScale(7.f, 7.f);
character.setPosition(242.0f, 242.0f);
character.setCurrentTile(34);
characterAnimation.addFrame({sf::IntRect(0, 0, 16, 16), 3});
characterAnimation.addFrame({sf::IntRect(16, 0, 16, 16), 3});
characterAnimation.addFrame({sf::IntRect(32, 0, 16, 16), 3});
characterAnimation.addFrame({sf::IntRect(48, 0, 16, 16), 3});
//animation for player fireball
fireballAnimation.addFrame({sf::IntRect(0, 0, 16, 16), 3});
fireballAnimation.addFrame({sf::IntRect(16, 0, 16, 16), 3});
fireballAnimation.addFrame({sf::IntRect(32, 0, 16, 16), 3});
fireballAnimation.addFrame({sf::IntRect(48, 0, 16, 16), 3});
fireballAnimation.update(0.5); //work around to prevent an issue with global bounds being wrong when shooting fireball for first time.
}
/********************************************************
* score_text - function to initialize score text
*
* Parameters - none
*
* Return - void
********************************************************/
void score_text(){
scoreText.setFont(scoreFont);
scoreText.setCharacterSize(50);
scoreText.setFillColor(sf::Color::White);
scoreText.setPosition(860, 30);
}
/********************************************************
* hero_lives - function to initialize player lives text
*
* Parameters - none
*
* Return - void
********************************************************/
void hero_lives(){
number_of_lives_text.setFont(scoreFont);
number_of_lives_text.setCharacterSize(50);
number_of_lives_text.setFillColor(sf::Color::White);
number_of_lives_text.setPosition(400,30);
}
/********************************************************
* score_font - function to initialize score text
*
* Parameters - none
*
* Return - void
********************************************************/
void score_font(){
scoreFont.loadFromFile("../Assets/Hack-Regular.ttf");
}
/********************************************************
* resize_window - function to handle proper scaling of the game when
* the window is resized to prevent the game from looking stretched
*
* Parameters - none
*
* Return - void
*
* //code source: https://github.com/SFML/SFML/wiki/Source%3A-Letterbox-effect-using-a-view
********************************************************/
void resize_window(){
if (event.type == sf::Event::Resized){
float windowRatio = event.size.width / (float) event.size.height;
float viewRatio = view.getSize().x / (float) view.getSize().y;
float sizeX = 1;
float sizeY = 1;
float posX = 0;
float posY = 0;
bool horizontalSpacing = true;
if (windowRatio < viewRatio)
horizontalSpacing = false;
// If horizontalSpacing is true, the black bars will appear on the left and right side.
// Otherwise, the black bars will appear on the top and bottom.
if (horizontalSpacing) {
sizeX = viewRatio / windowRatio;
posX = (1 - sizeX) / 2.f;
}
else {
sizeY = windowRatio / viewRatio;
posY = (1 - sizeY) / 2.f;
}
view.setViewport( sf::FloatRect(posX, posY, sizeX, sizeY) );
}
}
/********************************************************
* update_KeyRelease - function to watch when a key is released
* so game movement happens properly.
*
* Parameters - none
*
* Return - void
********************************************************/
void update_KeyRelease(){
if (event.type == sf::Event::KeyReleased) {
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
wKeyReleased = true;
}
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
aKeyReleased = true;
}
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
sKeyReleased = true;
}
if (!sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
dKeyReleased = true;
}
}
}
void handle_userInput();
void waitForUnpause();
/********************************************************
* handle_collision - checks and handles object collision, ei lives, enemies dying, ect
*
* Parameters - none
*
* Return - void
********************************************************/
void handle_collision(){
if (enemy1.isAlive() && checkCollision(character.getSprite(), enemy1.getSprite())){
lives -=1;
hurtSound.play();
}
if (enemy2.isAlive() && checkCollision(character.getSprite(), enemy2.getSprite())){
lives -=1;
hurtSound.play();
}
if (enemy3.isAlive() && checkCollision(character.getSprite(), enemy3.getSprite())){
lives -=1;
hurtSound.play();
}
if (enemyFireBall1.getIsAlive() && checkCollision(character.getSprite(), enemyFireBall1.getSprite())){
lives -=1;
hurtSound.play();
enemyFireBall1.setIsAlive(false);
}
if (enemyFireBall2.getIsAlive() && checkCollision(character.getSprite(), enemyFireBall2.getSprite())){
lives -=1;
hurtSound.play();
enemyFireBall2.setIsAlive(false);
}
if (enemyFireBall3.getIsAlive() && checkCollision(character.getSprite(), enemyFireBall3.getSprite())){
lives -=1;
hurtSound.play();
enemyFireBall3.setIsAlive(false);
}
if (enemyFireBall4.getIsAlive() && checkCollision(character.getSprite(), enemyFireBall4.getSprite())){
lives -=1;
hurtSound.play();
enemyFireBall4.setIsAlive(false);
}
if(enemy1.isAlive() && checkCollision(playerFireball.getSprite(), enemy1.getSprite())){
score += 1;
cout << "Enemy hit!" << endl;
enemy1.set_Alive(false);
//get the fireball off-screen to prevent it from hitting an enemy randomly during enemy respawn.
playerFireball.setIsAlive(false);
playerFireball.playFizzleSound();
playerFireball.getSprite()->setPosition(-100, -100);
hurtSound.play();
}
if(enemy2.isAlive() && checkCollision(playerFireball.getSprite(), enemy2.getSprite())){
score += 1;
cout << "Enemy hit!" << endl;
enemy2.set_Alive(false);
//get the fireball off-screen to prevent it from hitting an enemy randomly during enemy respawn.
playerFireball.setIsAlive(false);
playerFireball.playFizzleSound();
playerFireball.getSprite()->setPosition(-100, -100);
hurtSound.play();
}
if(enemy3.isAlive() && checkCollision(playerFireball.getSprite(), enemy3.getSprite())){
score += 1;
cout << "Enemy hit!" << endl;
enemy3.set_Alive(false);
//get the fireball off-screen to prevent it from hitting an enemy randomly during enemy respawn.
playerFireball.setIsAlive(false);
playerFireball.playFizzleSound();
playerFireball.getSprite()->setPosition(-100, -100);
hurtSound.play();
}
// logic for fireballs
if(playerFireball.getIsAlive()){
if(!level.isTileXWalkable(playerFireball.getCurrentTile())){
playerFireball.setIsAlive(false);
playerFireball.playFizzleSound();
}
}
if(enemyFireBall1.getIsAlive()){
if(!level.isTileXWalkable(enemyFireBall1.getCurrentTile())){
enemyFireBall1.setIsAlive(false);
enemyFireBall1.playFizzleSound();
}
}
if(enemyFireBall2.getIsAlive()){
if(!level.isTileXWalkable(enemyFireBall2.getCurrentTile())){
enemyFireBall2.setIsAlive(false);
enemyFireBall2.playFizzleSound();
}
}
if(enemyFireBall3.getIsAlive()){
if(!level.isTileXWalkable(enemyFireBall3.getCurrentTile())){
enemyFireBall3.setIsAlive(false);
enemyFireBall3.playFizzleSound();
}
}
if(enemyFireBall4.getIsAlive()){
if(!level.isTileXWalkable(enemyFireBall4.getCurrentTile())){
enemyFireBall4.setIsAlive(false);
enemyFireBall4.playFizzleSound();
}
}
}
/********************************************************
* handle_levelChange - checks to see if the level should go to the next,
* spawns all proper enemies and changes layout of map
*
* Parameters - none
*
* Return - void
********************************************************/
void handle_levelChange(){
if(score == 1 and level.getCurrentLevel() == 1) {
level.nextLevel();\
character.setPosition(242, 242);
character.setCurrentTile(34);
enemy1.setPosition(1573, 242);
enemy1.setCurrentTile(45);
enemy1.setSprite("../Assets/Character_animation/FlyingSkull.png");
enemy1.set_Alive(true);
enemy2.setPosition(242, 726);
enemy2.setCurrentTile(98);
enemy2.set_Alive(true);
enemy2.setScale(7.5, 7.5);
enemy3.setPosition(1573, 726);
enemy3.setCurrentTile(109);
enemy3.set_Alive(true);
enemy3.setScale(7.5, 7.5);
} if(score == 4 && level.getCurrentLevel() == 2){
level.nextLevel();
character.setPosition(242, 242);
character.setCurrentTile(34);
enemy1.setPosition(968, 363);
enemy1.setCurrentTile(56);
enemy1.setSprite("../Assets/Character_animation/SkellyWithScyth.png");
enemy1.set_Alive(true);
enemy2.setSprite("../Assets/Character_animation/SkellyWithSword.png");
enemy2.setPosition(242, 726);
enemy2.setCurrentTile(98);
enemy2.set_Alive(true);
enemy2.setScale(7.5, 7.5);
enemy3.setSprite("../Assets/Character_animation/SkellyWithSword.png");
enemy3.setPosition(1573, 726);
enemy3.setCurrentTile(109);
enemy3.set_Alive(true);
enemy3.setScale(7.5, 7.5);
}
if(score == 7 && level.getCurrentLevel() == 3){
level.nextLevel();
character.setPosition(242, 484);
character.setCurrentTile(66);
enemy1.set_Alive(true);
enemy1.setBoss(true);
enemy1.setPosition(1573, 484);
enemy1.setCurrentTile(77);
enemy1.setSprite("../Assets/Character_animation/EvilKnight.png");
gameMusic.changeMusic("../Assets/Death-and-Axes-KevinMacLeod.ogg");
}
if (score == 8){
win = true;
}
}
/********************************************************
* shootEnemyFireballs - checks to see if boss shoots fireballs
*
* Parameters - none
*
* Return - void
********************************************************/
void shootEnemyFireballs(int rand){
rand = rand % 5;
if (rand == 0){
if (!enemyFireBall1.getIsAlive()) {
enemyFireBall1.shoot(1, enemy1.getCurrentTile() - 16, sf::Vector2f(enemy1.getSprite()->getPosition().x + enemyFireBall1.getSprite()->getGlobalBounds().width/2, enemy1.getSprite()->getPosition().y - 121 - enemyFireBall1.getSprite()->getGlobalBounds().height));
}
if (!enemyFireBall2.getIsAlive()) {
enemyFireBall2.shoot(2, enemy1.getCurrentTile() + 16, sf::Vector2f(enemy1.getSprite()->getPosition().x + enemyFireBall2.getSprite()->getGlobalBounds().width/2, enemy1.getSprite()->getPosition().y + 121 + enemyFireBall2.getSprite()->getGlobalBounds().height*2));
}
if (!enemyFireBall3.getIsAlive()) {
enemyFireBall3.shoot(3, enemy1.getCurrentTile() - 1, sf::Vector2f(enemy1.getSprite()->getPosition().x - 121 - enemyFireBall3.getSprite()->getGlobalBounds().width, enemy1.getSprite()->getPosition().y + enemyFireBall3.getSprite()->getGlobalBounds().height/2));
}
if (!enemyFireBall4.getIsAlive()) {
enemyFireBall4.shoot(4, enemy1.getCurrentTile() + 1, sf::Vector2f(enemy1.getSprite()->getPosition().x + 121 + enemyFireBall4.getSprite()->getGlobalBounds().width*2, enemy1.getSprite()->getPosition().y + enemyFireBall4.getSprite()->getGlobalBounds().height/2));
}
}
}
//ghost function definitions
void pause_game();
void game_menu_window();
void close_game_menu();
void game_over_window();
void you_win_window();
/********************************************************
* main - contains gameloop and window, allows for proper game flow
*
* Parameters - none
*
* Return - void
********************************************************/
int main() {
window.setView(view);
// scaling the map size for full screen
level.getTilemap()->setScale(7.5f,7.5f);
level.getTilemap()->setPosition(0,0);
window.setFramerateLimit(144);
window.setView(view);
//Create random number
srand((int) time(0));
sf::Clock clock;
playerFireball.getSprite()->setScale(7.5, 7.5);
playerFireball.getSprite()->setPosition(-100, -100);
hero_attributes();
enemies_init();
score_text();
score_font();
game_menu_window();
gameMusic.setVolume(50);
// testing hero lives
hero_lives();
while (window.isOpen()) {
if(lives == 0){
endGame = true;
}
while (window.pollEvent(event)) {
// handel_close_event();
//Code gotten from SFML wiki https://github.com/SFML/SFML/wiki/Source%3A-Letterbox-effect-using-a-view
resize_window();
if(event.type == sf::Event::KeyPressed && game_menu_toggle){
close_game_menu();
}
else if (event.type == sf::Event::KeyPressed && pause == false) {
handle_userInput();
handle_collision();
}
else if (event.type == sf::Event::KeyPressed && pause == true){
waitForUnpause();
}
handle_levelChange();
}
update_KeyRelease();
scoreText.setString("Score: " + to_string(score));
number_of_lives_text.setString("Lives " + to_string(lives));
window.clear();
window.setView(view);
window.draw(*level.getTilemap());
// Knight sprite
characterAnimation.update(0.1);
window.draw(*character.getSprite());
// enemy sprite
if(enemy1.isAlive()){
enemy1Animation.update(0.1);
window.draw(*enemy1.getSprite());
}
if(enemy2.isAlive()){
enemy2Animation.update(0.2);
window.draw(*enemy2.getSprite());
}
if(enemy3.isAlive()){
enemy3Animation.update(0.22);
window.draw(*enemy3.getSprite());
}
if (enemyFireBall1.getIsAlive()){
enemyFireBall1Animation.update(0.3);
window.draw(*enemyFireBall1.getSprite());
}
if (enemyFireBall2.getIsAlive()){
enemyFireBall2Animation.update(0.27);
window.draw(*enemyFireBall2.getSprite());
}
if (enemyFireBall3.getIsAlive()){
enemyFireBall3Animation.update(0.29);
window.draw(*enemyFireBall3.getSprite());
}
if (enemyFireBall4.getIsAlive()){
enemyFireBall4Animation.update(0.28);
window.draw(*enemyFireBall4.getSprite());
}
// this is to display the fireball
if (playerFireball.getIsAlive()) {
fireballAnimation.update(0.3);
window.draw(*playerFireball.getSprite());
}
// drawing the score
window.draw(scoreText);
window.draw(number_of_lives_text);
if(pause){
window.draw(game_pause);
window.draw(pause_screen_text);
}
if(game_menu_toggle){
window.draw(game_menu);
window.draw(game_menu_text);
window.draw(hero_main_screen_sprite);
window.draw(enemy1_main_screen_sprite);
window.draw(fireball_main_screen_sprite);
window.draw(fireball_skull_main_screen_sprite);
}
if (endGame == true){
cout << "You died. GAME OVER.";
game_over_window();
window.draw(game_win_lose_menu);
window.draw(game_win_lose_text);
window.display();
sf::sleep(sf::milliseconds(5000));
window.close();
}
if (win){
cout << "You win!\n";
you_win_window();
window.draw(game_win_lose_menu);
window.draw(game_win_lose_text);
window.display();
sf::sleep(sf::milliseconds(5000));
window.close();
}
window.display();
}
return 0;
}
/********************************************************
* handle_userInput - handles user input, allows game control
*
* Parameters - none
*
* Return - void
********************************************************/
void handle_userInput(){
bool playerMoved = false;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::P)){
pause_game();
pause = true;
return;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && !playerFireball.getIsAlive()){
playerFireball.shoot(1, character.getCurrentTile() - 16, sf::Vector2f(character.getPosition().x + playerFireball.getSprite()->getGlobalBounds().width/2, character.getPosition().y - 121 - playerFireball.getSprite()->getGlobalBounds().height));
playerMoved = true;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && !playerFireball.getIsAlive()){
playerFireball.shoot(2, character.getCurrentTile() + 16, sf::Vector2f(character.getPosition().x + playerFireball.getSprite()->getGlobalBounds().width/2, character.getPosition().y + 121 + playerFireball.getSprite()->getGlobalBounds().height*2));
playerMoved = true;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && !playerFireball.getIsAlive()){
playerFireball.shoot(3, character.getCurrentTile() - 1, sf::Vector2f(character.getPosition().x - 121 - playerFireball.getSprite()->getGlobalBounds().width, character.getPosition().y + playerFireball.getSprite()->getGlobalBounds().height/2));
playerMoved = true;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && !playerFireball.getIsAlive()){
playerFireball.shoot(4, character.getCurrentTile() + 1, sf::Vector2f(character.getPosition().x + 121 + playerFireball.getSprite()->getGlobalBounds().width*2, character.getPosition().y + playerFireball.getSprite()->getGlobalBounds().height/2));
playerMoved = true;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && wKeyReleased && (level.isTileXWalkable(character.getCurrentTile() - 16))) {
character.move(0.0f, -121.0f);
character.setCurrentTile(character.getCurrentTile() - 16);
playerMoved = true;
wKeyReleased = false;
playerFireball.move();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) && aKeyReleased && (level.isTileXWalkable(character.getCurrentTile() - 1))) {
character.move(-121, 0.0f);
character.setCurrentTile(character.getCurrentTile() - 1);
playerMoved = true;
aKeyReleased = false;
playerFireball.move();
}
if ((sf::Keyboard::isKeyPressed(sf::Keyboard::S)) && sKeyReleased && (level.isTileXWalkable(character.getCurrentTile() + 16))) {
character.move(0.0f, 121.0f);
character.setCurrentTile(character.getCurrentTile() + 16);
playerMoved = true;
sKeyReleased = false;
playerFireball.move();
}
if ((sf::Keyboard::isKeyPressed(sf::Keyboard::D)) && dKeyReleased && (level.isTileXWalkable(character.getCurrentTile() + 1))) {
character.move(121, 0.0f);
character.setCurrentTile(character.getCurrentTile() + 1);
playerMoved = true;
dKeyReleased = false;
playerFireball.move();
}
if (playerMoved){
enemy1.randomEnemyMove(rand(), &level);
enemy2.randomEnemyMove(rand(), &level);
enemy3.randomEnemyMove(rand(), &level);
if (enemyFireBall1.getIsAlive()){
enemyFireBall1.move();
}
if (enemyFireBall2.getIsAlive()){
enemyFireBall2.move();
}
if (enemyFireBall3.getIsAlive()){
enemyFireBall3.move();
}
if (enemyFireBall4.getIsAlive()){
enemyFireBall4.move();
}
if (enemy1.isAlive() && enemy1.getBoss()){
shootEnemyFireballs(rand());
}
}
}
/********************************************************
* waitForUnpause - if game is paused prevent all user input except unpause and escape
*
* Parameters - none
*
* Return - void
********************************************************/
void waitForUnpause(){
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::P)){
pause = false;
}
}
/********************************************************
* close_game_menu - closes beginning game menu when space pressed
*
* Parameters - none
*
* Return - void
********************************************************/
void close_game_menu(){
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){
game_menu_toggle = false;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
window.close();
}
}
/********************************************************
* pause_game - handles the game pause effects/UI
*
* Parameters - none
*
* Return - void
********************************************************/
void pause_game(){
game_pause.setSize(sf::Vector2f(VIEW_WITDH, VIEW_HEIGHT));
game_pause.setFillColor(sf::Color{105,108, 125, 50});
game_pause.setOutlineColor(sf::Color::Transparent);
game_pause.setOutlineThickness(5);
pause_screen_text.setFont(scoreFont);
pause_screen_text.setString(" GAME IS PAUSED!\nPRESS P TO UNPAUSE");
pause_screen_text.setCharacterSize(40);
pause_screen_text.setFillColor(sf::Color::White);
pause_screen_text.setPosition(700,500);
}
/********************************************************
* game_menu_window - handles the beginning UI for the game start screen
*
* Parameters - none
*
* Return - void
********************************************************/
void game_menu_window(){
game_menu.setSize(sf::Vector2f(VIEW_WITDH, VIEW_HEIGHT));
game_menu.setFillColor(sf::Color::Black);
game_menu.setOutlineThickness(5);
game_menu_text.setFont(scoreFont);
game_menu_text.setCharacterSize(35);
game_menu_text.setPosition(170, 30);
hero_menu_texture.loadFromFile("../Assets/Knight_Game_Menu.png");
hero_main_screen_sprite.setTexture(hero_menu_texture);
hero_main_screen_sprite.setScale(7.f, 7.f);
hero_main_screen_sprite.setPosition(150, 900);
enemy1_menu_texture.loadFromFile("../Assets/Vampire_Game_Menu.png");
enemy1_main_screen_sprite.setTexture(enemy1_menu_texture);
enemy1_main_screen_sprite.setScale(7.f, 7.f);
enemy1_main_screen_sprite.setPosition(430, 900);
fireball_menu_texture.loadFromFile("../Assets/fire.png");
fireball_main_screen_sprite.setTexture(fireball_menu_texture);
fireball_main_screen_sprite.setScale(7.f, 7.f);
fireball_main_screen_sprite.setPosition(1050, 900);
fireball_skull_menu_texture.loadFromFile("../Assets/FlyingSkull_Game_Menu.png");
fireball_skull_main_screen_sprite.setTexture(fireball_skull_menu_texture);
fireball_skull_main_screen_sprite.setScale(7.f, 7.f);
fireball_skull_main_screen_sprite.setPosition(730, 910);
game_menu_text.setString("Welcome to Dungeon Quest!\n\n"
"Here is how you play the game:\n\nTo move press W to go up, S to go down, A to go left, and D to go right\n\n"
"Press ESC to close the window at any time and press p to pause the game\n\n"
"The hero can shoot a fireball by pressing the arrow keys\n\n"
"You can only have 1 active fireball at a time\n\n"
"And kill the enemy/s to move onto the next level\n\n"
"You will start off with 3 lives, so use them wisely\n\n"
"Be careful as danger lurks behind these walls muhahahahah!!\n\n"
"Press the space bar to begin playing!\n\n"
"Hero\t\tEnemy\t\tEnemy 2\t\tFireball");
}
/********************************************************
* game_over_window - handles the game over window
*
* Parameters - none
*
* Return - void
********************************************************/
void game_over_window(){
game_win_lose_menu.setSize(sf::Vector2f(VIEW_WITDH, VIEW_HEIGHT));
game_win_lose_menu.setFillColor(sf::Color::Black);
game_win_lose_menu.setOutlineThickness(5);
game_win_lose_text.setFont(scoreFont);
game_win_lose_text.setCharacterSize(100);
game_win_lose_text.setFillColor(sf::Color::White);
game_win_lose_text.setPosition(650, 400);
game_win_lose_text.setString("GAME OVER");
}
/********************************************************
* you_win_window - handles the you win window
*
* Parameters - none
*
* Return - void
********************************************************/
void you_win_window(){
game_win_lose_menu.setSize(sf::Vector2f(VIEW_WITDH, VIEW_HEIGHT));
game_win_lose_menu.setFillColor(sf::Color::Black);
game_win_lose_menu.setOutlineThickness(5);
game_win_lose_text.setFont(scoreFont);
game_win_lose_text.setCharacterSize(70);
game_win_lose_text.setFillColor(sf::Color::White);
game_win_lose_text.setPosition(150, 400);
game_win_lose_text.setString("YOU WIN, YOU HAVE RID THE WORLD OF EVIL!");
}