/
effect.asm
643 lines (545 loc) · 10 KB
/
effect.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
; This file is part of Pwn Adventure Z.
; Pwn Adventure Z 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 3 of the License, or
; (at your option) any later version.
; Pwn Adventure Z 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 Pwn Adventure Z. If not, see <http://www.gnu.org/licenses/>.
.include "defines.inc"
.code
PROC init_effect_sprites
jsr load_effect_sprites
ldx #0
lda #EFFECT_NONE
loop:
sta effect_type, x
inx
cpx #EFFECT_MAX_COUNT
bne loop
rts
.endproc
PROC load_effect_sprites
LOAD_ALL_TILES $100 + SPRITE_TILE_BULLET, bullet_tiles
LOAD_ALL_TILES $100 + SPRITE_TILE_SPLAT, splat_tiles
LOAD_ALL_TILES $100 + SPRITE_TILE_ORB, orb_tiles
LOAD_ALL_TILES $100 + SPRITE_TILE_WARP, warp_tiles
LOAD_ALL_TILES $100 + SPRITE_TILE_ROCKET, rocket_tiles
LOAD_ALL_TILES $100 + SPRITE_TILE_FIREBALL, fireball_tiles
LOAD_ALL_TILES $100 + SPRITE_TILE_GRENADE, grenade_tiles
lda equipped_weapon
cmp #ITEM_AXE
beq loadaxetile
LOAD_ALL_TILES $100 + SPRITE_TILE_MELEE, sword_tiles
jmp doneloading
loadaxetile:
LOAD_ALL_TILES $100 + SPRITE_TILE_MELEE, axe_tiles
doneloading:
rts
.endproc
PROC update_effects
lda #0
sta cur_effect
loop:
ldx cur_effect
lda effect_type, x
cmp #EFFECT_NONE
bne valid
jmp next
valid:
; Call the effect's tick function
lda effect_type, x
asl
tay
lda effect_descriptors, y
sta ptr
lda effect_descriptors + 1, y
sta ptr + 1
ldy #EFFECT_DESC_TICK
lda (ptr), y
sta temp
ldy #EFFECT_DESC_TICK + 1
lda (ptr), y
sta temp + 1
jsr call_temp
; Ensure effect is still valid
ldx cur_effect
lda effect_type, x
cmp #EFFECT_NONE
bne collidepos
jmp next
collidepos:
; Compute collision check position
lda effect_type, x
asl
tay
lda effect_descriptors, y
sta ptr
lda effect_descriptors + 1, y
sta ptr + 1
lda effect_x, x
sta effect_collide_left_x
ldy #EFFECT_DESC_COLLIDE_WIDTH
lda (ptr), y
clc
adc effect_x, x
sta effect_collide_right_x
lda effect_y, x
sta effect_collide_top_y
ldy #EFFECT_DESC_COLLIDE_HEIGHT
lda (ptr), y
clc
adc effect_y, x
sta effect_collide_bot_y
; Check for enemy collision
checkcollide:
lda #0
sta cur_enemy
enemyloop:
ldx cur_enemy
lda enemy_type, x
cmp #ENEMY_NONE
beq nextenemy
lda enemy_x, x
sec
sbc effect_collide_left_x
cmp #$f0
bcs xoverlapenemy
lda enemy_x, x
sec
sbc effect_collide_right_x
cmp #$f0
bcs xoverlapenemy
jmp nextenemy
xoverlapenemy:
lda enemy_y, x
sec
sbc effect_collide_top_y
cmp #$f0
bcs yoverlapenemy
lda enemy_y, x
sec
sbc effect_collide_bot_y
cmp #$f0
bcs yoverlapenemy
jmp nextenemy
yoverlapenemy:
; Call the enemy collide function
ldx cur_effect
lda effect_type, x
asl
tay
lda effect_descriptors, y
sta ptr
lda effect_descriptors + 1, y
sta ptr + 1
ldy #EFFECT_DESC_ENEMY_COLLIDE
lda (ptr), y
sta temp
ldy #EFFECT_DESC_ENEMY_COLLIDE + 1
lda (ptr), y
sta temp + 1
jsr call_temp
; Ensure effect is still valid after callback
ldx cur_effect
lda effect_type, x
cmp #EFFECT_NONE
bne nextenemy
jmp next
nextenemy:
ldx cur_enemy
inx
stx cur_enemy
cpx #ENEMY_MAX_COUNT
bne enemyloop
; Check for player collision
lda player_x
sec
sbc effect_collide_left_x
cmp #$f0
bcs xoverlapplayer
lda player_x
sec
sbc effect_collide_right_x
cmp #$f0
bcs xoverlapplayer
jmp noplayercollide
xoverlapplayer:
lda player_y
sec
sbc effect_collide_top_y
cmp #$f0
bcs yoverlapplayer
lda player_y
sec
sbc effect_collide_bot_y
cmp #$f0
bcs yoverlapplayer
jmp noplayercollide
yoverlapplayer:
; Call the player collide function
ldx cur_effect
lda effect_type, x
asl
tay
lda effect_descriptors, y
sta ptr
lda effect_descriptors + 1, y
sta ptr + 1
ldy #EFFECT_DESC_PLAYER_COLLIDE
lda (ptr), y
sta temp
ldy #EFFECT_DESC_PLAYER_COLLIDE + 1
lda (ptr), y
sta temp + 1
jsr call_temp
; Ensure effect is still valid after callback
ldx cur_effect
lda effect_type, x
cmp #EFFECT_NONE
bne noplayercollide
jmp next
; Check for world collision
noplayercollide:
lda effect_collide_left_x
lsr
lsr
lsr
lsr
tax
lda effect_collide_top_y
lsr
lsr
lsr
lsr
tay
jsr read_projectile_collision_at
bne next
; World collision detected, issue callback
ldx cur_effect
lda effect_type, x
asl
tay
lda effect_descriptors, y
sta ptr
lda effect_descriptors + 1, y
sta ptr + 1
ldy #EFFECT_DESC_WORLD_COLLIDE
lda (ptr), y
sta temp
ldy #EFFECT_DESC_WORLD_COLLIDE + 1
lda (ptr), y
sta temp + 1
jsr call_temp
next:
ldx cur_effect
inx
stx cur_effect
cpx #EFFECT_MAX_COUNT
beq done
jmp loop
done:
rts
.endproc
PROC update_effect_sprites
; NES only supports 8 sprites per scan line, so rotate priority of effect sprites
; to ensure all of them get screen time. This will create the classic flicker
; effect when there is a lot going on.
lda effect_sprite_rotation
clc
adc #7
sta effect_sprite_rotation
ldx #0
spriteloop:
lda effect_type, x
cmp #EFFECT_NONE
bne ok
jmp empty
ok:
asl
tay
lda effect_descriptors, y
sta ptr
lda effect_descriptors + 1, y
sta ptr + 1
ldy #EFFECT_DESC_PALETTE
lda (ptr), y
sta arg0
ldy #EFFECT_DESC_FLAGS
lda (ptr), y
and #$c0
ora arg0
sta arg0
lda (ptr), y
and #1
bne large
txa
clc
adc effect_sprite_rotation
and #EFFECT_MAX_COUNT - 1
asl
asl
asl
clc
adc #SPRITE_OAM_EFFECTS
tay
lda effect_y, x
clc
adc #7
sta sprites, y
lda #$ff
sta sprites + 4, y
lda effect_x, x
clc
adc #8
sta sprites + 3, y
lda effect_tile, x
clc
adc #1
sta sprites + 1, y
lda arg0
sta sprites + 2, y
jmp next
large:
txa
clc
adc effect_sprite_rotation
and #EFFECT_MAX_COUNT - 1
asl
asl
asl
clc
adc #SPRITE_OAM_EFFECTS
tay
lda arg0
and #$40
bne fliphoriz
lda effect_tile, x
clc
adc #1
sta sprites + 1, y
adc #2
sta sprites + 5, y
jmp finish
fliphoriz:
lda effect_tile, x
clc
adc #1
sta sprites + 5, y
adc #2
sta sprites + 1, y
finish:
lda effect_y, x
clc
adc #7
sta sprites, y
sta sprites + 4, y
lda effect_x, x
clc
adc #8
sta sprites + 3, y
adc #8
sta sprites + 7, y
lda arg0
sta sprites + 2, y
sta sprites + 6, y
jmp next
empty:
txa
clc
adc effect_sprite_rotation
and #EFFECT_MAX_COUNT - 1
asl
asl
asl
clc
adc #SPRITE_OAM_EFFECTS
tay
lda #$ff
sta sprites, y
sta sprites + 4, y
next:
inx
cpx #EFFECT_MAX_COUNT
beq done
jmp spriteloop
done:
rts
.endproc
.segment "FIXED"
PROC remove_effect
ldx cur_effect
lda #EFFECT_NONE
sta effect_type, x
rts
.endproc
PROC create_effect
lda current_bank
pha
lda #0
jsr bankswitch
; Find a free effect slot
ldx next_effect_spawn_index
loop:
lda effect_type, x
cmp #EFFECT_NONE
beq found
inx
cpx next_effect_spawn_index
beq overwrite
cpx #EFFECT_MAX_COUNT
bne loop
ldx #0
cpx next_effect_spawn_index
beq overwrite
jmp loop
overwrite:
; Unable to find an effect slot, try to overwrite a lower priority effect
lda effect_type, x
cmp arg2
beq found
bcs found
inx
cpx next_effect_spawn_index
beq failed
cpx #EFFECT_MAX_COUNT
bne overwrite
ldx #0
cpx next_effect_spawn_index
beq failed
jmp overwrite
failed:
; Failed to find a valid slot, don't create the effect
pla
jsr bankswitch
lda #$ff
rts
found:
; Found a slot, add the effect
lda arg0
sta effect_x, x
lda arg1
sta effect_y, x
lda arg2
sta effect_type, x
lda arg3
sta effect_direction, x
lda #0
sta effect_time, x
lda arg4
sta effect_data_0, x
lda arg5
sta effect_data_1, x
lda arg2
asl
tay
lda effect_descriptors, y
sta ptr
lda effect_descriptors + 1, y
sta ptr + 1
ldy #EFFECT_DESC_TILE
lda (ptr), y
sta effect_tile, x
txa
clc
adc #1
cmp #EFFECT_MAX_COUNT
bne nowrap
lda #0
nowrap:
sta next_effect_spawn_index
pla
jsr bankswitch
txa
rts
.endproc
.segment "TEMP"
VAR cur_effect
.byte 0
VAR next_effect_spawn_index
.byte 0
VAR effect_sprite_rotation
.byte 0
VAR effect_collide_left_x
.byte 0
VAR effect_collide_right_x
.byte 0
VAR effect_collide_top_y
.byte 0
VAR effect_collide_bot_y
.byte 0
VAR effect_type
.repeat EFFECT_MAX_COUNT
.byte 0
.endrepeat
VAR effect_x
.repeat EFFECT_MAX_COUNT
.byte 0
.endrepeat
VAR effect_y
.repeat EFFECT_MAX_COUNT
.byte 0
.endrepeat
VAR effect_tile
.repeat EFFECT_MAX_COUNT
.byte 0
.endrepeat
VAR effect_direction
.repeat EFFECT_MAX_COUNT
.byte 0
.endrepeat
VAR effect_time
.repeat EFFECT_MAX_COUNT
.byte 0
.endrepeat
;Laser uses to store fractional pixel
VAR effect_data_0
.repeat EFFECT_MAX_COUNT
.byte 0
.endrepeat
;Laser uses to store accumulator
VAR effect_data_1
.repeat EFFECT_MAX_COUNT
.byte 0
.endrepeat
; used to determine if delta x or delta y is larger one bit per EFFECT
VAR x_or_y_dominate
.byte 0, 0
.data
VAR effect_descriptors
.word player_axe_descriptor
.word player_axe_hit_descriptor
.word player_sword_descriptor
.word player_sword_hit_descriptor
.word player_rocket_left_descriptor
.word player_rocket_right_descriptor
.word player_rocket_up_descriptor
.word player_rocket_down_descriptor
.word player_fireball_descriptor
.word player_grenade_descriptor
.word player_bullet_descriptor
.word player_lmg_bullet_descriptor
.word player_ak_bullet_descriptor
.word player_sniper_bullet_descriptor
.word player_shotgun_bullet_descriptor
.word player_left_bullet_descriptor
.word player_right_bullet_descriptor
.word player_hand_cannon_bullet_descriptor
.word drop_descriptor
.word enemy_death_descriptor
.word player_bullet_damage_descriptor
.word player_bullet_hit_descriptor
.word shark_laser_descriptor
.word shark_laser_hit_descriptor
.word shark_laser_damage_descriptor
.word explosion_stage_1_descriptor
.word explosion_stage_2_descriptor
.word reflected_laser_descriptor
.word fireball_hit_descriptor
.word warp_descriptor
TILES bullet_tiles, 2, "tiles/effects/bullet.chr", 6
TILES rocket_tiles, 4, "tiles/weapons/rocket.chr", 12
TILES fireball_tiles, 4, "tiles/weapons/fire2.chr", 8