-
Notifications
You must be signed in to change notification settings - Fork 0
/
astroattack.asm
2155 lines (1856 loc) · 44.9 KB
/
astroattack.asm
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
;*******************************************************
;*_____________________________________________________*
;* ASTRO ATTACK *
;* a Gameboy Game by *
;* FISCH03 *
;*_____________________________________________________*
;* *
;* *
;* NOTE: These files won't compile because the *
;* EasterEgg files are missing. *
;* In that case, change the USEEASTEREGG Constant to 0 *
;* *
;* Please consult the README for more Information *
;*******************************************************
USEEASTEREGG EQU 1 ;Change this to 0 if you got these files without the EasterEgg files
;-----------------PRE HEADER IMPORTS-------------------
;------------------------------------------------------
INCLUDE "src/gbhw.inc" ;Hardware Definitions
INCLUDE "src/ibmpc1.inc" ;ibmpc1 ACII text
;-----------------CONSTANTS-----------------
;-------------------------------------------
;__Game Rules__
MaxHealth EQU $05 ;Maximum Health / Starting Health
PointSize EQU $06 ;Number of Digits in the Point Display
AsteroidLifetime EQU $40 ;How long an Asteroid should last
DashDistance EQU 6
TilesPerFrame EQU 16 ;How many Tiles we Update per Frame (See UpdateCycle Variable)
ShakeDuration EQU 10 ;How long a Screenshake should Last
ShakeIntensity EQU 2
StartTextSpeed EQU 50 ;How fast the "Press Start" Text should Blink (smaller=faster)
PMinX EQU $28 ;Minimum X the Player can have
PMaxX EQU $80 ;Maximum X the Player can have
PMinY EQU $10 ;Minimum Y the Player can have
PMaxY EQU $68 ;Maximum Y the Player can have
FramesPerBeat EQU 26 ;~140 BPM
;__Data__
;X Coordinates of all Squares
X1 EQU $28
X2 EQU $38
X3 EQU $48
X4 EQU $58
X5 EQU $68
X6 EQU $78
;Y Coordinates of all Squares
Y1 EQU $10
Y2 EQU $20
Y3 EQU $30
Y4 EQU $40
Y5 EQU $50
Y6 EQU $60
;X Coordinates of all Square Tiles
X1T EQU $04
X2T EQU $06
X3T EQU $08
X4T EQU $0A
X5T EQU $0C
X6T EQU $0E
;Y Coordinates of all Square Tiles
Y1T EQU SCRN_VY_B*0
Y2T EQU SCRN_VY_B*2
Y3T EQU SCRN_VY_B*4
Y4T EQU SCRN_VY_B*6
Y5T EQU SCRN_VY_B*8
Y6T EQU SCRN_VY_B*10
;OAM Location offsets
OAMY EQU 0
OAMX EQU 1
OAMPatt EQU 2
OAMFlag EQU 3
PlayerOAM EQU 0*4
RocketOAM EQU 1*4
Bomb1OAM EQU 5*4
Bomb2OAM EQU 9*4
Bomb3OAM EQU 13*4
;__Cheats__
Invincible EQU 0
NoSpeedUp EQU 1
;__Other__
CharsetSize EQU 16*96
;-----------------HEADER AND INTERRUPTS--------------------
;----------------------------------------------------------
;Interrupts
SECTION "Vblank", ROM0[$0040] ;Interrupt at VBlank
jp $FF80
SECTION "LCDC", ROM0[$0048]
IF USEEASTEREGG
jp EEFX
ELSE
jp HBlankI
ENDC
SECTION "Timer_Overflow", ROM0[$0050] ;Interrupt at Timer Overflow
jp IncreaseTick ;Update the Tick Value
SECTION "Serial", ROM0[$0058]
reti
SECTION "p1thru4", ROM0[$0060] ;Joypad Interrupt
reti
;-----------------VARIABLES--------------------
;----------------------------------------------
SECTION "variables",WRAM0
Variables:
;__GAME__
;DMA
OamData: ds 40*4 ;Mirror of the OamData in Memory location $FE00-$FE9F, used for DMA transfers
VBlankF: ds 1 ;This is set when a DMA Transfer occurs, and when the Gameboy wakes up after a halt used to check if it was because of a VBlank Interrupt
;POINTS AND HEALTH
Points: ds PointSize ;1 Byte for each Digit of the Points Display
Health: ds 1
;BOMBS
Bombs: ds 1 ;How many Bombs the Player has
BombActive: ds 1
;PLAYER LOCATION AND MOVEMENT
PSI: ds 1 ;Index Number of the Square the Player is on
PSX: ds 1 ;X Coordinate of the Square the Player is on
PSY: ds 1 ;Y Coordinate of the Square the Player is on
PlayerOnAsteroid: ds 1
MoveDistance: ds 1 ;Saves how many Steps the Player moves every Frame
AllowDash: ds 1
;RNG
LFSRSeed: ds 1 ;Seed for the RNG
;TIMING
TimerTicks: ds 1 ;Increases each Time the Timer Overflows
TimerTicksDiv: ds 1 ;Increases each Time, TimerTicks gets Reset
SpawnAsteroidF: ds 1 ;If this is set, the Main Loop should Spawn a new Asteroid
UpdateAsteroidF: ds 1 ;If this is set, the Main Loop should update the Asteroids Stage
DrawAsteroidF: ds 1 ;If this is set, the Main Loop should update the Asteroids on Screen
SpawnRocketF: ds 1 ;If this is set, the Main Loop should spawn a new Rocket
;SCREEN EFFECTS
ScreenShakeF: ds 1 ;If this is Higher than 0, we shake the Screen
RollingLine: ds 1
;MUSIC
BeatProgress: ds 1 ;Gets incremented every Frame, at 140bpm one Beat takes ~26 Frames
CurrentBeat: ds 1
Beatx2: ds 1
Beatx4: ds 1
CH1Note: ds 1
CH2Note: ds 1
CH3Note: ds 1
;ASTEROID AND ROCKET INFORMATION
UpdateCycle: ds 1 ;The Gameboy can't update all the Tiles at once, so we only Update a few every Frame. This Variable saves what Tile we are on
AsteroidLocations: ds 36 ;A list of all Squares and their Status
RocketInfo: ds 1 ;Status of the Rocket
RocketY: ds 1
;__MENU__
InGame: ds 1
;TITLE
StartTextBlinkTime: ds 1 ;A simulated Timer to blink the "Press Start" Text on and off
StartTextStatus: ds 1 ;Whether the Text is on or off
;SPLASH
HeartDir: ds 1
LogoOffset: ds 1
OffsetDirection: ds 1
;Pause Screen
Paused: ds 1
VariablesEnd:
SECTION "start", ROM0[$0100]
nop
jp Start ;Jump to begin flag
ROM_HEADER ROM_NOMBC, ROM_SIZE_32KBYTE, RAM_SIZE_0KBYTE ;Rom Header
TileData:
chr_IBMPC1 2,4 ;Load Charset
;-----------------PRE INIT IMPORTS-------------------
;----------------------------------------------------
INCLUDE "src/memory.asm" ;Memory Operations
INCLUDE "src/Tiles.z80" ;Tileset
INCLUDE "src/Background.z80" ;Background Tilemap
INCLUDE "src/Title.z80" ;Same for Title Screen
INCLUDE "src/TitleBG.z80"
IF USEEASTEREGG
INCLUDE "src/EasterEggs.inc" ;The File containing all Code for Eastereggs, you wont find it on Github for obvious reasons ;D
ENDC
;-----------------INIT--------------------
;-----------------------------------------
Start: ;Program Start
nop
di ;Disable Interrupts to prevent write errors
call setup_dma ;Copy the DMA Routine into HRAM
ld sp, $ffff ;Set Stack Pointer to last Memory Address
ld a, STATF_MODE00
ld [rSTAT], a
AnimateNintendoLogo:
ld a, $00
ld [LogoOffset], a
ld [OffsetDirection], a
ld [InGame], a
IF USEEASTEREGG
call EEEarlyInit
ENDC
ld a, IEF_LCDC ;Enable the VBlank Interrupt
ld [rIE], a
ei
.loop:
call WaitVblankOld
call WaitVblankOld
ld a, [LogoOffset]
inc a
ld [LogoOffset], a
cp $80
jp nz, .loop
di
call TurnLCDOff
;Set Scroll registers to 0 (Right Corner)
ld a, $00
ld [rSCX], a
ld [rSCY], a
ld a, %11100100 ;Load Color Palette (11 10 01 00)
ld [rBGP], a ;Save it into $ff47
;Set Sprite Palletes
ldh [rOBP0], a
ldh [rOBP1], a
;Enable Sound
ld a, $80
ld [rAUDENA], a
;Start the timer at the Highest speed possible
ld a, TACF_START | TACF_262KHZ
ld [rTAC], a
InitSound:
;Load the Saw Waveform into the Wavetable RAm
ld hl, SawWave
ld de, _AUD3WAVERAM
ld bc, 16
call mem_Copy
InitVariables:
;Clear Variables
ld a, 0
ld hl, Variables
ld bc, VariablesEnd-Variables
call mem_Set
IF USEEASTEREGG
call InitEE
ENDC
;Init Health
ld a, MaxHealth
ld [Health], a
;Init MoveDistance
ld a, $01
ld [MoveDistance], a
ld a, $03
ld [Bombs], a
InitVRAM: ;Load all Necessary Data from ROMto VRAM
;Clear OAM
ld a, 0
ld hl, _OAMRAM
ld bc, $9F
call mem_Set
;Clear Background
ld a, $00
ld hl, _SCRN0
ld bc, SCRN_VX_B * SCRN_VY_B
call mem_SetVRAM
ld hl, TileData ;Load the Charset into VRAM
ld de, _VRAM
ld bc, 8*96 ;8 Bytes * 96 Chars
call mem_CopyMono
;Load Game Tiles
ld hl, Tiles
ld de, _VRAM+CharsetSize ;Get VRAM Location
ld bc, 16*55;16 Bytes per Tile
call mem_CopyVRAM ;Copy Tileset into VRAM
;Load Title Tiles
ld hl, Title ;Load the Title Tiles into VRAM
ld de, _VRAM+CharsetSize+16*55 ;Load Tiles after the Charset and Game Tiles
ld bc, TitletilesSize ;Size of Tiles
call mem_CopyVRAM
ld hl, MadewithText
ld de, _SCRN0+(SCRN_VY_B*4)+5
ld bc, MadewithTextEnd-MadewithText
call mem_CopyVRAM
ld hl, ByFisch03Text
ld de, _SCRN0+(SCRN_VY_B*11)+5
ld bc, ByFisch03TextEnd-ByFisch03Text
call mem_CopyVRAM
;Init Heart
ld hl, Heart
ld de, OamData
ld bc, HeartEnd-Heart
call mem_Copy
;Copy the Playing Field into SCRN1, this is only used for the Pause Screen. Since SCRN1 is only used for this, we do it earlier to decrease Loading Time
ld hl, Background
ld de, _SCRN1
ld bc, SCRN_VX_B * SCRN_VY_B
call mem_CopyVRAM
ld hl, PointsText
ld de, _SCRN1+(SCRN_VY_B*14)+2
ld bc, PointsTextEnd-PointsText
call mem_CopyVRAM
ld hl, HealthText
ld de, _SCRN1+(SCRN_VY_B*15)+2
ld bc, HealthTextEnd-HealthText
call mem_CopyVRAM
;Add a "Paused Text"
ld hl, PauseText
ld de, _SCRN1+(SCRN_VY_B*5)+7
ld bc, PauseTextEnd-PauseText
call mem_CopyVRAM
;Turn Screen on again
ld a, LCDCF_ON|LCDCF_BG8000|LCDCF_BG9800|LCDCF_OBJ8|LCDCF_OBJON|LCDCF_BGON
ld [rLCDC], a
ld a, IEF_VBLANK ;Enable the VBlank Interrupt
ld [rIE], a
ei
;-----------------SPLASH TEXT---------------------
;-------------------------------------------------
ld a, $00
Splash:
push af
call WaitVblank
pop af
inc a
cp 100
jr nz, Splash
ld a, $74
ld [rNR10], a
ld a, $7F
ld [rNR11], a
ld a, $F2
ld [rNR12], a
ld a, $11
ld [rNR13],a
ld a, $85
ld [rNR14], a
Splash_up:
call WaitVblank
ld a, [OamData]
sub a, $02
ld [OamData], a
cp $4A-$A
jr nz, Splash_up
Splash_down:
call WaitVblank
ld a, [OamData]
add a, $02
ld [OamData], a
cp $4A
jr nz, Splash_down
REPT 30
call WaitVblank
ENDR
;-----------------TITLE SCREEN--------------------
;-------------------------------------------------
InitTitle:
call TurnLCDOff
di
ld a, $00
ld [OamData], a
ld hl, Titlebg ;Load the Title Background Map into VRAM
ld de, _SCRN0
ld bc, SCRN_VX_B * SCRN_VY_B
call mem_CopyVRAM
ld hl, StartText
ld de, _SCRN0+(SCRN_VY_B*17)+4
ld bc, StartTextEnd-StartText
call mem_CopyVRAM
;Turn Screen on again
ld a, LCDCF_ON|LCDCF_BG8000|LCDCF_BG9800|LCDCF_OBJ8|LCDCF_OBJON|LCDCF_BGON
ld [rLCDC], a
;jp EasterEggStart
;Start a Kick Drum on CH1
ld hl, CH1Kick
ld de, rNR10
ld bc, $5
call mem_Copy
;Init CH2 and CH3
ld a, $FF
ld [rNR30], a
ld a, $80
ld [rNR21], a
ld [rNR31], a
ld a, $50
ld [rNR22], a
ld a, $40
ld [rNR32], a
;Play the first Notes on CH2
ld a, [CH2NotesA]
ld [rNR23], a
ld a, [CH2NotesB]
ld [rNR24], a
ld a, [CH3NotesA]
ld [rNR33], a
ld a, [CH3NotesB]
ld [rNR34], a
ei
TitleScreen:
call Music
IF USEEASTEREGG
call EasterEgg
ENDC
ld a, [StartTextBlinkTime]
cp StartTextSpeed
jr z, blinktext
inc a
ld [StartTextBlinkTime], a
blinktext_return:
call WaitVblank
ld a, [StartTextStatus]
cp $00
jr z, textoff
ld hl, StartText
ld de, _SCRN0+(SCRN_VY_B*17)+4
ld bc, StartTextEnd-StartText
call mem_CopyVRAM
textoff_return:
ld a, P1F_4
ld [rP1], a
REPT 6
ld a, [rP1] ;Read Keypresses, we are doing this multiple Times to reduce Noise
ENDR
and $08
cp $08
jr z, TitleScreen
.waitforrelease:
call WaitVblank
call Music
ld a, P1F_4 ;Select Buttons
ld [rP1], a
REPT 6
ld a, [rP1] ;Read Keypresses, we are doing this multiple Times to reduce Noise
ENDR
and $08
cp $08
jr nz, .waitforrelease
jr Game_Init
blinktext:
ld a, $00
ld [StartTextBlinkTime], a
ld a, [StartTextStatus]
xor $01
ld [StartTextStatus], a
jr blinktext_return
textoff:
ld a, $00
ld hl, _SCRN0+(SCRN_VY_B*17)+4
ld bc, StartTextEnd-StartText
call mem_SetVRAM
jr textoff_return
;-----------------GAME INIT--------------------
;----------------------------------------------
Game_Init:
di ;Disable Interrupts again to prevent any Write errors
call TurnLCDOff
;Load Tiles
ld hl, Tiles
ld de, _VRAM+16*96 ;Get VRAM Location
ld bc, 16*43;16 Bytes per Tile
call mem_CopyVRAM ;Copy Tileset into VRAM
;Draw Background
ld hl, Background
ld de, _SCRN0
ld bc, SCRN_VX_B * SCRN_VY_B
call mem_CopyVRAM
;Display the Points Text
ld hl, PointsText
ld de, _SCRN0+(SCRN_VY_B*14)+2
ld bc, PointsTextEnd-PointsText
call mem_CopyVRAM
;Display the Health Text
ld hl, HealthText
ld de, _SCRN0+(SCRN_VY_B*15)+2
ld bc, HealthTextEnd-HealthText
call mem_CopyVRAM
;Init Player
ld hl, Player
ld de, OamData
ld bc, PlayerEnd-Player
call mem_Copy
;Init Bombs
ld hl, Bomb1
ld de, OamData+Bomb1OAM
ld bc, Bomb1End-Bomb1
call mem_Copy
ld hl, Bomb2
ld de, OamData+Bomb2OAM
ld bc, Bomb2End-Bomb2
call mem_Copy
ld hl, Bomb3
ld de, OamData+Bomb3OAM
ld bc, Bomb3End-Bomb3
call mem_Copy
ld a, OAMF_PAL0
ld [OamData+ RocketOAM+ OAMFlag], a
ld [OamData+ RocketOAM+4+ OAMFlag], a
ld [OamData+ RocketOAM+8+ OAMFlag], a
ld [OamData+ RocketOAM+12+ OAMFlag], a
;Turn Screen on again
ld a, LCDCF_ON|LCDCF_BG8000|LCDCF_BG9800|LCDCF_OBJ8|LCDCF_OBJON|LCDCF_BGON
ld [rLCDC], a
ld a, $01
ld [InGame], a
;Get the Time passed, and save it into the RNG Seed
ld a, [rTIMA]
ld [LFSRSeed], a
;Start the Timer at a slower Frequency and Reset it
ld a, TACF_START | TACF_4KHZ
ld [rTAC], a
ld a, $00
ld [rTIMA], a
;Enable Timer and VBlank Interrupts
ld a, IEF_VBLANK | IEF_TIMER
ld [rIE], a
ei ;Turn Interrupts back on again
call WaitVblank ;Wait for VBlank before starting the Main Loop to make sure the Background has been drawn
;-----------------MAIN GAME--------------------
;----------------------------------------------
MainLoop: ;Main Game Loop
call Music ;Play the Music
call CheckPauseScreen
call GetPlayerSquare ;Get the Number of the Square the Player is standing on
call UpdateAsteroids ;Update all Asteroids if requested
call SpawnAsteroid ;Spawn a new Asteroid if requested
call UpdateRocket
call SpawnRocket
call TakeDamage ;Take Damage if hit
call ReadJoypad ;Read Joypad presses, calculate Collisions and Move the Player
call FixPoints ;Fix the Point Bytes to keep them in Decimal Range
call WaitVblank ;Wait for VBlank to allow VRAM Modifications
call DisplayFX
call DisplayAsteroids
call DisplayHealth ;Update Healthbar
call DisplayPoints ;Update Points
jp MainLoop
PauseLoop:
REPT 2
call WaitVblank
call Music
call CheckMainLoop
ENDR
ld a, [RollingLine]
inc a
ld [RollingLine], a
jp PauseLoop
;-----------------DATA FUNCTIONS--------------------
;---------------------------------------------------
CheckPauseScreen:
ld a, P1F_4 ;Select Buttons
ld [rP1], a
REPT 6
ld a, [rP1] ;Read Keypresses, we are doing this multiple Times to reduce Noise
ENDR
and $08
cp $08
jr z, .endfunction
.waitforrelease:
call WaitVblank
call Music
ld a, P1F_4 ;Select Buttons
ld [rP1], a
REPT 6
ld a, [rP1] ;Read Keypresses, we are doing this multiple Times to reduce Noise
ENDR
and $08
cp $08
jr nz, .waitforrelease
.preparepause:
ld a, LCDCF_ON|LCDCF_BG8000|LCDCF_BG9C00|LCDCF_OBJ8|LCDCF_OBJON|LCDCF_BGON
ld [rLCDC], a
ld a, IEF_VBLANK | IEF_LCDC
ld [rIE], a
pop af
ld a, $01
ld [Paused], a
;Hide all Sprites
ld a, %00000000
ld [rOBP0], a
ld [rOBP1], a
jp PauseLoop
.endfunction:
ret
CheckMainLoop:
ld a, P1F_4 ;Select Buttons
ld [rP1], a
REPT 6
ld a, [rP1] ;Read Keypresses, we are doing this multiple Times to reduce Noise
ENDR
and $08
cp $08
jr z, .endfunction
.waitforrelease:
call WaitVblank
call Music
ld a, P1F_4 ;Select Buttons
ld [rP1], a
REPT 6
ld a, [rP1] ;Read Keypresses, we are doing this multiple Times to reduce Noise
ENDR
and $08
cp $08
jr nz, .waitforrelease
.preparemain:
pop af
call WaitVblank
ld a, LCDCF_ON|LCDCF_BG8000|LCDCF_BG9800|LCDCF_OBJ8|LCDCF_OBJON|LCDCF_BGON
ld [rLCDC], a
ld a, $00
ld [rSCX], a
ld a, IEF_VBLANK | IEF_TIMER
ld [rIE], a
ld a, $00
ld [Paused], a
;Show all Sprites
ld a, %11100100
ld [rOBP0], a
ld [rOBP1], a
ld a, $00
ld [rSCX], a
jp MainLoop
.endfunction:
ret
GetPlayerSquare:
.CheckY1:
ld a, [OamData] ;Load the Player Y Coordinate
add $04 ;Add 4 to look at the center of the player
cp Y2 ;Compare it with the Y Coordinate of the Square below
jr nc, .CheckY2 ;If it is Higher, jump to the next Check
ld b, $01 ;If it is lower or Equal, save the Square number to b
jp .CheckX1 ;Y Coordinate was found, Check for X Coordinate
.CheckY2: ;See CheckY1
cp Y3
jr nc, .CheckY3
ld b, $02
jp .CheckX1
.CheckY3: ;See CheckY1
cp Y4
jr nc, .CheckY4
ld b, $03
jp .CheckX1
.CheckY4: ;See CheckY1
cp Y5
jr nc, .CheckY5
ld b, $04
jp .CheckX1
.CheckY5: ;See CheckY1
cp Y6
jr nc, .CheckYEnd
ld b, $05
jp .CheckX1
.CheckYEnd: ;The remaining Y Coordinate can only be 6
ld b, $06
.CheckX1: ;See CheckY1
ld a, [OamData+1]
add $04
cp X2
jr nc, .CheckX2
ld c, $01
jp .CheckEnd
.CheckX2: ;See CheckY1
cp X3
jr nc, .CheckX3
ld c, $02
jp .CheckEnd
.CheckX3: ;See CheckY1
cp X4
jr nc, .CheckX4
ld c, $03
jp .CheckEnd
.CheckX4: ;See CheckY1
cp X5
jr nc, .CheckX5
ld c, $04
jp .CheckEnd
.CheckX5: ;See CheckY1
cp X6
jr nc, .CheckXEnd
ld c, $05
jp .CheckEnd
.CheckXEnd: ;The remaining X Coordinate can only be 6
ld c, $06
.CheckEnd:
ld a, c ;Save the X Coordinate
ld [PSX], a
ld a, b ;Save the Y Coordinate
ld [PSY], a
;All done, now we give each Square its own Index Number by Calculating I=(Y*6)+X
ld c, $00 ;Set c to Zero, it will be the result of our Multiplication
dec a ;Decrease a, making it range from 0-5
cp $00 ;Check if we need to Multiplicate
jr z, .endSCheck
.multloop:
push af ;If yes, save our iteration count for later
ld a, c ;Add 6 to c
add $06
ld c, a
pop af ;Get the iteration count back
dec a ;Decrease it by 1
cp $00 ;Check if the Multiplication is done
jr nz, .multloop
.endSCheck
ld a, [PSX] ;If it is, get the X Coordinate and save it to b
ld b, a
ld a, c ;Get our multiplication Result
add b ;Add our X Coordinate to it
dec a ;Sub one to make the result Range from 0-35
ld [PSI], a ;Save it into the PlayerSquareIndex
.getplayerstatus: ;Check if the Player is Currently standing on an asteroid
ld b, $00
ld c, a
ld hl, AsteroidLocations
add hl, bc
ld a, [hl]
cp $06
jr nz, .noasteroid
ld a, $01
ld [PlayerOnAsteroid], a
jr .endfunction
.noasteroid:
ld a, $00
ld [PlayerOnAsteroid], a
.endfunction:
ret
SpawnAsteroid:
ld a, [SpawnAsteroidF] ;Check if a new Asteroid is requested
or a
jr z, .endspawn ;If not, exit the Function
call RandLFSR
ld a, [LFSRSeed] ;Decide if the asteroid gets Spawned at the Player location, or a randowm one
and $03
or a
jr z, .playerasteroid
.randomfinderloop:
call RandLFSR ;Get a new Random Number
ld a, [LFSRSeed]
cp $24 ;Check if it is below $24 (36 in decimal)
jr nc, .randomfinderloop ;If not, try again
ld hl, AsteroidLocations ;Load the Memory Location of the array AsteroidLocations into hl
ld b, $00 ;Save a into the 16-bit register pair bc
ld c, a
add hl, bc ;Add bc to hl
ld a, [hl] ;Check if the asteroid is already set
cp $00
jr nz, .randomfinderloop ;If it is, find a new Location
ld a, $01 ;If not, set it
ld [hl], a
jr .endspawn
.playerasteroid: ;Spawn an Asteroid at the Players location
ld hl, AsteroidLocations ;Load the Memory Location of the array AsteroidLocations into hl
ld a, [PSI] ;Get the Index of the Square the Player is standing on
ld b, $00 ;Save a into the 16-bit register pair bc
ld c, a
add hl, bc ;Add bc to hl, hl now points to the Square the player is Standing on
ld a, [hl]
cp $00 ;Check if the asteroid is already set
jr nz, .randomfinderloop ;If it is, place an asteroid a Random Location
ld a, $01 ;If not, set it
ld [hl], a
.endspawn: ;Finish off by telling the Main Loop that we are done
ld a, $00 ;Reset the Asteroid Request
ld [SpawnAsteroidF], a
ret
UpdateAsteroids:
ld a, [UpdateAsteroidF] ;Check if the Asteroids need to be Updated
or a
jr z, .endupdate ;If not, Quit the Function
ld hl, AsteroidLocations
ld b, $00
.updateloop:
ld a, [hl] ;Get Asteroid Stage from AsteroidLocations Array
cp $00 ;If it is 0, skip the Update
jr z, .prepnextloop
cp AsteroidLifetime ;If it is too high, remove the Asteroid
jr z, .removeasteroid
inc a ;If not, Increase the Stage
ld [hl], a
cp $05 ;If the Stage is now 5 (The Asteroid hits the Floor), give the Player Points
jr nz, .prepnextloop
ld a, [Points+5]
add a, 25
ld [Points+5], a
.prepnextloop:
inc b ;Increase the Counter
inc hl ;Increase the Pointer towards the Current AsteroidStage
ld a, b ;Check if we are done with all Squares
cp 36
jr nz, .updateloop
ld a, $01 ;Tell the Main Loop, that we want to draw the new Asteroids
ld [DrawAsteroidF], a
ld a, $00 ;Tell the Main Loop, that we are done with drawing the Asteroids
ld [UpdateAsteroidF], a
.endupdate
ret
.removeasteroid:
ld a, $00
ld [hl], a
jr .prepnextloop
SpawnRocket:
ld a, [SpawnRocketF]
or a
jr z, .endfunction
ld a, [RocketInfo]
or a
jr nz, .endfunction
.tryspawn
call RandLFSR
ld a, [LFSRSeed]
cp RocketStartsEnd-RocketStarts
jr nc, .tryspawn
ld [RocketY], a
ld e, a
ld a, $00
ld d, a
ld hl, RocketStarts
add hl, de
ld a, [hl]
ld [OamData+ RocketOAM+ OAMY], a
ld [OamData+ RocketOAM+8+ OAMY], a
add a, $08
ld [OamData+ RocketOAM+4+ OAMY], a
ld [OamData+ RocketOAM+12+ OAMY], a
ld a, X1
ld [OamData+ RocketOAM+ OAMX], a
ld [OamData+ RocketOAM+4+ OAMX], a
add a, $08
ld [OamData+ RocketOAM+8+ OAMX], a
ld [OamData+ RocketOAM+12+ OAMX], a
ld a, $8B
ld [OamData+ RocketOAM+ OAMPatt], a
inc a
ld [OamData+ RocketOAM+4+ OAMPatt], a
inc a
ld [OamData+ RocketOAM+8+ OAMPatt], a
inc a
ld [OamData+ RocketOAM+12+ OAMPatt], a
ld a, $01
ld [RocketInfo], a
.endfunction:
ld a, $00
ld [SpawnRocketF], a
ret
UpdateRocket:
ld a, [RocketInfo]
or a
jr z, .endfunction
cp $D0
jr nc, .despawn
inc a
ld [RocketInfo], a