-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
2294 lines (1896 loc) · 119 KB
/
main.c
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 "warper.h"
#include "battleSystem.h"
#include "mapMaker.h"
#include "cutsceneMaker.h"
#include "warperInterface.h"
#include "warperCutscene.h"
//test/debug functions
int gameDevMenu();
void createTestMap(warperTilemap* tilemap);
void debugPrintStatProgression();
void playTestAnimation();
void playTestWizardAnimation();
//game control/state functions
int gameLoop(warperTilemap tilemap, cScene* gameScene, warperTeam* playerTeam, warperTeam* enemyTeam);
int pauseMenu(cScene* gameScene, warperTeam* playerTeam);
bool optionsMenu(bool inGame);
int battleLoop(warperTilemap tilemap, cScene* scene, warperTeam* playerTeam, warperTeam* enemyTeam);
cDoubleVector getTilemapCollision(cSprite playerSprite, warperTilemap tilemap);
void checkWarperUnitHover(SDL_MouseMotionEvent motion, warperTeam* playerTeam, warperTeam* enemyTeam, cCamera* camera);
#define TEST_TILEMAP_X 80 //(global.windowW / TILE_SIZE)
#define TEST_TILEMAP_Y 60 //(global.windowH / TILE_SIZE)
#define CONFIRM_NONE 0
#define CONFIRM_MOVEMENT 1
#define CONFIRM_TELEPORT 2
#define CONFIRM_ATTACK 3
#define CONFIRM_ATTACK_RESULT 4
#define CONFIRM_ENEMY_ATTACK 5
#define BATTLE_OPTION_MOVE 0
#define BATTLE_OPTION_TELEPORT 1
#define BATTLE_OPTION_ATTACK 2
#define BATTLE_OPTION_MODS 3
#define BATTLE_OPTION_PASS 4
#define WMENU_PAUSE 0
#define WMENU_PARTY 1
#define WMENU_PARTYMEMBER 4
#define WMENU_INVENTORY 2
#define WMENU_ITEM 5
#define WMENU_OPTIONS 3
bool global_debug; //global debug flag
int main(int argc, char** argv)
{
if (argc > 1 && (strcmp(argv[1], "--debug") || strcmp(argv[1], "-d")))
global_debug = true;
else
global_debug = false;
int error = initCoSprite("./assets/cb.bmp", "Warper", SCREEN_PX_WIDTH, SCREEN_PX_HEIGHT, "./assets/Px437_ITT_BIOS_X.ttf", TILE_SIZE, (SDL_Color) {255, 28, 198, 0xFF}, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
initCLogger(&warperLogger, "./logs/log.txt", NULL, global_debug);
/*
if (global_debug)
cLogEvent(warperLogger, "DEBUG", "Warper - Startup", "Started Warper in Debug mode");
//*/
loadWarperOptions();
warperTilemap tilemap;
initCSprite(&cursorSprite, NULL, "./assets/uiTilesheet.png", 0, (cDoubleRect) {0, 0, 16, 16}, (cDoubleRect) {0, 2 * TILE_SIZE, 16, 16}, NULL, 1.0, SDL_FLIP_NONE, 0.0, true, true, NULL, 1);
SDL_ShowCursor(false);
bool quitAll = false;
while(!quitAll)
{
//main menu
int selection = gameDevMenu();
if (selection == 5)
{
//create new cutscene
if (createNewCutscene()) //if it returns 1, aka if we're force quitting
selection = 8; //treat it as a quit
}
if (selection == 4)
{ //create new map
if (createNewMap(&tilemap, TILE_SIZE)) //if it returns 1, aka if we're force quitting
selection = 8; //treat it as a quit
}
if (selection == 3)
{ //load created test map
importWarperTilemap(&tilemap, "./maps/testMap.txt", 0);
}
if (selection == 2)
{ //create temp map
createTestMap(&tilemap);
}
if (selection == 8 || selection == 5) //if we don't want to start playing (completed a cutscene or quitting entirely)
{
if (selection == 8)
quitAll = true; //we want to immediately close the window and quit, as the user requests
}
else
{
cCamera gameCamera;
initCCamera(&gameCamera, (cDoubleRect) {0, 0, global.windowW, global.windowH}, 1.0, 0.0, 10); //init camera
c2DModel mapModel_layer1, mapModel_layer2, mapModel_gridLayer;
loadTilemapModels(tilemap, &mapModel_layer1, &mapModel_layer2);
loadGridModel(tilemap, &mapModel_gridLayer, options.gridOpacity);
cScene gameScene;
initCScene(&gameScene, (SDL_Color) {0xFF, 0xFF, 0xFF, 0xFF}, &gameCamera, (cSprite*[1]) {&cursorSprite}, 1, (c2DModel*[3]) {&mapModel_layer1, &mapModel_layer2, &mapModel_gridLayer}, 3, NULL, 0, NULL, 0);
warperTeam playerTeam, enemyTeam;
//TEST squads init
cSprite* enemySquadSprites = calloc(5, sizeof(cSprite));
warperItem testWeapon = (warperItem) {itemMelee, 0, "Test Weapon", 1, (warperWeaponStats) {1, 1, 0}};
warperUnit enemySquad[5];
for(int i = 0; i < 5; i++)
{
initCSprite(&(enemySquadSprites[i]), NULL, "./assets/characterTilesheet.png", 6 + i,
(cDoubleRect) {(tilemap.width - 3 - 2 * i) * tilemap.tileSize, (tilemap.height - 6 + 2 * (i % 2)) * tilemap.tileSize, 44, 96},
(cDoubleRect) {0, 3 * tilemap.tileSize / 2, 44, 96},
NULL, 1.0, SDL_FLIP_NONE, 0, false, false, NULL, 4);
enemySquad[i] = (warperUnit) {&enemySquadSprites[i], "Enemy", 1, 0, 0, 0, 0, classNone, &testWeapon, (warperStats) {1, 1, 1, 1, 1, 1, 0}, (warperBattleData) {0, statusNone, 0, 0, 0, false}};
calculateStats(&enemySquad[i], true); //fill in both regular and battle stats
}
cSprite* playerSquadSprites = calloc(5, sizeof(cSprite));
cDoublePt testSquadPts[5] = {(cDoublePt) {3 * tilemap.tileSize, 3 * tilemap.tileSize}, (cDoublePt) {6 * tilemap.tileSize, 6 * tilemap.tileSize}, (cDoublePt) {6 * tilemap.tileSize, 3 * tilemap.tileSize}, (cDoublePt) {3 * tilemap.tileSize, 6 * tilemap.tileSize}, (cDoublePt) {9 * tilemap.tileSize, 4.5 * tilemap.tileSize}};
warperUnit playerSquad[5];
char* testSquadNames[5] = {"You", "Alessia", "Marc", "Samael", "Marie"};
enum warperClass testSquadClasses[5] = {classNone, classAttacker, classAttacker, classShooter, classTechnomancer};
cDoublePt squadSprLocations[5] = {(cDoublePt) {0, 0}, (cDoublePt) {0, tilemap.tileSize / 2}, (cDoublePt) {tilemap.tileSize / 2, 0}, (cDoublePt) {tilemap.tileSize, 0}, (cDoublePt) {tilemap.tileSize / 2, tilemap.tileSize / 2}};
const int testUnitsLevel = 10;
for(int i = 0; i < 5; i++)
{
initCSprite(&(playerSquadSprites[i]), NULL, "./assets/characterTilesheet.png", 1 + i,
(cDoubleRect) {testSquadPts[i].x, testSquadPts[i].y, 2 * tilemap.tileSize, 2 * tilemap.tileSize},
(cDoubleRect) {squadSprLocations[i].x, squadSprLocations[i].y, tilemap.tileSize / 2, tilemap.tileSize / 2},
NULL, 1.0, SDL_FLIP_NONE, 0, false, false, NULL, 4);
playerSquad[i] = (warperUnit) {&(playerSquadSprites[i]), testSquadNames[i], testUnitsLevel, 0, 0, 0, 0, testSquadClasses[i], &testWeapon, (warperStats) {testUnitsLevel, testUnitsLevel, testUnitsLevel, testUnitsLevel, testUnitsLevel, testUnitsLevel, 0}, (warperBattleData) {0, statusNone, 0, 0, 0, false}};
calculateStats(&playerSquad[i], true); //fill in regular stats and battle stats
}
initWarperTeam(&playerTeam, (warperUnit*[5]) {&(playerSquad[0]), &(playerSquad[1]), &(playerSquad[2]), &(playerSquad[3]), &(playerSquad[4])}, 5, (warperItem[1]) {testWeapon}, 1, 0);
initWarperTeam(&enemyTeam, (warperUnit*[5]) {&(enemySquad[0]), &(enemySquad[1]), &(enemySquad[2]), &(enemySquad[3]), &(enemySquad[4])}, 5, NULL, 0, 0);
//end TEST squads init
addSpriteToCScene(&gameScene, playerTeam.units[0]->sprite);
addSpriteToCScene(&gameScene, enemyTeam.units[0]->sprite);
bool quit = false;
int controlCode = 0;
while (!quit)
{
if (controlCode != 3) //if we did not try to pause from the battle loop
controlCode = gameLoop(tilemap, &gameScene, &playerTeam, &enemyTeam);
if (controlCode == 2 || controlCode == 3) //if we are going into a battle, or returning to the battle from the pause menu
{ //WE WANT TO BATTLE
if (controlCode == 2)
{ //add in the player's squad and the enemy's squad
for(int i = 1; i < playerTeam.unitsSize; i++)
addSpriteToCScene(&gameScene, playerTeam.units[i]->sprite);
for(int i = 1; i < enemyTeam.unitsSize; i++)
addSpriteToCScene(&gameScene, enemyTeam.units[i]->sprite);
}
controlCode = battleLoop(tilemap, &gameScene, &playerTeam, &enemyTeam);
if (controlCode != 3) //if we are ending the battle for good, quitting or just returning to the overworld
{ //clean up the sprites for the player's squad and the enemy squad
for(int i = 1; i < playerTeam.unitsSize; i++)
removeSpriteFromCScene(&gameScene, playerTeam.units[i]->sprite, -1, false);
for(int i = 1; i < enemyTeam.unitsSize; i++)
removeSpriteFromCScene(&gameScene, enemyTeam.units[i]->sprite, -1, false);
//show main character sprite again
playerTeam.units[0]->sprite->renderLayer = 4;
}
}
if (controlCode == 1 || controlCode == 3)
{ //execute pause menu if we are trying to access it from the game or battle loops
int pauseCode = pauseMenu(&gameScene, &playerTeam);
if (pauseCode < 0)
controlCode = pauseCode; //if pause menu returns that we want to quit then quit to main menu or entirely
}
if (controlCode < 0)
{
quit = true;
if (controlCode == -2)
quitAll = true;
}
}
destroyCScene(&gameScene);
destroyWarperTeam(&playerTeam, false);
destroyWarperTeam(&enemyTeam, false);
free(enemySquadSprites);
free(playerSquadSprites);
destroyWarperTilemap(&tilemap);
}
}
saveWarperOptions();
destroyCSprite(&cursorSprite); //cursorSprite is set to be global, so it won't get destroyed automatically by destroyCScene
closeCoSprite();
return error;
}
int gameDevMenu()
{
cScene menuScene;
char* optionsArray[] = {"Alpha Build Menu", " ", "Load Test Map", "Load Created Map", "Create New Map", "Create New Cutscene", "Options", "Print Progression Info", "Quit"};
warperTextBox menuBox;
createMenuTextBox(&menuBox, (cDoubleRect) {TILE_SIZE, TILE_SIZE, global.windowW - 2 * TILE_SIZE, global.windowH - 2 * TILE_SIZE}, (cDoublePt) {412, 8}, 4, true, 0xFF, optionsArray, (bool[9]) {false, false, true, true, true, true, true, true, true}, 9, &(global.mainFont));
cResource menuBoxResource;
initCResource(&menuBoxResource, (void*) &menuBox, drawWarperTextBox, destroyWarperTextBox, 5);
cCamera gameCamera;
initCCamera(&gameCamera, (cDoubleRect) {0, 0, global.windowW, global.windowH}, 1.0, 0.0, 5);
initCScene(&menuScene, (SDL_Color) {0xFF, 0xFF, 0xFF, 0xFF}, &gameCamera, (cSprite*[1]) {&cursorSprite}, 1, NULL, 0, (cResource*[1]) {&menuBoxResource}, 1, NULL, 0);
cInputState input;
//int fps = 0;
while(menuBox.selection == -1)
{
input = cGetInputState(true);
if (input.quitInput)
menuBox.selection = 8; //quit
if (input.motion.x >= 0 && input.motion.x <= SCREEN_PX_WIDTH && input.motion.y >= 0 && input.motion.y <= SCREEN_PX_HEIGHT)
updateCursorIcon(CURSOR_NORMAL);
checkWarperTextBoxHover(&menuBox, input.motion);
if (input.isClick)
{
//if we clicked
if (menuBoxResource.renderLayer != 0)
checkWarperTextBoxSelection(&menuBox, input.click.x, input.click.y);
}
if (menuBox.selection == 6)
{
bool optionsQuit = optionsMenu(false);
menuBox.selection = (optionsQuit) ? 8 : -1; //if we are trying to force quit, set equal to the quit value, else stay in the menu
}
if (menuBox.selection == 7)
{
debugPrintStatProgression();
menuBox.selection = -1;
playTestWizardAnimation();
}
updateCursorPos(input.motion, false);
drawCScene(&menuScene, true, true, NULL, NULL, options.framerate);
}
int selection = menuBox.selection;
destroyCScene(&menuScene);
updateCursorIcon(0);
return selection;
}
void debugPrintStatProgression()
{
//* testing stat progression
for(int classType = 0; classType < 4; classType++)
{
printf("Class id %d\n", classType);
for(int i = 1; i <= 100; i++)
{
warperUnit testUnit = (warperUnit) {NULL, "Test Unit", i, 0, 0, 0, 0, classType, NULL, (warperStats) {i, i, i, i, i, i, 0}, (warperBattleData) {0, statusNone, 0, 0, 0, false}};
calculateStats(&testUnit, true);
printf("Character with stats level %d: HP %d, Stamina %d, Energy %d\n", i, testUnit.maxHp, testUnit.maxStamina, testUnit.maxEnergy);
warperUnit attackingUnit = (warperUnit) {NULL, "Test Attacker", i, 0, 0, 0, 0, classTechnomancer, NULL, (warperStats) {i, i, i, i, i, i, 0}, (warperBattleData) {0, statusNone, 0, 0, 0, false}};
warperAttackCheck checkResult = checkAttack(&attackingUnit, &testUnit, 0);
printf(" >Attacker character dmg %d. ttk: %d\n", checkResult.damage, (int) round((double) testUnit.maxHp / checkResult.damage + 0.45));
/*
int maxDmgRequired = 0, minDmgRequired = 0;
if (i < 25) //approx. early game
{
maxDmgRequired = testUnit.maxHp - 1; //2+ hits
minDmgRequired = testUnit.maxHp / 2;
}
if (i >= 25 && i < 66) //approx. mid game
{
maxDmgRequired = testUnit.maxHp / 2 - 1; //3+ hits
minDmgRequired = testUnit.maxHp / 3;
}
if (i >= 66) //approx late or post-game
{
maxDmgRequired = testUnit.maxHp / 3 - 1; //4+ hits
minDmgRequired = testUnit.maxHp / 4;
}
//printf(" >max damage to maintain average time to kill at this lv: %d\n >min damage to maintain average time to kill at this lv: %d\n", maxDmgRequired, minDmgRequired);
//*/
}
printf("-----------------\n");
}
//*/
}
void playTestAnimation()
{
cSprite spr;
warperAnimatedSprite aSpr;
initCSprite(&spr, NULL, "./assets/characterTilesheet.png", 0, (cDoubleRect) {0, 0, 2 * TILE_SIZE, 2 * TILE_SIZE}, (cDoubleRect) {0, 0, 16, 16}, NULL, 1.0, SDL_FLIP_NONE, 0.0, false, false, &aSpr, 5);
cDoubleRect animationRects[5] = {(cDoubleRect) {16, 0, 0, 0}, (cDoubleRect) {16, 0, 0, 0}, (cDoubleRect) {-32, 16, 0, 0}, (cDoubleRect) {16, 0, 0, 0}, (cDoubleRect) {-16, -16, 0, 0}};
cDoubleRect finalAnimations[60];
double rotations[10] = {15, 15, 15, 15, 15, 15, -30, -30, -30, 0};
double finalRotations[60];
int layerSettings[60];
for(int i = 0; i < 60; i++)
{
finalAnimations[i] = (cDoubleRect) {0, 0, 0, 0};
finalRotations[i] = 0;
layerSettings[i] = 5;
if (i % 12 == 0)
finalAnimations[i] = animationRects[i / 12];
if (i % 6 == 0)
finalRotations[i] = rotations[i / 6];
}
initWarperAnimatedSprite(&aSpr, &spr, (cDoubleRect*) finalAnimations, (double*) finalRotations, NULL, NULL, NULL, layerSettings, 60, -1);
warperAnimatedSprite anime;
cSprite animeSprite;
{
char* exportedSpr = exportWarperAnimatedSprite(aSpr, -1);
char* copiedOver = calloc(strlen(exportedSpr) + 1, sizeof(char));
strcpy(copiedOver, exportedSpr);
free(exportedSpr);
printf("%s\n", copiedOver);
anime.sprite = &animeSprite;
int index = -1;
importWarperAnimatedSprite(&anime, copiedOver, &index);
free(copiedOver);
printf("-- %d\n", index);
//aSpr = anime;
}
warperActor actorOneAnimations[5];
warperAnimation animations[5];
cDoubleRect animationPos[5] = {(cDoubleRect) {0, 0, 64, 64}, (cDoubleRect) {64, 0, 64, 64}, (cDoubleRect) {64, 64, 64, 64}, (cDoubleRect) {128, 64, 64, 64}, (cDoubleRect) {128, 128, 64, 64}};
warperCutscene cutscene;
for(int i = 0; i < 5; i++)
{
initWarperActor(&actorOneAnimations[i], animationPos[i], &aSpr, true);
initWarperAnimation(&animations[i], (warperActor[1]) {actorOneAnimations[i]}, 1, 12);
}
warperCutsceneBox emptyBox;
initWarperCutsceneBox(&emptyBox, NULL, NULL, 0);
warperCutsceneBox otherBoxes[2];
warperTextBox box;
createBattleTextBox(&box, (cDoubleRect) {0, 0, SCREEN_PX_WIDTH, SCREEN_PX_HEIGHT}, (cDoublePt) {0, 0}, 0, true, (char*[2]) {"Test", "Box"}, (bool[2]) {false, false}, 2, TILE_SIZE);
initWarperCutsceneBox(&otherBoxes[0], (warperTextBox*[1]) {&box}, (int[1]) {6}, 1);
initWarperCutsceneBox(&otherBoxes[1], (warperTextBox*[1]) {&box}, (int[1]) {6}, 1);
warperCutsceneBox boxes[5] = {emptyBox, otherBoxes[0], emptyBox, emptyBox, otherBoxes[1]};
initWarperCutscene(&cutscene, animations, boxes, 5, ".", -1);
exportWarperCutscene(cutscene, "./assets/testCutscene.txt");
cCamera camera;
initCCamera(&camera, (cDoubleRect) {0, 0, global.windowW, global.windowH}, 1.0, 0.0, 5);
cScene scene;
initCScene(&scene, (SDL_Color) {0xFF, 0xFF, 0xFF, 0xFF}, &camera, (cSprite*[2]) {&cursorSprite, aSpr.sprite}, 2, NULL, 0, (cResource*[2]) {otherBoxes[0].boxResources[0], otherBoxes[1].boxResources[0]}, 2, NULL, 0);
bool quit = false;
cInputState input;
while(!quit)
{
input = cGetInputState(true);
if (input.quitInput || input.keyStates[SDL_SCANCODE_ESCAPE] || input.keyStates[SDL_SCANCODE_RETURN])
quit = true;
if (input.isClick)
{
if (cutscene.waitingForBox)
incrementWarperCutsceneBox(&cutscene);
}
if (input.isMotion)
{
cursorSprite.drawRect.x = input.motion.x;
cursorSprite.drawRect.y = input.motion.y;
}
drawCScene(&scene, true, true, NULL, NULL, WARPER_FRAME_LIMIT);
if (cutscene.currentAnimation < cutscene.numAnimations)
{
for(int i = 0; i < cutscene.animations[cutscene.currentAnimation].numActors; i++)
{ //iterate through each actor in the current animation step
if (!(cutscene.waitingForBox && cutscene.animations[cutscene.currentAnimation].actors[i].pauseAnimationWhenWaiting)) //if we aren't supposed to pause animations for this sprite when the textbox is open and the textbox is indeed open
iterateWarperAnimatedSprite(cutscene.animations[cutscene.currentAnimation].actors[i].animatedSpr); //iterate the animation of the sprite
}
}
iterateWarperCutscene(&cutscene);
}
destroyWarperCutscene(&cutscene, true, true, false);
destroyWarperAnimatedSprite(&aSpr, false);
destroyCScene(&scene);
}
void playTestWizardAnimation()
{
warperCutscene cutscene;
/*
cSprite spr;
warperAnimatedSprite aSpr;
warperCutsceneBox* otherBoxes;
const int SPR_W = 400;
const int SPR_H = 400;
initCSprite(&spr, NULL, "./assets/mockups/testing/wizard_animated.png", 0, (cDoubleRect) {0, 0, SPR_W, SPR_H}, (cDoubleRect) {0, 0, 100, 100}, NULL, 1.0, SDL_FLIP_NONE, 0.0, false, false, &aSpr, 5);
cDoubleRect animationRects[5] = {(cDoubleRect) {100, 0, 0, 0}, (cDoubleRect) {100, 0, 0, 0}, (cDoubleRect) {100, 0, 0, 0}, (cDoubleRect) {100, 0, 0, 0}, (cDoubleRect) {100, 0, 0, 0}};
cDoubleRect finalAnimations[90];
double finalRotations[90];
int layerSettings[90];
for(int i = 0; i < 90; i++)
{
finalAnimations[i] = (cDoubleRect) {0, 0, 0, 0};
finalRotations[i] = 0;
layerSettings[i] = 5;
if (i % 18 == 0)
finalAnimations[i] = animationRects[i / 18];
}
initWarperAnimatedSprite(&aSpr, &spr, (cDoubleRect*) finalAnimations, (double*) finalRotations, NULL, NULL, NULL, layerSettings, 90, -1);
warperActor actorOneAnimations[5];
warperAnimation animations[5];
cDoubleRect animationPos[5] = {(cDoubleRect) {0, 0, SPR_W, SPR_H}, (cDoubleRect) {64, 0, SPR_W, SPR_H}, (cDoubleRect) {64, 64, SPR_W, SPR_H}, (cDoubleRect) {128, 64, SPR_W, SPR_H}, (cDoubleRect) {128, 128, SPR_W, SPR_H}};
for(int i = 0; i < 5; i++)
{
initWarperActor(&actorOneAnimations[i], animationPos[i], &aSpr, true);
initWarperAnimation(&animations[i], (warperActor[1]) {actorOneAnimations[i]}, 1, 18);
}
warperCutsceneBox emptyBox;
initWarperCutsceneBox(&emptyBox, NULL, NULL, 0);
otherBoxes = calloc(2, sizeof(warperCutsceneBox));
warperTextBox box1, box2, box3;
createBattleTextBox(&box1, (cDoubleRect) {0, 0, SCREEN_PX_WIDTH, SCREEN_PX_HEIGHT}, (cDoublePt) {0, 0}, 0, true, (char*[2]) {"Wizard:", "Aw hell nah I'mma quit this animation"}, (bool[2]) {false, false}, 2, TILE_SIZE);
initWarperCutsceneBox(&otherBoxes[0], (warperTextBox*[1]) {&box1}, (int[1]) {6}, 1);
createBattleTextBox(&box2, (cDoubleRect) {0, 0, SCREEN_PX_WIDTH, SCREEN_PX_HEIGHT}, (cDoublePt) {0, 0}, 0, true, (char*[2]) {"Wizard:", "Later idiot"}, (bool[2]) {false, false}, 2, TILE_SIZE);
createBattleTextBox(&box3, (cDoubleRect) {SCREEN_PX_WIDTH / 4, SCREEN_PX_HEIGHT / 4, SCREEN_PX_WIDTH / 2, SCREEN_PX_HEIGHT / 2}, (cDoublePt) {0, 0}, 0, true, (char*[2]) {"Wizard:", "*He leaves*"}, (bool[2]) {false, false}, 2, TILE_SIZE);
initWarperCutsceneBox(&otherBoxes[1], (warperTextBox*[2]) {&box2, &box3}, (int[2]) {6, 8}, 2);
warperCutsceneBox boxes[5] = {emptyBox, otherBoxes[0], emptyBox, emptyBox, otherBoxes[1]};
initWarperCutscene(&cutscene, animations, boxes, 5, ".", -1);
exportWarperCutscene(cutscene, "./assets/testWizCutscene.txt");
//*/
cSprite** cutsceneSprites;
cResource** cutsceneResources;
int numSprites, numResources;
importWarperCutscene(&cutscene, "./assets/testWizCutscene.txt", &cutsceneSprites, &numSprites, &cutsceneResources, &numResources);
cutsceneSprites[numSprites++] = &cursorSprite; //an extra slot in the cSprite* array is allocated specifically for the cursor (but numSprites will need to be updated)
cCamera camera;
initCCamera(&camera, (cDoubleRect) {0, 0, global.windowW, global.windowH}, 1.0, 0.0, 5);
cScene scene;
initCScene(&scene, (SDL_Color) {0xFF, 0xFF, 0xFF, 0xFF}, &camera, cutsceneSprites, numSprites, NULL, 0, cutsceneResources, numResources, NULL, 0);
bool quit = false;
cInputState input;
while(!quit)
{
input = cGetInputState(true);
if (input.quitInput || input.keyStates[SDL_SCANCODE_ESCAPE] || input.keyStates[SDL_SCANCODE_RETURN])
quit = true;
if (input.isClick)
{
if (cutscene.waitingForBox)
incrementWarperCutsceneBox(&cutscene);
}
if (input.isMotion)
{
cursorSprite.drawRect.x = input.motion.x;
cursorSprite.drawRect.y = input.motion.y;
}
drawCScene(&scene, true, true, NULL, NULL, WARPER_FRAME_LIMIT);
if (cutscene.currentAnimation < cutscene.numAnimations)
{
for(int i = 0; i < cutscene.animations[cutscene.currentAnimation].numActors; i++)
{ //iterate through each actor in the current animation step
if (!(cutscene.waitingForBox && cutscene.animations[cutscene.currentAnimation].actors[i].pauseAnimationWhenWaiting)) //if we aren't supposed to pause animations for this sprite when the textbox is open and the textbox is indeed open
iterateWarperAnimatedSprite(cutscene.animations[cutscene.currentAnimation].actors[i].animatedSpr); //iterate the animation of the sprite
}
}
iterateWarperCutscene(&cutscene);
}
destroyWarperCutscene(&cutscene, true, true, false);
//destroyWarperAnimatedSprite(&aSpr, false);
destroyCScene(&scene);
}
void createTestMap(warperTilemap* tilemap)
{
tilemap->width = TEST_TILEMAP_X;
tilemap->height = TEST_TILEMAP_Y;
tilemap->tileSize = TILE_SIZE;
tilemap->spritemap_layer1 = calloc(tilemap->width, sizeof(int*));
tilemap->spritemap_layer2 = calloc(tilemap->width, sizeof(int*));
tilemap->collisionmap = calloc(tilemap->width, sizeof(int*));
for(int x = 0; x < tilemap->width; x++)
{
tilemap->spritemap_layer1[x] = calloc(tilemap->height, sizeof(int));
tilemap->spritemap_layer2[x] = calloc(tilemap->height, sizeof(int));
tilemap->collisionmap[x] = calloc(tilemap->height, sizeof(int));
for(int y = 0; y < tilemap->height; y++)
{
tilemap->spritemap_layer1[x][y] = 4; //normal sprite
tilemap->spritemap_layer2[x][y] = 798; //invisible sprite?
if (x == 1) //left row sprite
tilemap->spritemap_layer1[x][y] = 1;
if (y == 1) //top row sprite
tilemap->spritemap_layer1[x][y] = 3;
if (x == tilemap->width - 2) //right row sprite
tilemap->spritemap_layer1[x][y] = 7;
if (y == tilemap->height - 2) //bottom row sprite
tilemap->spritemap_layer1[x][y] = 5;
if (x == 0 || y == 0 || x + 1 == tilemap->width || y + 1 == tilemap->height || (y == 20 && x > tilemap->width / 2) || (y == 40 && x < tilemap->height / 2))
{
if (x == 0 || y == 0 || x + 1 == tilemap->width || y + 1 == tilemap->height)
tilemap->spritemap_layer1[x][y] = 36; //skyscraper sprite
else
tilemap->spritemap_layer1[x][y] = 13; //collision sprite
tilemap->collisionmap[x][y] = 1;
}
else
tilemap->collisionmap[x][y] = 0;
if (x == 1 && y == 1) //top-left corner sprite
tilemap->spritemap_layer1[x][y] = 0;
if (x == tilemap->width - 2 && y == tilemap->height - 2) //bottom-right corner sprite
tilemap->spritemap_layer1[x][y] = 8;
if (x == 1 && y == tilemap->height - 2) //bottom-left corner sprite
tilemap->spritemap_layer1[x][y] = 2;
if (x == tilemap->width - 2 && y == 1) //top-right corner sprite
tilemap->spritemap_layer1[x][y] = 6;
}
}
}
int gameLoop(warperTilemap tilemap, cScene* gameScene, warperTeam* playerTeam, warperTeam* enemyTeam)
{
cSprite* overworldSprite = playerTeam->units[0]->sprite;
cSprite* overworldEnemySprite = enemyTeam->units[0]->sprite;
int controlCode = 0;
bool quit = false;
cInputState input;
int framerate = 0;
while(!quit)
{
input = cGetInputState(true);
if (input.quitInput || input.keyStates[SDL_SCANCODE_ESCAPE])
{
quit = true;
if (input.quitInput)
controlCode = -2; //quit EVERYTHING
if (input.keyStates[SDL_SCANCODE_ESCAPE])
controlCode = 1;
}
//character movement
if (input.keyStates[SDL_SCANCODE_W] || input.keyStates[SDL_SCANCODE_A] || input.keyStates[SDL_SCANCODE_S] || input.keyStates[SDL_SCANCODE_D])
{
//double lastX = overworldSprite->drawRect.x, lastY = overworldSprite->drawRect.y;
double speed = 6.0; //just a good speed value, nothing special. Pixels/frame at 60 FPS
if ((input.keyStates[SDL_SCANCODE_W] || input.keyStates[SDL_SCANCODE_S]) && (input.keyStates[SDL_SCANCODE_A] || input.keyStates[SDL_SCANCODE_D]))
speed *= sin(degToRad(45)); //diagonal speed component
if (input.keyStates[SDL_SCANCODE_W])
overworldSprite->drawRect.y -= (int) (speed * 60.0 / framerate);
if (input.keyStates[SDL_SCANCODE_S])
overworldSprite->drawRect.y += (int) (speed * 60.0 / framerate);
cDoubleVector mtv = getTilemapCollision(*overworldSprite, tilemap);
if (mtv.magnitude)
{ //apply collision after doing y movements
//*
overworldSprite->drawRect.x += mtv.magnitude * cos(degToRad(mtv.degrees));
overworldSprite->drawRect.y += mtv.magnitude * sin(degToRad(mtv.degrees));
//printf("translating %f at %f\n", mtv.magnitude, mtv.degrees);
//*/
//overworldSprite->drawRect.y = lastY;
}
if (input.keyStates[SDL_SCANCODE_A])
{
overworldSprite->drawRect.x -= (int) (speed * 60.0 / framerate);
overworldSprite->flip = SDL_FLIP_HORIZONTAL;
}
if (input.keyStates[SDL_SCANCODE_D])
{
overworldSprite->drawRect.x += (int) (speed * 60.0 / framerate);
overworldSprite->flip = SDL_FLIP_NONE;
}
mtv = getTilemapCollision(*overworldSprite, tilemap);
if (mtv.magnitude)
{ //apply collision again after doing x movements (allows smooth collision sliding. The only way I could figure out how to fix it without 100% hard-coding)
//*
overworldSprite->drawRect.x += mtv.magnitude * cos(degToRad(mtv.degrees));
overworldSprite->drawRect.y += mtv.magnitude * sin(degToRad(mtv.degrees));
//printf("translating %f at %f\n", mtv.magnitude, mtv.degrees);
//*/
//overworldSprite->drawRect.x = lastX;
}
gameScene->camera->rect.x = (int) (overworldSprite->drawRect.x - gameScene->camera->rect.w / 2); //set the camera to center on the player
gameScene->camera->rect.y = (int) (overworldSprite->drawRect.y - gameScene->camera->rect.h / 2);
if (gameScene->camera->rect.y < 0) //if the camera is set out of bounds in the -y, fix it
gameScene->camera->rect.y = 0;
if (gameScene->camera->rect.y > (tilemap.height - gameScene->camera->rect.h / tilemap.tileSize) * tilemap.tileSize) //if the camera is set out of bounds in the +y, fix it
gameScene->camera->rect.y = (tilemap.height - gameScene->camera->rect.h / tilemap.tileSize) * tilemap.tileSize;
if (gameScene->camera->rect.x < 0) //if the camera is set out of bounds in the -x, fix it
gameScene->camera->rect.x = 0;
if (gameScene->camera->rect.x > (tilemap.width - gameScene->camera->rect.w / tilemap.tileSize) * tilemap.tileSize) //if the camera is set out of bounds in the +x, fix it
gameScene->camera->rect.x = (tilemap.width - gameScene->camera->rect.w / tilemap.tileSize) * tilemap.tileSize;
}
if (input.keyStates[SDL_SCANCODE_B] || (getDistance(overworldSprite->drawRect.x, overworldSprite->drawRect.y, overworldEnemySprite->drawRect.x, overworldEnemySprite->drawRect.y) < 9 * tilemap.tileSize && overworldEnemySprite->renderLayer != 0))
{
//have battle take place
quit = true;
controlCode = 2; //battle control code
}
/*
if (input.isClick)
{
//if we clicked
//if we clicked the text box
if (textBoxResource.renderLayer != 0 && (input.click.x > textBox.rect.x && input.click.x < textBox.rect.x + textBox.rect.w && input.click.y > textBox.rect.y && input.click.y < textBox.rect.y + textBox.rect.h))
{
for(int i = 0; i < textBox.textsSize; i++)
{
if (input.click.x > textBox.texts[i].rect.x && input.click.x < textBox.texts[i].rect.x + textBox.texts[i].rect.w &&
input.click.y > textBox.texts[i].rect.y && input.click.y < textBox.texts[i].rect.y + textBox.texts[i].rect.h)
{
//we clicked on an element
textBox.selection = i;
}
}
}
}
//*/
//camera movement
if (input.keyStates[SDL_SCANCODE_UP])
gameScene->camera->rect.y -= 10 * 60 / framerate;
if (input.keyStates[SDL_SCANCODE_DOWN])
gameScene->camera->rect.y += 10 * 60 / framerate;
if (input.keyStates[SDL_SCANCODE_LEFT])
gameScene->camera->rect.x -= 10 * 60 / framerate;
if (input.keyStates[SDL_SCANCODE_RIGHT])
gameScene->camera->rect.x += 10 * 60 / framerate;
updateCursorPos(input.motion, false);
drawCScene(gameScene, true, true, NULL, &framerate, 60);
}
return controlCode;
}
int pauseMenu(cScene* gameScene, warperTeam* playerTeam)
{
int menuLevel = 0, menuId = WMENU_PAUSE, subMenuSelection = -1, menuOptsLength = 7;
warperTextBox menuLayers[3];
warperTextBox* pauseBox;
char* optionsArray[] = {"PAUSE", " ", "Resume", "Party", "Items", "Options", "Quit"};
bool isOptions[] = {false, false, true, true, true, true, true};
createMenuTextBox(&menuLayers[0], (cDoubleRect) {0, 0, global.windowW, global.windowH}, (cDoublePt) {0, 8}, 6, true, 0xB0, optionsArray, isOptions, menuOptsLength, &global.mainFont);
pauseBox = &menuLayers[0]; //start keeping track of menu layers
cResource pauseBoxResource;
initCResource(&pauseBoxResource, (void*) pauseBox, drawWarperTextBox, destroyWarperTextBox, 2);
addResourceToCScene(gameScene, &pauseBoxResource);
cInputState input;
//int fps = 0;
while(pauseBox->selection == -1)
{
input = cGetInputState(true);
if (input.quitInput)
pauseBox->selection = -3; //quit
if (input.motion.x >= 0 && input.motion.x <= SCREEN_PX_WIDTH && input.motion.y >= 0 && input.motion.y <= SCREEN_PX_HEIGHT)
updateCursorIcon(CURSOR_NORMAL);
checkWarperTextBoxHover(pauseBox, input.motion);
if (input.isClick)
{
//if we clicked
if (pauseBoxResource.renderLayer != 0)
checkWarperTextBoxSelection(pauseBox, input.click.x, input.click.y);
}
updateCursorPos(input.motion, false);
drawCScene(gameScene, true, true, NULL, NULL, WARPER_FRAME_LIMIT);
if (menuId == WMENU_PAUSE)
{
if (pauseBox->selection > 2 && pauseBox->selection < 6)
{
menuLevel++;
if (pauseBox->selection == 3)
{
//party menu
menuId = WMENU_PARTY;
menuOptsLength = 4 + playerTeam->unitsSize;
char** partyArray = calloc(menuOptsLength, sizeof(char*));
partyArray[0] = "PARTY";
partyArray[1] = calloc(9 + digits(playerTeam->money), sizeof(char));
snprintf(partyArray[1], 8 + digits(playerTeam->money), "Money: %d", playerTeam->money);
partyArray[2] = " ";
for(int i = 0; i < playerTeam->unitsSize; i++)
{
partyArray[3 + i] = calloc(strlen(playerTeam->units[i]->name) + 1, sizeof(char));
strcpy(partyArray[3 + i], playerTeam->units[i]->name);
}
partyArray[menuOptsLength - 1] = "Back";
bool* partyIsOptions = calloc(menuOptsLength, sizeof(bool));
partyIsOptions[0] = false;
partyIsOptions[1] = false;
partyIsOptions[2] = false;
for(int i = 0; i < playerTeam->unitsSize; i++)
{
partyIsOptions[3 + i] = true;
}
partyIsOptions[menuOptsLength - 1] = true;
createMenuTextBox(&menuLayers[menuLevel], (cDoubleRect) {0, 0, global.windowW, global.windowH}, (cDoublePt) {0, 8}, 6, true, 0xB0, partyArray, partyIsOptions, menuOptsLength, &global.mainFont);
for(int i = 0; i < menuOptsLength; i++)
{
if (i != 0 && i != 2 && i != 3 + playerTeam->unitsSize)
free(partyArray[i]);
}
free(partyArray);
free(partyIsOptions);
pauseBox = &menuLayers[menuLevel]; //set the new menu to be what the loop accesses
pauseBoxResource.subclass = pauseBox; //set the new menu to be displayed
}
if (pauseBox->selection == 4)
{
//inventory menu
menuId = WMENU_INVENTORY;
menuOptsLength = (3 + playerTeam->inventorySize > 20) ? 20 : 3 + playerTeam->inventorySize;
//bool menuOverflow = 3 + playerTeam->inventorySize > 20;
char** inventoryArray = calloc(menuOptsLength, sizeof(char*));
inventoryArray[0] = "INVENTORY";
inventoryArray[1] = " ";
for(int i = 0; i < menuOptsLength - 3; i++)
{
inventoryArray[i + 2] = calloc(strlen(playerTeam->inventory[i].name), sizeof(char));
strcpy(inventoryArray[i + 2], playerTeam->inventory[i].name);
}
/* add pagination options
if (menuOverflow)
inventoryArray[menuOptsLength - 1] = "Next;"
//*/
inventoryArray[menuOptsLength - 1] = "Back";
bool* inventoryIsOptions = calloc(menuOptsLength, sizeof(bool));
inventoryIsOptions[0] = false;
inventoryIsOptions[1] = false;
for(int i = 0; i < menuOptsLength - 3; i++)
inventoryIsOptions[i + 2] = true;
inventoryIsOptions[menuOptsLength - 1] = true;
createMenuTextBox(&menuLayers[menuLevel], (cDoubleRect) {0, 0, global.windowW, global.windowH}, (cDoublePt) {512, 8}, 0, false, 0xB0, inventoryArray, inventoryIsOptions, menuOptsLength, &global.mainFont);
for(int i = 0; i < menuOptsLength - 3; i++)
{
free(inventoryArray[i + 2]);
}
free(inventoryArray);
free(inventoryIsOptions);
pauseBox = &menuLayers[menuLevel]; //set the new menu to be what the loop accesses
pauseBoxResource.subclass = pauseBox; //set the new menu to be displayed
}
if (pauseBox->selection == 5)
{
//options menu
if(optionsMenu(true)) //run the options menu and if it returns true (a force quit)
pauseBox->selection = -2; //quit the game
}
if (pauseBox->selection > -1) //if we aren't trying to force quit (or, as implied by the if statement encapsulating, leave the pause menu)
pauseBox->selection = -1; //do not leave the loop
}
if (pauseBox->selection == 6)
pauseBox->selection = -2; //quit the game
}
if (menuId == WMENU_PARTY && pauseBox->selection > 2)
{
if (pauseBox->selection == menuOptsLength - 1)
{
//back to pause menu
destroyWarperTextBox((void*) &menuLayers[menuLevel]);
menuLevel--;
menuId = WMENU_PAUSE;
pauseBox = &menuLayers[menuLevel]; //set the previous menu to be what the loop accesses
pauseBoxResource.subclass = pauseBox; //set the previous menu to be displayed
}
else
{
//go into a party member's menu
subMenuSelection = pauseBox->selection - 3;
menuId = WMENU_PARTYMEMBER;
menuLevel++;
char* memberArray[12] = {"Name", " ", "Class", "HP", "Attack", "Speed", "TP Range", "Tech Affinity", "Luck", "Stat Pts", "Gear", "Back"};
memberArray[0] = calloc(strlen(playerTeam->units[subMenuSelection]->name), sizeof(char));
strcpy(memberArray[0], playerTeam->units[subMenuSelection]->name);
bool memberIsOptions[12] = {false, false, false, false, false, false, false, false, false, false, false, true};
createMenuTextBox(&menuLayers[menuLevel], (cDoubleRect) {0, 0, global.windowW, global.windowH}, (cDoublePt) {0, 8}, 6, true, 0xB0, memberArray, memberIsOptions, 12, &global.mainFont);
free(memberArray[0]);
pauseBox = &menuLayers[menuLevel]; //set the new menu to be what the loop accesses
pauseBoxResource.subclass = pauseBox; //set the new menu to be displayed
}
pauseBox->selection = -1; //do not leave the loop
}
if (menuId == WMENU_PARTYMEMBER && pauseBox->selection == 11)
{
//back to party menu
destroyWarperTextBox((void*) &menuLayers[menuLevel]);
menuLevel--;
menuId = WMENU_PARTY;
pauseBox = &menuLayers[menuLevel]; //set the previous menu to be what the loop accesses
pauseBoxResource.subclass = pauseBox; //set the previous menu to be displayed
pauseBox->selection = -1;
}
if (menuId == WMENU_INVENTORY && pauseBox->selection > 2)
{
if (pauseBox->selection == menuOptsLength - 1)
{
//back to pause menu
destroyWarperTextBox((void*) &menuLayers[menuLevel]);
menuLevel--;
menuId = WMENU_PAUSE;
pauseBox = &menuLayers[menuLevel];
pauseBoxResource.subclass = pauseBox;
pauseBox->selection = -1;
}
else
{
//choose the item
}
}
}
int selection = pauseBox->selection;
if (selection < -1)
selection++; //-2 -> -1 (return to menu), -3 -> -2 (exit entirely)
else
selection = 0;
removeResourceFromCScene(gameScene, &pauseBoxResource, -1, true); //this will destroy pauseBox so we need to save its selection value
updateCursorIcon(0);
return selection;
}
bool optionsMenu(bool inGame)
{
bool quitGame = false;
const int menuOptsLength = 7;
warperTextBox optionsBox;
char* optionsArray[] = {"OPTIONS", " ", NULL, NULL, NULL, NULL, "Back"};