-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.asm
1184 lines (1013 loc) · 34.3 KB
/
game.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
;*******************************************************************************
;
; Copyright (C) 2023 Dave Moore
;
; This file is part of Space-Hockey.
;
; Space-Hockey is free software: you can redistribute it and/or modify it under
; the terms of the GNU General Public License as published by the Free Software
; Foundation, either version 2 of the License, or (at your option) any later
; version.
;
; Space-Hockey is distributed in the hope that it will be useful, but WITHOUT
; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
; details.
;
; You should have received a copy of the GNU General Public License along with
; Space-Hockey. If not, see <http://www.gnu.org/licenses/>.
;
; If you modify this program, or any covered work, by linking or combining it
; with the libraries referred to in README (or a modified version of said
; libraries), containing parts covered by the terms of said libraries, the
; licensors of this program grant you additional permission to convey the
; resulting work.
;
;*******************************************************************************
;###############################################################################
;
; Handle main game loop
;
; Corrupts: AF, BC, DE, HL
;
;###############################################################################
main_loop:
ld de, timer ; Check for game over (timer expired)
ld hl, time_game_over
ld b, TIME_DIGITS
call bcd_compare
ret z ; Otherwise continue
call refresh_game ; Redraw any changable game elements
call handle_p1 ; Move P1 if movement requested
call handle_p2 ; Move P2 if movement requested
call set_cd_vars ; Handle collision detection
call test_cd_vars ; Skip if don't need to move the Ball
cp #FF
jr nz, .continue
ld ix, col_det_state + 0
call do_move ; Move the Ball as a result of P1 Move
cp a, e
cp #FF
jr z, .continue ; If P1 has already moved the Ball, skip
ld ix, col_det_state + 2
call do_move ; Move the Ball as a result of P2 Move
.continue:
ld de, timer ; Decrement the timer
ld hl, time_decrement
ld b, TIME_DIGITS
call bcd_subtract
jp main_loop ; Loop
;###############################################################################
;
; Display title screen
;
; Corrupts: AF, BC, HL
;
;###############################################################################
show_title:
call SCR_RESET ; Initialise and clear the screen
call SCR_CLEAR
ld a,1 ; Set screen mode
call SCR_SET_MODE
ld bc, #0B0B ; Set border colour
call SCR_SET_BORDER
ld a, 1 ; Set pen and paper
call TXT_SET_PEN
ld a, 0
call TXT_SET_PAPER
ld hl, #0802 ; Display title text
call TXT_SET_CURSOR
ld hl, str_title
call print_string
ld a, 2 ; Display credits
call TXT_SET_PEN
ld hl, #0304
call TXT_SET_CURSOR
ld hl, str_credits_1
call print_string
ld hl, #0205
call TXT_SET_CURSOR
ld hl, str_credits_2
call print_string
ld a, 1 ; Display ready message
call TXT_SET_PEN
ld hl, #0717
call TXT_SET_CURSOR
ld hl, str_start_game
call print_string
ret
;###############################################################################
;
; Title screen keyboard loop
;
; Output: A = #FF if we want to quit, else #00 if we can continue
; Corrupts: AF
;
;###############################################################################
wait_for_key:
ld a, P1_FIRE ; Check for P1/P2 fire
call KM_TEST_KEY
jr nz, .continue
ld a, P2_FIRE
call KM_TEST_KEY
jr nz, .continue
ld a, KEY_QUIT ; Check for Q to quit
call KM_TEST_KEY
jr nz, .quit
jr wait_for_key ; Loop around
; If we do not want to quit, return from this with quit flag unset
.continue:
ld a, 0
ld (quit_flag), a
ret
; If we want to quit, return from this with quit flag set
.quit:
ld a, #FF
ld (quit_flag), a
ret
;###############################################################################
;
; Setup User-Defined Characters using the Firmware
;
; Corrupts: AF, DE, HL
;
;###############################################################################
setup_udc:
ld de, UDC_FIRST ; Set the start of the UDCs
ld hl, matrix_table
call TXT_SET_M_TABLE
ld a, CHR_BANNER ; Load the UDCs starting from UDC_FIRST
ld hl, udc_banner
call TXT_SET_MATRIX
ld a, CHR_UP
ld hl, udc_player_up
call TXT_SET_MATRIX
ld a, CHR_DOWN
ld hl, udc_player_down
call TXT_SET_MATRIX
ld a, CHR_LEFT
ld hl, udc_player_left
call TXT_SET_MATRIX
ld a, CHR_RIGHT
ld hl, udc_player_right
call TXT_SET_MATRIX
ld a, CHR_BALL
ld hl, udc_ball
call TXT_SET_MATRIX
ret
;###############################################################################
;
; Setup Window Streams
;
; Corrupts: AF, DE, HL
;
;###############################################################################
setup_windows:
ld a, 1 ; Switch to Stream #1
call TXT_STR_SELECT
ld h, 1
ld d, 38
ld l, 0
ld e, 21
call TXT_WIN_ENABLE ; Define a Window over the Playing Area
; as Stream #1
ld a, 0
call TXT_SET_PAPER
ld a, 0 ; Swap back to the default Stream (#0)
call TXT_STR_SELECT
ret
;###############################################################################
;
; Clear and initialise game state
;
; Corrupts: IX, HL
;
;###############################################################################
initalise:
call setup_windows
ld hl, timer ; Store the initial timer value
ld (hl), 0
inc hl
ld (hl), TIME_MSB
ld ix, game_state
ld (ix + P1_SCORE), 0
ld (ix + P2_SCORE), 0
; This is also called after a goal is scored
reset_game_state:
ld ix, game_state
ld (ix + P1_Y), 16 ; Initial data for P1
ld (ix + P1_X), 5
ld (ix + P1_CHAR), CHR_RIGHT
ld (ix + P2_Y), 5 ; Initial data for P2
ld (ix + P2_X), 35
ld (ix + P2_CHAR), CHR_LEFT
ld (ix + BALL_Y), 11 ; Initial Ball position
ld (ix + BALL_X), 20
ld (ix + P1_OLD_Y), 16
ld (ix + P1_OLD_X), 5
ld (ix + P2_OLD_Y), 5
ld (ix + P2_OLD_X), 35
ld (ix + BALL_OLD_Y), 11
ld (ix + BALL_OLD_X), 20
ret
;###############################################################################
;
; Draw game UI
;
; Corrupts: AF, BC, DE, HL
;
;###############################################################################
refresh_ui:
call MC_WAIT_FLYBACK ; Wait for flyback to avoid flicker
ld a, 0 ; Set inks
ld b, 0
ld c, 0
call SCR_SET_INK
ld a, 1
call TXT_SET_PEN
ld a, 0
call TXT_SET_PAPER
ld a, CHR_GOALS ; Draw the goals
ld hl, #0105
call TXT_SET_CURSOR
ld a, CHR_GOALS
call TXT_OUTPUT
ld hl, #2805
call TXT_SET_CURSOR
ld a, CHR_GOALS
call TXT_OUTPUT
ld hl, #0111
call TXT_SET_CURSOR
ld a, CHR_GOALS
call TXT_OUTPUT
ld hl, #2811
call TXT_SET_CURSOR
ld a, CHR_GOALS
call TXT_OUTPUT
ld a, CHR_BANNER ; Draw bottom HUD
ld b, 40
ld hl, #0117
.loop:
push hl
push bc
call TXT_SET_CURSOR
ld a, CHR_BANNER
call TXT_OUTPUT
pop bc
pop hl
inc h
djnz .loop
ld hl, #0818
call TXT_SET_CURSOR
ld hl, str_bottom_text
call print_string
ld hl, #0419 ; Draw scores
call TXT_SET_CURSOR
ld hl, str_game_score_p1
call print_string
ld hl, #2419
call TXT_SET_CURSOR
ld hl, str_game_score_p2
call print_string
ld hl, #0719
call TXT_SET_CURSOR
ld a, (game_state + P1_SCORE)
call print_int
ld hl, #2019
call TXT_SET_CURSOR
ld a, (game_state + P2_SCORE)
call print_int
ld hl, #1219 ; Draw time left
call TXT_SET_CURSOR
ld de, timer
ld b, TIME_DIGITS
call bcd_show
ret
;###############################################################################
;
; Show the game over screen
;
; Corrupts: AF, HL
;
;###############################################################################
show_game_over:
call SCR_CLEAR ; Clear the screen
ld a, 1 ; Set pen and paper
call TXT_SET_PEN
ld a, 0
call TXT_SET_PAPER
; Display game over message
ld hl, #0D02
call TXT_SET_CURSOR
ld hl, str_game_over
call print_string
; Display player scores
ld a, 3
call TXT_SET_PEN
ld hl, #0F08
call TXT_SET_CURSOR
ld hl, str_p1_name
call print_string
ld hl, #1908
call TXT_SET_CURSOR
ld a, (game_state + P1_SCORE)
call print_int
ld a, 1
call TXT_SET_PEN
ld hl, #0F0A
call TXT_SET_CURSOR
ld hl, str_p2_name
call print_string
ld hl, #190A
call TXT_SET_CURSOR
ld a, (game_state + P2_SCORE)
call print_int
; Display play again message
ld a, 1
call TXT_SET_PEN
ld hl, #0517
call TXT_SET_CURSOR
ld hl, str_play_again
call print_string
call play_end_sound ; Play Game Over Sound
ret
;###############################################################################
;
; Update any game elements (such as player or ball positions) that might change
;
; Corrupts: AF, DE, HL
;
;###############################################################################
refresh_game
call MC_WAIT_FLYBACK ; Wait for flyback to avoid flicker
; Draw Timer
ld a, 1
call TXT_SET_PEN
ld hl, #1219
call TXT_SET_CURSOR
ld de, timer
ld b, TIME_DIGITS
call bcd_show
; Draw scores
ld hl, #0719
call TXT_SET_CURSOR
ld a, (game_state + P1_SCORE)
call print_int
ld hl, #2019
call TXT_SET_CURSOR
ld a, (game_state + P2_SCORE)
call print_int
; Erase and redraw Ball
ld a, 3
call TXT_SET_PEN
ld hl, (game_state + BALL_OLD_Y)
call TXT_SET_CURSOR
ld a, CHR_SPACE
call TXT_OUTPUT
ld a, 2
call TXT_SET_PEN
ld hl, (game_state + BALL_Y)
call TXT_SET_CURSOR
ld a, CHR_BALL
call TXT_OUTPUT
; Erase and redraw P1
ld a, 3
call TXT_SET_PEN
ld hl, (game_state + P1_OLD_Y)
call TXT_SET_CURSOR
ld a, CHR_SPACE
call TXT_OUTPUT
ld a, 3
call TXT_SET_PEN
ld hl, (game_state + P1_Y)
call TXT_SET_CURSOR
ld a, (game_state + P1_CHAR)
call TXT_OUTPUT
; Erase and redraw P2
ld a, 3
call TXT_SET_PEN
ld hl, (game_state + P2_OLD_Y)
call TXT_SET_CURSOR
ld a, CHR_SPACE
call TXT_OUTPUT
ld a, 1
call TXT_SET_PEN
ld hl, (game_state + P2_Y)
call TXT_SET_CURSOR
ld a, (game_state + P2_CHAR)
call TXT_OUTPUT
ret
;###############################################################################
;
; Check for any inputs for Player 1 using Firmware Calls
;
; Corrupts: AF
;
;###############################################################################
handle_p1:
ld a, RESET_KEY
call KM_TEST_KEY
jp nz, .return_ball
ld a, P1_UP
call KM_TEST_KEY
jp nz, .up
ld a, P1_DOWN
call KM_TEST_KEY
jp nz, .down
ld a, P1_LEFT
call KM_TEST_KEY
jp nz, .left
ld a, P1_RIGHT
call KM_TEST_KEY
jp nz, .right
ld a, P1_FIRE
call KM_TEST_KEY
jp nz, .goal
.return: ; Exit point for the movement routines
ret
.up:
ld a, (game_state + P1_Y) ; Check for edge of playing area
cp a, 1
jp z, .return ; If we are at the edge don't do anything
ld a, (game_state + P1_X) ; Store the current location
ld (game_state + P1_OLD_X), a
ld a, (game_state + P1_Y)
ld (game_state + P1_OLD_Y), a
dec a ; Move
ld (game_state + P1_Y), a
ld a, CHR_UP ; Set player orientation character
ld (game_state + P1_CHAR), a
jp .return
.down:
ld a, (game_state + P1_Y) ; Check for edge of playing area
cp a, 22
jp z, .return ; If we are at the edge don't do anything
ld a, (game_state + P1_X) ; Store the current location
ld (game_state + P1_OLD_X), a
ld a, (game_state + P1_Y)
ld (game_state + P1_OLD_Y), a
inc a ; Move
ld (game_state + P1_Y), a
ld a, CHR_DOWN ; Set player orientation character
ld (game_state + P1_CHAR), a
jp .return
.left:
ld a, (game_state + P1_X) ; Check for edge of playing area
cp a, 2
jp z, .return ; If we are at the edge don't do anything
ld a, (game_state + P1_Y) ; Store the current location
ld (game_state + P1_OLD_Y), a
ld a, (game_state + P1_X)
ld (game_state + P1_OLD_X), a
dec a ; Move
ld (game_state + P1_X), a
ld a, CHR_LEFT ; Set player orientation character
ld (game_state + P1_CHAR), a
jp .return
.right:
ld a, (game_state + P1_X) ; Check for edge of playing area
cp a, 39
jp z, .return ; If we are at the edge don't do anything
ld a, (game_state + P1_Y) ; Store the current location
ld (game_state + P1_OLD_Y), a
ld a, (game_state + P1_X)
ld (game_state + P1_OLD_X), a
inc a ; Move
ld (game_state + P1_X), a
ld a, CHR_RIGHT ; Set player orientation character
ld (game_state + P1_CHAR), a
jp .return
.goal:
ld a, (game_state + P1_X) ; Store the current location
ld (game_state + P1_OLD_X), a
ld a, (game_state + P1_Y)
ld (game_state + P1_OLD_Y), a
ld a, 2 ; Return to goal
ld (game_state + P1_X), a
ld a, CHR_RIGHT ; Set player orientation character
ld (game_state + P1_CHAR), a
jp .return
.return_ball:
call reset_game_state
call clear_playing_area
call refresh_ui
jp .return
;###############################################################################
;
; Check for any inputs for Player 2 using Firmware Calls
;
; Corrupts: AF
;
;###############################################################################
handle_p2:
ld a, P2_UP
call KM_TEST_KEY
jp nz, .up
ld a, P2_DOWN
call KM_TEST_KEY
jp nz, .down
ld a, P2_LEFT
call KM_TEST_KEY
jp nz, .left
ld a, P2_RIGHT
call KM_TEST_KEY
jp nz, .right
ld a, P2_FIRE
call KM_TEST_KEY
jp nz, .goal
.return: ; Exit point for the movement routines
ret
.up:
ld a, (game_state + P2_Y) ; Check for edge of playing area
cp a, 1
jp z, .return ; If we are at the edge don't do anything
ld a, (game_state + P2_X) ; Store the current location
ld (game_state + P2_OLD_X), a
ld a, (game_state + P2_Y)
ld (game_state + P2_OLD_Y), a
dec a ; Move
ld (game_state + P2_Y), a
ld a, CHR_UP ; Set player orientation character
ld (game_state + P2_CHAR), a
jp .return
.down:
ld a, (game_state + P2_Y) ; Check for edge of playing area
cp a, 22
jp z, .return ; If we are at the edge don't do anything
ld a, (game_state + P2_X) ; Store the current location
ld (game_state + P2_OLD_X), a
ld a, (game_state + P2_Y)
ld (game_state + P2_OLD_Y), a
inc a ; Move
ld (game_state + P2_Y), a
ld a, CHR_DOWN ; Set player orientation character
ld (game_state + P2_CHAR), a
jp .return
.left:
ld a, (game_state + P2_X) ; Check for edge of playing area
cp a, 2
jp z,.return ; If we are at the edge don't do anything
ld a, (game_state + P2_Y) ; Store the current location
ld (game_state + P2_OLD_Y), a
ld a, (game_state + P2_X)
ld (game_state + P2_OLD_X), a
dec a ; Move
ld (game_state + P2_X), a
ld a, CHR_LEFT ; Set player orientation character
ld (game_state + P2_CHAR), a
jp .return
.right:
ld a, (game_state + P2_X) ; Check for edge of playing area
cp a, 39
jp z, .return ; If we are at the edge don't do anything
ld a, (game_state + P2_Y) ; Store the current location
ld (game_state + P2_OLD_Y), a
ld a, (game_state + P2_X)
ld (game_state + P2_OLD_X), a
inc a ; Move
ld (game_state + P2_X), a
ld a, CHR_RIGHT ; Set player orientation character
ld (game_state + P2_CHAR), a
jp .return
.goal:
ld a, (game_state + P2_X) ; Store the current location
ld (game_state + P2_OLD_X), a
ld a, (game_state + P2_Y)
ld (game_state + P2_OLD_Y), a
ld a, 2 ; Return to goal
ld (game_state + P2_X), a
ld a, CHR_LEFT ; Set player orientation character
ld (game_state + P1_CHAR), a
jp .return
.return_ball:
call reset_game_state
call refresh_ui
jp .return
;###############################################################################
;
; See if we need to move the ball, will check the bytes populated in set_cd_vars
; and if both ABS(P1 bytes) = 0 or 1, set the P1 adjacent flag; and similiarly
; for P2; if either adjacent flag is set, return A = #FF, else A = #00
;
; Output: A = #FF or A = #00
; Corrupts: AF, BC, DE, HL
;
;###############################################################################
test_cd_vars:
ld de, col_det_state + 4 ; Load P1 Adjacent Flag
ld bc, col_det_state + 0 ; Load P1_Y_diff
call .inner ; Set A = #FF if ABS(BC) = 0 or 1
ld h, a
inc bc ; Load P1_X_diff
call .inner ; Set A = #FF if ABS(BC) = 0 or 1
ld l, a
call check_hl_for_both_ff
ld (de), a
inc de ; Load P2 Adjacent Flag
inc bc ; Load P2_Y_diff
call .inner ; Set A = #FF if ABS(BC) = 0 or 1
ld h, a
inc bc ; Load P2_X_diff
call .inner ; Set A = #FF if ABS(BC) = 0 or 1
ld l, a
call check_hl_for_both_ff
ld (de), a
ld hl, (col_det_state + 4) ; Set A = #FF if either is #FF
call check_hl_for_ff
ret
.inner:
ld a, (bc)
call find_abs_a ; Get the ABS(byte) into A
cp 2 ; Is it 0 or 1?
ret z
ret nc
ld a, #FF ; Set the Flag is its 0 or 1
ret
;###############################################################################
;
; To aid us in collision detection, we store the distance from each Player to
; the Ball every time a Player moves. This will also clear the Player Adjacent
; flags at col_det_state + 4 and col_det_state + 5 as well.
;
; Corrupts: AF, BC, HL
;
;###############################################################################
set_cd_vars:
ld b, 6 ; Reset the game data
ld hl, col_det_state
ld a, 0
.loop:
ld (hl), 0
inc hl
djnz .loop
; P1
ld hl, col_det_state ; HL points to the 4 storage bytes
ld a, (game_state + BALL_Y)
ld b, a
ld a, (game_state + P1_Y)
sub b
ld (hl), a
inc hl
ld a, (game_state + BALL_X)
ld b, a
ld a, (game_state + P1_X)
sub b
ld (hl), a
; P2
inc hl
ld a, (game_state + BALL_Y)
ld b, a
ld a, (game_state + P2_Y)
sub b
ld (hl), a
inc hl
ld a, (game_state + BALL_X)
ld b, a
ld a, (game_state + P2_X)
sub b
ld (hl), a
ret
;###############################################################################
;
; Workout how to move the Ball after it has been touched. We test the offsets
; stored at col_det_state+0 to +3 to see which way we need to move the ball,
; starting with P1. Note that if P1 moves the ball we skip over checking for P2.
; The Ball is moved, and if a goal is scored, points are added as appropriate
; and the Game Field is reset.
;
; Input: X = col_det_state_p1_y for P1 or col_det_state_p2_y for P2
; Output: E = #FF is ball has been moved else #00
; Corrupts: AF, BC, DE
;
;###############################################################################
do_move:
ld d, (IX + 0) ; Y
ld e, (IX + 1) ; X
.test_s:
ld a, d
cp -1 ; P_Y = BALL_Y - 1
jr nz, .test_n
ld a, e
cp 0 ; P_X = BALL_X
jp nz, .test_n
ld b, 0 ; Move Ball South
ld c, 5
jp .move
.test_n:
ld a, d ; P_Y = BALL_Y + 1
cp 1
jr nz, .test_w
ld a, e
cp 0 ; P_X = BALL_X
jp nz, .test_w
ld b, 0 ; Move Ball North
ld c, -5
jp .move
.test_w:
ld a, d ; P_Y = BALL_Y
cp 0
jr nz, .test_e
ld a, e
cp 1 ; P_X = BALL_X + 1
jp nz, .test_e
ld b, -5 ; Move Ball West
ld c, 0
jp .move
.test_e:
ld a, d
cp 0 ; P_Y = BALL_Y
jr nz, .test_ne
ld a, e
cp -1 ; P_X = BALL_X - 1
jp nz, .test_ne
ld b, 5 ; Move Ball East
ld c, 0
jp .move
.test_ne:
ld a, d
cp 1 ; P_Y = BALL_Y + 1
jr nz, .test_nw
ld a, e
cp -1 ; P_X = BALL_X - 1
jp nz, .test_nw
ld b, 5 ; Move Ball North-East
ld c, -5
jp .move
.test_nw:
ld a, d
cp 1 ; P_Y = BALL_Y + 1
jr nz, .test_se
ld a, e
cp 1 ; P_X = BALL_X + 1
jp nz, .test_se
ld b, -5 ; Move Ball North-West
ld c, -5
jp .move
.test_se:
ld a, d
cp -1 ; P_Y = BALL_Y - 1
jr nz, .test_sw
ld a, e
cp -1 ; P_X = BALL_X + 1
jp nz, .test_sw
ld b, 5 ; Move Ball South-East
ld c, 5
jp .move
.test_sw:
ld a, d
cp -1 ; P_Y = BALL_Y - 1
jr nz, .test_ret
ld a, e
cp 1 ; P_X = BALL_X - 1
jp nz, .test_ret
ld b, -5 ; Move Ball South-West
ld c, 5
jp .move
.test_ret:
ld e, #00
ret
.move:
call move_ball
call play_ball_sound
call check_for_goal
cp #FF
call nz, check_clip_ball
ld e, #FF
ret
;###############################################################################
;
; Move the Ball (not clipped to the edge of the playing area)
;
; Input: B = x squares to move (signed) (will be -5, 0, or 5)
; Input: C = y squares to move (signed) (will be -5, 0, or 5)
; Output: E = #FF if Ball moved, else #00 if Ball not moved
;
; Corrupts: AF, BC, DE
;
;###############################################################################
move_ball:
ld e, 0
.x:
ld a, b ; Skip if no movement for this axis
cp 0
jr z, .y
.x_minus_5:
cp -5 ; If -5
jr nz, .x_plus_5
ld a, (game_state + BALL_X)
ld (game_state + BALL_OLD_X), a
sub a, 5
ld (game_state + BALL_X), a
ld e, #FF
jr .y
.x_plus_5:
cp 5 ; If 5
jr nz, .y
ld a, (game_state + BALL_X)
ld (game_state + BALL_OLD_X), a
add a, 5
ld (game_state + BALL_X), a
ld e, #FF
jr .y
.y:
ld a, c ; Skip if no movement for this axis
cp 0
ret z
.y_minus_5:
cp -5 ; If -5
jr nz, .y_plus_5
ld a, (game_state + BALL_Y)
ld (game_state + BALL_OLD_Y), a
sub a, 5
ld (game_state + BALL_Y), a
ld e, #FF
ret
.y_plus_5:
cp 5 ; If 5
ret nz
ld a, (game_state + BALL_Y)
ld (game_state + BALL_OLD_Y), a
add a, 5
ld (game_state + BALL_Y), a
ld e, #FF
ret
;###############################################################################
;
; Check the Ball Position and keep it in bounds, clamping it to the edge of the
; playing area. This is only called once the ball has been moved and we are sure
; a goal has not been scored
;
; Corrupts: AF
;
;###############################################################################
check_clip_ball:
.x1:
ld a, (game_state + BALL_X)
cp 3
jp m, .x1_set ; If A < 3, set it to 3
jr .x2
.x1_set:
ld a, 3