-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2dgame.cpp
More file actions
1150 lines (924 loc) · 44.5 KB
/
2dgame.cpp
File metadata and controls
1150 lines (924 loc) · 44.5 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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "raylib.h"
#include "raymath.h"
#include "CollisionSystem.h"
#include "Samurai.h"
#include "Demon.h"
#include <iostream>
#include <vector>
#include <stdlib.h> // For exit()
#include <unistd.h> // For getcwd()
#include <limits.h> // For PATH_MAX
#include <cstdio>
#include "StartScreen.h"
#include <functional>
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
// Define RAYTMX_IMPLEMENTATION to include the implementation of the library
#define RAYTMX_IMPLEMENTATION
#include "raytmx.h"
// Define the global variable for collision box visibility
bool showCollisionBoxes = false;
// Global audio variables
Music backgroundMusic = { 0 };
Music menuMusic = { 0 };
float masterVolume = 0.7f;
// Global background texture
// Dialogue System for Room3
bool showDialogue = false;
std::string dialogueText;
float dialogueTimer = 0.0f;
float dialogueDuration = 4.0f; // seconds
std::vector<std::string> randomDialogues = {
"Sorry, the demon you seek is in another portal!",
"You've reached the wrong realm, try again!",
"Wrong portal, warrior. Your demon lies elsewhere.",
"Nope, no demons here. Just regrets.",
"You must seek the next portal, brave samurai."
};
void triggerRoom3Dialogue() {
// Set dialogue state
showDialogue = true;
// Select a random dialogue message
dialogueText = randomDialogues[GetRandomValue(0, randomDialogues.size() - 1)];
// Reset timer to start counting up
dialogueTimer = 0.0f;
// Print debug information
printf("Dialogue triggered: %s\n", dialogueText.c_str());
}
Texture2D backgroundTexture = { 0 };
Camera2D camera = { 0 };
TmxMap* map = NULL;
enum GameState {START_SCREEN, MAIN_GAME, EXIT};
bool isPaused = false;
bool isComplete = false;
bool gameover = false;
// Fade transition on map switch
bool isTransitioning = false;
float transitionAlpha = 0.0f;
bool transitionFadeIn = false;
std::function<void()> transitionAction;
void startTransition(std::function<void()> action) {
isTransitioning = true;
transitionAlpha = 0.0f;
transitionFadeIn = false;
transitionAction = action;
}
// Helper function to check collision between two collision boxes
bool checkCharacterCollision(const CollisionBox& box1, const CollisionBox& box2) {
if (box1.active && box2.active) {
return CheckCollisionRecs(box1.rect, box2.rect);
}
return false;
}
// Helper function to handle attack collision and damage
void handleAttackCollision(CollisionBox* attackBox, CollisionBox* hurtBox, int& health, int damage) {
if (attackBox && hurtBox && attackBox->active && hurtBox->active &&
checkCharacterCollision(*attackBox, *hurtBox)) {
health -= damage;
if (health < 0) health = 0;
// Deactivate attack box to prevent multiple hits from the same attack
attackBox->active = false;
}
}
// Custom exit function that bypasses normal cleanup
void safeExit() {
// Unload audio resources
if (backgroundMusic.ctxData != NULL) {
StopMusicStream(backgroundMusic);
UnloadMusicStream(backgroundMusic);
}
// Unload the background texture
if (backgroundTexture.id != 0) {
UnloadTexture(backgroundTexture);
}
// Clean up Raylib
CloseAudioDevice();
CloseWindow();
// Exit directly without going through normal cleanup
_Exit(0); // Use _Exit instead of exit to bypass any atexit handlers
}
// Function to draw collision boxes for debugging
void drawCollisionBox(const CollisionBox& box) {
if (!box.active) return;
Color color;
switch (box.type) {
case BODY:
color = BLUE;
break;
case ATTACK:
color = RED;
break;
case HURTBOX:
color = GREEN;
break;
default:
color = YELLOW;
break;
}
// Draw the collision box as a rectangle outline
DrawRectangleLines(box.rect.x, box.rect.y, box.rect.width, box.rect.height, color);
// Draw a small label to identify the box type
const char* label = "";
switch (box.type) {
case BODY: label = "BODY"; break;
case ATTACK: label = "ATTACK"; break;
case HURTBOX: label = "HURT"; break;
default: label = "UNKNOWN"; break;
}
DrawText(label, box.rect.x, box.rect.y - 15, 10, color);
}
// Function to check if a file exists
bool fileExists(const char* fileName) {
FILE* file = fopen(fileName, "r");
if (file) {
fclose(file);
return true;
}
return false;
}
void loadLevel() {
map = LoadTMX("maps/LevelDesign.tmx");
if (!map) {
printf("Failed to Load TMX File.\n");
exit (1);
} else {
printf("Loaded TMX File.");
}
// Loop through tilesets (assuming map->tilesets is a pointer to an array of TmxTileset)
for (int i = 0; i < map->tilesetsLength; i++) {
TmxTileset* tileset = &map->tilesets[i]; // Accessing tileset by pointer
// Check if the image source is valid (not empty)
if (tileset->image.source[0] != '\0') { // Using image.source to check validity
Texture2D tilesetTexture = LoadTexture("maps/16 x16 Purple Dungeon Sprite Sheet.png");
if (tilesetTexture.id == 0) {
std::cout << "Error loading tileset image" << std::endl;
}
}
}
}
void renderLevel() {
if (map) {
DrawTMX(map, &camera, 0, 0, WHITE);
}
}
void checkTileCollisions(TmxMap* map, Samurai& player) {
for (unsigned int i = 0; i < map->layersLength; i++) {
TraceLog(LOG_DEBUG, "current layer is %d: %s", i, map->layers[i].name);
if (strcmp(map->layers[i].name, "Object Layer 1") == 0 && map->layers[i].type == LAYER_TYPE_OBJECT_GROUP)
{
TmxObject col;
if (CheckCollisionTMXObjectGroupRec(map->layers[i].exact.objectGroup, player.getRect(), &col))
{
TraceLog(LOG_DEBUG, "We've made contact!");
Vector2 newVel = player.getVelocity();
newVel.y = 0;
player.setVelocity(newVel);
Rectangle newRect = player.getRect();
newRect.y = (col.aabb.y - newRect.height);
player.setRect(newRect);
if (player.isJumping()) {
player.land();
}
}
}
}
return;
}
int main()
{
// Print current working directory
char cwd[PATH_MAX];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
printf("Current working directory: %s\n", cwd);
} else {
perror("getcwd() error");
}
// Set up error handling
SetTraceLogLevel(LOG_WARNING);
// Initialize window
const int screenWidth = 1920;
const int screenHeight = 1080;
InitWindow(screenWidth, screenHeight, "2D Game");
// Define floor level to match where the non-zero tiles (floor tiles) are in Room1.tmx
// This value is used for all characters to ensure consistent vertical positioning
const float floorLevel = 10000.0f; // Exact floor level matching the non-zero floor tiles in TMX
const float floorHeight = 50.0f; // Height of the floor rectangle if needed
// Loading the Background.
Texture2D background = LoadTexture("maps/Dungeon_brick_wall_purple.png.png");
// Background Scale Factors.
float scalebgx = (float)screenWidth / (float)background.width;
float scalebgy = (float)screenHeight / (float)background.height;
float scalebg = (scalebgx < scalebgy) ? scalebgx : scalebgy;
scalebg /= 4.5f;
// Positions for Background Positions.
float bgposX = ((screenWidth - background.width * scalebg) / 2) - 600;
float bgposY = ((screenHeight - background.height * scalebg) / 2) - 210;
// Horizontal and Vertical Sliders for Background.
int scaledW = background.width * scalebg;
int scaledH = background.height * scalebg;
int tilesX = (screenWidth / scaledW) + 50;
int tilesY = (screenHeight / scaledH) + 15;
// Initialize audio device before loading music
InitAudioDevice();
// Load Music.
backgroundMusic = LoadMusicStream("music/03. Hunter's Dream.mp3");
menuMusic = LoadMusicStream("music/Soul Of Cinder.mp3");
SetTargetFPS(60);
// Initialize camera
camera.target = (Vector2){ 100, 0 };
camera.offset = (Vector2){ screenWidth/2.0f, screenHeight/2.0f };
camera.rotation = 0.0f;
camera.zoom = 3.3f; // Zoom in for better visibility.
// Initialize characters using stack allocation - all characters now use the same floorLevel
Samurai samurai(510, 2223, floorLevel);
// Don't delete this. This is for teleporting to the second main level.
//samuraiRect.x >= 18760 && samuraiRect.x <= 18840 && samuraiRect.y >= 3660
//Samurai samurai(18760, 3660, floorLevel);
// Initialize dash sound volume to match master volume
samurai.setDashSoundVolume(0.8f * masterVolume);
StartScreen startScreen;
GameState gameState = START_SCREEN;
loadLevel();
// Start playing menu music initially
PlayMusicStream(menuMusic);
SetMusicVolume(menuMusic, 0.5f * masterVolume);
bool isPlayingMenuMusic = true;
// Check which map are we in
bool mapSwitchedToRoom2 = false;
bool mapSwitchedToRoom3 = false;
bool mapSwitchedToRoom4 = false;
bool mapSwitchedToMainLevel2 = false;
//New map for main level 2
bool mapSwitchedToRoom5 = false;
bool mapSwitchedToRoom6 = false;
bool mapSwitchedToRoom7 = false;
bool mapSwitchedToRoom8 = false;
// Create a demon for Room2
Demon* demon = nullptr;
// Game loop
while (!WindowShouldClose()) {
// Update currently playing music
UpdateMusicStream(isPlayingMenuMusic ? menuMusic : backgroundMusic);
// Handle game state updates based on current game state
switch(gameState) {
case EXIT: {
// Handle exit (save state, cleanup, etc.)
safeExit();
break;
}
case START_SCREEN: {
if (!isPlayingMenuMusic) {
PlayMusicStream(menuMusic);
isPlayingMenuMusic = true;
}
startScreen.Update();
if (startScreen.ShouldStartGame()) {
gameState = MAIN_GAME; // Transition to main game
}
if (startScreen.ShouldExitGame()) {
gameState = EXIT; // Exit the game
}
BeginDrawing();
startScreen.Draw(); // Draw the start screen
EndDrawing();
break;
}
case MAIN_GAME: {
if (isPlayingMenuMusic) {
PlayMusicStream(backgroundMusic);
isPlayingMenuMusic = false;
}
if (IsKeyPressed(KEY_P)) {
isPaused = !isPaused;
}
// Toggle music with M key
if (IsKeyPressed(KEY_M)) {
if (IsMusicStreamPlaying(backgroundMusic)) {
PauseMusicStream(backgroundMusic);
} else {
ResumeMusicStream(backgroundMusic);
}
}
// Toggle collision box visibility with F1 key
if (IsKeyPressed(KEY_F1)) {
showCollisionBoxes = !showCollisionBoxes;
printf("Collision boxes visibility: %s\n", showCollisionBoxes ? "ON" : "OFF");
}
// Get frame time for updates
float deltaTime = GetFrameTime();
if (!isPaused && !isComplete) {
// Update samurai character
samurai.updateSamurai();
}
// You are in the first main level
if(!mapSwitchedToMainLevel2)
{
samurai.deathBarrier();
}
// You are now in the second main level. Wow.
if (mapSwitchedToMainLevel2)
{
samurai.secondDeathBarrier();
}
// Get samurai position for collision detection
Vector2 samuraiPos = {0, 0};
CollisionBox* samuraiBody = samurai.getCollisionBox(BODY);
if (samuraiBody && samuraiBody->active) {
samuraiPos.x = samuraiBody->rect.x + samuraiBody->rect.width / 2;
samuraiPos.y = samuraiBody->rect.y + samuraiBody->rect.height / 2;
}
// Check for collisions between Samurai's attack and enemies
CollisionBox* samuraiAttack = samurai.getCollisionBox(ATTACK);
// Check for enemy attacks hitting Samurai
CollisionBox* samuraiHurtbox = samurai.getCollisionBox(HURTBOX);
checkTileCollisions(map, samurai);
// Update camera to follow player, ensuring it stays within map boundaries
Rectangle samuraiRect = samurai.getRect();
float samuraiCenterX = samuraiRect.x + samuraiRect.width / 2;
float samuraiCenterY = samuraiRect.y + samuraiRect.height / 2;
if (!samurai.checkDeath()) {
camera.target = (Vector2){ samuraiPos.x, samuraiPos.y };
// Add some camera boundary checks to avoid the camera going out of bounds:
float halfScreenWidth = screenWidth / (2.0f * camera.zoom);
float halfScreenHeight = screenHeight / (2.0f * camera.zoom);
// Clamp X and Y positions with easing towards boundaries
camera.target.x = Lerp(camera.target.x, Clamp(camera.target.x, halfScreenWidth, map->width * map->tileWidth - halfScreenWidth), 0.1f);
camera.target.y = Lerp(camera.target.y, Clamp(camera.target.y, halfScreenHeight, map->height * map->tileHeight - halfScreenHeight), 0.1f);
// Ensure camera doesn't go out of bounds
if (camera.target.x < halfScreenWidth) camera.target.x = halfScreenWidth + 100;
if (camera.target.y < halfScreenHeight) camera.target.y = halfScreenHeight;
// Camera zoom controls
if (camera.target.x < halfScreenWidth) camera.target.x = halfScreenWidth;
if (camera.target.x > map->width * 16 - halfScreenWidth) camera.target.x = map->width * 16 - halfScreenWidth;
if (camera.target.y < halfScreenHeight) camera.target.y = halfScreenHeight;
if (camera.target.y > map->height * 16 - halfScreenHeight) camera.target.y = map->height * 16 - halfScreenHeight;
} else {
// Stop moving the camera when the player is dead
camera.target = camera.target; // Keeps the camera locked in place
}
// Switching map :o
// Main Level to Room2
if (!mapSwitchedToMainLevel2 && !mapSwitchedToRoom2 && samuraiRect.x >= 920 && samuraiRect.x <= 930 && samuraiRect.y == 1502)
{
// Debug output to confirm portal detection
printf("Portal to Room2 detected! Player position: %.2f, %.2f\n", samuraiRect.x, samuraiRect.y);
startTransition([&]()
{
mapSwitchedToRoom2 = true;
if (map)
{
UnloadTMX(map);
}
map = LoadTMX("maps/Room2.tmx");
if (!map)
{
std::cerr << "Failed to load Room2.tmx!" << std::endl;
}
Rectangle newPos = samurai.getRect();
newPos.x = 540;
newPos.y = 2222;
samurai.setRect(newPos);
camera.target = { newPos.x, newPos.y };
// Create demon in Room2
if (demon == nullptr) {
Vector2 demonPos = { 1000.0f, 2165.0f }; // Position the demon in Room2 at same ground level as samurai
demon = new Demon(demonPos, 50.0f, 500);
std::cout << "Demon spawned in Room2" << std::endl;
}
});
}
// Room2 to Main Level
if (!mapSwitchedToMainLevel2 && mapSwitchedToRoom2 && samuraiRect.x >= 530 && samuraiRect.x <= 540 && samuraiRect.y >= 2170 && samuraiRect.y <= 2180)
{
// Debug output to confirm return portal detection
printf("Return portal detected! Player position: %.2f, %.2f\n", samuraiRect.x, samuraiRect.y);
startTransition([&]()
{
mapSwitchedToRoom2 = false;
if (map)
{
UnloadTMX(map);
}
map = LoadTMX("maps/LevelDesign.tmx");
if (!map)
{
std::cerr << "Failed to load LevelDesign.tmx!" << std::endl;
}
Rectangle newPos = samurai.getRect();
newPos.x = 920;
newPos.y = 1519.5;
samurai.setRect(newPos);
camera.target = { newPos.x, newPos.y };
});
}
// Main Level to Room 3
if (!mapSwitchedToMainLevel2 && !mapSwitchedToRoom3 && samuraiRect.x >= 5415 && samuraiRect.x <= 5435 && samuraiRect.y >= 877 && samuraiRect.y <= 878)
{
printf("Portal to Room3 detected! Player position: %.2f, %.2f\n", samuraiRect.x, samuraiRect.y);
startTransition([&]()
{
mapSwitchedToRoom3 = true;
if (map)
{
UnloadTMX(map);
}
map = LoadTMX("maps/Room3.tmx"); // Load Room 3
if (!map)
{
printf("Failed to load Room3.tmx\n");
safeExit(); // Exit if loading fails
}
else
{
printf("Loaded Room3.tmx successfully.\n");
}
Rectangle newPos = samurai.getRect();
newPos.x = 1560;
newPos.y = 2190.25;
samurai.setRect(newPos);
});
}
// Room 3 to Main Level
if (!mapSwitchedToMainLevel2 && mapSwitchedToRoom3 && samuraiRect.x >= 1540 && samuraiRect.x <= 1570 && samuraiRect.y >= 2173 && samuraiRect.y <= 2175)
{
printf("Portal back to LevelDesign detected! Player position: %.2f, %.2f\n", samuraiRect.x, samuraiRect.y);
startTransition([&]()
{
mapSwitchedToRoom3 = false;
if (map)
{
UnloadTMX(map);
}
map = LoadTMX("maps/LevelDesign.tmx"); // Load the main level
if (!map)
{
printf("Failed to Load TMX File: LevelDesign.tmx\n");
safeExit();
}
else
{
printf("Loaded TMX File: LevelDesign.tmx\n");
}
Rectangle newPos = samurai.getRect();
newPos.x = 5895;
newPos.y = 892;
samurai.setRect(newPos);
camera.target = { newPos.x, newPos.y };
});
}
// Main Level to Room 4
if (!mapSwitchedToMainLevel2 && !mapSwitchedToRoom4 && samuraiRect.x >= 8300 && samuraiRect.x <= 8320 && samuraiRect.y >= 2173 && samuraiRect.y <= 2176)
{
startTransition([&]()
{
mapSwitchedToRoom4 = true;
if (map)
{
UnloadTMX(map);
}
map = LoadTMX("maps/Room4.tmx");
if (!map)
{
std::cerr << "Failed to load Room4.tmx!" << std::endl;
}
Rectangle newPos = samurai.getRect();
newPos.x = 665;
newPos.y = 2222;
samurai.setRect(newPos);
camera.target = { newPos.x, newPos.y };
});
}
// Room 4 to Main Level
if (!mapSwitchedToMainLevel2 && mapSwitchedToRoom4 && samuraiRect.x >= 3050 && samuraiRect.x <= 3070 && samuraiRect.y >= 2170.00)
{
startTransition([&]()
{
mapSwitchedToRoom4 = false;
if (map)
{
UnloadTMX(map);
}
map = LoadTMX("maps/LevelDesign.tmx");
if (!map)
{
std::cerr << "Failed to load LevelDesign.tmx!" << std::endl;
}
Rectangle newPos = samurai.getRect();
newPos.x = 9385;
newPos.y = 2062.25;
samurai.setRect(newPos);
camera.target = { newPos.x, newPos.y };
});
}
// Main Level to Main Level 2
if (!mapSwitchedToMainLevel2 && samuraiRect.x >= 18760 && samuraiRect.x <= 18840 && samuraiRect.y >= 3660)
{
startTransition([&]()
{
mapSwitchedToMainLevel2 = true;
if (map)
{
UnloadTMX(map);
}
map = LoadTMX("maps/LevelDesign2.tmx");
if (!map)
{
std::cerr << "Failed to load LevelDesign2.tmx!" << std::endl;
}
Rectangle newPos = samurai.getRect();
newPos.x = 200;
newPos.y = 1500;
samurai.setRect(newPos);
camera.target = { newPos.x, newPos.y };
});
}
if (mapSwitchedToMainLevel2 && !mapSwitchedToRoom5 &&
samuraiRect.x > 4400 && samuraiRect.x < 4430 &&
samuraiRect.y > 2760 && samuraiRect.y < 2780)
{
startTransition([&]() {
mapSwitchedToRoom5 = true;
// Unload current map
if (map) {
UnloadTMX(map);
}
// Load new TMX map (e.g., Room5)
map = LoadTMX("maps/Lv2RoomOne.tmx");
if (!map) {
std::cerr << "Failed to load Room5.tmx!" << std::endl;
}
// Set Samurai position to destination portal
Rectangle newPos = samurai.getRect();
newPos.x = 0; // Adjust to coordinates of portal in Room5
newPos.y = 224;
samurai.setRect(newPos);
// Recenter camera
camera.target = { newPos.x, newPos.y };
});
}
// New portal (e.g., Room5 -> Level2)
if (mapSwitchedToMainLevel2 && mapSwitchedToRoom5 &&
samuraiRect.x > 1000 && samuraiRect.x < 1100 &&
samuraiRect.y > 1200 && samuraiRect.y < 1300)
{
startTransition([&]() {
mapSwitchedToRoom5 = false;
if (map) {
UnloadTMX(map);
}
map = LoadTMX("maps/LevelDesign2.tmx");
Rectangle newPos = samurai.getRect();
newPos.x = 3820; // back to original portal
newPos.y = 1218.77;
samurai.setRect(newPos);
camera.target = { newPos.x, newPos.y };
});
}
// New portal (e.g., Level2 -> Room6)
if (mapSwitchedToMainLevel2 && !mapSwitchedToRoom6 &&
samuraiRect.x > 5600 && samuraiRect.x < 5700 &&
samuraiRect.y > 3300 && samuraiRect.y < 3400)
{
startTransition([&]() {
mapSwitchedToRoom6 = true;
// Unload current map
if (map) {
UnloadTMX(map);
}
// Load new TMX map
map = LoadTMX("maps/Lv2RoomTwo.tmx");
if (!map) {
std::cerr << "Failed to load Lv2RoomTwo.tmx!" << std::endl;
}
// Set Samurai position to destination portal
Rectangle newPos = samurai.getRect();
newPos.x = 0; // Adjust to coordinates of portal in Room5
newPos.y = 224;
samurai.setRect(newPos);
// Recenter camera
camera.target = { newPos.x, newPos.y };
});
}
// New portal (e.g., Room6 -> Level2)
if (mapSwitchedToMainLevel2 && mapSwitchedToRoom6 &&
samuraiRect.x > 1600 && samuraiRect.x < 1610 &&
samuraiRect.y > 3300 && samuraiRect.y < 3500)
{
startTransition([&]() {
mapSwitchedToRoom6 = false;
// Unload current map
if (map) {
UnloadTMX(map);
}
// Load new TMX map
map = LoadTMX("maps/LevelDesign2.tmx");
if (!map) {
std::cerr << "Failed to load Room5.tmx!" << std::endl;
}
// Set Samurai position to destination portal
Rectangle newPos = samurai.getRect();
newPos.x = 8390; // Adjust to coordinates of portal in Room6
newPos.y = 1313.78;
samurai.setRect(newPos);
// Recenter camera
camera.target = { newPos.x, newPos.y };
});
}
// New portal (e.g., Level2 -> Room7)
if (mapSwitchedToMainLevel2 && !mapSwitchedToRoom7 &&
samuraiRect.x > 7500 && samuraiRect.x < 7580 &&
samuraiRect.y > 2900 && samuraiRect.y < 3000)
{
startTransition([&]() {
mapSwitchedToRoom5 = true;
// Unload current map
if (map) {
UnloadTMX(map);
}
// Load new TMX map (e.g., Room5)
map = LoadTMX("maps/Lv2Room3.tmx");
if (!map) {
std::cerr << "Failed to load Room5.tmx!" << std::endl;
}
// Set Samurai position to destination portal
Rectangle newPos = samurai.getRect();
newPos.x = 0; // Adjust to coordinates of portal in Room5
newPos.y = 224;
samurai.setRect(newPos);
// Recenter camera
camera.target = { newPos.x, newPos.y };
});
}
// New portal (e.g., Level2 -> Room8)
if (mapSwitchedToMainLevel2 && !mapSwitchedToRoom7 &&
samuraiRect.x > 9100 && samuraiRect.x < 9200 &&
samuraiRect.y > 2000 && samuraiRect.y < 2100)
{
startTransition([&]() {
mapSwitchedToRoom5 = true;
// Unload current map
if (map) {
UnloadTMX(map);
}
// Load new TMX map (e.g., Room5)
map = LoadTMX("maps/Lv2Room4.tmx");
if (!map) {
std::cerr << "Failed to load Room5.tmx!" << std::endl;
}
// Set Samurai position to destination portal
Rectangle newPos = samurai.getRect();
newPos.x = 0; // Adjust to coordinates of portal in Room5
newPos.y = 224;
samurai.setRect(newPos);
// Recenter camera
camera.target = { newPos.x, newPos.y };
});
}
if(samurai.checkDeath()) {
gameover = true;
}
if(mapSwitchedToMainLevel2 && samurai.getRect().x >= 12610 && samurai.getRect().x <= 12655 && samuraiRect.y >= 2304) {
isComplete = true;
}
// Begin drawing
BeginDrawing();
ClearBackground(BLACK);
// 2D camera mode for proper drawing
BeginMode2D(camera);
// Draw Background.
for (int x = 0; x < tilesX; x++) {
for (int y = 0; y < tilesY; y++) {
float posX = x * scaledW;
float posY = y * scaledH;
DrawTextureEx(background, Vector2{posX + bgposX, posY + bgposY}, 0.0f, scalebg, GRAY);
}
}
renderLevel();
// Draw Samurai.
samurai.draw();
// Update and draw demon if in Room2
if (mapSwitchedToRoom2 && demon != nullptr) {
// Update demon animation
demon->updateAnimation();
// Update demon AI behavior
if (!demon->isDead && !isPaused) {
// Get distance to player
Rectangle demonRect = demon->rect;
Vector2 demonPos = { demonRect.x + demonRect.width/2, demonRect.y + demonRect.height/2 };
Vector2 samuraiPos = { samuraiRect.x + samuraiRect.width/2, samuraiRect.y + samuraiRect.height/2 };
float distance = Vector2Distance(demonPos, samuraiPos);
// Chase player if within range
if (distance < demon->chaseRange && distance > demon->attackRange) {
demon->state = WALK_DEMON;
demon->direction = (samuraiPos.x < demonPos.x) ? LEFT_DEMON : RIGHT_DEMON;
// Move toward player
float moveDir = (demon->direction == LEFT_DEMON) ? -1.0f : 1.0f;
demon->velocity.x = moveDir * demon->moveSpeed * 100.0f;
}
// Attack if close enough
else if (distance <= demon->attackRange) {
if (!demon->isAttacking) {
demon->attack();
}
}
// Idle if too far
else {
demon->state = IDLE_DEMON;
demon->velocity.x = 0;
}
// Apply velocity
demon->applyVelocity();
}
// Draw the demon
demon->draw();
// Check for collision between Samurai's attack and demon
CollisionBox* samuraiAttack = samurai.getCollisionBox(ATTACK);
if (samuraiAttack && samuraiAttack->active) {
for (auto& box : demon->collisionBoxes) {
if (box.type == HURTBOX && box.active) {
if (checkCharacterCollision(*samuraiAttack, box)) {
demon->takeDamage(25); // Samurai deals 25 damage
//samuraiAttack->active = false; // Prevent multiple hits
break;
}
}
}
}
// Check for collision between demon's attack and Samurai
CollisionBox* samuraiHurtbox = samurai.getCollisionBox(HURTBOX);
for (auto& box : demon->collisionBoxes) {
if (box.type == ATTACK && box.active) {
if (samuraiHurtbox && samuraiHurtbox->active) {
if (checkCharacterCollision(box, *samuraiHurtbox)) {
// Check if samurai is blocking to reduce damage
if (samurai.isBlocking()) {
// Apply damage reduction when blocking (half damage)
int reducedDamage = static_cast<int>(15 * samurai.getBlockDamageReduction());
samurai.takeDamage(reducedDamage);
std::cout << "Blocked attack! Reduced damage: " << reducedDamage << std::endl;
} else {
samurai.takeDamage(15); // Full damage when not blocking
}
box.active = false; // Prevent multiple hits
break;
}
}
}
}
}
std::cout << "X: " << samurai.getRect().x << std::endl;
std::cout << "Y: " << samurai.getRect().y << std::endl;
// End camera mode and finalize drawing
EndMode2D();
// Draw dialogue textbox after 2D mode
if (showDialogue) {
// Update dialogue timer
dialogueTimer += GetFrameTime();
// Create a visually appealing dialogue box with fixed screen coordinates (not affected by camera)
int boxWidth = 800;
int boxHeight = 120; // Slightly taller for better visibility
int boxX = GetScreenWidth() / 2 - boxWidth / 2;
int boxY = 100; // Position at top of screen for better visibility
// Draw the dialogue box with a semi-transparent background and thicker border
DrawRectangle(boxX, boxY, boxWidth, boxHeight, Fade(BLACK, 0.9f));
DrawRectangleLines(boxX, boxY, boxWidth, boxHeight, WHITE);
DrawRectangleLines(boxX+1, boxY+1, boxWidth-2, boxHeight-2, WHITE); // Double border for emphasis
// Draw a title for the dialogue box
DrawText("RESIDENT GNOME", boxX + boxWidth/2 - MeasureText("MYSTERIOUS VOICE", 20)/2, boxY + 15, 20, GOLD);
// Draw the dialogue text centered in the box
DrawText(dialogueText.c_str(), boxX + 20, boxY + 50, 24, WHITE);
// Print debug info when F2 is pressed
if (IsKeyPressed(KEY_F2)) {
printf("Dialogue active: %s (Timer: %.2f/%.2f)\n",
dialogueText.c_str(), dialogueTimer, dialogueDuration);
}
// Hide dialogue after duration expires
if (dialogueTimer >= dialogueDuration) {
showDialogue = false;
dialogueTimer = 0.0f;
printf("Dialogue ended.\n");
}
}