From b238fa2d375ccd3097736ba259db568628a6f4fb Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Mon, 1 Jun 2015 21:23:53 -0400 Subject: [PATCH] Collision detection between player and enemy shots. --- memory.asm | 10 ++++--- pewpew.asm | 74 ++++++++++++++++++++++++++++++++++++++++++++++++-- sprites32.pcx | Bin 2258 -> 2268 bytes 3 files changed, 78 insertions(+), 6 deletions(-) diff --git a/memory.asm b/memory.asm index ae3c391..93f24e8 100644 --- a/memory.asm +++ b/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 diff --git a/pewpew.asm b/pewpew.asm index 5978d5e..75be138 100644 --- a/pewpew.asm +++ b/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. diff --git a/sprites32.pcx b/sprites32.pcx index af599427e198776708c4edb6e6d812f3da23c60a..99f3f32eea6ff3dc0b7cbd8a51727509216d1baa 100644 GIT binary patch delta 150 zcmca4ct>zT7yDthy9|ff?@XL1QGb|=?KT7ZVJ`OD3?~^FIhYS|urRSQUT0wBWIn_R zVxD7QLsD}BtD5sDYK}87au^#3+#CVl~k%yg!9n3z@0JWFlD8pe!wnN+;hgdet IF>YZ808ZX0c>n+a delta 105 zcmca3cu8