Check collisions between player shots & enemies.

This commit is contained in:
Colin McMillen 2015-06-07 08:55:45 -04:00
parent c5c3297237
commit ac5dcc504a
2 changed files with 89 additions and 18 deletions

View File

@ -11,6 +11,7 @@
; 0023: shot cooldown timer. ; 0023: shot cooldown timer.
; 0024: next-shot state. ; 0024: next-shot state.
; 0025: number of frames until the next enemy ship spawns. ; 0025: number of frames until the next enemy ship spawns.
; 0026-0027: player score.
; [gap] ; [gap]
; 0030-003F: (x, y) velocities of each of the 8 possible shot states. ; 0030-003F: (x, y) velocities of each of the 8 possible shot states.
; 0040-009F: {sprite, x, y, x-velocity, y-velocity, unused} per player shot. ; 0040-009F: {sprite, x, y, x-velocity, y-velocity, unused} per player shot.
@ -36,6 +37,7 @@
.define shotCooldown $23 .define shotCooldown $23
.define nextShotState $24 .define nextShotState $24
.define enemyShipSpawnCooldown $25 .define enemyShipSpawnCooldown $25
.define playerScore $26
.define shotVelocityTable $30 .define shotVelocityTable $30
.define playerShotArray $40 .define playerShotArray $40
.define playerShotArrayLength 16 .define playerShotArrayLength 16

View File

@ -735,17 +735,18 @@ ShotDone:
; Expects: ; Expects:
; $00: x-coordinate of ship's center. ; $00: x-coordinate of ship's center.
; $01: y-coordinate of ship's center. ; $01: y-coordinate of ship's center.
; $02: x-coordinate of shot's upper-left. ; $02: half of the shot's size.
; $03: y-coordinate of shot's upper-left. ; $03: x-coordinate of shot's upper-left.
; $04: half of the shot's size. ; $04: y-coordinate of shot's upper-left.
; ;
; Modifies: ; Modifies:
; $05 ; $05
; A: set to non-zero if there was a collision, zero otherwise. ; A: set to non-zero if there was a collision, zero otherwise.
CheckCollision: CheckCollision:
lda $02 lda $03
clc clc
adc $04 ; Get the center of the shot. adc $02 ; Get the center of the shot.
sbc $00 sbc $00
bpl + ; If the result is positive, great! bpl + ; If the result is positive, great!
eor #$ff ; Otherwise, negate it. eor #$ff ; Otherwise, negate it.
@ -759,9 +760,9 @@ CheckCollision:
+ +
sta $05 ; Save dx for later. sta $05 ; Save dx for later.
; Find dy. ; Find dy.
lda $03 lda $04
clc clc
adc $04 ; Get the center of the shot. adc $02 ; Get the center of the shot.
sbc $01 sbc $01
bpl + ; If the result is positive, great! bpl + ; If the result is positive, great!
eor #$ff ; Otherwise, negate it. eor #$ff ; Otherwise, negate it.
@ -780,6 +781,9 @@ CheckCollision:
CheckCollisionsWithPlayer: CheckCollisionsWithPlayer:
lda #2 ; Half of shot's size.
sta $02
; Store player position statically. ; Store player position statically.
clc clc
lda playerX lda playerX
@ -793,42 +797,107 @@ CheckCollisionsWithPlayer:
sta $01 sta $01
ldx #0 ldx #0
-- -
lda enemyShotArray, X lda enemyShotArray, X
cmp #0 ; Check whether it's active. cmp #0 ; Check whether it's active.
beq ++ beq +
lda enemyShotArray + 1, X ; x. lda enemyShotArray + 1, X ; x.
sta $02
lda enemyShotArray + 2, X ; y.
sta $03 sta $03
lda enemyShotArray + 2, X ; y.
sta $04
jsr CheckCollision jsr CheckCollision
cmp #0 cmp #0
beq ++ beq +
; OK, we got a hit! ; OK, we got a hit! Disable the shot.
; Disable the shot.
lda #0 lda #0
sta enemyShotArray, X sta enemyShotArray, X
; And decrement the player's life. ; ... and decrement the player's life.
lda playerHealth lda playerHealth
cmp #0 cmp #0
beq ++ beq +
dec playerHealth dec playerHealth
++ +
.rept shotSize .rept shotSize
inx inx
.endr .endr
cpx #(enemyShotArrayLength * shotSize) cpx #(enemyShotArrayLength * shotSize)
bne -- bne -
rts rts
CheckCollisionsWithEnemies: CheckCollisionsWithEnemies:
lda #2 ; Half of shot's size.
sta $02
ldy #0 ; Index into enemyShipArray.
--
lda enemyShipArray, Y
cmp #0 ; Check whether it's active.
beq ++
; Store enemy position statically.
clc
lda enemyShipArray + 1, Y ; x.
adc #16 ; Can't overflow.
sta $00 ; Store the center.
lda enemyShipArray + 2, Y ; y.
adc #15
sta $01 ; Store the center.
ldx #0 ; Index into playerShotArray.
-
lda playerShotArray, X
cmp #0
beq +
lda playerShotArray + 1, X ; x.
sta $03
lda playerShotArray + 2, X ; y.
sta $04
jsr CheckCollision
cmp #0
beq +
; OK, we got a hit! Disable the shot.
lda #0
sta playerShotArray, X
; ... and also the enemy ship.
sta enemyShipArray, Y
; Give that player some points. Players love points.
; TODO: convert to decimal only at display time?
SetA16Bit
sed ; Set decimal mode.
clc
lda playerScore
adc #8
sta playerScore
cld ; Clear decimal mode.
SetA8Bit
bra ++ ; ... we're done with this ship; no need to check more shots.
+
.rept shotSize
inx
.endr
cpx #(playerShotArrayLength * shotSize)
bne -
++
.rept enemyShipSize
iny
.endr
cpy #(enemyShipArrayLength * enemyShipSize)
bne --
rts rts