Browse Source

Collision detection between player and enemy shots.

main
Colin McMillen 9 years ago
parent
commit
b238fa2d37
  1. 10
      memory.asm
  2. 74
      pewpew.asm
  3. BIN
      sprites32.pcx

10
memory.asm

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

74
pewpew.asm

@ -214,11 +214,13 @@ InitWorld:
lda #4
sta backgroundBlue
; Player's initial starting location.
; Player's initial starting location and health.
lda #(256 / 4)
sta playerX
lda #((224 - 32) / 2)
sta playerY
lda #10
sta playerHealth
; (x-velocity, y-velocity) of 4 different player shot patterns.
lda #6
@ -471,6 +473,7 @@ UpdateWorld:
jsr UpdateShotCooldown
jsr SpawnEnemyShots
jsr UpdateShotPositions
jsr CheckCollisionsWithPlayer
jsr UpdateBackgroundScroll
rts
@ -490,7 +493,7 @@ UpdateShotCooldown:
SpawnEnemyShots:
lda vBlankCounter
bit #%00001111
bit #%00001111 ; Spawn shots every this-many frames.
beq +
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:
; Make the background scroll. Horizontal over time; vertical depending on
; player's y-coordinate.

BIN
sprites32.pcx

Loading…
Cancel
Save