-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprites.asm
2397 lines (2013 loc) · 43.7 KB
/
sprites.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
; this sub routine calls all sprite
; routines that are needed
; if sprtie_disable flag is set no updates will occur
; inputs:
; sprite_tile_size -> amount of sprites in use on current map
; side effects:
; updates all sprites up to 8 per frame
update_sprites:
lda game_flags
and #%01000000 ; sprite enable flag
beq @done
; we need sprite tile size + 1
ldx sprite_tile_size
inx
stx temp
; shuffle sprites to
; get everyone to render every once in a while
ldy sprite_tile_size
cpy #$FF
beq @done
@shuffle_loop:
lda sprite_tile_obj, y
tax
inx
txa
sec
sbc #AI_SPRITES_START
; and #%00001111 ; remove this bit
cmp temp ; sprite tile size
bcc @not_out_of_bounds
@out_of_bounds:
ldx #AI_SPRITES_START
@not_out_of_bounds:
txa
sta sprite_tile_obj, y
dey
cpy #$FF
bne @shuffle_loop
ldy sprite_tile_size
cpy #$FF
beq @done
@loop:
ldx sprite_tile_ai, y
lda sprite_ai_lo, x
sta src_ptr
lda sprite_ai_hi, x
sta src_ptr+1
jsr jsr_indirect
dey
cpy #$FF
bne @loop
@done:
rts
; this sub routine adjusts sprite positon
; inputs:
; y -> pointing to sprite data offset
; sprite data documentation:
; this AI type uses sprite_data as an offset to its x or y position
; the lower 4 bits are the actual offset value
; 7th bit = 1 -> sbc; = 0 -> adc
; 6th bit = 1 -> x position; = 0 -> y position
; 5th bit = 1 -> inits falling animation, if lower 4 bits are 0 hides sprite
; returns:
; x position in temp
; y positon in temp+1
sprite_pos_adjust:
; load x position
ldx sprite_tile_x, y
lda tile_convert_table, x
sta temp
; load y position
ldx sprite_tile_y, y
lda tile_convert_table, x
sta temp+1
; set if we need fine tuning
lda sprite_tile_data, y
and #%00001111
beq @no_adjust ; if the lower 4 bits are already 0 there is no need to adjust
; now test what value we need to use as a base
lda sprite_tile_data, y
sta temp+3 ; used for bit instruction
bit temp+3 ; N flag = bit 7, V flag = bit 6
bvs @y_position ; test bit 7
; x position adjust
; test if sbc or adc
bpl @x_add
; x sub
lda sprite_tile_data, y
and #%00001111
sta temp+3 ; store for sub
lda temp
sec
sbc temp+3
sta temp
jmp @adjust_done
@x_add:
lda sprite_tile_data, y
lda sprite_tile_data, y
and #%00001111
clc
adc temp
sta temp
jmp @adjust_done
@y_position:
; y position adjust
; test if sbc or adc
bpl @y_add
; y sub
lda sprite_tile_data, y
and #%00001111
sta temp+3 ; store for sub
lda temp+1
sec
sbc temp+3
sta temp+1
jmp @adjust_done
@y_add:
lda sprite_tile_data, y
lda sprite_tile_data, y
and #%00001111
clc
adc temp+1
sta temp+1
@adjust_done:
lda sprite_tile_data, y
sec
sbc #$01
sta sprite_tile_data, y
@no_adjust:
rts
; this sub routine hides a sprite if it is outside of
; the players visible range
; in low visiblity mode
; inputs:
; load_flags -> to test for low visibility mode
; sprite_ptr -> pointing to the sprite in question
; player_x and player_y -> player position
; get_tile_x and get_tile_y -> sprites positon
; returns:
; a = 0 -> was offscreen
; a = 1 -> was onscreen
; side effects:
; changes sprites graphics to $24 (empty) if out of range
sprite_offscreen:
lda load_flags
and #%01000000
beq @done ; if not in low mode skip
; check if x and y are offscreen
lda player_x
ldx get_tile_x
jsr calc_distance
cmp #VISIBILITY_RADIUS
bcs @offscreen ; if greater it is offscreen
lda player_y
ldx get_tile_y
jsr calc_distance
cmp #VISIBILITY_RADIUS
bcc @done
@offscreen:
ldy #$01
lda #$24
sta (sprite_ptr), y
lda #$00
rts
@done:
lda #$01
rts
; this sub routine verifies a sprite move
; handles general collision
; inputs:
; y -> pointing to sprite data offset
; get_tile_x -> proposed x positon
; get_tile_y -> proposed y position
; returns:
; a = 0 -> no collision
; a = 1 -> collision
; sets bit 6 of sprite_tile_data to 1 if collision occured with empty tile
; sets sprite_tile_data lower 4 bits to fine-tune offset
; side effects:
; sprtie position is updated if no collision occurs
verify_sprite_move:
sty temp ; need y value again
; verify that the move can go ahead
jsr get_tile
and #%01111111 ; bit 7 does not matter
cmp #$24 ; empty tile
bne @not_empty
ldy temp ; need y value again
; if it is an empty tile set the disable flag (or falling flag),
; this will remove the sprite when the offset in
; tile_data reaches 0
lda sprite_tile_data, y
ora #%00100000
sta sprite_tile_data, y
; this is a branch always since a is a constant value
; saves a byte
bne @no_collision
@not_empty:
cmp #CLEARABLE_TILES_START
bcc @collision
cmp #CLEARABLE_TILES_END
bcs @collision
; test all active sprites to verify we are not colliding
ldy sprite_tile_size
cpy #$FF
beq @no_collision
@sprite_collision_loop:
lda sprite_tile_flags, y
and #%10000000 ; check if sprite is enabled in the first place
beq @skip ; if not, skip
; verify location matches, if so collision
lda sprite_tile_x, y
cmp get_tile_x
bne @skip
lda sprite_tile_y, y
cmp get_tile_y
beq @collision
@skip:
dey
cpy #$FF
bne @sprite_collision_loop
@no_collision:
ldy temp
lda sprite_tile_data, y
ora #$08 ; fine tuning offset for sprite to move
sta sprite_tile_data, y
lda get_tile_x
sta sprite_tile_x, y
lda get_tile_y
sta sprite_tile_y, y
lda #$00
rts
@collision:
ldy temp ; restore temp
lda #$01
rts
; this sub routine loops through all sprite tiles
; inputs:
; player_x, _y
; returns:
; a = 1 if collision occured
; a = 0 if no collision
sprite_collision:
ldx #$00
txa
pha ; push value to stack, this value is the collision result
sta collision_counter ; reuse menu select to count amount of collisions
@loop:
lda sprite_tile_flags, x
and #%10000000 ; enable flag,
beq @no_collision
lda sprite_tile_x, x
cmp player_x
bne @no_collision
lda sprite_tile_y, x
cmp player_y
beq @collision ; if both passed collision occured
@no_collision:
inx
cpx #SPRITE_TILES
bne @loop
pla ; pull result ; lda #$00
rts
@collision:
lda sprite_tile_ai, x
tay
; call collision routine
lda sprite_collision_lo, y
sta src_ptr
lda sprite_collision_hi, y
sta src_ptr+1
txa
pha ; store x value for later
tay ; put data offset into y
jsr jsr_indirect
sta temp
pla
tax ; get x value back
pla
ora temp
pha ; next result
inc collision_counter
; This was part of the code when
; collision was checked during time critical code
; it is now redundant
; lda collision_counter
; cmp #$02 ; no more than 3 please
; bne @no_wait
; lag frame if too many collisions happen
; lda #$00
; sta collision_counter
; vblank_wait
; sta $2005
; sta $2005 ; no scroll
; @no_wait:
jmp @no_collision
; default sprite init, no special stuff
; inits the sprite as a barrier
; this routine should not destroy any registers
; inputs:
; y -> pointing to sprite data offset
sprite_init_default:
pha
tya
pha
txa
pha
; reset sprite flags and data
lda #$00
sta sprite_tile_data, y
sta sprite_tile_temp, y
lda level
sta sprite_tile_hp, y
lda #%00000000 ; turn off because collision is handeled by tile in this case
sta sprite_tile_flags, y
; set up location
lda sprite_tile_obj, y
tax
lda obj_index_to_addr, x
sta sprite_ptr
tya
tax ; need y value, but need y for indirect,y
lda sprite_tile_x, x
sta temp
lda sprite_tile_y, x
sta temp+1
ldx temp+1
lda tile_convert_table, x ; y position
ldy #$00 ; for indirect, y
sta (sprite_ptr), y
ldx temp
lda tile_convert_table, x ; x position
ldy #$03
sta (sprite_ptr), y
pla
tax
pla
tay
pla
rts
; default sprite update routine
; this routine should not destry any registers
; updates a barrier sprite
; inputs:
; y -> pointing to sprite data offset
; stores if it was already onscreen once in sprite_tile_temp
sprite_update_default:
pha
tya
pha
txa
pha
tya
pha ; push y value for later
; test hit flag
; this gets triggered every frame
; due to the hit flag being cleared
; this is fine as long as weapon timer is even
; and gives a flickering effect
lda sprite_tile_flags, y
and #%01000000
bne @not_hit
; test for sword hit
lda weapon_x
cmp sprite_tile_x, y
bne @not_hit
lda weapon_y
cmp sprite_tile_y, y
bne @not_hit
; invert barrier on hit, wastes a turn
lda map_flags
eor #%10000000
sta map_flags
; set hit flag, this flag is reset at the end of attack animation only
lda sprite_tile_flags, y
ora #%01000000
sta sprite_tile_flags, y
@not_hit:
; load x position
ldx sprite_tile_x, y
stx get_tile_x
lda tile_convert_table, x
sta temp
; load y position
ldx sprite_tile_y, y
stx get_tile_y
lda tile_convert_table, x
sta temp+1
; set up pointer
lda sprite_tile_obj, y
tax
lda obj_index_to_addr, x
sta sprite_ptr
; check if barrier flag is set, if so make
; tile appear as barrier
lda map_flags
and #%10000000
bne @barrier_clear
; barrier is not clear
lda #%10000000
sta sprite_tile_flags, y
lda #$50
bne @done
; barrier is clear
@barrier_clear:
lda #%00000000
sta sprite_tile_flags, y
lda #$24 ; empty
@done:
; store in sprite
ldy #$01
sta (sprite_ptr), y
; positon sprite
ldy #$00
ldx temp+1
dex
txa
sta (sprite_ptr), y
; attributes
lda #$00
ldy #$02
sta (sprite_ptr), y
ldy #$03
lda temp
sta (sprite_ptr), y
; avoid moving offscreen if
; it was alrady onscreen once before
pla
tay
lda sprite_tile_temp, y
bne @no_offscreen
sty temp+1 ; need it again
jsr sprite_offscreen
ldy temp+1
sta sprite_tile_temp, y
@no_offscreen:
pla
tax
pla
tay
pla
rts
; same routine as above, but it inverts the barrier behaviour
sprite_update_barrier_invert:
pha
tya
pha
txa
pha
tya
pha ; keep y for later
; test hit flag
; this gets triggered every frame
; due to the hit flag being cleared
; this is fine as long as weapon timer is even
; and gives a flickering effect
lda sprite_tile_flags, y
and #%01000000
bne @not_hit
; test for sword hit
lda weapon_x
cmp sprite_tile_x, y
bne @not_hit
lda weapon_y
cmp sprite_tile_y, y
bne @not_hit
; invert barrier on hit, wastes a turn
lda map_flags
eor #%10000000
sta map_flags
; set hit flag, this flag is reset at the end of attack animation only
lda sprite_tile_flags, y
ora #%01000000
sta sprite_tile_flags, y
@not_hit:
; load x position
ldx sprite_tile_x, y
stx get_tile_x
lda tile_convert_table, x
sta temp
; load y position
ldx sprite_tile_y, y
stx get_tile_y
lda tile_convert_table, x
sta temp+1
; set up pointer
lda sprite_tile_obj, y
tax
lda obj_index_to_addr, x
sta sprite_ptr
; check if barrier flag is set, if so make
; tile appear as barrier
lda map_flags
and #%10000000
sta sprite_tile_flags, y
beq @barrier_clear
; barrier is not clear
lda #$50
bne @done
; barrier is clear
@barrier_clear:
lda #$24 ; empty
@done:
; store in sprite
ldy #$01
sta (sprite_ptr), y
; positon sprite
ldy #$00
ldx temp+1 ; adjust by -1
dex
txa
sta (sprite_ptr), y
ldy #$03
lda temp
sta (sprite_ptr), y
; attributes
lda #$00
ldy #$02
sta (sprite_ptr), y
; avoid moving offscreen if
; it was alrady onscreen once before
pla
tay
lda sprite_tile_temp, y
bne @no_offscreen
sty temp+1 ; need it again
jsr sprite_offscreen
ldy temp+1
sta sprite_tile_temp, y
@no_offscreen:
pla
tax
pla
tay
pla
rts
; default sprite on collision handler
; inputs:
; y -> pointing to sprite data offset
; returns:
; a = 1 if collision was valid
; a = 0 when collision was invalid
sprite_on_collision:
lda #$01
rts
; inits the push tile
; inputs:
; y -> sprite data offset
sprite_init_push:
pha
tya
pha
txa
pha
jsr sprite_init_default
lda #$80 ; enable flag
sta sprite_tile_flags, y
pla
tax
pla
tay
pla
rts
rts
; this sub routine updates a push block
; inputs:
; y -> pointing to sprite data offset
; sprite data documentation:
; this AI type uses sprite_data as an offset to its x or y position
; the lower 4 bits are the actual offset value
; 7th bit = 1 -> sbc; = 0 -> adc
; 6th bit = 1 -> x position; = 0 -> y position
; 5th bit = 1 -> inits falling animation, if lower 4 bits are 0 hides sprite
; stores if object was on-screen before in sprite_tile_temp:
; 0th bit = 1 -> was oncreen before
sprite_update_push:
pha
tya
pha
txa
pha
; is being hit? destroy
lda sprite_tile_x, y
cmp weapon_x
bne @no_weapon_hit
lda sprite_tile_y, y
cmp weapon_y
bne @no_weapon_hit
; test if hit flag is set
lda sprite_tile_flags, y
and #%01000000
bne @no_weapon_hit
lda sprite_tile_flags, y
ora #%01000000
sta sprite_tile_flags, y ; set hit flag
lda sprite_tile_hp, y
tax
dex
txa
sta sprite_tile_hp, y
bne @no_weapon_hit ; if not 0 hp do not move
jsr init_hit_noise ; noise for hit
lda sprite_tile_x, y
sta get_tile_x
lda sprite_tile_y, y
sta get_tile_y
lda #$00
jsr init_damage_animation
lda #$00 ; if hit move offscreen
sta sprite_tile_x, y
sta sprite_tile_y, y
@no_weapon_hit:
jsr sprite_pos_adjust
lda #$37
sta temp+2 ; sprite value
; check data flag to see if we need to disable
lda sprite_tile_data, y
; check falling flag (disable flag)
and #%00100000 ; both flag needs to be set and the offset needs to be 0
beq @no_disable
; load smaller sprite
lda #$39
sta temp+2
lda sprite_tile_data, y
and #%00001111 ; although both need to be true it needs to be 2 checks
bne @no_disable
lda #$24 ; empty tile
sta temp+2
@no_disable:
; set up pointer
lda sprite_tile_obj, y
tax
lda obj_index_to_addr, x
sta sprite_ptr
; set up sprite values for oov check
lda sprite_tile_x, y
sta get_tile_x
lda sprite_tile_y, y
sta get_tile_y
tya
pha ; need y again later
ldy #$00
lda temp+1
sta (sprite_ptr), y
iny
lda temp+2
sta (sprite_ptr), y
; attributes
lda #$00
ldy #$02
sta (sprite_ptr), y
ldy #$03
lda temp
sta (sprite_ptr), y
pla
tay
lda sprite_tile_temp, y
bne @no_offscreen
sty temp+1 ; need it again
jsr sprite_offscreen
ldy temp+1
sta sprite_tile_temp, y
@no_offscreen:
pla
tax
pla
tay
pla
rts
rts
; this sub routine handles collision with a push sprite
; inputs:
; y -> pointing to sprite data offset
sprite_push_collision:
; check which direction the player is coming from
lda player_y
cmp player_y_bac
beq @not_up_down ; if equal skip
bcs @down ; if greater than player went down
@up:
ldx sprite_tile_y, y
dex
stx get_tile_y
ldx sprite_tile_x, y
stx get_tile_x
lda #%01000000
sta sprite_tile_data, y
jmp @tile_got
@down:
ldx sprite_tile_y, y
inx
stx get_tile_y
ldx sprite_tile_x, y
stx get_tile_x
lda #%11000000
sta sprite_tile_data, y
jmp @tile_got
@not_up_down:
lda player_x
cmp player_x_bac
beq @not_left_right ; if equal skip
bcs @right ; if greater player went right
@left:
ldx sprite_tile_x, y
dex
stx get_tile_x
ldx sprite_tile_y, y
stx get_tile_y
lda #%00000000
sta sprite_tile_data, y
jmp @tile_got
@right:
ldx sprite_tile_x, y
inx
stx get_tile_x
ldx sprite_tile_y, y
stx get_tile_y
lda #%10000000
sta sprite_tile_data, y
jmp @tile_got
@not_left_right:
@collision:
lda #$01
rts
@tile_got:
jsr verify_sprite_move
pha
bne @no_noise ; no collision
jsr init_push_noise
@no_noise:
pla
rts
; this sub routine handles collision with a key sprite
; if touched increments key count by 1 and disables sprite
; inputs:
; y -> pointing to sprite data offset
sprite_key_collision:
jsr sprite_chest_collision
bne @collision
jsr init_coin_noise
; test collected flag
lda sprite_tile_data, y
and #%10000000
bne @done
; add one to key count and flag key as collected
inc key_count
lda #$F0
sta sprite_tile_data, y
@done:
lda #$00
rts
@collision:
lda #$01
rts
; this sub routine updates key sprites
; inputs:
; y -> pointing to sprite data offset
; sprite data documentation:
; 7th bit = 1 -> key was collected, disable functionality
sprite_key_update:
pha
tya
pha
txa
pha
; load x position
ldx sprite_tile_x, y
lda tile_convert_table, x
sta temp
; load y position
ldx sprite_tile_y, y
lda tile_convert_table, x
sta temp+1
; test if key is makred as collected
lda sprite_tile_data, y
and #%10000000
bne @collected
; enable sprite for collision
lda #$F0
sta sprite_tile_flags, y
; set up pointer
lda sprite_tile_obj, y
tax
lda obj_index_to_addr, x
sta sprite_ptr
; set up sprite values for oov check
lda sprite_tile_x, y
sta get_tile_x
lda sprite_tile_y, y
sta get_tile_y
; if hp is not zero display chest
lda sprite_tile_hp, y
beq @no_chest
jsr sprite_chest_update
jmp @offscreen_check
@no_chest:
ldy #$00
lda temp+1
sta (sprite_ptr), y
iny
lda #$59
sta (sprite_ptr), y
; attributes
lda #$00
ldy #$02
sta (sprite_ptr), y
ldy #$03
lda temp
sta (sprite_ptr), y
jmp @offscreen_check
@collected:
; enable sprite for no collision
lda #$00
sta sprite_tile_flags, y
; set up pointer
lda sprite_tile_obj, y
tax
lda obj_index_to_addr, x
sta sprite_ptr
; set up sprite values for oov check
lda sprite_tile_x, y
sta get_tile_x
lda sprite_tile_y, y
sta get_tile_y
; if it was collected we display the key in the bottom left
; corner to indicate a key is in players posession unless key count is 0
ldy #$00
lda #$00
sta (sprite_ptr), y
ldy #$03
lda #$00
sta (sprite_ptr), y
; attributes