/
space.c
executable file
·1139 lines (941 loc) · 30.9 KB
/
space.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 "space.h"
#include "trigmath.h"
#include "dynamic_libs/os_functions.h"
/**
This class is a bit of a mess, but it basically does "everything else" in the game.
The most interesting function is rotating the bitmap (makeRotationMatrix).
Other things it handles:
- Joystick input (p1Move)
- Bullet firing (p1Shoot)
- Star drawing (renderStars)
- Status bar drawing (renderTexts)
- Decompressing sprites (decompress_bitmap)
- Handling the menu at the title screen (doMenuAction)
It relies heavily on a SpaceGlobals struct defined in space.h. This is a carry over from the libwiiu
pong example, but also I believe necesary since global variables don't seem to be able to be set(?)
**/
#define xMinBoundry 0
#define xMaxBoundry 427
#define yMinBoundry 0
#define yMaxBoundry 240
void blackout()
{
fillScreen(0,0,0,0);
}
void increaseScore(struct SpaceGlobals* mySpaceGlobals, int inc)
{
// count the number of 5000s that fit into the score before adding
int fiveThousandsBefore = mySpaceGlobals->score / 5000;
// increase the score
mySpaceGlobals->score += inc;
// count them again
int fiveThousandsAfter = mySpaceGlobals->score / 5000;
// if it increased, levelup
if (fiveThousandsAfter > fiveThousandsBefore)
{
mySpaceGlobals->level ++;
}
}
void p1Shoot(struct SpaceGlobals * mySpaceGlobals)
{
if (mySpaceGlobals->playerExplodeFrame > 1)
return;
// float sensitivity = 0.1f;
//
// float xdif = 0;
// float ydif = 0;
if (mySpaceGlobals->touched)
// if ((fabs(mySpaceGlobals->rstick.x) > sensitivity) || (fabs(mySpaceGlobals->rstick.y) > sensitivity))
{
// if (fabs(mySpaceGlobals->rstick.x) > sensitivity)
// xdif = mySpaceGlobals->p1X - (mySpaceGlobals->p1X + (mySpaceGlobals->rstick.x * 18));
// if (fabs(mySpaceGlobals->rstick.y) > sensitivity)
// ydif = mySpaceGlobals->p1Y - (mySpaceGlobals->p1Y - (mySpaceGlobals->rstick.y * 18));
float xdif = mySpaceGlobals->p1X - mySpaceGlobals->touchX + 18;
float ydif = mySpaceGlobals->p1Y - mySpaceGlobals->touchY + 18;
mySpaceGlobals->angle = my_atan2(xdif, ydif);
// activateBullet(mySpaceGlobals, mySpaceGlobals->angle - 3.14159265, mySpaceGlobals->p1X, mySpaceGlobals->p1Y);
// shoot a bullet
// find an inactive bullet
float theta = mySpaceGlobals->angle - 3.14159265;
int xx;
for (xx=0; xx<20; xx++)
{
if (mySpaceGlobals->bullets[xx].active != 1)
{
mySpaceGlobals->bullets[xx].x = mySpaceGlobals->p1X + 18;
mySpaceGlobals->bullets[xx].y = mySpaceGlobals->p1Y + 18;
mySpaceGlobals->bullets[xx].m_x = 9*my_sin(theta); // 9 is the desired bullet speed
mySpaceGlobals->bullets[xx].m_y = 9*my_cos(theta); // we have to solve for the hypotenuese
mySpaceGlobals->bullets[xx].active = 1;
mySpaceGlobals->firstShotFired = 1;
if (mySpaceGlobals->score >= 1000)
mySpaceGlobals->displayHowToPlay = 0;
break;
}
}
}
moveBullets(mySpaceGlobals);
}
//Updates player1 location
void p1Move(struct SpaceGlobals *mySpaceGlobals) {
// can't move while exploding
if (mySpaceGlobals->playerExplodeFrame > 1)
return;
// Handle analog stick movements
Vec2D left = mySpaceGlobals->lstick;
Vec2D right = mySpaceGlobals->rstick;
// get the differences
float xdif = left.x + right.x;
float ydif = left.y + right.y;
// Handle D-pad movements as well
// max out speed at 1 or -1 in both directions
xdif = (xdif > 1 || mySpaceGlobals->button & VPAD_BUTTON_RIGHT)? 1 : xdif;
xdif = (xdif < -1 || mySpaceGlobals->button & VPAD_BUTTON_LEFT)? -1 : xdif;
ydif = (ydif > 1 || mySpaceGlobals->button & VPAD_BUTTON_UP)? 1 : ydif;
ydif = (ydif < -1 || mySpaceGlobals->button & VPAD_BUTTON_DOWN)? -1 : ydif;
// don't update angle if both are within -.1 < x < .1
// (this is an expenesive check... 128 bytes compared to just ==0)
if (xdif < 0.1 && xdif > -0.1 && ydif < 0.1 && ydif > -0.1) return;
// invalid view
mySpaceGlobals->invalid = 1;
// accept x and y movement from either stick
mySpaceGlobals->p1X += xdif*5;
mySpaceGlobals->p1Y -= ydif*5;
// calculate angle to face
mySpaceGlobals->angle = my_atan2(ydif, xdif) - 3.14159265/2;
// update score if on a frame divisible by 60 (gain 10 points every second)
if (mySpaceGlobals->frame % 60 == 0)
{
increaseScore(mySpaceGlobals, 10);
// if the score is at least 50 and a shot hasn't been fired yet, display a message about shooting
if (mySpaceGlobals->score >= 50 && !mySpaceGlobals->firstShotFired)
mySpaceGlobals->displayHowToPlay = 1;
}
};
void checkPause(struct SpaceGlobals * mySpaceGlobals)
{
if (mySpaceGlobals->button & VPAD_BUTTON_PLUS)
{
// switch to the pause state and mark view as invalid
mySpaceGlobals->state = 3;
mySpaceGlobals->invalid = 1;
}
}
void handleCollisions(struct SpaceGlobals * mySpaceGlobals)
{
int playerLeft = mySpaceGlobals->p1X;
int playerRight = playerLeft + 36;
int playerUp = mySpaceGlobals->p1Y;
int playerDown = playerUp + 36;
// don't let the player go offscreen
if (playerLeft < xMinBoundry)
mySpaceGlobals->p1X = xMinBoundry;
if (playerRight > xMaxBoundry)
mySpaceGlobals->p1X = xMaxBoundry - 36;
if (playerUp < yMinBoundry + 20)
mySpaceGlobals->p1Y = yMinBoundry + 20;
if (playerDown > yMaxBoundry)
mySpaceGlobals->p1Y = yMaxBoundry - 36;
// check enemies if they collide with the player or any of the 20 active bullets
int x;
for (x=0; x<100; x++)
{
if (mySpaceGlobals->enemies[x].position.active == 1)
{
// collision checkin from here: http://stackoverflow.com/a/1736741/1137828
// check player
int sqMe1 = ((mySpaceGlobals->enemies[x].position.x+7)-(mySpaceGlobals->p1X+9));
int sqMe2 = ((mySpaceGlobals->enemies[x].position.y+7)-(mySpaceGlobals->p1Y+9));
if (sqMe1*sqMe1 + sqMe2*sqMe2 <= (7+9)*(7+9))
{
if (mySpaceGlobals->playerExplodeFrame < 1)
{
// player was hit
mySpaceGlobals->playerExplodeFrame = 2;
initGameState(mySpaceGlobals);
}
}
int y;
for (y=0; y<20; y++)
{
if (mySpaceGlobals->bullets[y].active == 1)
{
// check player's bullets
sqMe1 = ((mySpaceGlobals->enemies[x].position.x+7)-(mySpaceGlobals->bullets[y].x+1));
sqMe2 = ((mySpaceGlobals->enemies[x].position.y+7)-(mySpaceGlobals->bullets[y].y+1));
if (sqMe1*sqMe1 + sqMe2*sqMe2 <= (7+1)*(7+1))
{
// enemy was hit, active = 2 is explode
increaseScore(mySpaceGlobals, 100); // 100 points for killing enemy
mySpaceGlobals->enemies[x].position.active = 2;
// bullet is destroyed with enemy
mySpaceGlobals->bullets[y].active = 0;
break;
}
}
}
}
}
}
void makeScaleMatrix(int frame, int width, void *orig, void *targ, int transIndex)
{
unsigned char (*original)[width] = (unsigned char (*)[width])(orig);
unsigned char (*target)[width] = (unsigned char (*)[width])(targ);
int x;
for (x=0; x<width; x++)
{
int y;
for (y=0; y<width; y++)
{
target[x][y] = transIndex;
}
}
int woffset = width/2;
for (x=0; x<width; x++)
{
int y=0;
for (y=0; y<width; y++)
{
// rotate the pixel by the angle into a new spot in the rotation matrix
int newx = (x-woffset)*frame + woffset;
int newy = (y-woffset)*frame + woffset;
if (original[newx][newy] == transIndex) continue;
if (newx < 0 || newx >= width) continue;
if (newy < 0 || newy >= width) continue;
target[newx][newy] = original[x][y];
}
}
}
void handleExplosions(struct SpaceGlobals* mySpaceGlobals)
{
int x;
for (x=0; x<100; x++)
{
if (mySpaceGlobals->enemies[x].position.active > 1)
{
makeScaleMatrix(mySpaceGlobals->enemies[x].position.active/2.0, 23, mySpaceGlobals->enemy, mySpaceGlobals->enemies[x].rotated_sprite, 9);
mySpaceGlobals->enemies[x].position.active ++;
if (mySpaceGlobals->enemies[x].position.active > 20)
mySpaceGlobals->enemies[x].position.active = 0;
}
}
if (mySpaceGlobals->playerExplodeFrame > 1)
{
makeScaleMatrix(mySpaceGlobals->playerExplodeFrame, 36, mySpaceGlobals->orig_ship, mySpaceGlobals->rotated_ship, mySpaceGlobals->transIndex);
mySpaceGlobals->playerExplodeFrame ++;
mySpaceGlobals->invalid = 1;
if (mySpaceGlobals->playerExplodeFrame > 20)
{
mySpaceGlobals->playerExplodeFrame = 0;
mySpaceGlobals->lives --;
if (mySpaceGlobals->lives <= 0)
{
// game over!
mySpaceGlobals->state = 4;
mySpaceGlobals->invalid = 1;
}
else
mySpaceGlobals->renderResetFlag = 1;
}
}
}
void makeRotationMatrix(float angle, int width, void *orig, void *targ, int transIndex)
{
unsigned char (*original)[width] = (unsigned char (*)[width])(orig);
unsigned char (*target)[width] = (unsigned char (*)[width])(targ);
int x;
for (x=0; x<width; x++)
{
int y;
for (y=0; y<width; y++)
{
target[x][y] = transIndex;
}
}
float woffset = width/2.0f;
int ix;
// go though every pixel in the target bitmap
for (ix=0; ix<width; ix++)
{
int iy;
for (iy=0; iy<width; iy++)
{
// rotate the pixel by the angle into a new spot in the rotation matrix
int oldx = (int)((ix-woffset)*my_cos(angle) + (iy-woffset)*my_sin(angle) + woffset);
int oldy = (int)((ix-woffset)*my_sin(angle) - (iy-woffset)*my_cos(angle) + woffset);
// if (oldx < 0) oldx += width;
// if (oldy < 0) oldy += width;
if (original[oldx][oldy] == transIndex) continue;
if (oldx < 0 || oldx >= width) continue;
if (oldy < 0 || oldy >= width) continue;
// TODO: crashes with this below line! When trying to assign to target, but only after doing the above math
target[ix][iy] = original[oldx][oldy];
}
}
}
void renderEnemies(struct SpaceGlobals *mySpaceGlobals)
{
// for all active bullets, advance them
int x=0;
for (x=0; x<20; x++)
{
if (mySpaceGlobals->bullets[x].active == 1)
{
int z, za;
for (z=0; z<4; z++)
for (za=0; za<2; za++)
drawPixel(mySpaceGlobals->bullets[x].x + z, mySpaceGlobals->bullets[x].y + za, 255, 0, 0);
}
}
// for all active enemies, advance them
for (x=0; x<100; x++) // up to 100 enemies at once
{
if (mySpaceGlobals->enemies[x].position.active >= 1)
{
drawBitmap(mySpaceGlobals->enemies[x].position.x, mySpaceGlobals->enemies[x].position.y, 23, 23, mySpaceGlobals->enemies[x].rotated_sprite, enemy_palette);
}
}
}
void render(struct SpaceGlobals *mySpaceGlobals)
{
if (mySpaceGlobals->invalid == 1)
{
blackout();
mySpaceGlobals->frame++;
if (mySpaceGlobals->renderResetFlag)
{
renderReset(mySpaceGlobals);
}
renderStars(mySpaceGlobals);
renderEnemies(mySpaceGlobals);
renderShip(mySpaceGlobals);
renderTexts(mySpaceGlobals);
flipBuffers();
mySpaceGlobals->invalid = 0;
}
}
// see the notes in images.c for more info on how this works
void decompress_sprite(int arraysize, int width, int height, const signed char* input, void *targ, char transIndex)
{
unsigned char (*target)[width] = (unsigned char (*)[width])(targ);
int cx = 0, cy = 0;
int x;
int posinrow = 0;
// go through input array
for (x=0; x<arraysize; x+=2)
{
int count = input[x];
char value = input[x+1];
int z;
if (count == -120) // full value rows of last index in palette
{
for (z=0; z<value; z++)
{
int za;
for (za=0; za<width; za++)
{
target[cy+z][cx+za] = transIndex;
}
}
cy += value;
continue;
}
////
if (count <= 0) // if it's negative, -count is value, and value is meaningless and advance by one
{
value = -1*count;
count = 1;
x--; // subtract one, so next time it goes up by 2, putting us at x+1
}
for (z=0; z<count; z++)
{
target[cy][cx] = value;
cx++;
}
posinrow += z;
if (posinrow >= width)
{
posinrow = 0;
cx = 0;
cy++;
}
}
}
void moveBullets(struct SpaceGlobals *mySpaceGlobals)
{
// for all active bullets, advance them
int x=0;
for (x=0; x<20; x++)
{
if (mySpaceGlobals->bullets[x].active == 1)
{
mySpaceGlobals->bullets[x].x += mySpaceGlobals->bullets[x].m_x;
mySpaceGlobals->bullets[x].y += mySpaceGlobals->bullets[x].m_y;
if (mySpaceGlobals->bullets[x].x > xMaxBoundry ||
mySpaceGlobals->bullets[x].x < xMinBoundry ||
mySpaceGlobals->bullets[x].y > yMaxBoundry ||
mySpaceGlobals->bullets[x].y < yMinBoundry + 20)
mySpaceGlobals->bullets[x].active = 0;
mySpaceGlobals->invalid = 1;
}
}
for (x=0; x<100; x++)
{
if (mySpaceGlobals->enemies[x].position.active == 1)
{
mySpaceGlobals->enemies[x].position.x += mySpaceGlobals->enemies[x].position.m_x;
mySpaceGlobals->enemies[x].position.y += mySpaceGlobals->enemies[x].position.m_y;
if (mySpaceGlobals->enemies[x].position.x > xMaxBoundry ||
mySpaceGlobals->enemies[x].position.x < xMinBoundry ||
mySpaceGlobals->enemies[x].position.y > yMaxBoundry ||
mySpaceGlobals->enemies[x].position.y < yMinBoundry + 20)
mySpaceGlobals->enemies[x].position.active = 0;
// rotate the enemy slowly
mySpaceGlobals->enemies[x].angle += 0.02f;
if (mySpaceGlobals->enemies[x].angle > 6.28318530f)
mySpaceGlobals->enemies[x].angle = 0.0f;
// int targetAngle = mySpaceGlobals->enemies[x].angle;
// TODO: the below crashes... with angle instead of 0
makeRotationMatrix(mySpaceGlobals->enemies[x].angle, 23, mySpaceGlobals->enemy, mySpaceGlobals->enemies[x].rotated_sprite, 9);
// makeScaleMatrix(3, 23, mySpaceGlobals->enemy, mySpaceGlobals->enemies[x].rotated_sprite, 9);
mySpaceGlobals->invalid = 1;
}
}
}
void renderTexts(struct SpaceGlobals *mySpaceGlobals)
{
fillRect(0, 0, xMaxBoundry, 20, 0, 0, 0);
char score[255];
if (mySpaceGlobals->dontKeepTrackOfScore == 1)
__os_snprintf(score, 255, "Score: N/A");
else
__os_snprintf(score, 255, "Score: %09d", mySpaceGlobals->score);
drawString(0, -1, score);
drawStringTv(0, -1, score);
char level[255];
__os_snprintf(level, 255, "Lv %d", mySpaceGlobals->level+1);
drawString(30, -1, level);
drawStringTv(51, -1, level);
char lives[255];
__os_snprintf(lives, 255, "Lives: %d", mySpaceGlobals->lives);
drawString(55, -1, lives);
drawStringTv(90, -1, lives);
if (mySpaceGlobals->displayHowToPlay)
{
char nag[255];
__os_snprintf(nag, 255, "Touch and hold on the screen to rapid fire!");
drawString(20, 17, nag);
drawStringTv(56, 27, nag);
}
}
void renderShip(struct SpaceGlobals *mySpaceGlobals)
{
const int posx = (int)mySpaceGlobals->p1X;
const int posy = (int)mySpaceGlobals->p1Y;
if (mySpaceGlobals->playerExplodeFrame < 2)
makeRotationMatrix(mySpaceGlobals->angle, 36, mySpaceGlobals->orig_ship, mySpaceGlobals->rotated_ship, mySpaceGlobals->transIndex);
drawBitmap(posx, posy, 36, 36, mySpaceGlobals->rotated_ship, mySpaceGlobals->curPalette);
}
void renderStars(struct SpaceGlobals *mySpaceGlobals)
{
// don't draw stars if the player is on their last life and died
if (mySpaceGlobals->lives == 1 && mySpaceGlobals->playerExplodeFrame > 1)
return;
drawPixels(mySpaceGlobals->stars);
}
//Reset the game
void reset(struct SpaceGlobals *mySpaceGlobals) {
mySpaceGlobals->button = 0;
//Set flag to render reset screen;
mySpaceGlobals->renderResetFlag = 1;
};
void initGameState(struct SpaceGlobals *mySpaceGlobals)
{
// init bullets
int x;
for (x=0; x<20; x++)
{
mySpaceGlobals->bullets[x].active = 0;
}
// init x and y pos of player
// mySpaceGlobals->p1X = 40;
// mySpaceGlobals->p1Y = 150;
// init enemies
for (x=0; x<100; x++)
{
mySpaceGlobals->enemies[x].position.active = 0;
mySpaceGlobals->enemies[x].angle = 3.14f;
makeRotationMatrix(0, 23, mySpaceGlobals->enemy, mySpaceGlobals->enemies[x].rotated_sprite, 9);
}
}
void initStars(struct SpaceGlobals *mySpaceGlobals)
{
// create the stars randomly
int x;
for (x=0; x<200; x++)
{
mySpaceGlobals->stars[x].x = (int)(prand(&mySpaceGlobals->seed)*xMaxBoundry);
mySpaceGlobals->stars[x].y = (int)(prand(&mySpaceGlobals->seed)*yMaxBoundry);
int randomNum = (int)(prand(&mySpaceGlobals->seed)*4);
// half of the time make them white, 1/4 yellow, 1/4 blue
mySpaceGlobals->stars[x].r = (randomNum <= 2)? 255 : 0;
mySpaceGlobals->stars[x].g = (randomNum <= 2)? 255 : 0;
mySpaceGlobals->stars[x].b = (randomNum != 2)? 255 : 0;
}
}
void displayTitle(struct SpaceGlobals * mySpaceGlobals)
{
if (mySpaceGlobals->invalid == 1)
{
blackout();
// draw some stars
renderStars(mySpaceGlobals);
// display the bitmap in upper center screen
drawBitmap(107, 30, 200, 100, mySpaceGlobals->title, title_palette);
char credits[255];
__os_snprintf(credits, 255, "by vgmoose");
char musiccredits[255];
__os_snprintf(musiccredits, 255, "~*cruise*~ by (T-T)b");
char license[255];
__os_snprintf(license, 255, "MIT License");
char play[255];
__os_snprintf(play, 255, "Start Game");
char password[255];
__os_snprintf(password, 255, "Password");
//display the menu under it
drawString(37, 9, credits);
drawStringTv(62, 14, credits);
drawString(25, 12, play);
drawStringTv(44, 17, play);
drawString(26, 13, password);
drawStringTv(45, 18, password);
drawString(45, 17, musiccredits);
drawStringTv(80, 27, musiccredits);
drawString(-2, 17, license);
drawStringTv(-2, 27, license);
drawMenuCursor(mySpaceGlobals);
flipBuffers();
mySpaceGlobals->invalid = 0;
}
}
void drawMenuCursor(struct SpaceGlobals *mySpaceGlobals)
{
// cover up any old cursors (used to be needed before changing to draw everything mode)
// fillRect(155, 155, 16, 30, 0, 0, 0);
// fillRect(240, 155, 16, 30, 0, 0, 0);
// display the cursor on the correct item
char cursor[255];
__os_snprintf(cursor, 255, ">> <<");
drawString(22, 12 + mySpaceGlobals->menuChoice, cursor);
drawStringTv(41, 17 + mySpaceGlobals->menuChoice, cursor);
}
void doMenuAction(struct SpaceGlobals *mySpaceGlobals)
{
// if we've seen the A button not being pressed
if (!(mySpaceGlobals->button & VPAD_BUTTON_A))
{
mySpaceGlobals->allowInput = 1;
}
if (mySpaceGlobals->button & VPAD_BUTTON_A && mySpaceGlobals->allowInput)
{
// if we're on the title menu
if (mySpaceGlobals->state == 1)
{
if (mySpaceGlobals->menuChoice == 0)
{
totallyRefreshState(mySpaceGlobals);
// start game chosen
mySpaceGlobals->state = 7; // switch to game state
mySpaceGlobals->renderResetFlag = 1; // redraw screen
}
else if (mySpaceGlobals->menuChoice == 1)
{
// password screen chosen
mySpaceGlobals->state = 2;
}
}
// password screen
// else if (mySpaceGlobals->state == 2)
// {
// // this is handled by the password menu action function
// }
// pause screen
else if (mySpaceGlobals->state == 3)
{
if (mySpaceGlobals->menuChoice == 0)
{
// resume chosen
mySpaceGlobals->state = 7; // switch to game state
}
else if (mySpaceGlobals->menuChoice == 1)
{
// quit chosen
totallyRefreshState(mySpaceGlobals);
mySpaceGlobals->state = 1;
}
}
// game over screen
else if (mySpaceGlobals->state == 4)
{
totallyRefreshState(mySpaceGlobals);
if (mySpaceGlobals->menuChoice == 0)
{
// try again chosen
//player stays on the same level
mySpaceGlobals->state = 7; // switch to game state
}
else if (mySpaceGlobals->menuChoice == 1)
{
// quit chosen
mySpaceGlobals->state = 1;
}
}
// reset the choice
mySpaceGlobals->menuChoice = 0;
// disable menu input after selecting to prevent double selects
mySpaceGlobals->allowInput = 0;
// mark view invalid to redraw
mySpaceGlobals->invalid = 1;
}
float stickY = mySpaceGlobals->lstick.y + mySpaceGlobals->rstick.y;
if (mySpaceGlobals->button & VPAD_BUTTON_DOWN || stickY < -0.3)
{
mySpaceGlobals->menuChoice = 1;
mySpaceGlobals->invalid = 1;
}
if (mySpaceGlobals->button & VPAD_BUTTON_UP || stickY > 0.3)
{
mySpaceGlobals->menuChoice = 0;
mySpaceGlobals->invalid = 1;
}
}
void displayPause(struct SpaceGlobals * mySpaceGlobals)
{
if (mySpaceGlobals->invalid == 1)
{
blackout();
// display the password here
char resume[255];
__os_snprintf(resume, 255, "Resume");
char quit[255];
__os_snprintf(quit, 255, "Quit");
drawString(27, 12, resume);
drawStringTv(46, 17, resume);
drawString(28, 13, quit);
drawStringTv(47, 18, quit);
drawMenuCursor(mySpaceGlobals);
flipBuffers();
mySpaceGlobals->invalid = 0;
}
}
void doPasswordMenuAction(struct SpaceGlobals * mySpaceGlobals)
{
// if we've seen up, down, left, right, and a buttons not being pressed
if (!(mySpaceGlobals->button & VPAD_BUTTON_A ||
mySpaceGlobals->button & VPAD_BUTTON_UP ||
mySpaceGlobals->button & VPAD_BUTTON_DOWN ||
mySpaceGlobals->button & VPAD_BUTTON_LEFT ||
mySpaceGlobals->button & VPAD_BUTTON_RIGHT ))
{
mySpaceGlobals->allowInput = 1;
}
if (mySpaceGlobals->allowInput)
{
if (mySpaceGlobals->button & VPAD_BUTTON_B)
{
// go back to title screen
mySpaceGlobals->state = 1;
// update the menu choice
mySpaceGlobals->menuChoice = 0;
// disable menu input after selecting to prevent double selects
mySpaceGlobals->allowInput = 0;
// mark view invalid to redraw
mySpaceGlobals->invalid = 1;
}
if (mySpaceGlobals->button & VPAD_BUTTON_A)
{
// try the password
tryPassword(mySpaceGlobals);
// disable menu input after selecting to prevent double selects
mySpaceGlobals->allowInput = 0;
// update the menu choice
mySpaceGlobals->menuChoice = 0;
// mark view invalid to redraw
mySpaceGlobals->invalid = 1;
}
float stickY = mySpaceGlobals->lstick.y + mySpaceGlobals->rstick.y;
float stickX = mySpaceGlobals->lstick.x + mySpaceGlobals->rstick.x;
int down = (mySpaceGlobals->button & VPAD_BUTTON_DOWN || stickY < -0.3);
int up = (mySpaceGlobals->button & VPAD_BUTTON_UP || stickY > 0.3);
int left = (mySpaceGlobals->button & VPAD_BUTTON_LEFT || stickX < -0.3);
int right = (mySpaceGlobals->button & VPAD_BUTTON_RIGHT || stickX > 0.3);
if (up || down)
{
int offset = 1, x;
// keep going up in the 10s place to match current choice
for (x=0; x<(4 - mySpaceGlobals->menuChoice); x++)
offset *= 10;
if (up)
mySpaceGlobals->passwordEntered += offset;
if (down)
mySpaceGlobals->passwordEntered -= offset;
mySpaceGlobals->invalid = 1;
mySpaceGlobals->allowInput = 0;
}
if (left || right)
{
if (right)
mySpaceGlobals->menuChoice ++;
if (left)
mySpaceGlobals->menuChoice --;;
// bound the menu choices
if (mySpaceGlobals->menuChoice < 0)
mySpaceGlobals->menuChoice = 0;
if (mySpaceGlobals->menuChoice > 4)
mySpaceGlobals->menuChoice = 4;
mySpaceGlobals->invalid = 1;
mySpaceGlobals->allowInput = 0;
}
// bound the password
if (mySpaceGlobals->passwordEntered < 0)
mySpaceGlobals->passwordEntered = 0;
if (mySpaceGlobals->passwordEntered > 99999)
mySpaceGlobals->passwordEntered = 99999;
}
}
void displayPasswordScreen(struct SpaceGlobals * mySpaceGlobals)
{
if (mySpaceGlobals->invalid == 1)
{
blackout();
// drawPasswordMenuCursor(mySpaceGlobals);
char password[255];
__os_snprintf(password, 255, "Password:");
char up_cur[255];
__os_snprintf(up_cur, 255, "v");
char cur_pw[255];
__os_snprintf(cur_pw, 255, "%05d", mySpaceGlobals->passwordEntered);
char down_cur[255];
__os_snprintf(down_cur, 255, "^");
drawString(22, 8, password);
drawStringTv(41, 14, password);
drawString(32 + mySpaceGlobals->menuChoice, 7, up_cur);
drawStringTv(51 + mySpaceGlobals->menuChoice, 13, up_cur);
drawString(32, 8, cur_pw);
drawStringTv(51, 14, cur_pw);
drawString(32 + mySpaceGlobals->menuChoice, 9, down_cur);
drawStringTv(51 + mySpaceGlobals->menuChoice, 15, down_cur);
flipBuffers();
mySpaceGlobals->invalid = 0;
}
}
void addNewEnemies(struct SpaceGlobals *mySpaceGlobals)
{
if (mySpaceGlobals->noEnemies || mySpaceGlobals->playerExplodeFrame > 1)
return;
// here we make a new enemy with a certain speed based on the level
// get a random position from one of the sides with a random int 0-3
int side = (int)(prand(&mySpaceGlobals->seed)*4);
// // randomly decide to set starting angle right for the player
// float seekPlayer = prand(&mySpaceGlobals->seed)*2;
float difficulty = mySpaceGlobals->level/100.0;
float randVal = prand(&mySpaceGlobals->seed);
// set the enemy count (max enemies on screen at once) based on level
int enemyCount = 10 + difficulty*90*randVal;
if (enemyCount > 100) enemyCount = 100;
// set speed randomly within difficulty range
int speed = 3 + (difficulty)*12*randVal;
int startx, starty;
float theta = prand(&mySpaceGlobals->seed)*3.14159265;
randVal = prand(&mySpaceGlobals->seed);
// horiz size
if (side < 2)
{
startx = (side == 0)? 0 : xMaxBoundry;
starty = randVal*yMaxBoundry;
if (startx != 0)
theta -= 3.14159265;
}
else
{
starty = (side == 2)? 20 : yMaxBoundry;
startx = randVal*xMaxBoundry;
if (starty == 20)
theta -= 3.14159265/2;
else
theta += 3.14159265/2;
}
// seek directly to the player
if (mySpaceGlobals->enemiesSeekPlayer == 1)
{
float xdif = startx + 11 - (mySpaceGlobals->p1X + 18);
float ydif = starty + 11 - (mySpaceGlobals->p1Y + 18);
theta = my_atan2(xdif, ydif) - 3.14159265;
}
int xx;
for (xx=0; xx<enemyCount; xx++)
{
if (mySpaceGlobals->enemies[xx].position.active == 0)
{
mySpaceGlobals->enemies[xx].position.x = startx;
mySpaceGlobals->enemies[xx].position.y = starty;
mySpaceGlobals->enemies[xx].position.m_x = speed*my_sin(theta); // speed is the desired enemy speed
mySpaceGlobals->enemies[xx].position.m_y = speed*my_cos(theta); // we have to solve for the hypotenuese
mySpaceGlobals->enemies[xx].position.active = 1;
break;
}
}
}
void totallyRefreshState(struct SpaceGlobals *mySpaceGlobals)
{
initGameState(mySpaceGlobals);
mySpaceGlobals->displayHowToPlay = 0;
mySpaceGlobals->firstShotFired = 0;
mySpaceGlobals->lives = 3;
mySpaceGlobals->playerExplodeFrame = 0;
mySpaceGlobals->score = 0;
mySpaceGlobals->level = 0;
mySpaceGlobals->dontKeepTrackOfScore = 0;
mySpaceGlobals->noEnemies = 0;
mySpaceGlobals->enemiesSeekPlayer = 0;
}
void displayGameOver(struct SpaceGlobals *mySpaceGlobals)
{
if (mySpaceGlobals->invalid == 1)
{
blackout();
char gameover[255];
__os_snprintf(gameover, 255, "Game Over!");
drawString(25, 5, gameover);
drawStringTv(44, 10, gameover);
// only display score + pw if the player didn't use cheats
if (mySpaceGlobals->dontKeepTrackOfScore != 1)
{
char finalscore[255];
__os_snprintf(finalscore, 255, "Score: %08d", mySpaceGlobals->score);
char pass[255];
__os_snprintf(pass, 255, "Lv %d Password: %05d", mySpaceGlobals->level+1, mySpaceGlobals->passwordList[mySpaceGlobals->level]);
drawString(23, 7, finalscore);
drawStringTv(42, 12, finalscore);
drawString(21, 8, pass);
drawStringTv(40, 13, pass);
}