-
Notifications
You must be signed in to change notification settings - Fork 0
/
IKD.c
830 lines (775 loc) · 24 KB
/
IKD.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
/** \file IKD.c
* \brief A Uzebox remake of the tank game in Atari's Combat.
* \author Dan MacDonald
* BIG thanks to D3thAdd3r for his fantastic C64 Commando theme rendition!
* Score drawing code borrowed from Bradley Boccuzzi's Uzebox port of Pong.
* \date 2024
*/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <uzebox.h>
#include "data/patches.inc"
#include "data/tileset.inc"
#include "data/music-compressed.inc"
int tank1Prev = 0; // Previous button
int tank1Held = 0; // buttons that are held right now
int tank1Pressed = 0; // buttons that were pressed this frame
int tank1Released = 0; // buttons that were released this frame
int tank2Prev = 0;
int tank2Held = 0;
int tank2Pressed = 0;
int tank2Released = 0;
int seed, maze, nextX, nextY = 0;
float angles[] = {0, 23, 45, 68, 90, 113, 135, 158,
180, 203, 225, 248, 270, 293, 315, 338};
#define WALL_TILE 0x64
const char *tank1_sprites[16] = {tank1_000, tank1_023, tank1_045, tank1_068,
tank1_090, tank1_113, tank1_135, tank1_158,
tank1_180, tank1_203, tank1_225, tank1_248,
tank1_270, tank1_293, tank1_315, tank1_338};
const char *tank2_sprites[16] = {tank2_000, tank2_023, tank2_045, tank2_068,
tank2_090, tank2_113, tank2_135, tank2_158,
tank2_180, tank2_203, tank2_225, tank2_248,
tank2_270, tank2_293, tank2_315, tank2_338};
const char *tank1_current_sprite, *tank2_current_sprite;
unsigned char Score[2] = {0, 0};
unsigned char Tens[2] = {0, 0};
const unsigned char menu_opts[5] PROGMEM = {22, 23, 24, 25, 26};
const char *mazes[4] = {maze0, maze1, maze2, maze3};
const char *numbers[10] = {n0, n1, n2, n3, n4, n5, n6, n7, n8, n9};
const char *numbers2[10] = {sc0, sc1, sc2, sc3, sc4, sc5, sc6, sc7, sc8, sc9};
struct bulletStruct {
float x;
float y;
float vX;
float vY;
bool active;
int age;
int gridX;
int gridY;
int top; // Top edge of current bullet's current grid
int bottom;
int left;
int right;
int tside;
int rside;
int bside;
int lside;
int pitch;
};
struct bulletStruct p1_bullet, p2_bullet;
bool bounce = true;
struct tankStruct {
float top; // Top corner of the sprite in pixels
float bottom;
float left;
float right;
float vX;
float vY;
int angle; // Which direction is the tank facing? 0 = up/north, 4 = right/east
int x; // Current 8x8 pixel tile horizontal grid position, starting from the left
int y; // Vertical tile position, starting from the top of the screen
bool advance; // Can the tank move forward in its current location and angle?
};
struct tankStruct p1_tank, p2_tank;
void hyperTanks(void);
void initIKD(void);
void initMaze(void);
void processTrig(void);
void processBullets(void);
void processTank1(void);
void processTank1Forward(void);
void processTank2(void);
void processTank2Forward(void);
void processScore(void);
void cuzeboxCOut(char str[]);
void cuzeboxHOut(int num);
void wallTankCollision(int tankN, int tankX, int tankY, int tankAngle);
void drawMainMenu(void);
void processMainMenu(void);
int wallCheck(int gridX, int gridY, int side);
// cuzeboxCOut() is used to print debug strings to the cuzebox console.
void cuzeboxCOut(char str[]) {
for (int i = 0; str[i] != '\0'; i++) {
_SFR_IO8(0X1a)=str[i];
}
_SFR_IO8(0X1a)='\n'; // Add newline after last character
}
// cuzeboxHOut() is used to print a hex value to the cuzebox console.
// eg cuzeboxHOut(GetTile(2,8));
void cuzeboxHOut(int num) {
_SFR_IO8(25)=num;
_SFR_IO8(0X1a)='\n'; // Add newline
}
typedef enum {MAIN_MENU, GAME} state;
state game_state = MAIN_MENU; ///< Tracks current state of the game.
int main() {
while(1)
{
if(game_state == GAME)
{
// Basic prep work
initIKD();
// Reset scores
Score[0] = 0;
Score[1] = 0;
// Load maze
initMaze();
// Main loop
while (1) {
// wait until the next frame
WaitVsync(1);
seed++;
processTank1();
processTank2();
processBullets();
processScore();
if(!IsRunningInEmulator() && IsPowerSwitchPressed())
{
hyperTanks();
}
}
}
if(game_state == MAIN_MENU)
{
// Menu code here
initIKD();
if(!IsSongPlaying()){
StartSong(commando);/*TriggerFx(SFX_FIRE,255,1);WaitVsync(60);TriggerFx(SFX_EXPLODE,255,1);WaitVsync(60);TriggerFx(SFX_ENGINE,255,1);WaitVsync(60);TriggerFx(SFX_BOING,255,1);WaitVsync(60);*/}
drawMainMenu();
}
while(game_state == MAIN_MENU)
{
WaitVsync(1);
processMainMenu();
}
StopSong();
FadeOut(1,1);
FadeIn(3,0);
while(GetMasterVolume() > 16){
SetMasterVolume(GetMasterVolume()-16);
WaitVsync(1);
}
StopSong();
SetMasterVolume(IKD_MASTER_VOL);
}
}
void initIKD(void) {
InitMusicPlayer(patches);
SetMasterVolume(IKD_MASTER_VOL);
SetSpritesTileTable(tileset);
SetTileTable(tileset); // Tile set to use for ClearVram()
SetFontTilesIndex(0);
ClearVram(); // fill entire screen with first tile in the tileset
}
void processTank1(void) {
MoveSprite(0, p1_tank.left, p1_tank.top, 1, 1); // position tank 1 sprite
tank1Held = ReadJoypad(0); // read in our player one joypad input
tank1Pressed = tank1Held & (tank1Held ^ tank1Prev);
if (tank1Pressed & BTN_RIGHT) {
p1_tank.angle++; // move forward to next animation frame
if (p1_tank.angle == 16) {
p1_tank.angle = 0;
}
tank1_current_sprite = tank1_sprites[p1_tank.angle];
processTrig();
MapSprite2(0, tank1_current_sprite, 0);
wallTankCollision(0,p1_tank.x,p1_tank.y,p1_tank.angle);
}
if (tank1Pressed & BTN_LEFT) {
if (p1_tank.angle == 0) {
p1_tank.angle = 15;
} else {
p1_tank.angle--; // move back to previous animation frame
}
tank1_current_sprite = tank1_sprites[p1_tank.angle];
processTrig();
MapSprite2(0, tank1_current_sprite, 0);
wallTankCollision(0,p1_tank.x,p1_tank.y,p1_tank.angle);
}
if (tank1Pressed & BTN_A) {
srand((unsigned)seed);
if (p1_bullet.active == false) {
p1_bullet.age = 0;
p1_bullet.x = p1_tank.left;
p1_bullet.y = p1_tank.top;
p1_bullet.vX = p1_tank.vX;
p1_bullet.vY = p1_tank.vY;
p1_bullet.active = true;
p1_bullet.pitch = 75;
MapSprite2(1, bullet, 0); // map bullet
MoveSprite(1, p1_bullet.x, p1_bullet.y, 1, 1);
TriggerFx(SFX_FIRE, 0xFF, true);
}
}
if (tank1Held & BTN_UP) {
if (p1_tank.advance == true) {
processTank1Forward();
}
}
if (tank1Held & BTN_B) {
if (p1_tank.advance == true) {
processTank1Forward();
}
}
tank1Prev = tank1Held;
}
void processTank2(void) {
MoveSprite(2, p2_tank.left, p2_tank.top, 1, 1); // position tank 2 sprite
tank2Held = ReadJoypad(1); // read player 2 input
tank2Pressed = tank2Held & (tank2Held ^ tank2Prev);
if (tank2Pressed & BTN_RIGHT) {
p2_tank.angle++; // move forward to next animation frame
if (p2_tank.angle == 16) {
p2_tank.angle = 0;
}
tank2_current_sprite = tank2_sprites[p2_tank.angle];
processTrig();
MapSprite2(2, tank2_current_sprite, 0);
wallTankCollision(1,p2_tank.x,p2_tank.y,p2_tank.angle);
}
if (tank2Pressed & BTN_LEFT) {
if (p2_tank.angle == 0) {
p2_tank.angle = 15;
} else {
p2_tank.angle--; // move back to previous animation frame
}
tank2_current_sprite = tank2_sprites[p2_tank.angle];
processTrig();
MapSprite2(2, tank2_current_sprite, 0);
wallTankCollision(1,p2_tank.x,p2_tank.y,p2_tank.angle);
}
if (tank2Pressed & BTN_A) {
srand((unsigned)seed);
if (p2_bullet.active == false) {
p2_bullet.age = 0;
p2_bullet.x = p2_tank.left;
p2_bullet.y = p2_tank.top;
p2_bullet.vX = p2_tank.vX;
p2_bullet.vY = p2_tank.vY;
p2_bullet.active = true;
p2_bullet.pitch = 75;
MapSprite2(3, bullet, 0); // map bullet
MoveSprite(3, p2_bullet.x, p2_bullet.y, 1, 1);
TriggerFx(SFX_FIRE, 0xFF, true);
}
}
if (tank2Held & BTN_UP) {
if (p2_tank.advance == true) {
processTank2Forward();
}
}
if (tank2Held & BTN_B) {
if (p2_tank.advance == true) {
processTank2Forward();
}
}
tank2Prev = tank2Held;
}
void processBullets(void) {
if (p1_bullet.active == true && p1_bullet.age < 60) {
p1_bullet.age++;
p1_bullet.x += p1_bullet.vX * 3;
p1_bullet.y += p1_bullet.vY * 3;
p1_bullet.gridX = p1_bullet.x / 8;
p1_bullet.gridY = p1_bullet.y / 8;
p1_bullet.top = p1_bullet.gridY * 8;
p1_bullet.bottom = p1_bullet.top + 8;
p1_bullet.left = p1_bullet.gridX * 8;
p1_bullet.right = p1_bullet.left + 8;
p1_bullet.tside = wallCheck(p1_bullet.gridX, p1_bullet.gridY,0);
p1_bullet.rside = wallCheck(p1_bullet.gridX, p1_bullet.gridY,1);
p1_bullet.bside = wallCheck(p1_bullet.gridX, p1_bullet.gridY,2);
p1_bullet.lside = wallCheck(p1_bullet.gridX, p1_bullet.gridY,3);
MoveSprite(1, p1_bullet.x, p1_bullet.y, 1, 1);
if (p1_bullet.x >= p2_tank.left && p1_bullet.x <= p2_tank.right &&
p1_bullet.y >= p2_tank.top && p1_bullet.y <= p2_tank.bottom) {
p1_bullet.active = false;
MapSprite2(1, blank, 0);
TriggerFx(SFX_EXPLODE, 0xFF, true);
Score[0]++;
if (Score[0] > 9) {
Tens[0]++;
Score[0] = 0;
}
p2_tank.x = rand() % 27;
p2_tank.y = rand() % 21;
// This loop stops the tank respawning on a wall
while (GetTile(p2_tank.x, p2_tank.y) == WALL_TILE) {
p2_tank.x = rand() % 27;
p2_tank.y = rand() % 21;
}
p2_tank.left = p2_tank.x * 8;
p2_tank.top = p2_tank.y * 8;
p2_tank.right = p2_tank.left + 8;
p2_tank.bottom = p2_tank.top + 8;
p2_tank.angle = rand() % 15;
tank2_current_sprite = tank2_sprites[p2_tank.angle];
processTrig();
MapSprite2(2, tank2_current_sprite, 0);
p2_bullet.active = false;
p2_bullet.age = 0;
}
else if (p1_bullet.y <= 8 || p1_bullet.y >= 168) {
if (bounce == true) {
p1_bullet.vY = p1_bullet.vY * -1;
TriggerNote(2, 38, p1_bullet.pitch, 127);
p1_bullet.pitch++;
}
else {
p1_bullet.active = false;
MapSprite2(1, blank, 0);
}
}
else if (p1_bullet.x >= (p1_bullet.right - 2)) {
if (p1_bullet.rside == 1) {
if (bounce == true) {
p1_bullet.vX = p1_bullet.vX * -1;
TriggerNote(2, 38, p1_bullet.pitch, 127);
p1_bullet.pitch++;
}
else {
p1_bullet.active = false;
MapSprite2(1, blank, 0);
}
}
}
else if (p1_bullet.x <= (p1_bullet.left + 2)) {
if (p1_bullet.lside == 1) {
if (bounce == true) {
p1_bullet.vX = p1_bullet.vX * -1;
TriggerNote(2, 38, p1_bullet.pitch, 127);
p1_bullet.pitch++;
}
else {
p1_bullet.active = false;
MapSprite2(1, blank, 0);
}
}
}
else if (p1_bullet.y <= (p1_bullet.top + 2)) {
if (p1_bullet.tside == 1) {
if (bounce == true) {
p1_bullet.vY = p1_bullet.vY * -1;
TriggerNote(2, 38, p1_bullet.pitch, 127);
p1_bullet.pitch++;
}
else {
p1_bullet.active = false;
MapSprite2(1, blank, 0);
}
}
}
else if (p1_bullet.y >= (p1_bullet.bottom - 2)) {
if (p1_bullet.bside == 1) {
if (bounce == true) {
p1_bullet.vY = p1_bullet.vY * -1;
TriggerNote(2, 38, p1_bullet.pitch, 127);
p1_bullet.pitch++;
}
else {
p1_bullet.active = false;
MapSprite2(1, blank, 0);
}
}
}
} else {
p1_bullet.active = false;
MapSprite2(1, blank, 0);
}
if (p2_bullet.active == true && p2_bullet.age < 60) {
p2_bullet.age++;
p2_bullet.x += p2_bullet.vX * 3;
p2_bullet.y += p2_bullet.vY * 3;
p2_bullet.gridX = p2_bullet.x / 8;
p2_bullet.gridY = p2_bullet.y / 8;
p2_bullet.top = p2_bullet.gridY * 8;
p2_bullet.bottom = p2_bullet.top + 8;
p2_bullet.left = p2_bullet.gridX * 8;
p2_bullet.right = p2_bullet.left + 8;
p2_bullet.tside = wallCheck(p2_bullet.gridX, p2_bullet.gridY,0);
p2_bullet.rside = wallCheck(p2_bullet.gridX, p2_bullet.gridY,1);
p2_bullet.bside = wallCheck(p2_bullet.gridX, p2_bullet.gridY,2);
p2_bullet.lside = wallCheck(p2_bullet.gridX, p2_bullet.gridY,3);
MoveSprite(3, p2_bullet.x, p2_bullet.y, 1, 1);
if (p2_bullet.x >= p1_tank.left && p2_bullet.x <= p1_tank.right &&
p2_bullet.y >= p1_tank.top && p2_bullet.y <= p1_tank.bottom) {
p2_bullet.active = false;
MapSprite2(3, blank, 0);
TriggerFx(SFX_EXPLODE, 0xFF, true);
Score[1]++;
if (Score[1] > 9) {
Tens[1]++;
Score[1] = 0;
}
p1_tank.x = rand() % 27;
p1_tank.y = rand() % 21;
while (GetTile(p1_tank.x, p1_tank.y) == WALL_TILE) {
p1_tank.x = rand() % 27;
p1_tank.y = rand() % 21;
}
p1_tank.left = p1_tank.x * 8;
p1_tank.top = p1_tank.y * 8;
p1_tank.right = p1_tank.left + 8;
p1_tank.bottom = p1_tank.top + 8;
p1_tank.angle = rand() % 15;
tank1_current_sprite = tank1_sprites[p1_tank.angle];
processTrig();
MapSprite2(0, tank1_current_sprite, 0);
p1_bullet.active = false;
p1_bullet.age = 0;
}
else if (p2_bullet.y <= 8 || p2_bullet.y >= 168) {
if (bounce == true) {
p2_bullet.vY = p2_bullet.vY * -1;
TriggerNote(2, 38, p2_bullet.pitch, 127);
p2_bullet.pitch++;
}
else {
p2_bullet.active = false;
MapSprite2(3, blank, 0);
}
}
else if (p2_bullet.x >= (p2_bullet.right - 2)) {
if (p2_bullet.rside == 1) {
if (bounce == true) {
p2_bullet.vX = p2_bullet.vX * -1;
TriggerNote(2, 38, p2_bullet.pitch, 127);
p2_bullet.pitch++;
}
else {
p2_bullet.active = false;
MapSprite2(3, blank, 0);
}
}
}
else if (p2_bullet.x <= (p2_bullet.left + 2)) {
if (p2_bullet.lside == 1) {
if (bounce == true) {
p2_bullet.vX = p2_bullet.vX * -1;
TriggerNote(2, 38, p2_bullet.pitch, 127);
p2_bullet.pitch++;
}
else {
p2_bullet.active = false;
MapSprite2(3, blank, 0);
}
}
}
else if (p2_bullet.y <= (p2_bullet.top + 2)) {
if (p2_bullet.tside == 1) {
if (bounce == true) {
p2_bullet.vY = p2_bullet.vY * -1;
TriggerNote(2, 38, p2_bullet.pitch, 127);
p2_bullet.pitch++;
}
else {
p2_bullet.active = false;
MapSprite2(3, blank, 0);
}
}
}
else if (p2_bullet.y >= (p2_bullet.bottom - 2)) {
if (p2_bullet.bside == 1) {
if (bounce == true) {
p2_bullet.vY = p2_bullet.vY * -1;
TriggerNote(2, 38, p2_bullet.pitch, 127);
p2_bullet.pitch++;
}
else {
p2_bullet.active = false;
MapSprite2(3, blank, 0);
}
}
}
} else {
p2_bullet.active = false;
MapSprite2(3, blank, 0);
}
}
void processTrig(void) {
p1_tank.vX = sin(2 * M_PI * (angles[p1_tank.angle] / 360));
p1_tank.vY = -cos(2 * M_PI * (angles[p1_tank.angle] / 360));
p2_tank.vX = sin(2 * M_PI * (angles[p2_tank.angle] / 360));
p2_tank.vY = -cos(2 * M_PI * (angles[p2_tank.angle] / 360));
}
void processScore(void) {
DrawMap2(6, 22, (numbers[Score[0]]));
DrawMap2(18, 22, (numbers2[Score[1]]));
PrintHexInt(1,24,(Tens[0]));
PrintHexInt(23,24,(Tens[1]));
}
void processTank1Forward(void) {
p1_tank.left += p1_tank.vX / 2;
p1_tank.top += p1_tank.vY / 2;
p1_tank.right = p1_tank.left + 8;
p1_tank.bottom = p1_tank.top + 8;
MoveSprite(0, p1_tank.left, p1_tank.top, 1, 1);
// Update tanks position on the tile grid
p1_tank.x = p1_tank.left / 8;
p1_tank.y = p1_tank.top / 8;
wallTankCollision(0,p1_tank.x,p1_tank.y,p1_tank.angle);
TriggerNote(2, 2, 20, 127);
}
void processTank2Forward(void) {
p2_tank.left += p2_tank.vX / 2;
p2_tank.top += p2_tank.vY / 2;
p2_tank.right = p2_tank.left + 8;
p2_tank.bottom = p2_tank.top + 8;
MoveSprite(2, p2_tank.left, p2_tank.top, 1, 1);
// Update tank 2's position on the tile grid
p2_tank.x = p2_tank.left / 8;
p2_tank.y = p2_tank.top / 8;
wallTankCollision(1,p2_tank.x,p2_tank.y,p2_tank.angle);
TriggerNote(1, 2, 20, 127);
}
void initMaze(void) {
DrawMap2(0, 0, mazes[maze]);
MapSprite2(0, tank1_090, 0); // setup tank 1 for drawing
p1_tank.left = 8; // set tank to the left
p1_tank.top = 80; // center tank vertically
p1_tank.right = 13;
p1_tank.bottom = 93;
p1_tank.angle = 4; // face right
p1_tank.x = 1;
p1_tank.y = 10;
p1_tank.vX = 1;
p1_tank.vY = 0;
p1_tank.advance = true;
p1_bullet.vX = 1;
p1_bullet.vY = 0;
p1_bullet.active = false;
p1_bullet.age = 0;
p1_bullet.gridX = 1;
p1_bullet.gridY = 10;
p1_bullet.top = 8;
p1_bullet.bottom = 16;
p1_bullet.left = 8;
p1_bullet.right = 16;
p1_bullet.tside = wallCheck(p1_bullet.gridX, p1_bullet.gridY,0);
p1_bullet.bside = wallCheck(p1_bullet.gridX, p1_bullet.gridY,2);
p1_bullet.lside = wallCheck(p1_bullet.gridX, p1_bullet.gridY,3);
p1_bullet.rside = wallCheck(p1_bullet.gridX, p1_bullet.gridY,1);
MapSprite2(2, tank2_270, 0); // setup tank 2 for drawing
p2_tank.left = 208; // set tank to the right
p2_tank.top = 80; // center tank vertically
p2_tank.right = 210;
p2_tank.bottom = 93;
p2_tank.angle = 12; // face left
p2_tank.x = 26;
p2_tank.y = 10;
p2_tank.vX = -1;
p2_tank.vY = 0;
p2_tank.advance = true;
p2_bullet.vX = -1;
p2_bullet.vY = 0;
p2_bullet.active = false;
p2_bullet.age = 0;
p2_bullet.gridX = 25;
p2_bullet.gridY = 10;
p2_bullet.top = p2_bullet.gridY * 8;
p2_bullet.bottom = p2_bullet.top + 8;
p2_bullet.left = p2_bullet.gridX * 8;
p2_bullet.right = p2_bullet.left + 8;
p2_bullet.tside = wallCheck(p2_bullet.gridX, p2_bullet.gridY,0);
p2_bullet.rside = wallCheck(p2_bullet.gridX, p2_bullet.gridY,1);
p2_bullet.bside = wallCheck(p2_bullet.gridX, p2_bullet.gridY,2);
p2_bullet.lside = wallCheck(p2_bullet.gridX, p2_bullet.gridY,3);
}
void drawMainMenu()
{
ClearVram();
Print(2,0,PSTR("A TRIBUTE TO ATARI'S COMBAT"));
Print(13,2,PSTR("V1.1"));
DrawMap2(8,4,title_map);
Print(7,18,PSTR("BY DAN MACDONALD"));
Print(10,22,PSTR("NO MAZE"));
Print(10,23,PSTR("MAZE #1"));
Print(10,24,PSTR("MAZE #2"));
Print(10,25,PSTR("MAZE #3"));
if (bounce == true) {
Print(10,26,PSTR("BOUNCE ON"));
}
else {
Print(10,26,PSTR("BOUNCE OFF"));
}
SetTile(7,pgm_read_byte(&menu_opts[maze]),101);
}
void processMainMenu()
{
tank1Held = ReadJoypad(0); // read in our player one joypad input
//pressing something and it isn't the same buttons as last frame so it's a new button press, not a hold
if (tank1Held!=tank1Prev) {
if (tank1Held & BTN_DOWN) {
maze++;
TriggerNote(1, 38, 75, 127);
if (maze > 4) {
maze = 0;
}
drawMainMenu();
}
if (tank1Held & BTN_UP) {
maze--;
TriggerNote(1, 38, 75, 127);
if (maze < 0) {
maze = 4;
}
drawMainMenu();
}
if (tank1Held & BTN_LEFT) {
if (maze == 4) {
bounce = !bounce;
TriggerNote(1, 38, 75, 127);
}
drawMainMenu();
}
if (tank1Held & BTN_RIGHT) {
if (maze == 4) {
bounce = !bounce;
TriggerNote(1, 38, 75, 127);
}
drawMainMenu();
}
if (tank1Held & BTN_START) {
if (maze != 4) {
game_state = GAME;
}
}
tank1Prev = tank1Held;
}
}
// To implement bouncy bullets, we need a quick and easy way to check if a
// tile next to the current one contains a wall tile, hence wallCheck():
int wallCheck(int gridX, int gridY, int side) {
if (side == 0) { // Check the grid location above for a wall tile
if (gridY <= 0) {
return 0;
}
else if (GetTile(gridX, (gridY - 1)) == WALL_TILE) {
return 1;
}
else {
return 0;
}
}
else if (side == 1) { // Check grid to the right for a wall tile
if (gridX >= 28) {
return 0;
}
else if (GetTile((gridX + 1), gridY) == WALL_TILE) {
return 1;
}
else {
return 0;
}
}
else if (side == 2) { // Check grid location below for a wall tile
if (gridY >= 22) {
return 0;
}
else if (GetTile(gridX, (gridY + 1)) == WALL_TILE) {
return 1;
}
else {
return 0;
}
}
else if (side == 3) { // Check grid to the left for a wall tile
if (gridX <= 0) {
return 0;
}
else if (GetTile((gridX - 1), gridY) == WALL_TILE) {
return 1;
}
else {
return 0;
}
}
else {
return 0;
}
}
void wallTankCollision(int tankN, int tankX, int tankY, int tankAngle) {
// Calculate the next position based on the tank's angle
if (tankAngle == 0) {
nextX = tankX;
nextY = tankY;
} else if (tankAngle == 1 || tankAngle == 2 || tankAngle == 3) {
nextX = tankX + 1;
nextY = tankY - 1;
} else if (tankAngle == 4) {
nextX = tankX + 1;
nextY = tankY;
} else if (tankAngle == 5 || tankAngle == 6 || tankAngle == 7) {
nextX = tankX + 1;
nextY = tankY + 1;
} else if (tankAngle == 8) {
nextX = tankX;
nextY = tankY + 1;
} else if (tankAngle == 9 || tankAngle == 10 || tankAngle == 11) {
nextX = tankX - 1;
nextY = tankY + 1;
} else if (tankAngle == 12) {
nextX = tankX;
nextY = tankY;
} else if (tankAngle == 13 || tankAngle == 14 || tankAngle == 15) {
nextX = tankX - 1;
nextY = tankY - 1;
}
// Check if the next tank position is occupied by a wall
if (GetTile(nextX, nextY) == WALL_TILE) {
if (tankN == 0) {
p1_tank.advance = false;
}
else if (tankN == 1) {
p2_tank.advance = false;
}
}
if (GetTile(nextX, nextY) == 0x0) {
if (tankN == 0) {
p1_tank.advance = true;
}
else if (tankN == 1) {
p2_tank.advance = true;
}
}
}
void hyperTanks(void) {
//Hyper tank 2
p2_tank.x = rand() % 27;
p2_tank.y = rand() % 21;
while (GetTile(p2_tank.x, p2_tank.y) == WALL_TILE) {
p2_tank.x = rand() % 27;
p2_tank.y = rand() % 21;
}
p2_tank.left = p2_tank.x * 8;
p2_tank.top = p2_tank.y * 8;
p2_tank.right = p2_tank.left + 8;
p2_tank.bottom = p2_tank.top + 8;
p2_tank.angle = rand() % 15;
tank2_current_sprite = tank2_sprites[p2_tank.angle];
processTrig();
MapSprite2(2, tank2_current_sprite, 0);
p2_bullet.active = false;
p2_bullet.age = 0;
//Hyper tank 1
p1_tank.x = rand() % 27;
p1_tank.y = rand() % 21;
while (GetTile(p1_tank.x, p1_tank.y) == WALL_TILE) {
p1_tank.x = rand() % 27;
p1_tank.y = rand() % 21;
}
p1_tank.left = p1_tank.x * 8;
p1_tank.top = p1_tank.y * 8;
p1_tank.right = p1_tank.left + 8;
p1_tank.bottom = p1_tank.top + 8;
p1_tank.angle = rand() % 15;
tank1_current_sprite = tank1_sprites[p1_tank.angle];
processTrig();
MapSprite2(0, tank1_current_sprite, 0);
p1_bullet.active = false;
p1_bullet.age = 0;
}