base:sprite_projectiles
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | base:sprite_projectiles [2016-06-26 17:47] (current) – created achim | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Sprite projectiles ====== | ||
+ | by Achim | ||
+ | Here's a small piece of code to make one sprite fly directly to another one. | ||
+ | If the player is aiming at an enemy (or vice versa), this routine will make | ||
+ | sure the projectile hits the target. | ||
+ | |||
+ | It's using the bresenham line algorithm for all four quadrants. | ||
+ | It only works with deltaX < $ff. | ||
+ | |||
+ | * Call " | ||
+ | * Call " | ||
+ | |||
+ | |||
+ | |||
+ | ; calculate slope | ||
+ | ; projectileY, | ||
+ | ; targetY, -X and -MSB = sprite coordinates | ||
+ | ; error, deltay, deltax, deltaxhi, tmp0, tmp1 = variables, locate them wherever you want | ||
+ | : | ||
+ | projectileslope lda projectileY | ||
+ | sec | ||
+ | sbc targetY | ||
+ | sta deltay | ||
+ | lda projectileX ; | ||
+ | sec | ||
+ | sbc targetX | ||
+ | sta deltax | ||
+ | lda projectileMSB | ||
+ | sbc targetMSB | ||
+ | bcc skip3 | ||
+ | sta deltaxhi | ||
+ | lda #0 | ||
+ | sta xdirectionflag+1 | ||
+ | jmp setupslope | ||
+ | skip3 | ||
+ | sec | ||
+ | sbc projectileX | ||
+ | sta deltax | ||
+ | lda targetMSB | ||
+ | sbc projectileMSB | ||
+ | sta deltaxhi | ||
+ | lda #1 | ||
+ | sta xdirectionflag+1 | ||
+ | setupslope | ||
+ | beq skip4 | ||
+ | rts | ||
+ | skip4 ldx #$fc ; y=y-4 | ||
+ | lda projectileY ; | ||
+ | cmp targetY | ||
+ | bcc skip5 | ||
+ | ldx #$04 ; y=y+4 | ||
+ | skip5 stx mirrory1+1 ; | ||
+ | stx mirrory2+1 | ||
+ | bpl skip6 | ||
+ | lda deltay ; invert deltaY if moving up | ||
+ | eor #$ff | ||
+ | clc | ||
+ | adc #1 | ||
+ | sta deltay | ||
+ | skip6 lda deltay | ||
+ | cmp deltax | ||
+ | bcs setyfast ; prepare y as fast direction if deltaY> | ||
+ | lda #1 ; set flag for x=fast | ||
+ | sta fastxoryflag+1 | ||
+ | lda deltax ; x=fast -> error=deltaX/ | ||
+ | lsr | ||
+ | sta error | ||
+ | lda projectileMSB ; | ||
+ | lsr ; projectile(x+msb)/ | ||
+ | lda projectileX | ||
+ | ror | ||
+ | sta tmp0 | ||
+ | lda targetMSB ; | ||
+ | lsr | ||
+ | lda targetX | ||
+ | ror | ||
+ | sta tmp1 | ||
+ | lda tmp0 ; tmp0< | ||
+ | cmp tmp1 | ||
+ | bcc fastx2right1 | ||
+ | lda #$fc ; -> x=x-4 | ||
+ | sta xsteps2+1 | ||
+ | lda #$e9 ; ' | ||
+ | sta msbcorr2 | ||
+ | rts | ||
+ | fastx2right1 | ||
+ | sta xsteps2+1 | ||
+ | lda #$69 ; ' | ||
+ | sta msbcorr2 | ||
+ | rts | ||
+ | setyfast | ||
+ | sta fastxoryflag+1 | ||
+ | lda deltay ; y=fast -> error=deltaY/ | ||
+ | lsr | ||
+ | sta error | ||
+ | xdirectionflag | ||
+ | bne fastx2right2 | ||
+ | lda #$fc ; -> x=x-4 | ||
+ | sta xsteps1+1 | ||
+ | lda #$e9 ; ' | ||
+ | sta msbcorr1 | ||
+ | rts | ||
+ | fastx2right2 | ||
+ | sta xsteps1+1 | ||
+ | lda #$69 ; ' | ||
+ | sta msbcorr1 | ||
+ | rts | ||
+ | ; | ||
+ | ; 1) move projectile | ||
+ | ; 2) check if projectile hits or leaves the screen | ||
+ | ; | ||
+ | projectileflying | ||
+ | bne projectiledecomm ; | ||
+ | nocollision | ||
+ | cmp #$32 ; if y<$32 -> decommission | ||
+ | bcc projectiledecom | ||
+ | cmp #$f8 ; if y>$f8 -> decommission | ||
+ | bcs projectiledecom | ||
+ | lda projectileMSB | ||
+ | beq skip0 | ||
+ | lda projectileX | ||
+ | cmp #$60 ; if x>$0160 -> decommission | ||
+ | bcc fastxoryflag ; | ||
+ | bcs projectiledecom | ||
+ | skip0 | ||
+ | cmp #$10 ; if x<$0010 -> decommission | ||
+ | bcs fastxoryflag ; | ||
+ | projectiledecom | ||
+ | sta projectileX | ||
+ | sta projectileMSB | ||
+ | lda #$ff ; y=$ff or do whatever is necessary here | ||
+ | sta projectileY | ||
+ | rts | ||
+ | fastxoryflag | ||
+ | bne xfast | ||
+ | lda projectileY | ||
+ | sec | ||
+ | mirrory1 | ||
+ | sta projectileY | ||
+ | lda error | ||
+ | sec | ||
+ | sbc deltax | ||
+ | sta error | ||
+ | bcs skip1 | ||
+ | lda projectileX | ||
+ | clc | ||
+ | xsteps1 | ||
+ | sta projectileX | ||
+ | lda projectileMSB | ||
+ | msbcorr1 | ||
+ | sta projectileMSB | ||
+ | lda error | ||
+ | clc | ||
+ | adc deltay | ||
+ | sta error | ||
+ | skip1 | ||
+ | xfast lda projectileX | ||
+ | clc | ||
+ | xsteps2 | ||
+ | sta projectileX | ||
+ | lda projectileMSB | ||
+ | msbcorr2 | ||
+ | sta projectileMSB | ||
+ | lda error | ||
+ | sec | ||
+ | sbc deltay | ||
+ | sta error | ||
+ | bcs skip2 | ||
+ | lda projectileY | ||
+ | sec | ||
+ | mirrory2 | ||
+ | sta projectileY | ||
+ | lda error | ||
+ | clc | ||
+ | adc deltax | ||
+ | sta error | ||
+ | skip2 rts |
base/sprite_projectiles.txt · Last modified: 2016-06-26 17:47 by achim