-
Notifications
You must be signed in to change notification settings - Fork 2
/
fireball.asm
488 lines (466 loc) · 7.45 KB
/
fireball.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
.ifndef FIREBALL_INC
FIREBALL_INC = 1
.include "sprite.asm"
.include "debug.asm"
FIREBALL_H_FRAME = 31
FIREBALL_V_FRAME = 30
FIREBALL_D_FRAME = 32
fireball1:
.byte FIREBALL1_idx
.byte 0 ; Bits 7-2: TBD | Bit 1: moving | Bit 0: on screen
.byte 0,0 ; x,y vector
fireball2:
.byte FIREBALL2_idx
.byte 0 ; Bits 7-2: TBD | Bit 1: moving | Bit 0: on screen
.byte 0,0 ; x,y vector
fireball3:
.byte FIREBALL3_idx
.byte 0 ; Bits 7-2: TBD | Bit 1: moving | Bit 0: on screen
.byte 0,0 ; x,y vector
fireball4:
.byte FIREBALL4_idx
.byte 0 ; Bits 7-2: TBD | Bit 1: moving | Bit 0: on screen
.byte 0,0 ; x,y vector
FIREBALL_ON_SCREEN = $01
FIREBALL_CLEAR = $FE
FIREBALL_MOVING = $02
FIREBALL_STOP = $FD
__fb_start_x: .word 0
__fb_start_y: .word 0
.macro FIREBALL_AIM start_x, start_y ; Other input: A=index offset, X/Y=vector
pha
phx
asl
asl
inc
tax
lda fireball1,x
bit #FIREBALL_ON_SCREEN
beq :+
plx
pla
bra :++
: lda start_x
sta __fb_start_x
lda start_x+1
sta __fb_start_x+1
lda start_y
sta __fb_start_y
lda start_y+1
sta __fb_start_y+1
plx
pla
jsr __fireball_aim
: nop
.endmacro
__fb_idx: .byte 0
__fireball_aim: ; A: index offset
; X/Y: vector
; __fb_start_x,__fb_start_y: starting screen position
pha
jsr __fb_normalize
pla
phx
asl
asl
tax
lda fireball1,x
sta __fb_idx
inx
lda fireball1,x
ora #FIREBALL_ON_SCREEN
sta fireball1,x
inx
pla
sta fireball1,x
inx
tya
sta fireball1,x
SPRITE_SET_SCREEN_POS __fb_idx, __fb_start_x, __fb_start_y
rts
__fb_rawx: .byte 0
__fb_rawy: .byte 0
__fb_normalize: ; Input: X/Y: raw vector
; Output: X/Y: normalized vector, magnitude ~= 4
stx __fb_rawx
sty __fb_rawy
;DEBUG_BYTE __fb_rawx, 0,0
;DEBUG_BYTE __fb_rawy, 0,1
cpx #0
bpl @eastern
jmp @western
@eastern:
cpy #0
bpl @southeastern
jmp @northeastern
@southeastern:
beq @se_4_0
cpx #0
beq @se_0_4
jsr __fb_get_norm
jmp @return
@se_4_0:
ldx #4
ldy #0
jmp @return
@se_0_4:
ldx #0
ldy #4
jmp @return
@northeastern:
cpx #0
beq @ne_0_4
lda #0
sec
sbc __fb_rawy
sta __fb_rawy
jsr __fb_get_norm
sty __fb_rawy
sec
lda #0
sbc __fb_rawy
tay
jmp @return
@ne_0_4:
ldx #0
ldy #(256-4)
jmp @return
@western:
sec
lda #0
sbc __fb_rawx
sta __fb_rawx
cpy #0
bpl @southwestern
jmp @northwestern
@southwestern:
beq @sw_4_0
jsr __fb_get_norm
stx __fb_rawx
sec
lda #0
sbc __fb_rawx
tax
jmp @return
@sw_4_0:
ldx #(256-4)
ldy #0
jmp @return
@northwestern:
lda #0
sec
sbc __fb_rawy
sta __fb_rawy
jsr __fb_get_norm
stx __fb_rawx
sec
lda #0
sbc __fb_rawx
tax
sty __fb_rawy
sec
lda #0
sbc __fb_rawy
tay
jmp @return
@return:
rts
__fb_get_norm: ; Input: __fb_rawx,__fb_rawx = absolute value of raw vector
; Output: normalized vector
lda #NORMX_BANK
sta RAM_BANK
lda #<RAM_WIN
sta ZP_PTR_1
lda #>RAM_WIN
sta ZP_PTR_1+1
lda __fb_rawy
dec
sta ZP_PTR_2
stz ZP_PTR_2+1
asl ZP_PTR_2
rol ZP_PTR_2+1
asl ZP_PTR_2
rol ZP_PTR_2+1
asl ZP_PTR_2
rol ZP_PTR_2+1
asl ZP_PTR_2
rol ZP_PTR_2+1
asl ZP_PTR_2
rol ZP_PTR_2+1
asl ZP_PTR_2
rol ZP_PTR_2+1
clc
lda ZP_PTR_1
adc ZP_PTR_2
sta ZP_PTR_1
lda ZP_PTR_1+1
adc ZP_PTR_2+1
sta ZP_PTR_1+1
lda __fb_rawx
dec
lsr
clc
adc ZP_PTR_1
sta ZP_PTR_1
lda ZP_PTR_1+1
adc #0
sta ZP_PTR_1+1
lda (ZP_PTR_1)
pha
lda __fb_rawx
bit #$01
beq @x_low_nibble
pla
and #$F0
lsr
lsr
lsr
lsr
tax
bra @get_y
@x_low_nibble:
pla
and #$0F
tax
@get_y:
lda #NORMY_BANK
sta RAM_BANK
lda (ZP_PTR_1)
pha
lda __fb_rawy
bit #$01
beq @y_low_nibble
pla
and #$F0
lsr
lsr
lsr
lsr
tay
bra @return
@y_low_nibble:
pla
and #$0F
tay
@return:
rts
fireball_tick:
bra @start
@offset: .byte 0
@status: .byte 0
@move_x: .byte 0
@move_y: .byte 0
@frame: .byte 0
@index: .byte 0
@flip: .byte 0
@start:
stz @offset
@loop:
lda @offset
cmp num_fireballs
bne @get_params
jmp @return
@get_params:
inc @offset
asl
asl
tax
lda fireball1,x
sta @index
inx
lda fireball1,x
sta @status
bit #FIREBALL_ON_SCREEN
bne @check_vector
lda @index
jsr sprite_disable
bra @loop
@check_vector:
inx
lda fireball1,x
sta @move_x
inx
lda fireball1,x
sta @move_y
;DEBUG_BYTE @move_x, 3,0
;DEBUG_BYTE @move_y, 3,1
lda @move_x
cmp #4
bne @check_west
lda #FIREBALL_H_FRAME
sta @frame
stz @flip
jmp @set_frame
@check_west:
cmp #(256-4)
bne @check_south
lda #FIREBALL_H_FRAME
sta @frame
lda #H_FLIP
sta @flip
jmp @set_frame
@check_south:
lda @move_y
cmp #4
bne @check_north
lda #FIREBALL_V_FRAME
sta @frame
stz @flip
jmp @set_frame
@check_north:
cmp #(256-4)
bne @check_se
lda #FIREBALL_V_FRAME
sta @frame
lda #V_FLIP
sta @flip
jmp @set_frame
@check_se:
lda @move_x
bmi @check_sw
lda @move_y
bmi @northeast
lda #FIREBALL_D_FRAME
sta @frame
stz @flip
jmp @set_frame
@northeast:
lda #FIREBALL_D_FRAME
sta @frame
lda #V_FLIP
sta @flip
jmp @set_frame
@check_sw:
lda @move_y
bmi @northwest
lda #FIREBALL_D_FRAME
sta @frame
lda #H_FLIP
sta @flip
jmp @set_frame
@northwest:
lda #FIREBALL_D_FRAME
sta @frame
lda #HV_FLIP
sta @flip
@set_frame:
lda @frame
ldx @index
ldy @flip
jsr sprite_frame
lda @status
bit #FIREBALL_MOVING
bne @move
jmp @loop
@move:
ldx @move_x
bmi @move_left
lda @index
jsr move_sprite_right
lda @index
ldx #1
jsr sprite_getpos
cpx #SPRITE_MAX_X
beq @off
bra @move_down
@move_left:
sec
lda #0
sbc @move_x
tax
lda @index
jsr move_sprite_left
lda @index
ldx #1
jsr sprite_getpos
cpx #SPRITE_MIN_X
beq @off
@move_down:
ldx @move_y
bmi @move_up
lda @index
jsr move_sprite_down
lda @index
ldx #1
jsr sprite_getpos
cpy #SPRITE_MAX_Y
beq @off
jmp @loop
@move_up:
sec
lda #0
sbc @move_y
tax
lda @index
jsr move_sprite_up
lda @index
ldx #1
jsr sprite_getpos
cpy #SPRITE_MIN_Y
beq @off
jmp @loop
@off:
lda @offset
dec
jsr fireball_offscreen
jmp @loop
@return:
rts
fireball_visible: ; Input: A: sprite index
; Output: A: 0=not visible, 1=visible
sec
sbc #FIREBALL1_idx
asl
asl
inc
tax
lda fireball1,x
and #FIREBALL_ON_SCREEN
rts
fireball_offscreen: ; A: index offset
asl
asl
inc
tax
lda fireball1,x
and #FIREBALL_CLEAR
sta fireball1,x
rts
fireball_stop:
ldx #1
lda fireball1,x
and #FIREBALL_STOP
sta fireball1,x
lda fireball2,x
and #FIREBALL_STOP
sta fireball2,x
lda fireball3,x
and #FIREBALL_STOP
sta fireball3,x
lda fireball4,x
and #FIREBALL_STOP
sta fireball4,x
rts
fireball_move:
ldx #1
lda fireball1,x
ora #FIREBALL_MOVING
sta fireball1,x
lda fireball2,x
ora #FIREBALL_MOVING
sta fireball2,x
lda fireball3,x
ora #FIREBALL_MOVING
sta fireball3,x
lda fireball4,x
ora #FIREBALL_MOVING
sta fireball4,x
rts
fireball_clear:
ldx #1
stz fireball1,x
stz fireball2,x
stz fireball3,x
stz fireball4,x
rts
.endif