-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
2420 lines (2052 loc) · 63.7 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
/*
* * Flicky's Flocky
** 12 player Flappy Bird clone for the Sega Saturn
** by Slinga
** https://github.com/slinga-homebrew/Flickys-Flock/
** MIT License
*/
/*
* * Jo Sega Saturn Engine
** Copyright (c) 2012-2017, Johannes Fetz (johannesfetz@gmail.com)
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** * Neither the name of the Johannes Fetz nor the
** names of its contributors may be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "main.h"
//
// globals
//
// numLivesChoice indexes into this array for the starting
// number of lives per player
// 0 = infinite lives
const int g_InitialLives[] = {0, 1, 3, 5, 9};
GAME g_Game = {0};
ASSETS g_Assets = {0};
FLICKY_SPRITES g_FlickySprites[MAX_FLICKY_SPRITES] = {0};
FLICKY g_FlickyTitleSprites[MAX_FLICKY_SPRITES] = {0};
FLICKY g_Players[MAX_PLAYERS] = {0};
PIPE g_Pipes[MAX_PIPES] = {0};
POWERUP g_PowerUps[MAX_POWER_UPS] = {0};
unsigned int g_StartingPositions[MAX_PLAYERS] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
void jo_main(void)
{
g_Game.gameState = GAMESTATE_UNINITIALIZED;
// extend the heap to use LWRAM as well otherwise our sprites won't fit in
// main memory
jo_add_memory_zone((unsigned char *)LWRAM, LWRAM_HEAP_SIZE);
jo_core_init(JO_COLOR_Black);
// load assets from the cd
loadSpriteAssets();
loadPCMAssets();
g_Game.randomFlicky = jo_random(MAX_PLAYERS) - 1;
g_Game.floorPosition_x = FLOOR_POSITION_X;
g_Game.numLivesChoice = 2; // default to 3 lives
//
// callbacks for the various game states
//
// ABC + start handler
jo_core_set_restart_game_callback(abcStart_gamepad);
// SSMTF logo
jo_core_add_callback(ssmtfScreen_draw);
// title screen
jo_core_add_callback(titleScreen_draw);
jo_core_add_callback(titleScreen_input);
// gameplay
jo_core_add_callback(gameplay_draw);
jo_core_add_callback(gameplay_input);
jo_core_add_callback(gameplay_checkForCollisions);
// game over/pause
jo_core_add_callback(gameOver_input);
jo_core_add_callback(gameOver_draw);
// debug info
//jo_core_add_callback(debugInfo);
g_Game.gameState = GAMESTATE_SSMTF_LOGO;
jo_core_run();
}
//
// callbacks
//
// prints debug info on the screen
void debugInfo(void)
{
/*
jo_printf(0, 0, "Sprite usage: %d RAM usage: %d ", jo_sprite_usage_percent(), jo_memory_usage_percent());
jo_printf(3, 2, "0 (%d, %d) ", g_Players[0].x_pos, g_Players[0].y_pos);
jo_printf(3, 3, "P (%d, %d) ", g_Pipes[0].x_pos, g_Pipes[0].y_pos);
jo_printf(3, 4, "y_speed (%d) ", g_Players[0].y_speed);
jo_printf(3, 5, "num pipes (%d) ", getNumberofPipes());
jo_printf(3, 6, "difficulty (%d) ", getDifficulty());
//
*/
jo_fixed_point_time();
slPrintFX(delta_time, slLocate(3,4));
/*
jo_printf(3, 2, "x: %d y: %d z: %d t: %d ", g_PowerUps[0].x_pos, g_PowerUps[0].y_pos, g_PowerUps[0].z_pos, g_PowerUps[0].type);
jo_printf(3, 3, "sg: %d l: %d ss: %d ", g_Players[0].reverseGravityTimer, g_Players[0].lightningTimer, g_Players[0].stoneSneakersTimer);
// did the player hit start
if(jo_is_pad1_key_pressed(JO_KEY_Z))
{
if(g_Game.input.pressedStart == false)
{
g_Game.input.pressedStart = true;
applyPowerUp(&g_Players[0], &g_PowerUps[0]);
return;
}
}
*/
}
// exits to title screen if player one presses ABC+Start
void abcStart_gamepad(void)
{
transitionToTitleScreen();
return;
}
// draws the Sega Saturn Multiplayer Task Force logo
void ssmtfScreen_draw(void)
{
if(g_Game.gameState != GAMESTATE_SSMTF_LOGO)
{
return;
}
// wait a bit before starting the music track
if(g_Game.frame == 30)
{
jo_audio_play_cd_track(LIFE_UP_TRACK, LIFE_UP_TRACK, false);
}
g_Game.frame++;
if(g_Game.frame < SSMTF_LOGO_TIMER - 60)
{
// SSMTF text
jo_sprite_draw3D(g_Assets.SSMTF1Sprite, 0, -42, 500);
jo_sprite_draw3D(g_Assets.SSMTF2Sprite, 0, -14, 500);
jo_sprite_draw3D(g_Assets.SSMTF3Sprite, 0, 14, 500);
jo_sprite_draw3D(g_Assets.SSMTF4Sprite, 0, 42, 500);
}
// check if the timer has expired
if(g_Game.frame > SSMTF_LOGO_TIMER)
{
transitionToTitleScreen();
return;
}
return;
}
// randomize the location of the title screen flickies
void randomizeTitleFlicky(PFLICKY flicky)
{
flicky->x_pos = -500 + jo_random(300);
flicky->y_pos = 500 - jo_random(250);
flicky->angle = -8;
flicky->z_pos = 540;
flicky->flapping = jo_random(2) - 1;
flicky->frameTimer = FLICKY_FLAPPING_SPEED + jo_random(FLICKY_FLAPPING_SPEED);
}
// draws the flickys on the title screen
void drawTitleFlickys(void)
{
unsigned int i = 0;
for(i = 0; i < COUNTOF(g_FlickyTitleSprites); i++)
{
PFLICKY temp = &g_FlickyTitleSprites[i];
temp->frameTimer--;
if(temp->frameTimer <= 0)
{
temp->flapping = !temp->flapping;
temp->frameTimer = FLICKY_FLAPPING_SPEED;
}
// draw the Flicky
if(temp->flapping == false)
{
jo_sprite_draw3D_and_rotate(g_FlickySprites[i%12].up, temp->x_pos, temp->y_pos, temp->z_pos, temp->angle);
}
else
{
jo_sprite_draw3D_and_rotate(g_FlickySprites[i%12].down, temp->x_pos, temp->y_pos, temp->z_pos, temp->angle);
}
temp->x_pos += 1;
temp->y_pos -= 1;
// reset the Flicky if we are off screen
if(temp->x_pos > 300)
{
randomizeTitleFlicky(temp);
}
// check if we need to flap
temp->frameTimer--;
if(temp->frameTimer < 0)
{
temp->flapping = !temp->flapping;
temp->frameTimer = FLICKY_FLAPPING_SPEED + jo_random(FLICKY_FLAPPING_SPEED);
}
}
}
// draws the title screen and menu
void titleScreen_draw(void)
{
if(g_Game.gameState != GAMESTATE_TITLE_SCREEN)
{
return;
}
g_Game.frame++;
// version #
jo_printf(33, 28, "%s", VERSION);
// Flicky's Flock title screen
jo_sprite_draw3D(g_Assets.titleSprite, 0, 0, 500);
// floor
drawTitleFlickys();
// lives, position, and start text
jo_sprite_draw3D(g_Assets.livesSprite, -100, TITLE_SCREEN_OPTIONS_Y, 500);
jo_sprite_draw3D(g_Assets.positionSprite, -10, TITLE_SCREEN_OPTIONS_Y, 500);
jo_sprite_draw3D(g_Assets.startSprite, 100, TITLE_SCREEN_OPTIONS_Y, 500);
int livesSprite = 0;
switch(g_Game.numLivesChoice)
{
case 0:
livesSprite = g_Assets.livesInfSprite;
break;
case 1:
livesSprite = g_Assets.lives1Sprite;
break;
case 2:
livesSprite = g_Assets.lives3Sprite;
break;
case 3:
livesSprite = g_Assets.lives5Sprite;
break;
case 4:
livesSprite = g_Assets.lives9Sprite;
break;
default:
livesSprite = g_Assets.lives1Sprite;
break;
}
// number of lives text (infinite, 1, 3, 5, 9)
jo_sprite_draw3D(livesSprite, -70, TITLE_SCREEN_OPTIONS_Y, 500);
int startingPositionSprite = 0;
switch(g_Game.startingPositionChoice)
{
case 0:
startingPositionSprite = g_Assets.fixedSprite;
break;
case 1:
startingPositionSprite = g_Assets.randomSprite;
break;
default:
startingPositionSprite = g_Assets.fixedSprite;
break;
}
// starting position choice (fixed or rand)
jo_sprite_draw3D(startingPositionSprite, 25, TITLE_SCREEN_OPTIONS_Y, 500);
// check if we need to flap
if(g_Game.frame >= FLICKY_FLAPPING_SPEED)
{
g_Game.titleFlapping = !g_Game.titleFlapping;
g_Game.frame = 0;
}
int titleFlickyOffset = 0;
switch(g_Game.titleScreenChoice)
{
case 0:
titleFlickyOffset = -134;
break;
case 1:
titleFlickyOffset = -36;
break;
case 2:
titleFlickyOffset = 69;
break;
default:
// should be impossible!
titleFlickyOffset = 0;
break;
}
// menu selection Flicky
if(g_Game.titleFlapping == false)
{
jo_sprite_draw3D(g_FlickySprites[g_Game.randomFlicky].up, titleFlickyOffset, TITLE_SCREEN_OPTIONS_Y, 501);
}
else
{
jo_sprite_draw3D(g_FlickySprites[g_Game.randomFlicky].down, titleFlickyOffset, TITLE_SCREEN_OPTIONS_Y, 501);
}
return;
}
// handles input for the title screen menu
// only player one can control the title screen
void titleScreen_input(void)
{
int titleScreenChoice = g_Game.titleScreenChoice;
int numLivesChoice = g_Game.numLivesChoice;
int startingPositionChoice = g_Game.startingPositionChoice;
if(g_Game.gameState != GAMESTATE_TITLE_SCREEN)
{
return;
}
// did player one hit start
if(jo_is_pad1_key_pressed(JO_KEY_START))
{
if(g_Game.input.pressedStart == false)
{
g_Game.input.pressedStart = true;
transitionToGameplay(true);
return;
}
}
else
{
g_Game.input.pressedStart = false;
}
// did player one hit a direction key
if (jo_is_pad1_key_pressed(JO_KEY_LEFT))
{
if(g_Game.input.pressedLeft == false)
{
titleScreenChoice--;
}
g_Game.input.pressedLeft = true;
}
else
{
g_Game.input.pressedLeft = false;
}
if (jo_is_pad1_key_pressed(JO_KEY_RIGHT))
{
if(g_Game.input.pressedRight == false)
{
titleScreenChoice++;
}
g_Game.input.pressedRight = true;
}
else
{
g_Game.input.pressedRight = false;
}
if (jo_is_pad1_key_pressed(JO_KEY_UP))
{
if(titleScreenChoice == 0 && g_Game.input.pressedUp == false)
{
numLivesChoice++;
}
g_Game.input.pressedUp = true;
}
else
{
g_Game.input.pressedUp = false;
}
if (jo_is_pad1_key_pressed(JO_KEY_DOWN))
{
if(titleScreenChoice == 0 && g_Game.input.pressedDown == false)
{
numLivesChoice--;
}
g_Game.input.pressedDown = true;
}
else
{
g_Game.input.pressedDown = false;
}
// validate the title screen choices
if(titleScreenChoice < 0)
{
titleScreenChoice = 2;
}
if(titleScreenChoice > 2)
{
titleScreenChoice = 0;
}
g_Game.titleScreenChoice = titleScreenChoice;
// did the player hit ABC
if (jo_is_pad1_key_pressed(JO_KEY_A) ||
jo_is_pad1_key_pressed(JO_KEY_B) ||
jo_is_pad1_key_pressed(JO_KEY_C))
{
if(g_Game.titleScreenChoice == 2)
{
if(g_Game.input.pressedABC == false)
{
g_Game.gameState = GAMESTATE_GAMEPLAY;
g_Game.input.pressedABC = true;
transitionToGameplay(true);
return;
}
}
else if(g_Game.titleScreenChoice == 1)
{
if(g_Game.input.pressedABC == false)
{
g_Game.input.pressedABC = true;
startingPositionChoice++;
}
}
else if(g_Game.titleScreenChoice == 0)
{
if(g_Game.input.pressedABC == false)
{
g_Game.input.pressedABC = true;
numLivesChoice++;
}
}
}
else
{
g_Game.input.pressedABC = false;
}
// validate the number of lives
if(numLivesChoice < 0)
{
numLivesChoice = 4;
}
if(numLivesChoice > 4)
{
numLivesChoice = 0;
}
g_Game.numLivesChoice = numLivesChoice;
// validate the position choice
if(startingPositionChoice < 0)
{
startingPositionChoice = 1;
}
if(startingPositionChoice > 1)
{
startingPositionChoice = 0;
}
g_Game.startingPositionChoice = startingPositionChoice;
return;
}
// draws the gameplay sprites
void gameplay_draw(void)
{
bool allDead = false;
int topScore = 0;
int hunds = 0;
int tens = 0;
int ones = 0;
if(g_Game.gameState != GAMESTATE_GAMEPLAY)
{
return;
}
g_Game.frame++;
// draw players
for(int i = 0; i < MAX_PLAYERS; i++)
{
g_Players[i].spawnFrameTimer++;
if(g_Players[i].spawnFrameTimer >= SPAWN_FRAME_TIMER)
{
// 5 seconds have passed, player must play
g_Players[i].hasFlapped = true;
}
else
{
// the player just spawned, skip every 4th frame so they flash
// don't flash invulnerable on the first spawn
if(g_Players[i].numDeaths > 0 && jo_random(100) < 15)
{
continue;
}
}
// only draw flying player or dying players
if(g_Players[i].state == FLICKYSTATE_FLYING)
{
// if reverse gravity is enabled, flip the player upside down
if(g_Players[i].reverseGravityTimer > 0)
{
jo_sprite_enable_vertical_flip();
}
// if lightning is enabled, shrink the player
if(g_Players[i].lightningTimer > 0)
{
jo_sprite_change_sprite_scale(0.75);
}
// if flying, draw player with their angle
if(g_Players[i].flapping)
{
jo_sprite_draw3D_and_rotate(g_FlickySprites[g_Players[i].spriteID].up, g_Players[i].x_pos, g_Players[i].y_pos, g_Players[i].z_pos, g_Players[i].angle);
}
else
{
jo_sprite_draw3D_and_rotate(g_FlickySprites[g_Players[i].spriteID].down, g_Players[i].x_pos, g_Players[i].y_pos, g_Players[i].z_pos, g_Players[i].angle);
}
if(g_Players[i].reverseGravityTimer > 0)
{
jo_sprite_disable_vertical_flip();
}
if(g_Players[i].lightningTimer > 0)
{
jo_sprite_change_sprite_scale(1);
}
}
else if(g_Players[i].state == FLICKYSTATE_DYING)
{
// if lightning is enabled, shrink the player
if(g_Players[i].lightningTimer > 0)
{
jo_sprite_change_sprite_scale(0.75);
}
jo_sprite_draw3D(g_FlickySprites[g_Players[i].spriteID].death, g_Players[i].x_pos, g_Players[i].y_pos, g_Players[i].z_pos);
if(g_Players[i].lightningTimer > 0)
{
jo_sprite_change_sprite_scale(1);
}
// death animination flies up and pauses
if(g_Players[i].frameTimer >= DEATH_FRAME_TIMER_FLYING)
{
// only adjust the height if the player is below the max
// if I don't do this they will float off the top of the screen
if(g_Players[i].y_pos > MAX_DEATH_HEIGHT)
{
g_Players[i].y_pos -= 2;
}
}
g_Players[i].frameTimer--;
if(g_Players[i].frameTimer <= 0)
{
g_Players[i].state = FLICKYSTATE_DEAD;
g_Players[i].frameTimer = FLICKY_FLAPPING_SPEED + jo_random(FLICKY_FLAPPING_SPEED);
g_Players[i].numDeaths++;
}
}
}
// check if all players are dead
allDead = areAllPlayersDead();
if(allDead == true)
{
g_Game.frameDeathTimer++;
}
else
{
g_Game.frameDeathTimer = 0;
}
// check if all players are dead for enough time to end the game
if(g_Game.frameDeathTimer >= ALL_DEAD_FRAME_TIMER)
{
transitionToGameOverOrPause(false);
return;
}
// draw the pipes
for(int i = 0; i < MAX_PIPES; i++)
{
if(g_Pipes[i].state != PIPESTATE_INITIALIZED)
{
continue;
}
for(int j = 0; j < g_Pipes[i].numSections; j++)
{
if(j == 0 )
{
jo_sprite_draw3D(g_Assets.pipeTopSprite, g_Pipes[i].x_pos, g_Pipes[i].y_pos + (j*16) + 8, 504); // top of the bottom pipe
}
//else
{
jo_sprite_draw3D(g_Assets.pipeSprite, g_Pipes[i].x_pos, g_Pipes[i].y_pos + (j*16), 505); // bottom pipe
}
if(j == g_Pipes[i].numSections -1)
{
jo_sprite_enable_vertical_flip();
jo_sprite_draw3D(g_Assets.pipeTopSprite, g_Pipes[i].x_pos, g_Pipes[i].top_y_pos + (j*16) - 8, 504); // top pipe
jo_sprite_disable_vertical_flip();
}
//else
{
jo_sprite_draw3D(g_Assets.pipeSprite, g_Pipes[i].x_pos, g_Pipes[i].top_y_pos + (j*16), 505); // bottom of the top pipe
}
}
// shift the pipe to the left
g_Pipes[i].x_pos--;
if(g_Pipes[i].x_pos < -256)
{
initPipe(&g_Pipes[i]);
}
}
// draw the powerups
for(int i = 0; i < MAX_POWER_UPS; i++)
{
if(g_PowerUps[i].state != POWERUP_INITIALIZED)
{
continue;
}
jo_sprite_draw3D(g_Assets.powerUpSprites[g_PowerUps[i].type], g_PowerUps[i].x_pos, g_PowerUps[i].y_pos, g_PowerUps[i].z_pos);
// shift the power-up to the left
g_PowerUps[i].x_pos--;
if(g_PowerUps[i].x_pos < -256)
{
initPowerUp(&g_PowerUps[i]);
}
}
// draw the floor
drawFloor();
// shift the floor
g_Game.floorPosition_x--;
if(g_Game.floorPosition_x <= -480)
{
g_Game.floorPosition_x = -240;
}
// draw the score
topScore = getTopScore();
hunds = (topScore/100);
tens = (topScore/10) % 10;
ones = topScore % 10;
// check if the player passed 100
if(topScore >= VICTORY_CONDITION)
{
g_Game.topScore = topScore;
transitionToGameOverOrPause(false);
return;
}
if(hunds != 0)
{
jo_sprite_draw3D(g_Assets.largeDigitSprites[hunds], -16, -76, 502);
jo_sprite_draw3D(g_Assets.largeDigitSprites[tens], 0, -76, 502);
jo_sprite_draw3D(g_Assets.largeDigitSprites[ones], 16, -76, 502);
}
else if(tens != 0)
{
jo_sprite_draw3D(g_Assets.largeDigitSprites[tens], -8, -76, 502);
jo_sprite_draw3D(g_Assets.largeDigitSprites[ones], 8, -76, 502);
}
else
{
jo_sprite_draw3D(g_Assets.largeDigitSprites[ones], 0, -76, 502);
}
return;
}
// handles the input during gameplay
void gameplay_input(void)
{
if(g_Game.gameState != GAMESTATE_GAMEPLAY)
{
return;
}
// did player one pause the game?
if (jo_is_pad1_key_pressed(JO_KEY_START))
{
if(g_Game.input.pressedStart == false)
{
transitionToGameOverOrPause(true);
}
g_Game.input.pressedStart = true;
}
else
{
g_Game.input.pressedStart = false;
}
// check inputs for all players
for(int i = 0; i < MAX_PLAYERS; i++)
{
bool flapping = false;
if(g_Players[i].state == FLICKYSTATE_UNINITIALIZED ||
g_Players[i].state == FLICKYSTATE_DYING )
{
continue;
}
// L and R trigger allow the player to change their Flicky
if (jo_is_input_key_down(g_Players[i].playerID, JO_KEY_L))
{
if(g_Players[i].input.pressedLT == false)
{
g_Players[i].spriteID = getNextFlickySprite(g_Players[i].spriteID, -1);
}
g_Players[i].input.pressedLT = true;
}
else
{
g_Players[i].input.pressedLT = false;
}
if (jo_is_input_key_down(g_Players[i].playerID, JO_KEY_R))
{
if(g_Players[i].input.pressedRT == false)
{
g_Players[i].spriteID = getNextFlickySprite(g_Players[i].spriteID, 1);
}
g_Players[i].input.pressedRT = true;
}
else
{
g_Players[i].input.pressedRT = false;
}
// did the player flap
// if the player is dead, flapping respawns them
if (jo_is_input_key_down(g_Players[i].playerID, JO_KEY_A) ||
jo_is_input_key_down(g_Players[i].playerID, JO_KEY_B) ||
jo_is_input_key_down(g_Players[i].playerID, JO_KEY_C))
{
if(g_Players[i].input.pressedABC == false)
{
g_Players[i].input.pressedABC = true;
g_Players[i].hasFlapped = true;
// if the player was dead and hit ABC, respawn them
if(g_Players[i].state == FLICKYSTATE_DEAD)
{
spawnPlayer(i, true);
continue;
}
flapping = true;
}
}
else
{
g_Players[i].input.pressedABC = false;
}
// calculate the player's y speed
if(flapping == true)
{
g_Players[i].flapping = !g_Players[i].flapping;
g_Players[i].frameTimer = FLICKY_FLAPPING_SPEED;
g_Players[i].y_speed = FLAP_Y_SPEED;
//
// power-ups
//
// stone sneakers make jumps heavier
if(g_Players[i].stoneSneakersTimer > 0)
{
g_Players[i].y_speed += 4;
}
// lightning makes jumps floatier
if(g_Players[i].lightningTimer > 0)
{
g_Players[i].y_speed -= 3;
}
// reverse gravity swaps up and down
if(g_Players[i].reverseGravityTimer > 0)
{
g_Players[i].y_speed *= -1;
}
}
else
{
// player didn't flap
g_Players[i].frameTimer--;
if(g_Players[i].frameTimer <= 0)
{
g_Players[i].flapping = !g_Players[i].flapping;
g_Players[i].frameTimer = FLICKY_FLAPPING_SPEED;
}
}
// adjust the players height, but only if they are active
if(g_Players[i].hasFlapped == true)
{
if(g_Players[i].reverseGravityTimer == 0)
{
// reverse gravity not enabled
g_Players[i].y_pos += g_Players[i].y_speed/5 * 1;
g_Players[i].y_speed += FALLING_CONSTANT * 1;
}
else
{
// reverse gravity enabled
g_Players[i].y_pos += g_Players[i].y_speed/5 * 1;
g_Players[i].y_speed -= FALLING_CONSTANT * 1;
}
}
// validate speeds
if(g_Players[i].y_speed > MAX_Y_SPEED)
{
g_Players[i].y_speed = MAX_Y_SPEED;
}
if(g_Players[i].reverseGravityTimer > 0)
{
if(g_Players[i].y_speed < MAX_Y_SPEED * -1)
{
g_Players[i].y_speed = MAX_Y_SPEED * -1;
}
}
g_Players[i].angle = calculateFlickyAngle(g_Players[i].y_speed);
if(g_Players[i].x_pos > SCREEN_RIGHT)
{
g_Players[i].x_pos = SCREEN_RIGHT;
}
if(g_Players[i].x_pos < SCREEN_LEFT)
{
g_Players[i].x_pos = SCREEN_LEFT;
}
if(g_Players[i].y_pos > SCREEN_BOTTOM)
{
g_Players[i].y_pos = SCREEN_BOTTOM;
}
if(g_Players[i].y_pos < SCREEN_TOP)
{
g_Players[i].y_pos = SCREEN_TOP;
}
//
// decrement power-up timers
//
if(g_Players[i].stoneSneakersTimer > 0)
{
g_Players[i].stoneSneakersTimer--;
}
if(g_Players[i].lightningTimer > 0)
{
g_Players[i].lightningTimer--;
}
if(g_Players[i].reverseGravityTimer > 0)
{
g_Players[i].reverseGravityTimer--;
if(g_Players[i].reverseGravityTimer == 0)
{
// reverse gravity is jarring, so 0 out their speed first
g_Players[i].y_speed = 0;
}
}
}
return;
}
// checks for collisions between the Flicky and the ground and pipes
// also checks if the Flicky scored
// skips collision detection if Flicky is in an invulnerable state
void gameplay_checkForCollisions(void)
{
bool result = false;
if(g_Game.gameState != GAMESTATE_GAMEPLAY)
{
return;
}
for(int i = 0; i < MAX_PLAYERS; i++)
{
// don't check for collisions if the player is uninitialized or dead
if(g_Players[i].state == FLICKYSTATE_UNINITIALIZED ||
g_Players[i].state == FLICKYSTATE_DYING ||
g_Players[i].state == FLICKYSTATE_DEAD)
{
continue;
}
// if the player hasn't flapped yet they are still invulnerable
if(g_Players[i].hasFlapped == false)
{
continue;
}
// if the player just spawned, don't do collision detection
if(g_Players[i].spawnFrameTimer < SPAWN_FRAME_TIMER)
{
continue;
}
// check if the player hit the ground
if(g_Players[i].y_pos > GROUND_COLLISION)
{
killPlayer(i);
continue;
}
// loop through all the pipes
for(int j = 0; j < MAX_PIPES; j++)
{
if(g_Pipes[j].state != PIPESTATE_INITIALIZED)
{
continue;
}