-
Notifications
You must be signed in to change notification settings - Fork 2
/
enemy.asm
865 lines (837 loc) · 14.1 KB
/
enemy.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
.ifndef ENEMY_INC
ENEMY_INC = 1
.include "tiles.inc"
.include "globals.asm"
.include "sprite.asm"
.include "debug.asm"
.include "joystick.asm"
.include "tilelib.asm"
.include "timer.asm"
enemies_cleared: .byte 0
enemy_map: .byte 0,0,0,1,2,3
ENEMY1_INIT = $41
ENEMY2_INIT = $63
ENEMY3_INIT = $82
ENEMY4_INIT = $A2
; Enemy status:
; Bits 7-5: sprite index
; Bit 4: moving
; Bit 3: vulnerable
; Bit 2: eyes only
; Bit 1-0: Direction (0:R,1:L,2:D,3:U)
enemies:
enemy1: .byte ENEMY1_INIT
enemy2: .byte ENEMY2_INIT
enemy3: .byte ENEMY3_INIT
enemy4: .byte ENEMY4_INIT
end_enemies:
ENEMY_SPRITE_IDX = $E0
ENEMY_MOVING = $10
ENEMY_VULNERABLE = $08
ENEMY_EYES_ONLY = $04
ENEMY_DIRECTION = $03
ENEMY1_TGT_X = 18
ENEMY2_TGT_X = 1
ENEMY3_TGT_X = 19
ENEMY4_TGT_X = 0
ENEMY1_TGT_Y = 0
ENEMY2_TGT_Y = 0
ENEMY3_TGT_Y = 14
ENEMY4_TGT_Y = 14
BOX_X = 9
BOX_Y = 7
target_x: .byte ENEMY1_TGT_X, ENEMY2_TGT_X, ENEMY3_TGT_X, ENEMY4_TGT_X
target_y: .byte ENEMY1_TGT_Y, ENEMY2_TGT_Y, ENEMY3_TGT_Y, ENEMY4_TGT_Y
body_frames: .byte 14, 14, 15, 16
vuln_frames: .byte 17, 17, 18, 19
enemy_flips: .byte NO_FLIP, H_FLIP, NO_FLIP, NO_FLIP
eye_frames: .byte 20, 20, 21, 21
eye_flips: .byte NO_FLIP, H_FLIP, NO_FLIP, V_FLIP
reverse_dir: .byte $1, $0, $3, $2
ticks_vuln_rem: .word 0
chase: .byte 0 ; 0=scatter mode, 1=chase mode
ticks_mode_rem: .word 300 ; ticks remaining until switch between chase and scatter
enemy_clear:
lda #1
sta enemies_cleared
jsr enemy_stop
lda #ENEMY1_idx
jsr sprite_disable
lda #ENEMY2_idx
jsr sprite_disable
lda #ENEMY3_idx
jsr sprite_disable
lda #ENEMY4_idx
jsr sprite_disable
rts
enemy_reset:
stz enemies_cleared
stz ticks_vuln_rem
stz ticks_vuln_rem+1
lda #ENEMY1_INIT
sta enemy1
lda #ENEMY2_INIT
sta enemy2
lda #ENEMY3_INIT
sta enemy3
lda #ENEMY4_INIT
sta enemy4
lda #1
jsr __enemy_scatter
rts
__enemy_scatter: ; A: 1=reset mode timer, 0=continue mode
cmp #0
beq @update_targets
stz chase
lda scatter_time
sta ticks_mode_rem
lda scatter_time+1
sta ticks_mode_rem+1
@update_targets:
lda #ENEMY1_TGT_X
sta target_x
lda #ENEMY2_TGT_X
sta target_x+1
lda #ENEMY3_TGT_X
sta target_x+2
lda #ENEMY4_TGT_X
sta target_x+3
lda #ENEMY1_TGT_Y
sta target_y
lda #ENEMY2_TGT_Y
sta target_y+1
lda #ENEMY3_TGT_Y
sta target_y+2
lda #ENEMY4_TGT_Y
sta target_y+3
rts
make_vulnerable: ; A: 15ths of seconds (0 to 17 seconds)
sta ticks_vuln_rem
asl ticks_vuln_rem
rol ticks_vuln_rem+1
asl ticks_vuln_rem
rol ticks_vuln_rem+1
ldx #0
@loop:
lda enemies,x
bit #ENEMY_EYES_ONLY
bne @end_loop
ora #ENEMY_VULNERABLE
sta enemies,x
@end_loop:
inx
cpx #(end_enemies-enemies)
bne @loop
lda #0
jsr __enemy_scatter
jsr __enemy_reverse
rts
enemy_tick:
lda enemies_cleared
cmp #0
beq @start
jmp @return
@start:
lda ticks_vuln_rem+1
cmp #0
bne @dec_ticks
lda ticks_vuln_rem
beq @mode_tick
@dec_ticks:
sec
lda ticks_vuln_rem
sbc #1
sta ticks_vuln_rem
lda ticks_vuln_rem+1
sbc #0
sta ticks_vuln_rem+1
bra @check_eyes_mode
@enemy_temp: .byte 0
@sprite_idx: .byte 0
@mode_tick:
jsr __enemy_mode_tick
lda chase
beq @check_eyes_mode
jsr __enemy_chase_targets
@check_eyes_mode:
jsr __enemy_eyes_targets
@start_loop:
ldx #0
@loop:
phx
lda enemies,x
sta @enemy_temp
lsr
lsr
lsr
lsr
lsr
sta @sprite_idx
lda @enemy_temp
bit #ENEMY_MOVING
bne @move
jmp @set_frame
@move:
jsr __enemy_move
plx
lda enemies,x
sta @enemy_temp
phx
@set_frame:
lda @enemy_temp
bit #ENEMY_VULNERABLE
beq @check_eyes
lda ticks_vuln_rem+1
bne @check_ending
lda ticks_vuln_rem
bne @check_ending
lda @enemy_temp
and #$F7
sta @enemy_temp
bra @normal
@check_ending:
lda ticks_vuln_rem+1
bne @vulnerable
lda ticks_vuln_rem
cmp #90
bcs @vulnerable
bit #ENEMY_VULNERABLE
bne @normal ; flash to normal frame every 8 ticks for last 1.5 seconds
@vulnerable:
lda @enemy_temp
and #ENEMY_DIRECTION
tax
lda vuln_frames,x
ldy enemy_flips,x
ldx @sprite_idx
jsr sprite_frame
jmp @end_loop
@check_eyes:
lda @enemy_temp
bit #ENEMY_EYES_ONLY
beq @normal
and #ENEMY_DIRECTION
tax
lda eye_frames,x
ldy eye_flips,x
ldx @sprite_idx
jsr sprite_frame
jmp @end_loop
@normal:
lda @enemy_temp
and #ENEMY_DIRECTION
tax
lda body_frames,x
ldy enemy_flips,x
ldx @sprite_idx
jsr sprite_frame
@end_loop:
plx
lda @enemy_temp
sta enemies,x
inx
cpx #(end_enemies-enemies)
beq @return
jmp @loop
@return:
rts
__enemy_mode_tick:
lda enemy1
bit #ENEMY_MOVING
beq @return
lda ticks_mode_rem+1
cmp #0
bne @dec_ticks
lda ticks_mode_rem
beq @change_mode
@dec_ticks:
sec
lda ticks_mode_rem
sbc #1
sta ticks_mode_rem
lda ticks_mode_rem+1
sbc #0
sta ticks_mode_rem+1
bra @return
@change_mode:
lda chase
bne @scatter
lda #1
sta chase
lda chase_time
sta ticks_mode_rem
lda chase_time+1
sta ticks_mode_rem+1
jsr __enemy_reverse
bra @return
nop
@scatter:
lda #1
jsr __enemy_scatter
jsr __enemy_reverse
@return:
rts
__enemy_chase_targets:
bra @start
@player_x: .byte 0
@player_y: .byte 0
@player_dir: .byte 0
@ref_x: .byte 0
@ref_y: .byte 0
@start:
lda #PLAYER_idx
ldx #1
jsr sprite_getpos
stx @player_x
sty @player_y
lda #$0C
and player
lsr
lsr
sta @player_dir
; ENEMY1: target = player tile
stx target_x
sty target_y
; ENEMY2: target = 4 tiles ahead of player
lda @player_dir
cmp #DIR_RIGHT
bne @e2_check_left
lda @player_x
clc
adc #2
sta target_x+2 ; Sneak in the start of enemy 3 targeting
adc #2
sta target_x+1
lda @player_y
sta target_y+1
sta target_y+2
jmp @enemy3
@e2_check_left:
lda @player_dir
cmp #DIR_LEFT
bne @e2_check_down
lda @player_x
sec
sbc #2
sta target_x+2
sbc #2
bpl @e2_left_set_x
lda #0
sta target_x+2
@e2_left_set_x:
sta target_x+1
lda @player_y
sta target_y+1
sta target_y+2
jmp @enemy3
@e2_check_down:
lda @player_dir
cmp #DIR_DOWN
bne @e2_up
lda @player_x
sta target_x+1
sta target_x+2
lda @player_y
clc
adc #2
sta target_y+2
adc #2
sta target_y+1
jmp @enemy3
@e2_up:
lda @player_x
sta target_x+1
sta target_x+2
lda @player_y
sec
sbc #2
sta target_y+2
sbc #2
bpl @e2_up_set_y
lda #0
sta target_y+2
@e2_up_set_y:
sta target_y+1
; ENEMY 3 : double the vector to half the offset of enemy 2's target
@enemy3:
lda #ENEMY1_idx
ldx #1
jsr sprite_getpos
stx @ref_x
sty @ref_y
lda target_x+2
sec
sbc @ref_x
sta target_x+2
clc
adc target_x+2
clc
adc @ref_x
bpl @e3_set_x
lda #0
@e3_set_x:
sta target_x+2
lda target_y+2
sec
sbc @ref_y
sta target_y+2
clc
adc target_y+2
clc
adc @ref_y
bpl @e3_set_y
lda #0
@e3_set_y:
sta target_y+2
; ENEMY 4: same as enemy 1 unless < 8 tiles away from player, then scatter
lda #ENEMY4_idx
ldx #1
jsr sprite_getpos
stx @ref_x
sty @ref_y
lda @player_x
sec
sbc @ref_x
sta @ref_x
bpl @e4_check_x
lda #0
sec
sbc @ref_x
sta @ref_x
@e4_check_x:
cmp #8
bmi @e4_calc_y
jmp @e4_chase
@e4_calc_y:
lda @player_y
sec
sbc @ref_y
sta @ref_y
bpl @e4_check_y
lda #0
sec
sbc @ref_y
sta @ref_y
@e4_check_y:
cmp #8
bmi @e4_calc_x2
jmp @e4_chase
@e4_calc_x2:
lda @ref_x
tax
clc
@e4_x2_loop:
cpx #0
beq @e4_calc_y2
adc @ref_x
dex
bra @e4_x2_loop
@e4_calc_y2:
sta @ref_x
lda @ref_y
tax
clc
@e4_y2_loop:
cpx #0
beq @e4_calc_hyp
adc @ref_y
dex
bra @e4_y2_loop
@e4_calc_hyp:
adc @ref_x
cmp #64
bpl @e4_chase
lda #ENEMY4_TGT_X
sta target_x+3
lda #ENEMY4_TGT_Y
sta target_y+3
bra @return
@e4_chase:
lda target_x
sta target_x+3
lda target_y
sta target_y+3
@return:
rts
__enemy_eyes_targets:
ldy #0
@loop:
lda enemies,y
bit #ENEMY_EYES_ONLY
beq @next
and #ENEMY_SPRITE_IDX
lsr
lsr
lsr
lsr
lsr
ldx #1
phy
jsr sprite_getpos
tya
ply
cmp #BOX_Y
bne @set_target
cpx #BOX_X
bne @set_target
lda enemies,y
and #$FB
sta enemies,y
bra @next
@set_target:
lda #BOX_X
sta target_x,y
lda #BOX_Y
sta target_y,y
@next:
iny
cpy #(end_enemies-enemies)
bmi @loop
rts
__enemy_reverse:
ldx #0
@loop:
lda enemies,x
and #ENEMY_DIRECTION
tay
lda reverse_dir,y
tay
lda enemies,x
and #$FC
sta enemies,x
tya
ora enemies,x
sta enemies,x
inx
cpx #(end_enemies-enemies)
bne @loop
rts
__enemy_move: ; X: enemy offset (0-3)
bra @start
@offset: .byte 0
@enemy: .byte 0
@last_x: .byte 0
@last_y: .byte 0
@target_x: .byte 0
@target_y: .byte 0
@diff_x: .byte 0
@diff_y: .byte 0
@index: .byte 0
@last_dir: .byte 0
@available: .byte 0,0,0,0
@rand: .byte 0
@blocked: .byte 0 ; bit 3: N, bit 2: S, bit 1: W, bit 0: E
@EAST_BLOCKED = $01
@WEST_BLOCKED = $02
@SOUTH_BLOCKED = $04
@NORTH_BLOCKED = $08
@start:
stx @offset
@get_dir:
lda enemies,x
sta @enemy
and #ENEMY_DIRECTION
sta @last_dir
lda target_x,x
sta @target_x
lda target_y,x
sta @target_y
lda #ENEMY_SPRITE_IDX
and @enemy
lsr
lsr
lsr
lsr
lsr
sta @index
ldx #1
jsr sprite_getpos
stx @last_x
sty @last_y
cmp #0
beq @check_east
jmp @continue
@check_east:
stz @blocked
lda @last_dir
cmp #DIR_LEFT
beq @east_blocked
ldx @last_x
ldy @last_y
cpx #SPRITE_MAX_X
beq @east_blocked
inx
lda #1
jsr get_tile
cpx #HOME_FENCE
beq @east_blocked
cpx #BLANK
beq @check_west
cpx #PELLET
bpl @check_west
@east_blocked:
lda @blocked
ora #@EAST_BLOCKED
sta @blocked
@check_west:
lda @last_dir
cmp #DIR_RIGHT
beq @west_blocked
ldx @last_x
cpx #SPRITE_MIN_X
beq @west_blocked
dex
ldy @last_y
lda #1
jsr get_tile
cpx #HOME_FENCE
beq @west_blocked
cpx #BLANK
beq @check_south
cpx #PELLET
bpl @check_south
@west_blocked:
lda @blocked
ora #@WEST_BLOCKED
sta @blocked
@check_south:
lda @last_dir
cmp #DIR_UP
beq @south_blocked
ldx @last_x
ldy @last_y
cpy #SPRITE_MAX_Y
beq @south_blocked
iny
lda #1
jsr get_tile
cpx #BLANK
beq @check_north
cpx #HOME_FENCE
beq @check_eyes_fence
cpx #PELLET
bpl @check_north
bra @south_blocked
@check_eyes_fence:
lda @enemy
bit #ENEMY_EYES_ONLY
bne @check_north
@south_blocked:
lda @blocked
ora #@SOUTH_BLOCKED
sta @blocked
@check_north:
lda @last_dir
cmp #DIR_DOWN
beq @north_blocked
ldy @last_y
cpy #SPRITE_MIN_Y
beq @north_blocked
dey
ldx @last_x
lda #1
jsr get_tile
cpx #BLANK
beq @calc_dir
cpx #PELLET
bpl @calc_dir
@north_blocked:
lda @blocked
ora #@NORTH_BLOCKED
sta @blocked
bra @calc_dir
@continue:
lda @last_dir ; continue in last direction until fully into next tile
asl
tax
lda @index
jmp (@jmp_table,x)
@jmp_table:
.word @move_right
.word @move_left
.word @move_down
.word @move_up
@calc_dir:
sec
lda @target_x
sbc @last_x
sta @diff_x
sec
lda @target_y
sbc @last_y
sta @diff_y
bpl @check_sw
jmp @north
@check_sw:
lda @diff_x
bpl @southeast
jmp @southwest
@southeast:
lda @blocked
and #$05
cmp #$05
beq @se_check_north
cmp @diff_y
bmi @se_south
@se_east:
lda @blocked
bit #@EAST_BLOCKED
bne @se_south
jmp @set_right
@se_south:
lda @blocked
bit #@SOUTH_BLOCKED
bne @se_east
jmp @set_down
@se_check_north:
jmp @nw_north
@southwest:
lda @blocked
and #$06
cmp #$06
beq @sw_check_north
lda @diff_x
bpl @sw_south
sec
lda #0
sbc @diff_x
cmp @diff_y
bpl @sw_west
@sw_south:
lda @blocked
bit #@SOUTH_BLOCKED
bne @sw_west
jmp @set_down
@sw_west:
lda @blocked
bit #@WEST_BLOCKED
bne @sw_south
jmp @set_left
@sw_check_north:
jmp @ne_north
@north:
lda @diff_x
bpl @northeast
jmp @northwest
@northeast:
lda @blocked
and #$09
cmp #$09
beq @ne_check_west
lda @diff_y
bpl @ne_east
sec
lda #0
sbc @diff_y
cmp @diff_x
bmi @ne_east
@ne_north:
lda @blocked
bit #@NORTH_BLOCKED
bne @ne_east
jmp @set_up
@ne_east:
lda @blocked
bit #@EAST_BLOCKED
bne @ne_north
jmp @set_right
@ne_check_west:
jmp @sw_west
@northwest:
lda @blocked
and #$0A
cmp #$0A
beq @nw_check_south
lda @diff_x
cmp @diff_y
bmi @nw_north
@nw_west:
lda @blocked
bit #@WEST_BLOCKED
bne @nw_north
jmp @set_left
@nw_north:
lda @blocked
bit #@NORTH_BLOCKED
bne @nw_west
jmp @set_up
@nw_check_south:
jmp @se_south
@set_right:
lda #0
bra @set
@set_left:
lda #1
bra @set
@set_down:
lda #2
bra @set
@set_up:
lda #3
@set:
sta @last_dir
lda @enemy
and #$FC
ora @last_dir
ldx @offset
sta enemies,x
jmp @continue
@move_right:
ldx #TICK_MOVEMENT
jsr move_sprite_right
bra @return
@move_left:
ldx #TICK_MOVEMENT
jsr move_sprite_left
bra @return
@move_down:
ldx #TICK_MOVEMENT
jsr move_sprite_down
bra @return
@move_up:
ldx #TICK_MOVEMENT
jsr move_sprite_up
@return:
rts
enemy_check_vuln: ; Input: X: sprite index
; Output: A: 0=not vulnerable, 1=vulnerable
ldy enemy_map,x
lda enemies,y
and #ENEMY_VULNERABLE
lsr
lsr
lsr
rts
enemy_check_eyes: ; Input: X: sprite index
; Output: A: 0=full body, 1=eyes only
ldy enemy_map,x
lda enemies,y
and #ENEMY_EYES_ONLY
lsr
lsr
rts
enemy_release: ; X: sprite index
ldy enemy_map,x
lda enemies,y
ora #ENEMY_MOVING
sta enemies,y
rts
enemy_stop:
ldx #ENEMY1_idx
@loop:
ldy enemy_map,x
lda enemies,y
and #$EF
sta enemies,y
inx
cpx #(ENEMY4_idx+1)
bmi @loop
rts
enemy_eaten: ; X: sprite index
ldy enemy_map,x
lda enemies,y
and #$F7
ora #ENEMY_EYES_ONLY
sta enemies,y
rts
.endif