-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.asm
executable file
·4696 lines (4421 loc) · 84.2 KB
/
main.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
INCLUDE "macros.asm"
INCLUDE "constants.asm"
INCLUDE "charmap.asm"
SECTION "rst 00", ROM0 [$00]
ret
SECTION "rst 08", ROM0 [$08]
ret
SECTION "rst 10", ROM0 [$10]
ret
SECTION "rst 18", ROM0 [$18]
jp JumpToFuncInTable
SECTION "rst 20", ROM0 [$20]
ret
SECTION "rst 28", ROM0 [$28]
ret
SECTION "rst 30", ROM0 [$30]
ret
SECTION "rst 38", ROM0 [$38]
ret
SECTION "VBlank", ROM0 [$40]
jp VBlankInterruptHandler
SECTION "LCDC",ROM0[$48]
reti
SECTION "Timer", ROM0 [$50]
jp TimerInterruptHandler
SECTION "Serial",ROM0[$58]
jp SerialInterruptHandler
SECTION "Joypad", ROM0 [$60]
reti
SECTION "Entry", ROM0 [$100]
Entry: ; 0x100
nop
jp Start
SECTION "Header", ROM0 [$104]
; The header is generated by rgbfix.
; The space here is allocated to prevent code from being overwritten.
ds $150 - $104
SECTION "Main", ROM0 [$150]
Start: ; 0x150
di
ld sp, $ffff
xor a
ld [rLCDC], a ; Disable LCD Display
ld hl, $c000
ld bc, $2000
call ClearData ; Clear working RAM
ld hl, vTiles0
ld bc, $2000
call ClearData ; Clear VRAM
ld hl, $fe00
ld bc, $a0
call ClearData ; Clear OAM
ld hl, $ff80
ld bc, $7d ; Clear HRAM
call ClearData
ld hl, GameGfx
ld bc, $800
ld de, vTiles0
call LoadGfx
ld a, %11100100
ld [rBGP], a ; Set Background palette
ld [rOBP0], a ; Set Sprite Palette 0
ld [rOBP1], a ; Set Sprite Palette 1
call ClearOAMBuffer
call ClearBackground
ld sp, $dfff ; Initialize stack pointer to end of working RAM
call WriteDMACodeToHRAM
xor a
ld hl, wCurrentScreen
ld [hli], a
ld [hl], a
ld a, $93
ld [rLCDC], a ; Enable LCD Display
ld a,%00000001 ; Enable V-blank interrupt
ld [rIE], a
ei
ld a, MUSIC_GAME
call PlayMusic
jp Main
JumpToFuncInTable:
; Jumps to a function in the pointer table immediately following
; a "rst $18" call. Function must be in the same Bank as the pointer table.
sla a
pop hl
push de
ld e, a
ld d, $0
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld l, e
ld h, d
pop de
jp [hl]
VBlankInterruptHandler:
push af
push bc
push de
push hl
call ReadJoypad
call DrawCurrentScreen
call Music2_UpdateMusic
ld a, 1
ld [VBlankFlag], a
pop hl
pop de
pop bc
pop af
reti
DrawCurrentScreen:
ld a, [wCurrentScreen]
rst $18
DrawScreenFunctions:
dw DrawTitlescreen
dw DrawGame
dw DrawGameOver
DrawTitlescreen:
jp $ff80 ; OAM DMA transfer
DrawGame:
call DrawScore
call DrawPlayerPaddle
call DrawComputerPaddle
; Draw OAM sprites
call ClearOAMBuffer
call DrawBall
call DrawLasers
jp $ff80 ; OAM DMA transfer
DrawGameOver:
jp $ff80 ; OAM DMA transfer
TimerInterruptHandler:
reti
SerialInterruptHandler:
reti
; copies DMA routine to HRAM. By GB specifications, all DMA needs to be done in HRAM (no other memory section is available during DMA)
; This routine is taken from Pokemon Red Version.
WriteDMACodeToHRAM: ; 4bed (1:4bed)
ld c, $80
ld b, $a
ld hl, DMARoutine
.copyLoop
ld a, [hli]
ld [$ff00+c], a
inc c
dec b
jr nz, .copyLoop
ret
DMARoutine: ; 4bfb (1:4bfb)
ld a, (wOAMBuffer >> 8)
ld [$ff00+$46], a ; start DMA
ld a, $28
.waitLoop ; wait for DMA to finish
dec a
jr nz, .waitLoop
ret
ClearData:
; Clears bc bytes starting at hl with value in a.
; bc can be a maximum of $7fff, since it checks bit 7 of b when looping.
dec bc
.clearLoop
ld [hli], a
dec bc
bit 7, b
jr z, .clearLoop
ret
CopyData:
; Copies bc bytes starting at hl to de.
ld a, [hli]
ld [de], a
inc de
dec bc
ld a, b
or c ; have we copied all of the data?
jr nz, CopyData
ret
ClearOAMBuffer:
; Fills OAM buffer memory with $0.
xor a
ld hl, wOAMBuffer
ld bc, $a0 ; size of OAM buffer
jp ClearData
ClearBackground:
; Fills Background memory with $1.
ld a, 1
ld hl, vBGMap0
ld bc,$800 ; size of background memory
jp ClearData
ClearLasers:
; Deletes all lasers
xor a
ld hl, wLasers
ld bc, (MAX_LASERS * 2 * 5)
jp ClearData
LoadGfx:
; This loads data into VRAM. It waits for the LCD H-Blank to copy the data.
; input: hl = source of data
; de = destination for data
; bc = number of bytes to copy
.waitForHBlank
ld a, [$ff41] ; LCDC Status
and $3
jr nz, .waitForHBlank
ld a, [hli]
ld [de], a
inc de
dec bc
ld a, b
or c ; have we copied all of the data?
jr nz, .waitForHBlank
ret
ClearGfx:
; Clear data in VRAM. It waits for the LCD H-Blank to clear the data.
; input: hl = destination to clear
; bc = number of bytes to clear
; d = value to clear with
.waitForHBlank
ld a, [$ff41] ; LCDC Status
and $3
jr nz, .waitForHBlank
ld a, d
ld [hli], a
dec bc
ld a, b
or c ; have we cleared all of the data?
jr nz, .waitForHBlank
ret
PrintText:
; Loads text graphics.
; input: hl = pointer to "@"-terminated string
; de = destination for data
ld bc, $0000 ; Count number of bytes in string
push hl
.loop
ld a, [hli]
cp "@"
jr z, .finishedProcessingString
inc bc
jr .loop
.finishedProcessingString
pop hl
jp LoadGfx
ReadJoypad:
; Reads the current state of the joypad and saves the state into
; some registers the game uses during gameplay.
ld a, $20
ld [rJOYP], a
ld a, [rJOYP]
ld a, [rJOYP]
and $f
swap a
ld b, a
ld a, $30
ld [rJOYP], a
ld a, $10
ld [rJOYP], a
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
ld a, [rJOYP]
and $f
or b
cpl ; a contains currently-pressed buttons
ld [hJoypadState], a
ret
ResetScores:
xor a
ld hl, wPlayerScore
ld [hli], a
ld [hl], a
ret
InitPlayerPaddle:
; Initializes the player's paddle.
ld hl, wPlayerY
ld a, $00
ld [hli], a
ld a, $17
ld [hl], a
ld a, $18
ld [wPlayerHeight], a
ret
InitPlayerLasers:
; Initializes the player's lasers.
xor a
ld hl, wPlayerLasers
ld bc, 5 * MAX_LASERS
jp ClearData
InitComputerPaddle:
; Initializes the computer's paddle.
ld hl, wComputerY
ld a, $00
ld [hli], a
ld a, $17
ld [hl], a
ld a, $18
ld [wComputerHeight], a
ret
InitComputerLasers:
; Initializes the Computer's lasers.
xor a
ld hl, wComputerLasers
ld bc, 5 * MAX_LASERS
jp ClearData
DrawScore:
; Draws the score of the game.
ld a, [wPlayerScore]
cp 10
jr c, .oneDigit
push af
ld a, $21
hlCoord 2, 3, vBGMap0
ld [hl], a
pop af
sub 10
.oneDigit
add $20 ; base tile id for numbers
hlCoord 3, 3, vBGMap0
ld [hl], a
ld a, [wComputerScore]
cp 10
jr c, .oneDigit2
push af
ld a, $21
hlCoord 16, 3, vBGMap0
ld [hl], a
pop af
sub 10
.oneDigit2
add $20 ; base tile id for numbers
hlCoord 17, 3, vBGMap0
ld [hl], a
ret
DrawPlayerPaddle:
; Draws the player's paddle to the screen.
; This also fills in blank tiles along the column where the player's paddle doesn't overlap.
ld a, [wPlayerY + 1]
push af
srl a
srl a
srl a ; Divide by 8 to get the tile index we need to start drawing at.
hlCoord 0, 0, vBGMap0
ld bc, $0020
ld d, $00 ; Count which tile row we're on.
.addLoop
ld [hl], $01
and a
jr z, .gotHLCoord
inc d
add hl, bc ; Move the HL Coord down one row of tiles.
dec a
jr .addLoop
.gotHLCoord
pop af ; Reload player's paddle Y position
and $7 ; Get the pixel offset in the tile. (Tiles are 8 pixels wide)
jr z, .save8
push af
ld e, a
ld a, 8
sub e
ld e, a
pop af
jr .drawTile
.save8
ld e, 8
.drawTile
add $10 ; a now contains tile id for the top tile
ld [hl], a ; Draw the top tile
inc d
ld a, [wPlayerHeight]
sub e
.drawMiddleTiles
add hl, bc
cp a, 8
jr c, .drawBottomTile
sub 8
ld [hl], $10 ; Tile id of the solid paddle tile.
inc d
jr .drawMiddleTiles
.drawBottomTile
and a
jr z, .clearRemainingTiles
add $18
ld [hl], a
inc d
ld a, d
.clearRemainingTiles
cp 18 ; number of rows in the display
jr z, .done
add hl, bc
ld [hl], $01 ; blank tile
inc a
jr .clearRemainingTiles
.done
ret
DrawLasers:
; Loads the player's and the computer's laser sprites into the OAM buffer.
ld hl, wLasers
ld de, wLaserSprites ; OAM buffer destination for laser sprites
ld b, 1 + (MAX_LASERS * 2) ; We're drawing both the player's and the computer's lasers
.loop
dec b
ret z
ld a, [hli] ; check if laser is active
and a
jr nz, .activeLaser
; Laser is inactive, so don't draw it
push bc
ld bc, $0004
add hl, bc ; Move to next laser struct
pop bc
jr .loop
.activeLaser
inc hl ; Skip over the low byte of the y position
ld a, [hli] ; y position (high byte)
add 16 - 4 ; adjust for sprite screen offset (and center the sprite)
ld [de], a
inc de
inc hl ; Skip over the low byte of the x position
ld a, [hli] ; x position (high byte)
add 8 - 4 ; adjust for sprite screen offset (and center the sprite)
ld [de], a
inc de
ld a, $2 ; Laser tile id
ld [de], a
inc de
ld a, %00000000 ; sprite attributes
ld [de], a
inc de
jr .loop
DrawComputerPaddle:
; Draws the computer's paddle to the screen.
; This also fills in blank tiles along the column where the computer's paddle doesn't overlap.
ld a, [wComputerY + 1]
push af
srl a
srl a
srl a ; Divide by 8 to get the tile index we need to start drawing at.
hlCoord 019, 0, vBGMap0
ld bc, $0020
ld d, $00 ; Count which tile row we're on.
.addLoop
ld [hl], $01
and a
jr z, .gotHLCoord
inc d
add hl, bc ; Move the HL Coord down one row of tiles.
dec a
jr .addLoop
.gotHLCoord
pop af ; Reload computer's paddle Y position
and $7 ; Get the pixel offset in the tile. (Tiles are 8 pixels wide)
jr z, .save8
push af
ld e, a
ld a, 8
sub e
ld e, a
pop af
jr .drawTile
.save8
ld e, 8
.drawTile
add $10 ; a now contains tile id for the top tile
ld [hl], a ; Draw the top tile
inc d
ld a, [wComputerHeight]
sub e
.drawMiddleTiles
add hl, bc
cp a, 8
jr c, .drawBottomTile
sub 8
ld [hl], $10 ; Tile id of the solid paddle tile.
inc d
jr .drawMiddleTiles
.drawBottomTile
and a
jr z, .clearRemainingTiles
add $18
ld [hl], a
inc d
ld a, d
.clearRemainingTiles
cp 18 ; number of rows in the display
jr z, .done
add hl, bc
ld [hl], $01 ; blank tile
inc a
jr .clearRemainingTiles
.done
ret
InitBall:
; Initializes the Pong ball
ld hl, wBallY
xor a
ld [hli], a
ld a, BASE_BALL_Y_POSITION
ld [hli], a
xor a
ld [hli], a
ld a, BASE_BALL_X_POSITION
ld [hli], a
ld a, (BASE_BALL_Y_SPEED & $ff)
ld [hli], a
ld a, (BASE_BALL_Y_SPEED >> 8)
ld [hli], a
ld a, (BASE_BALL_X_SPEED & $ff)
ld [hli], a
ld a, (BASE_BALL_X_SPEED >> 8)
ld [hl], a
ret
PlayerScorePoint:
ld a, [wPlayerScore]
inc a
ld [wPlayerScore], a
ld a, START_PLAY_TIME
ld [wStartPlayTimer], a
call InitPlayerPaddle
call InitComputerPaddle
call InitBall
ld hl, wBallY
xor a
ld [hli], a
ld a, BASE_BALL_Y_POSITION
ld [hli], a
xor a
ld [hli], a
ld a, BASE_BALL_X_POSITION
ld [hli], a
ld a, (BASE_BALL_Y_SPEED & $ff)
ld [hli], a
ld a, (BASE_BALL_Y_SPEED >> 8)
ld [hli], a
ld a, (BASE_BALL_X_SPEED_NEGATIVE & $ff)
ld [hli], a
ld a, (BASE_BALL_X_SPEED_NEGATIVE >> 8)
ld [hl], a
call ClearLasers
call CheckForGameFinished
ret
ComputerScorePoint:
ld a, [wComputerScore]
inc a
ld [wComputerScore], a
ld a, START_PLAY_TIME
ld [wStartPlayTimer], a
call InitPlayerPaddle
call InitComputerPaddle
call InitBall
ld hl, wBallY
xor a
ld [hli], a
ld a, BASE_BALL_Y_POSITION
ld [hli], a
xor a
ld [hli], a
ld a, BASE_BALL_X_POSITION
ld [hli], a
ld a, (BASE_BALL_Y_SPEED & $ff)
ld [hli], a
ld a, (BASE_BALL_Y_SPEED >> 8)
ld [hli], a
ld a, (BASE_BALL_X_SPEED & $ff)
ld [hli], a
ld a, (BASE_BALL_X_SPEED >> 8)
ld [hl], a
call ClearLasers
call CheckForGameFinished
ret
CheckForGameFinished:
ld a, [wComputerScore]
cp POINTS_TO_WIN
jr c, .checkPlayer
; Computer won the game
; TODO: do stuff when game is won
ld a, 2 ; computer won
ld [wWinner], a
xor a
ld [wScreenState], a
ld a, SCREEN_GAME_OVER
ld [wCurrentScreen], a
ret
.checkPlayer
ld a, [wPlayerScore]
cp POINTS_TO_WIN
ret c
; Player won the game
ld a, 1 ; player won
ld [wWinner], a
xor a
ld [wScreenState], a
ld a, SCREEN_GAME_OVER
ld [wCurrentScreen], a
ret
DrawBall:
; Draws the Pong ball sprite to OAM.
ld hl, wBallSprite
ld a, [wBallY + 1]
add 16 - 4 ; Add 16 to adjust for the screen offset for sprites.
ld [hli], a
ld a, [wBallX + 1]
add 8 - 4 ; Add 8 to adjust for the screen offset for sprites.
ld [hli], a
ld a, $0 ; tile id for ball's sprite
ld [hli], a
ld a, %00000000
ld [hl], a
ret
MoveBall:
; Updates the ball's position according to its speed.
call UpdateBallYPosition
call UpdateBallXPosition
ret
UpdateBallYPosition:
ld a, [wBallY]
ld l, a
ld a, [wBallY + 1]
ld h, a ; hl contains ball's y position
ld a, [wBallYSpeed]
ld c, a
ld a, [wBallYSpeed + 1]
ld b, a ; bc contains ball's y speed
cp $80
jr c, .movingDown
; Ball is moving up because y speed is negative
add hl, bc
; Check if new position is beyond the top of the screen
ld a, h
cp 220 ; Arbitrary large number so we can detect underflow
jr c, .saveYPosition
; Invert the y speed, and set the y position equal to the top of the screen
call InvertBC
; bc now contains inverted y speed
ld a, c
ld [wBallYSpeed], a
ld a, b
ld [wBallYSpeed + 1], a
ld hl, $0000
jr .saveYPosition
.movingDown
; Ball is moving down because y speed is positive
add hl, bc
; Check if new position is beyond the bottom of the screen
ld a, h
cp 144
jr c, .saveYPosition
; Invert the y speed, and set the y position equal to the bottom of the screen
call InvertBC
; bc now contains inverted y speed
ld a, c
ld [wBallYSpeed], a
ld a, b
ld [wBallYSpeed + 1], a
ld h, 144
ld l, 0
.saveYPosition
ld a, l
ld [wBallY], a
ld a, h
ld [wBallY + 1], a
ret
UpdateBallXPosition:
ld a, [wBallX]
ld l, a
ld a, [wBallX + 1]
ld h, a ; hl contains ball's x position
ld a, [wBallXSpeed]
ld c, a
ld a, [wBallXSpeed + 1]
ld b, a ; bc contains ball's x speed
cp $80
jr c, .movingRight
; Ball is moving left because x speed is negative
add hl, bc
; Check if it hit the player's wall
ld a, h
cp 220 ; Arbitrary large number to check for underflow
jr c, .notTouchingLeftWall
; Computer scored a point!
call ComputerScorePoint
ret
.notTouchingLeftWall
; Check if the ball is hitting the player's paddle
ld a, h
sub 4
cp 4
jp c, .saveXPosition
cp 8
jp nc, .saveXPosition
ld a, [wBallY + 1]
ld e, a
ld a, [wPlayerY + 1]
cp e
jp nc, .saveXPosition
ld d, a
ld a, [wPlayerHeight]
add d
cp e
jp c, .saveXPosition
; Ball is hitting player's paddle
; Invert the x speed, and set the x position equal to the right side of player's paddle.
; Also set the x speed to its base speed.
ld bc, BASE_BALL_X_SPEED
ld a, c
ld [wBallXSpeed], a
ld a, b
ld [wBallXSpeed + 1], a
; Increase or decrease y speed based on where it hit the paddle
ld a, [wBallY + 1]
ld b, a
ld a, [wPlayerY + 1]
ld c, a
ld a, [wPlayerHeight]
srl a
add c ; a is midpoint of player's paddle
cp b
jr c, .bottomOfPlayerPaddle
; ball is hitting top half of player's paddle
ld a, [wBallYSpeed + 1]
cp $80
jr c, .decreaseBallYSpeed
jr .increaseBallYSpeed
.bottomOfPlayerPaddle
; ball is hitting bottom half of player's paddle
ld a, [wBallYSpeed + 1]
cp $80
jr c, .increaseBallYSpeed
jr .decreaseBallYSpeed
.increaseBallYSpeed
call IncreaseBallYSpeed
jr .doneChangingYSpeed
.decreaseBallYSpeed
call DecreaseBallYSpeed
.doneChangingYSpeed
ld hl, $0c00
jr .saveXPosition
.movingRight
add hl, bc
; Check if it hit the computer's wall
ld a, h
cp 160 ; Pixel position of right-side wall
jr c, .notTouchingRightWall
; Player scored a point!
call PlayerScorePoint
ret
.notTouchingRightWall
; Check if the ball is hitting the computer's paddle
ld a, h
add 4
cp 152
jr c, .saveXPosition
cp 156
jr nc, .saveXPosition
ld a, [wBallY + 1]
ld e, a
ld a, [wComputerY + 1]
cp e
jr nc, .saveXPosition
ld d, a
ld a, [wComputerHeight]
add d
cp e
jr c, .saveXPosition
; Ball is hitting computer's paddle
; Invert the x speed, and set the x position equal to the left side of computer's paddle.
; Also set the x speed to its base speed.
ld bc, BASE_BALL_X_SPEED_NEGATIVE
ld a, c
ld [wBallXSpeed], a
ld a, b
ld [wBallXSpeed + 1], a
; Increase or decrease y speed based on where it hit the paddle
ld a, [wBallY + 1]
ld b, a
ld a, [wComputerY + 1]
ld c, a
ld a, [wComputerHeight]
srl a
add c ; a is midpoint of Computer's paddle
cp b
jr c, .bottomOfComputerPaddle
; ball is hitting top half of Computer's paddle
ld a, [wBallYSpeed + 1]
cp $80
jr c, .decreaseBallYSpeed2
jr .increaseBallYSpeed2
.bottomOfComputerPaddle
; ball is hitting bottom half of Computer's paddle
ld a, [wBallYSpeed + 1]
cp $80
jr c, .increaseBallYSpeed2
jr .decreaseBallYSpeed2
.increaseBallYSpeed2
call IncreaseBallYSpeed
jr .doneChangingYSpeed2
.decreaseBallYSpeed2
call DecreaseBallYSpeed
.doneChangingYSpeed2
ld hl, $9400
.saveXPosition
ld a, l
ld [wBallX], a
ld a, h
ld [wBallX + 1], a
ret
InvertBC:
; Inverts the 16-bit value in bc
ld a, b
cpl
ld b, a
ld a, c
cpl
ld c, a
inc c
ret nz
inc b
ret
MoveLasers:
; Updates laser positions according to their speeds.
call MovePlayerLasers
jp MoveComputerLasers
MovePlayerLasers:
ld hl, wPlayerLasers
ld b, MAX_LASERS + 1
.loop
dec b
ret z
ld a, [hli]
and a
jr nz, .activeLaser
inc hl
inc hl
inc hl
inc hl ; Move to next laser struct
jr .loop
.activeLaser
inc hl
inc hl
push hl ; Save pointer to y position
ld a, [hli]
ld e, a
ld a, [hl]
ld d, a ; de contains laser x position
ld hl, BASE_LASER_SPEED_RIGHT
add hl, de
ld d, h
ld e, l
pop hl
ld a, e
ld [hli], a
ld a, d
ld [hli], a
jr .loop
MoveComputerLasers:
ld hl, wComputerLasers
ld b, MAX_LASERS + 1
.loop
dec b
ret z
ld a, [hli]
and a
jr nz, .activeLaser
inc hl
inc hl
inc hl
inc hl ; Move to next laser struct
jr .loop
.activeLaser
inc hl
inc hl
push hl ; Save pointer to y position
ld a, [hli]
ld e, a
ld a, [hl]
ld d, a ; de contains laser x position
ld hl, BASE_LASER_SPEED_LEFT
add hl, de
ld d, h
ld e, l
pop hl
ld a, e
ld [hli], a
ld a, d
ld [hli], a
jr .loop
ShootLasers:
call ShootPlayerLasers
call ShootComputerLasers
ret
ShootPlayerLasers:
; If player is pressing "shoot" button, fire a laser.
ld a, [hJoypadState]
bit BIT_A_BUTTON, a
jr z, .done
ld a, [wPlayerLaserCooldown]
and a
jr nz, .done
ld hl, wPlayerLasers
ld b, MAX_LASERS + 1
; Loop to find the first inactive laser, if there is one
.loop
dec b
jr z, .done ; Maximum number of lasers are currently in play
ld a, [hl]
and a
jr z, .foundInactiveLaser
; Check the next laser
inc hl
inc hl
inc hl
inc hl
inc hl
jr .loop
.foundInactiveLaser
ld a, 1
ld [hli], a ; Set laser state to "active"
; Set laser's position in front of the player's paddle
xor a
ld [hli], a ; Set low byte of laser's y position
ld a, [wPlayerY + 1]
ld c, a
ld a, [wPlayerHeight]
srl a
add c ; a is now the y-midpoint of the player's paddle
ld [hli], a
xor a
ld [hli], a
ld a, $a
ld [hli], a ; Set the laser's x position just to the right of the player's paddle
; Player has to wait awhile before firing another laser
ld a, LASER_COOLDOWN
ld [wPlayerLaserCooldown], a
ret