Collision detection between player and enemy shots.

This commit is contained in:
Colin McMillen 2015-06-01 21:23:53 -04:00
parent f5b67dd535
commit b238fa2d37
3 changed files with 78 additions and 6 deletions

View File

@ -7,8 +7,9 @@
; 001A-001B: 16-bit pointer to next random byte. ; 001A-001B: 16-bit pointer to next random byte.
; [gap] ; [gap]
; 0020-0021: (x, y) coordinates of player. ; 0020-0021: (x, y) coordinates of player.
; 0022: shot cooldown timer. ; 0022: player health.
; 0023: next-shot state. ; 0023: shot cooldown timer.
; 0024: next-shot state.
; [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.
@ -28,8 +29,9 @@
.define randomBytePtr $1A .define randomBytePtr $1A
.define playerX $20 .define playerX $20
.define playerY $21 .define playerY $21
.define shotCooldown $22 .define playerHealth $22
.define nextShotState $23 .define shotCooldown $23
.define nextShotState $24
.define shotVelocityTable $30 .define shotVelocityTable $30
.define playerShotArray $40 .define playerShotArray $40
.define playerShotArrayLength 16 .define playerShotArrayLength 16

View File

@ -214,11 +214,13 @@ InitWorld:
lda #4 lda #4
sta backgroundBlue sta backgroundBlue
; Player's initial starting location. ; Player's initial starting location and health.
lda #(256 / 4) lda #(256 / 4)
sta playerX sta playerX
lda #((224 - 32) / 2) lda #((224 - 32) / 2)
sta playerY sta playerY
lda #10
sta playerHealth
; (x-velocity, y-velocity) of 4 different player shot patterns. ; (x-velocity, y-velocity) of 4 different player shot patterns.
lda #6 lda #6
@ -471,6 +473,7 @@ UpdateWorld:
jsr UpdateShotCooldown jsr UpdateShotCooldown
jsr SpawnEnemyShots jsr SpawnEnemyShots
jsr UpdateShotPositions jsr UpdateShotPositions
jsr CheckCollisionsWithPlayer
jsr UpdateBackgroundScroll jsr UpdateBackgroundScroll
rts rts
@ -490,7 +493,7 @@ UpdateShotCooldown:
SpawnEnemyShots: SpawnEnemyShots:
lda vBlankCounter lda vBlankCounter
bit #%00001111 bit #%00001111 ; Spawn shots every this-many frames.
beq + beq +
rts rts
+ +
@ -608,6 +611,73 @@ ShotDone:
CheckCollisionsWithPlayer:
; Store player position statically.
clc
lda playerX
adc #16 ; Can't overflow.
sta $00 ; Store the center.
lda playerY
adc #16 ; Store the center.
sta $01
ldx #0
--
lda enemyShotArray, X
cmp #0 ; Check whether it's active.
beq ++
; Find dx.
lda enemyShotArray + 1, X ; x.
clc
adc #2 ; Get the center of the shot.
sbc $00
bpl + ; If the result is positive, great!
eor #$ff ; Otherwise, negate it.
inc A
+
; A now contains dx, guaranteed to be positive.
cmp #18 ; Threshold for "successful hit".
bcs ++ ; Already too far; bail.
sta $02
; Find dy.
lda enemyShotArray + 2, X ; y.
clc
adc #2
sbc $01
bpl + ; If the result is positive, great!
eor #$ff ; Otherwise, negate it.
inc A
+
; A now contains dy, guaranteed to be positive.
clc
adc $02 ; Add dx.
cmp #18 ; Threshold for "successful hit".
bcs ++
; OK, we got a hit!
; Disable the shot.
lda #0
sta enemyShotArray, X
; And decrement the player's life.
lda playerHealth
cmp #0
beq ++
dec playerHealth
++
.rept shotSize
inx
.endr
cpx #(enemyShotArrayLength * shotSize)
bne --
rts
UpdateBackgroundScroll: UpdateBackgroundScroll:
; Make the background scroll. Horizontal over time; vertical depending on ; Make the background scroll. Horizontal over time; vertical depending on
; player's y-coordinate. ; player's y-coordinate.

Binary file not shown.