-
Notifications
You must be signed in to change notification settings - Fork 0
/
asteroid1.asm
1984 lines (1892 loc) · 47.2 KB
/
asteroid1.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
; Import declarations from asteroid0.asm
INCLUDE "asteroid0.inc"
ShipsPerGame EQU 3
BulletTimerVal EQU 24 ; Bullet lifespan, gameloop cycles
WaveTimerVal EQU 60 ; Time before new wave, gameloop cycles
ShipHitTimerVal EQU 50 ; Time for ship explosion, gameloop cycles
GameOverTimerVal EQU 120 ; Time after Game Over sign to return to demo mode
;----------------------------------------------------------------------------
ORG Start
;Start:
; Clear the plane 3 ($8000-$BFFF) from any dirt
call ClearPlane3
; Draw two text strings under the title screen
call DrawTitles
; Waiting on the title screen
call WaitAnyKey
RestartDemo:
call ClearPlane0123 ; Clear the whole screen
ld a,(CurrentPlaneHi)
call SetPaletteGame
; ;TEST draw L-size rocks
; ld a,$C0
; call SetPaletteGame
; ld c,4
; Test_1:
; ld b,8
; Test_2:
; push bc
; ld hl,Test_4 ; where to return
; push hl
; ld a,b ; 1..8
; dec a ; 0..7
; add a,a
; add a,a
; ld hl,CurrentPlaneHi
; or (hl)
; ld h,a ; now we have screen hi
; ld a,c ; 1..4
; add a,a
; add a,a
; add a,a
; add a,a
; add a,a
; ld l,a ; now we have screen lo
; ex de,hl
; ld a,b ; 1..8
; dec a ; 0..7 - shift
; add a,a
; ld b,a ; save shift * 2
; inc hl ; now HL = object record + 1, at Type
; ld a,c ; 1..4
; dec a ; 0..3 - rock type
; rla
; rla
; rla
; rla ; -> bits 4..5
; and $30
; or b ; now A = draw procedure number * 2
; ld hl,DrawRockLAddrs ; table address
; add a,l
; ld l,a
; jp nc,Test_3
; inc h
; Test_3:
; ld c,(hl) ; get lo
; inc hl
; ld b,(hl) ; get hi; now BC = sprite drawing code address
; push bc ; store the sprite drawing code address to call
; ex de,hl ; now HL = screen address
; ret ; jumping to the sprite drawing code
; Test_4:
; pop bc
; dec b
; jp nz,Test_2
; dec c
; jp nz,Test_1
; di
; halt
; ;TEST HitTest
; ld a,1
; ld (AstPerWave),a
; call InitWaveVars
; ld a,$E0
; ld (CurrentPlaneHi),a
; call SwitchToPlane
; ld b,60
; Test_H0:
; push bc
; call UpdateObjects ; Update position for all objects
; call DrawObjects ; draw the asteroid
; ei
; halt
; ei
; halt
; pop bc
; dec b
; jp nz,Test_H0
; call ClearPlane0123 ; Clear the whole screen
; call DrawObjects ; draw the asteroid
; Test_H1:
; ld a,1
; ld (ShipShotObjects),a
; Test_H1R:
; call Random16
; ld a,h
; and $0F ; 0..$0FFF
; ld h,a
; ld de,$FAAB ; -$0555..$0AAA
; add hl,de
; ld a,h
; or a
; jp m,Test_H1R ; less than 0 => repeat
; ld (ShipShotObjects+2),hl ; X
; call Random16
; ld a,h
; and 7
; ld h,a
; ld (ShipShotObjects+4),hl ; Y
; ld hl,AstObjects+2
; ld de,ShipShotObjects+2
; ld a,130
; call HitTest
; jp c,Test_H2
; ld a,$C0
; jp Test_H3
; Test_H2:
; ld a,$A0
; Test_H3:
; ld (CurrentPlaneHi),a
; call SwitchToPlane
; call DrawObjects
; jp Test_H1
InitGame:
call InitGameVars ; Init vars and reset all objects
call DrawTitles ; Draw the titles for demo mode
ld hl,SAPressToStart
call DrawAString
call InitWaveVars ; Create wave for the demo mode
xor a
ld (NumPlayers),a ; select demo mode
call WaitKeyUp
; Game loop start
GameRunningLoop:
;DEBUG: Show frame count at right-top corner
ld hl,$9FFF
ld (TextAddr),hl
ld a,(LastIntCount)
add a,$30 ; '0'
call DrawChar ; show frame count
; Adjust procedures for the working plane
ld a,(CurrentPlaneHi)
call SwitchToPlane
; Clear the working plane
ld a,(CurrentPlaneHi)
call ClearPlaneA
ld hl,$9DFF
ld (TextAddr),hl
ld a,(CurAsteroids)
add a,$30 ; '0'
call DrawChar ; show CurAsteroids
ld a,(NumPlayers)
or a ; if in game mode
call nz,HitDectection ; Collision detection
call ReadKeyboard
call ProcessKeyboard
ld a,(ShipHitTimer)
or a
jp z,GameRunningLoop_3 ; = 0, jump
; ShipHitTimer non-zero - ship explosion
jp p,GameRunningLoop_2 ; > 0, jump
; ShipHitTimer < 0, this is a flag to start the explosion
call DoShipExplosion
jp GameRunningLoop_4
GameRunningLoop_2:
; ShipHitTimer > 0 - ship explosion is in progress
dec a
ld (ShipHitTimer),a
jp nz,GameRunningLoop_4
; ShipHitTimer = 0 - ship debris gone
ld a,(PlayerShips)
or a ; do we have lives left?
jp z,GameRunningLoop_GameOver ; no ships left, jump
; start with new ship
ld a,1
ld (ShipStatus),a ; re-activate the ship
call CenterShip
jp GameRunningLoop_4
GameRunningLoop_GameOver:
xor a
ld (NumPlayers),a ; switch to demo mode
ld a,GameOverTimerVal
ld (GameOverTimer),a ; set the timer to return back to demo mode
jp GameRunningLoop_4
GameRunningLoop_3: ; ShipHitTimer = 0
ld a,(NumPlayers)
or a ; if in game mode
jp nz,GameRunningLoop_G
; in demo mode
ld a,(GameOverTimer)
or a
jp z,GameRunningLoop_4
dec a
ld (GameOverTimer),a
jp nz,GameRunningLoop_4
; game over timer just ended => restart the demo mode
jp RestartDemo
GameRunningLoop_G: ; in the game mode
call UpdateShipFire ; Update ship firing
GameRunningLoop_4:
call UpdateObjects ; Update position for all objects
call DrawObjects ; draw all the objects
ld a,(NumPlayers)
or a
jp z,GameRunningLoop_B ; jump if not in game mode
; In game mode, update indicators
ld a,IndicatorsFlag
or a
call nz,DrawIndicators ; show updated score/lives indicators
; In game mode, check if the wave ended
ld a,(CurAsteroids)
or a ; do we have any rocks on the screen?
jp nz,GameRunningLoop_C
; Wave finished, check new wave timer
ld a,(WaveTimer)
or a
jp nz,GameRunningLoop_A ; wave timer is running => jump
; Wave just finished, start the timer
ld a,WaveTimerVal
ld (WaveTimer),a
ld hl,WaveNumber
inc (hl)
call DrawWaveSign ; show wave starting sign
jp GameRunningLoop_C
GameRunningLoop_A: ; wave timer is running
dec a
ld (WaveTimer),a
jp nz,GameRunningLoop_C
call InitWaveVars ; start the next wave
call ClearWaveSign ; remove wave starting sign
jp GameRunningLoop_C
GameRunningLoop_B: ; in demo mode
; In demo mode, check if Fire button pressed to start the game
ld a,(FireSw)
or a
call nz,PrepareNewGame ; Start the game
GameRunningLoop_C:
; Save interrupt counter value
ld a,(IntCount) ; get interrupt counter
cp 2
jp nc,GameRunningLoop_I
; wait one more interrupt, to make every game loop at least 2 frames
ei
halt
ld a,(IntCount)
GameRunningLoop_I:
ld (LastIntCount),a
xor a
ld (IntCount),a
; Switch working plane
ld a,(CurrentPlaneHi)
sub $20 ; CurrentPlaneHi cycles $E0 -> $C0 -> $A0
cp $80
jp nz,GameRunningLoop_Z
ld a,$E0
GameRunningLoop_Z:
ld (CurrentPlaneHi),a
; Set palette to show already drawn plane
; ld a,(CurrentPlaneHi)
call SetPaletteGame
jp GameRunningLoop ; continue the game loop
; Called from the game loop to start a new game
PrepareNewGame:
call ClearPlane0123 ; clear the whole screen
call InitGameVars ; Init vars and reset all objects
ld a,WaveTimerVal
ld (WaveTimer),a ; Wave tmer will count down to 0 then new wave will be started
ld a,1
ld (ShipStatus),a ; activate the ship
ld (NumPlayers),a ; set the game mode flag
ld (WaveNumber),a ; starting the first wave
call CenterShip ; Center ship on display and zero velocity
jp DrawWaveSign ; show wave starting sign
; ret ; return to the game loop
; Mark the ship not active; activate four debris objects;
; copy ship X,Y position to each debris object X,Y
DoShipExplosion:
ld hl,PlayerShips
dec (hl) ; one live less
jp nz,DoShipExplosion_0
ld hl,SAGameOver
call DrawAString
DoShipExplosion_0:
ld a,ShipHitTimerVal ; time to show the explosion
ld (ShipHitTimer),a ; set the timer
ld (IndicatorsFlag),a ; set flag to show the updated PlayerShips
xor a
ld (ShipStatus),a ; set Status not active
; prepare debris objects
ld bc,$0420 ; B = number of debris objects, C = status value for debris objects
ld hl,ShipDebrisObjects
DoShipExplosion_1:
ld (hl),c ; set status value
inc hl
inc hl ; now HL = debris object + 2, at X lo
ld de,ShipXPos ; start address for copying
REPT 4 ; copy 4 bytes
ld a,(de)
inc de
ld (hl),a
inc hl
ENDM
inc hl
inc hl ; now HL points to the next object record
dec b
jp nz,DoShipExplosion_1
ret ; return to the game loop
DrawWaveSign:
ld hl,SAWaveSign
call DrawAString
ld a,(WaveNumber)
add a,$30 ; '0'
jp DrawChar
ClearWaveSign:
ld hl,SAWaveSpace
jp DrawAString
LastIntCount: db 0
CurrentPlaneHi: db $E0 ; Current plane address hi byte: $E0, $C0, $A0
SATitle1: dw $821C
DEFM "ORIGINAL GAME 1979 ATARI INC",0
SATitle2: dw $800C
DEFM "VECTOR-06C DEMO VERSION NZEEMIN",0
SAPressToStart: dw $86E0
DEFM "PRESS FIRE TO START",0
SAWaveSign: dw $8CE0
DEFM "WAVE ",0
SAWaveSpace: dw $8CE0
DEFM " ",0
SAGameOver: dw $8BC0
DEFM "GAME OVER",0
; Draw two title strings
DrawTitles:
ld hl,SATitle1
call DrawAString
ld hl,SATitle2
jp DrawAString
ProcessKeyboard:
ld c,a
and 1 ; bit 0 - if Fire pressed
ld (FireSw),a ; save the fire key status
ld a,c
rra
rra ; bit 1 - if Left pressed
jp nc,ProcessKeyboard_1
ld c,a
ld a,(ShipDir)
dec a ; rotate left
and $1F
ld (ShipDir),a
ld a,c
ProcessKeyboard_1:
rra ; bit 2 - if Right pressed
jp nc,ProcessKeyboard_2
ld c,a
ld a,(ShipDir)
inc a ; rotate right
and $1F
ld (ShipDir),a
ld a,c
ProcessKeyboard_2:
ld c,a
and 1 ; bit 3 - if Thrust pressed
ld (ThrustSw),a
ld a,c
and 2 ; bit 4 - if Hyper pressed
ld (HyprSpcSw),a
ld a,c
and 4 ; bit 5 - if Enter/Esc pressed
;TODO
ret
;----------------------------------------------------------------------------
FireSw EQU $0100 ; Fire button status
ThrustSw EQU $0101 ; Thrust button status
HyprSpcSw EQU $0102 ; Hyper button status
NumPlayers EQU $0103 ; 0 = Not playing, 1 = In the Game
PlayerScore EQU $0104 ; Player score as two BCD bytes
PlayerShips EQU $0106 ; Current number of player ships
IndicatorsFlag EQU $0107 ; Flag indicating we have to redraw indicators - Score/Ships
ShipXAccel EQU $0108
ShipYAccel EQU $0109
WaveNumber EQU $010A
CurAsteroids EQU $010B ; Current number of asteroids
WaveTimer EQU $010C ; Timer used before new wave
ShipDir: db 8 ; Ship direction 0..31; 0 = W, 8 = N, 16 = E, 24 = S
ShipBulletSR: db 0 ; Counter to limit ship fire rate
ScrTimer: db 0 ; Countdown timer for saucer spawn
ScrTmrReload: db 0 ; Reload value for saucer timer
ShipHitTimer: db 0 ; Timer used on ship hit
GameOverTimer: db 0 ; Timer for timeout after the Game Over sign
; Object record format:
; + 0: Status byte: 0 = Not active
; + 1: Type 0..7 / Subtype
; + 2-3: X position
; + 4-5: Y position
; + 6: X speed
; + 7: Y speed
MaxObjects EQU 38 ; Total 38 object records
Objects:
; Ship object record
ShipStatus: db 0 ; 0=No Ship Or In Hyperspace, 1=Alive, $A0-FF=Ship Exploding
db 0 ; Type = Ship
ShipXPos: dw 1366
ShipYPos: dw 1024
ShipXSpeed: db 0
ShipYSpeed: db 0
; Soucer object record
ScrStatus: db 0
db 1 ; Type = Soucer
ScrXPos: dw 0
ScrYPos: dw 0
SaucerXSpeed: db 0
SaucerYSpeed: db 0
; Asteroid objects, 26 records
MaxAsteroids EQU 26
AstObjects:
REPT MaxAsteroids
db 0, 4, 0,0,0,0, 0,0
ENDM
; Ship bullet objects, 4 records
ShipShotObjects: db 0, 5, 0,0,0,0, 0,0
db 0, 5, 0,0,0,0, 0,0
db 0, 5, 0,0,0,0, 0,0
db 0, 5, 0,0,0,0, 0,0
; Soucer bullet objects, 2 records
ScrShotObjects: db 0, 5, 0,0,0,0, 0,0
db 0, 5, 0,0,0,0, 0,0
; Ship debris objects, 4 records
ShipDebrisObjects: db 0, $87, 0,0,0,0, -3,-2 ; goes SW
db 0, $87, 0,0,0,0, 2,3 ; goes NE
db 0, $07, 0,0,0,0, 3,-1 ; goes SE
db 0, $07, 0,0,0,0, -3,3 ; goes NW
; End of the Objects array
;
; X,Y shifts from object's sprite left-top corner to the object center
DrawObjsXYShift:
db 8, 8 ; 0 - ship
db 8, 7 ; 1 - sauser
db 4, 4 ; 2 - S-size rock
db 8, 8 ; 3 - M-size rock
db 12, 16 ; 4 - L-size rock
db 0, 0 ; 5 - ship or sauser bullets
db 8, 8 ; 6 - shrapnel
db 4, 4 ; 7 - debris
;
; Object's hit radius table to use in collision detection
ObjectsHitRadius:
db 64 ; 0 - ship
db 36 ; 1 - sauser
db 102 ; 2 - S-size rock
db 144 ; 3 - M-size rock
db 216 ; 4 - L-size rock
db 0 ; 5 - ship or sauser bullets
db 0 ; 6 - shrapnel
db 0 ; 7 - debris
;----------------------------------------------------------------------------
; Initialize game variables AND reset all objects
InitGameVars:
xor a
ld (WaveNumber),a
ld (ShipStatus),a
ld (PlayerScore),a
ld (PlayerScore+1),a
ld (CurAsteroids),a
ld a,ShipsPerGame
ld (PlayerShips),a
ld (IndicatorsFlag),a ; need to redraw the indicators
;
; Deactivate all objects
ResetAllObjects:
ld hl,Objects
ld b,MaxObjects
ld de,$0008
xor a
ResetAllObjects_0:
ld (hl),a ; set Status to inactive
add hl,de
dec b
jp nz,ResetAllObjects_0
ld (CurAsteroids),a ; reset the rocks counter
ret
UpdateShipFire:
; Check ShipBulletSR counter first
ld a,(ShipBulletSR)
or a
jp z,UpdateShipFire_1
dec a
ld (ShipBulletSR),a
ret
UpdateShipFire_1:
ld a,(FireSw)
or a
ret z
; Fire key pressed - creating new bullet
ld a,8 ; counter initial value
ld (ShipBulletSR),a
; Find an empty bullet slot
ld hl,ShipShotObjects
ld b,4 ; ship bullets max
ld de,$0008 ; object record size
UpdateShipFire_2:
ld a,(hl) ; check the status
or a
jp z,UpdateShipFire_3 ; inactive => use the slot
add hl,de ; next record
dec b
jp nz,UpdateShipFire_2; continue the loop
ret ; no free bullet slots => exit
UpdateShipFire_3: ; found free slot, HL = object record
; Create new bullet
ld a,BulletTimerVal ; active status + bullet timer value
ld (hl),a ; activate the bullet
inc hl
inc hl ; now HL = object record + 2, at X lo
ld de,ShipXPos
ld a,(de) ; get X lo
ld (hl),a ; set X lo
inc de
inc hl
ld a,(de) ; get X hi
ld (hl),a ; set X hi
inc de
inc hl
ld a,(de) ; get Y lo
ld (hl),a ; set Y lo
inc de
inc hl
ld a,(de) ; get Y hi
ld (hl),a ; set Y hi
inc hl
call GetShipDirSinCos
ld a,c ; get Cos(ShipDir)
ld (hl),a ; set X speed
inc hl
ld a,b ; get Sin(ShipDir)
ld (hl),a ; set Y speed
ret
; Get Sinus and Cosinus values for the ShipDir
; Returns: B = Sin(ShipDir), C = Cos(ShipDir)
GetShipDirSinCos:
push hl
; Get Sin(ShipDir)
ld a,(ShipDir)
ld c,a ; save it for later
ld hl,SineTbl
add a,l
ld l,a
jp nc,GetShipDirSinCos_1
inc h
GetShipDirSinCos_1:
ld b,(hl)
; Get Cos(ShipDir)
ld a,c ; restore ShipDir
sub 8
and $1F ; keep 0..31 value
ld hl,SineTbl
add a,l
ld l,a
jp nc,GetShipDirSinCos_2
inc h
GetShipDirSinCos_2:
ld c,(hl)
pop hl
ret
; ShipDir to Sine table
SineTbl:
db 0,10,20,29,37,43,48,51,52,51,48,43,37,29,20,10
db 0,-10,-20,-29,-37,-43,-48,-51,-52,-51,-48,-43,-37,-29,-20,-10
; Collision detection for all objects
HitDectection:
; Loop thru rock objects
ld hl,AstObjects
ld b,MaxAsteroids ; number of objects
HitDectection_R1:
ld a,(hl) ; get rock object status
or a
jp z,HitDectection_RS ; not alive, skipping
push hl ; save the rock object address
inc hl ; HL = rock object + 1, at Type
; skip shrapnel objects
ld a,(hl) ; get Type
and 7 ; 0..7
cp 5 ; check for temporary objects
jp nc,HitDectection_SN ; skip
inc hl ; HL = rock object + 2, at X lo
ex de,hl ; now DE = rock object + 2
; get hit box size for this rock type
call GetHitRadius ; now A = hit distance
ld (HitDectection_BH+1),a ; set as the parameter
ld (HitDectection_SH+1),a ; set as the parameter
; Check this rock against all bullets
;TODO: check UFO bullets too
ld hl,ShipShotObjects
ld c,4 ; number of objects
HitDectection_B1:
ld a,(hl) ; get bullet object status
or a
jp z,HitDectection_BS
push bc ; save counters B, C
push hl ; save the bullet object address
inc hl
inc hl ; HL = bullet object + 2, at X lo
; check if we have the hit
HitDectection_BH:
ld a,132 ; hit radius, mutable parameter!
call HitTest ; returns Carry=1 for hit
jp nc,HitDectection_BN
; We have a hit between the bullet and the rock
xor a
pop hl ; restore the bullet object address
ld (hl),a ; deactivate the bullet object
pop bc ; restore counters B, C
pop hl ; restore the rock object address
call DoAsteroidPoints ; give player points for crashing this asteroid
call BreakAsteroid ; split the rock
jp HitDectection_RS ; continue the loop by rocks
HitDectection_BN:
pop hl ; restore the bullet object address
pop bc ; restore counters B, C
HitDectection_BS:
dec c
jp z,HitDectection_BE
ld de,$0008
add hl,de ; to the next bullet record
jp HitDectection_B1
HitDectection_BE: ; end of loop by bulets
ld a,(ShipStatus)
or a ; is the ship active?
jp z,HitDectection_SN ; skip if no ship
pop de ; restore the rock object address
push de ; and save the rock object address again
inc de
inc de
push bc ; save counter B
; Check this rock against the ship object
ld hl,ShipXPos
HitDectection_SH:
ld a,128+64 ; hit radius, mutable parameter!
call HitTest ; returns Carry=1 for hit
jp nc,HitDectection_SE
; We have a hit between the ship and the rock
pop bc ; restore B
pop hl ; restore the rock object address
; convert the asteroid object to shrapnel
inc hl
ld a,6 ; type = shrapnel
ld (hl),a ; set Type
dec hl ; now HL = object record + 0, at Status
ld a,8 ; timer value
ld (hl),a ; set Status as a timer
ld a,$FF
ld (ShipHitTimer),a ; set the flag for explosion
ld hl,CurAsteroids
dec (hl) ; one less
jp HitDectection_RS
HitDectection_SE:
pop bc ; restore B
HitDectection_SN:
pop hl ; restore the rock object address
HitDectection_RS:
ld de,$0008
add hl,de ; to the next record
dec b
jp nz,HitDectection_R1 ; continue loop by rocks
ret
GetHitRadius:
ld hl,ObjectsHitRadius
add a,l
ld l,a
jp nc,GetHitRadius_1
inc h
GetHitRadius_1:
ld a,(hl)
ret
; Give player some points for the crashed asteroid
; HL = asteroid object record
DoAsteroidPoints:
push hl
inc hl
ld a,(hl) ; get Type
and 7
call AddPlayerPoints
pop hl
ret
; Update player's score
; A = how much to add, in 10th of points
AddPlayerPoints:
ld hl,PlayerScore
add a,(hl)
daa
ld (hl),a
jp nc,AddPlayerPoints_1
inc hl
ld a,(hl)
add a,1
daa
ld (hl),a
; set the flag to redraw the score
ld a,1
ld (IndicatorsFlag),a
AddPlayerPoints_1:
ret
; Check if we have a collision
; HL = object 1 address + 2 - at X lo
; DE = object 2 address + 2 - at X lo
; A = hit distance
; Returns: Carry=1 - hit, Carry=0 - no hit
HitTest:
; get X1 and X2
ex de,hl ; now DE = object1 + 2, HL = object2 + 2
ld c,(hl) ; get X2 lo
inc hl
ld b,(hl) ; get X2 hi, now BC = X2
inc hl ; now HL = object2 + 4
push hl ; save HL = object2 + 4
ex de,hl ; now HL = object1 + 2
ld e,(hl) ; get X1 lo
inc hl
ld d,(hl) ; get X1 hi; now DE = X1
inc hl
push hl ; save HL = object1 + 4
; calculate dX = abs(X1 - X2)
push af ; save A = hit distance
call SubAbs ; HL = abs(X1 - X2); Z=1 if H is zero
pop bc ; restore B = hit distance
; check if dX < hit distance
jp nz,HitTest_1 ; Z=0 => dX > 255 => not a hit
ld a,l
cp b ; compare dX lo to hit distance
jp c,HitTest_2 ; dX < hit distance => within the hit box by X, let's check by Y
; distance >= hit distance => not a hit
HitTest_1:
pop hl
pop hl ; empty stack
ret ; Carry=0 - no hit
HitTest_2:
; get Y1 and Y2
pop hl ; HL = object1 + 4, at Y1 lo
pop de ; DE = object2 + 4, at Y2 lo
push bc ; save B = hit distance
ld c,(hl) ; get Y1 lo
inc hl
ld b,(hl) ; get Y1 hi, now BC = Y1
ex de,hl ; now HL = object2 + 4
ld e,(hl) ; get Y2 lo
inc hl
ld d,(hl) ; get Y2 hi; now DE = Y2
; calculate dY = abd(Y2 - Y1)
call SubAbs ; HL = abs(Y2 - Y1); Z=1 if H is zero
pop bc ; restore B = hit distance
; check if dY < hit distance
ret nz ; Z=0 => dY > 255 => not a hit, Carry=0
ld a,l
cp b ; compare dY lo to hit distance
; ret nc ; if Carry=0 then dY >= hit distance => not a hit
ret ; if Carry=1 then dy < hit distance => within the hit box
; Subtract and get absolute value
; Result HL = abs(DE - BC); returns Z=1 if H is zero - result is 0..255
SubAbs:
; calculate HL = DE - BC
ld a,e
sub c ; sub lo
ld l,a
ld a,d
sbc a,b ; sub hi with carry
ld h,a
ret nc ; no Carry means we have a positive value or zero
; calculate HL = BC - DE
ld a,c
sub e ; sub lo
ld l,a
ld a,b
sbc a,d ; sub hi with carry
ld h,a
ret
DrawObjects:
ld b,MaxObjects ; number of objects
ld hl,Objects
DrawObjects_1:
ld a,(hl) ; get status byte
or a ; empty object?
jp z,DrawObjects_next ; yes => skip it
push bc
push hl ; store object address
; get object type and call the draw procedure
inc hl
ld a,(hl) ; get object type
push af ; store object type
inc hl ; now HL points to the object coords
call CalcScreenAddr
ex de,hl ; now DE = screen address
ld (DrawObjects_2+1),a ; store the shift
; calculate draw procedure address
pop af ; restore object type
and 7 ; 0..7
add a,a
ld c,a
ld b,0
ld hl,DrawProcTable
add hl,bc
ld a,(hl)
inc hl
ld h,(hl)
ld l,a ; now HL = draw procedure address
; call the draw procedure
ld (DrawObjects_3+1),hl ; prepare the call parameter
DrawObjects_2:
ld a,$00 ; shift 0..7
pop hl ; restore record address
push hl ; and store again
DrawObjects_3:
call $0000 ; call the draw procedure; HL = object address, DE = screen address, A = shift 0..7
; finishing the iteration
pop hl ; restore record address
pop bc
DrawObjects_next:
ld de,$0008 ; record size
add hl,de ; next object record
dec b
jp nz,DrawObjects_1 ; continue the loop by objects
DrawNullProc: ; call point for a void draw procedure
ret
DrawProcTable:
dw DrawShipProc ; 0 - ship
dw DrawSauserProc ; 1 - sauser
dw DrawRockSProc ; 2 - S-size rock
dw DrawRockMProc ; 3 - M-size rock
dw DrawRockLProc ; 4 - L-size rock
dw DrawBulletProc ; 5 - ship or sauser bullets
dw DrawShrapnelProc ; 6 - shrapnel
dw DrawDebrisProc ; 7 - debris
; For all draw procedures registers are:
; HL = object address, DE = screen address, A = shift 0..7
DrawShipProc:
push af ; save A = shoft 0..7
ld a,(ShipDir) ; get ship direction
and $1F ; 0..31
cp 16 ; 0..15 or 16..31 ?
ld hl,DrawSprite24x16 ; draw sprite up-to-down
jp c,DrawShipProc_2 ; ShipDir = 0..15 => jump
; ShipDir = 16..31
ld c,a
ld a,32
sub c ; A = 16..1
ld hl,DrawSprite24x16R ; draw sprite down-to-up
DrawShipProc_2:
ld (DrawShipProc_4+1),hl ; put draw sprite call address
; Calculate sprite address, A = 0..16
add a,a
ld l,a
ld h,0
ld bc,TableMul384
add hl,bc
ld a,(hl) ; lo byte
inc hl
ld h,(hl) ; hi byte
ld l,a
ld bc,Ship00S0 ; base sprite address
add hl,bc ; now HL = base sprite address for the ship direction
; Chech the Thrust status, adjust the sprite if needed
ld a,(ThrustSw) ; get Thrust button status
or a
jp z,DrawShipProc_3
ld bc,384*17 ; shift from Ship sprite to Ship+Fire sprite
add hl,bc
DrawShipProc_3:
pop af ; restore shift A = 0..7
call Multiply48Base ; calculate sprite address based on shift A = 0..7
ex de,hl ; now HL = screen address, DE = sprite address
DrawShipProc_4:
call DrawSprite24x16
ret
DrawSauserProc:
;TODO
ret
DrawRockSProc:
push af ; save shift
inc hl ; now HL = object record + 1, at Type
ld a,(hl) ; get type/subtype
and $C0 ; bits 6-7 = subtype
ld l,a
ld h,0
add hl,hl ; subtype 0..3 * 128
ld bc,RockS0S0 ; base sprite address
add hl,bc
pop af ; restore shift
call Multiply16Base ; calculate sprite address based on shift A = 0..7
ex de,hl ; now HL = screen address, DE = sprite address
call DrawSprite16x8
ret
DrawRockMProc:
push af ; save shift
inc hl ; now HL = object record + 1, at Type
ld a,(hl) ; get type/subtype
rla
rla
and 3 ; subtype 0..3
add a,a
ld l,a
ld h,0
ld bc,TableMul384
add hl,bc
ld a,(hl) ; lo byte
inc hl
ld h,(hl) ; hi byte
ld l,a
ld bc,RockM1S0 ; base sprite address
add hl,bc
pop af
call Multiply48Base ; calculate sprite address based on shift A = 0..7
ex de,hl ; now HL = screen address, DE = sprite address
call DrawSprite24x16
ret
DrawRockLProc:
add a,a
ld c,a ; save shift * 2
inc hl ; now HL = object record + 1, at Type
ld a,(hl) ; get type/subtype
rra
rra ; subtype bits 6..7 -> bits 4..5
and $30
or c ; now A = draw procedure number * 2
ld hl,DrawRockLAddrs ; table address
add a,l
ld l,a
jp nc,DrawRockLProc_1
inc h
DrawRockLProc_1: ; now HL = address in the table
ld c,(hl) ; get lo
inc hl
ld b,(hl) ; get hi; now BC = sprite drawing code address
ex de,hl ; now HL = screen address
call MarkColumnDirty
push bc ; store the sprite drawing code address to call
ret ; jumping to the sprite drawing code
DrawRockLAddrs:
dw DrawRockL0S0, DrawRockL0S1, DrawRockL0S2, DrawRockL0S3
dw DrawRockL0S4, DrawRockL0S5, DrawRockL0S6, DrawRockL0S7
dw DrawRockL1S0, DrawRockL1S1, DrawRockL1S2, DrawRockL1S3
dw DrawRockL1S4, DrawRockL1S5, DrawRockL1S6, DrawRockL1S7
dw DrawRockL2S0, DrawRockL2S1, DrawRockL2S2, DrawRockL2S3
dw DrawRockL2S4, DrawRockL2S5, DrawRockL2S6, DrawRockL2S7