Add the code for detecting player collisions, exploding the player, decrementing the number of lives and maybe ending the game.

This commit is contained in:
Jeremy Rand 2020-11-11 23:51:09 -05:00
parent 6e49c4c483
commit 2abb57b1a5
9 changed files with 223 additions and 8 deletions

View File

@ -208,7 +208,6 @@ nextWord anop
startGame entry
stz gameRunning
stz numSegments
jsl addRandomMushrooms
jsl scoreStartGame
jsl initPlayer
@ -216,10 +215,37 @@ startGame entry
jsl levelInit
; Fall through intentionally here...
startLevel entry
jsl segmentsInitLevel
jsl scorpionInitLevel
jsl spiderInitLevel
jsl fleaInitLevel
jsl playerLevelStart
jmp levelStart
gameOver entry
lda #1
sta gameRunning
jsl segmentsInitLevel
jsl scorpionInitLevel
jsl spiderInitLevel
jsl fleaInitLevel
ldx #0
gameOver_loop anop
lda tileType,x
beq gameOver_tileEmpty
stz tileType,x
lda #TILE_STATE_DIRTY
sta tileDirty,x
gameOver_tileEmpty anop
inx
inx
cpx #RHS_FIRST_TILE_OFFSET
blt gameOver_loop
jmp checkHighScore
checkKeyboard entry
checkKey_loop2 anop

View File

@ -23,6 +23,9 @@ FLEA_SCREEN_SPEED_FAST equ 4*SCREEN_BYTES_PER_ROW
FLEA_SLOW_UPDATES_PER_TILE equ TILE_PIXEL_HEIGHT/2-1
FLEA_FAST_UPDATES_PER_TILE equ TILE_PIXEL_HEIGHT/4-1
fleaInitLevel entry
stz fleaState
rtl
drawFlea entry
lda fleaState
@ -64,6 +67,12 @@ jumpInst jmp >flea1
updateFlea entry
lda playerState
cmp #PLAYER_STATE_ONSCREEN
beq updateFlea_playerOnscreen
rtl
updateFlea_playerOnscreen anop
lda fleaState
beq updateFlea_maybeAdd
@ -171,8 +180,6 @@ updateFlea_nextTile anop
updateFlea_bottom anop
stz fleaState
; Uncomment the next line to continuously display fleas.
; jsl addFlea
rtl
updateFlea_nextAction anop

View File

@ -15,6 +15,10 @@ gamePlayer start
using tileData
using screenData
PLAYER_EXPLOSION_FRAME_COUNT equ 4
PLAYER_RESTART_LEVEL_FRAME_COUNT equ 20
initPlayer entry
ldy #STARTING_NUM_LIVES
sty numLives
@ -33,6 +37,8 @@ initPlayer_loop anop
playerLevelStart entry
lda #PLAYER_STATE_ONSCREEN
sta playerState
lda #STARTING_MOUSE_X
sta mouseX
lda #STARTING_MOUSE_Y
@ -69,6 +75,84 @@ updatePlayer entry
beq updatePlayer_gameRunning
rtl
updatePlayer_gameRunning anop
lda playerState
cmp #PLAYER_STATE_NONE
bne updatePlayer_notNone
lda playerFrameCount
bne updatePlayer_wait
lda numLives
beq updatePlayer_gameOver
jmp startLevel
updatePlayer_gameOver anop
jmp gameOver
updatePlayer_wait anop
dec a
sta playerFrameCount
rtl
updatePlayer_notNone anop
cmp #PLAYER_STATE_EXPLODING
beq updatePlayer_exploding
jmp updatePlayer_notExploding
updatePlayer_exploding anop
lda playerFrameCount
beq updatePlayer_nextExplosion
dec a
sta playerFrameCount
bra updatePlayer_drawExplosion
updatePlayer_nextExplosion anop
lda playerExplosionOffset
beq updatePlayer_doneExplosion
sec
sbc #4
sta playerExplosionOffset
bra updatePlayer_drawExplosion
updatePlayer_doneExplosion anop
lda #PLAYER_RESTART_LEVEL_FRAME_COUNT
sta playerFrameCount
lda #PLAYER_STATE_NONE
sta playerState
rtl
updatePlayer_drawExplosion anop
lda mouseAddress
sec
sbc #SCREEN_ADDRESS
and #$fffc
tax
lda >screenToTileOffset,x
tax
lda #TILE_STATE_DIRTY
sta tileDirty,x
ldy tileBelow,x
cpy #INVALID_TILE_NUM
beq updatePlayer_drawExplosionSkipBelow1
sta tileDirty,y
updatePlayer_drawExplosionSkipBelow1 anop
ldy tileRight,x
sta tileDirty,y
ldx tileBelow,y
cpx #INVALID_TILE_NUM
beq updatePlayer_drawExplosionSkipBelow2
sta tileDirty,x
updatePlayer_drawExplosionSkipBelow2 anop
ldx tileRight,y
sta tileDirty,x
ldy tileBelow,x
cpy #INVALID_TILE_NUM
beq updatePlayer_drawExplosionSkipBelow3
sta tileDirty,y
updatePlayer_drawExplosionSkipBelow3 anop
ldy mouseAddress
ldx playerExplosionOffset
lda shipExplosionJumpTable,x
sta jumpInst+1
lda shipExplosionJumpTable+2,x
sta jumpInst+3
jumpInst anop
jmp >shipExplosion1
nop
updatePlayer_notExploding anop
ldx #0
ldy #0
; This code for reading the mouse data is based on some code which John Brooks helpfully provided, although I did things
@ -166,8 +250,44 @@ updatePlayer_shift anop
updatePlayer_dirty anop
beq updatePlayer_noCollision
; Player collision here...
; brk $00
lda #PLAYER_STATE_EXPLODING
sta playerState
lda mouseAddress
sec
sbc #TILE_BYTE_WIDTH/2
sta mouseAddress
sec
sbc #SCREEN_ADDRESS
and #$fffc
tax
lda >screenToTileOffset,x
cmp #RHS_FIRST_TILE_OFFSET
bge updatePlayer_explosionOffLeft
tay
ldx tileRight,y
ldy tileRight,x
cpy #RHS_FIRST_TILE_OFFSET
blt updatePlayer_contCollision
lda mouseAddress
dec a
and #$fffc
sta mouseAddress
bra updatePlayer_contCollision
updatePlayer_explosionOffLeft anop
lda mouseAddress
clc
adc #TILE_BYTE_WIDTH
and #$fffc
sta mouseAddress
updatePlayer_contCollision anop
lda #PLAYER_EXPLOSION_FRAME_COUNT-1
sta playerFrameCount
lda #SHIP_EXPLOSION_LAST_OFFSET
sta playerExplosionOffset
jmp updatePlayer_exploding
updatePlayer_noCollision anop
lda mouseAddress
sec
@ -202,4 +322,22 @@ mouseX dc i2'0'
mouseY dc i2'0'
mouseDown dc i2'0'
playerFrameCount dc i2'0'
playerExplosionOffset dc i2'0'
SHIP_EXPLOSION_LAST_OFFSET equ 7*4
shipExplosionJumpTable anop
dc i4'shipExplosion8'
dc i4'shipExplosion7'
dc i4'shipExplosion6'
dc i4'shipExplosion5'
dc i4'shipExplosion4'
dc i4'shipExplosion3'
dc i4'shipExplosion2'
dc i4'shipExplosion1'
end

View File

@ -27,6 +27,11 @@ SCORPION_FAST_UPDATES_PER_TILE equ TILE_PIXEL_WIDTH/2-1
SCORPION_NUM_POSSIBLE_ROWS equ 15
scorpionInitLevel entry
stz scorpionState
rtl
drawScorpion entry
lda scorpionState
bne drawScorpion_cont
@ -78,6 +83,11 @@ jumpInst jmp >leftScorpion1
updateScorpion entry
lda playerState
cmp #PLAYER_STATE_ONSCREEN
beq updateScorpion_playerOnscreen
rtl
updateScorpion_playerOnscreen anop
lda scorpionState
bne updateScorpion_cont
lda gameLevel

View File

@ -40,6 +40,18 @@ SEGMENT_FACING_RIGHT equ 128
SEGMENT_MAX_POSITION_OFFSET equ TILE_PIXEL_WIDTH*SEGMENT_MAX_NUM*2-2
segmentsInitLevel entry
stz numSegments
ldx #SEGMENT_MAX_OFFSET
lda #SEGMENT_STATE_NONE
segmentsInitLevel_loop anop
sta segmentStates,x
dex
dex
bpl segmentsInitLevel_loop
rtl
drawSegments entry
ldx #SEGMENT_MAX_OFFSET
@ -178,6 +190,11 @@ segmentBodyJump_jumpInst anop
nop
updateSegments entry
lda playerState
cmp #PLAYER_STATE_ONSCREEN
beq updateSegments_playerOnscreen
rtl
updateSegments_playerOnscreen anop
; Clear the segment mask to start.
stz segmentTileMask+0
stz segmentTileMask+2

View File

@ -62,13 +62,16 @@ SPIDER_ADD_TIME equ 120
spiderInitGame entry
stz spiderAddTime
stz spiderState
lda #SPIDER_STARTING_TOP_ROW
sta spiderTopRow
lda #SPRITE_SPEED_SLOW
jmp setSpiderSpeed
spiderInitLevel entry
stz spiderAddTime
stz spiderState
rtl
drawSpider entry
lda spiderState
@ -135,6 +138,11 @@ jumpInst jmp >spider1
updateSpider entry
lda playerState
cmp #PLAYER_STATE_ONSCREEN
beq updateSpider_cont
rtl
updateSpider_cont anop
ldx spiderState
cpx #SPIDER_STATE_LEFT_DIAG_DOWN
blt updateSpider_testState

View File

@ -19,6 +19,10 @@ globalData data
SEGMENT_DIR_LEFT equ 0
SEGMENT_DIR_RIGHT equ 1
PLAYER_STATE_NONE equ 0
PLAYER_STATE_EXPLODING equ 1
PLAYER_STATE_ONSCREEN equ 2
; The code uses segmentPixelOffset and the segment speed to figure out whether to draw the shifted sprite
; or the regular sprite. By AND-ing with the speed, if the result is 0, then we want a non-shifted sprite.
; If the result is non-zero, we want a shifted sprite. Then, we just need a per segment speed instead of a
@ -133,6 +137,7 @@ numInfieldMushrooms dc i2'0'
; tileType
playerState dc i2'PLAYER_STATE_NONE'
mouseAddress dc i2'0'
backupStack dc i2'0'

View File

@ -8,7 +8,7 @@ s6d2 =
s7d1 = /Users/jrand/Library/Developer/Xcode/DerivedData/BuGS-bffpexoblaghkzcbtjtzxeulnuto/Build/Products/Debug/BuGS.2mg
g_limit_speed = 0
g_limit_speed = 3
bram1[00] = 00 00 00 01 00 00 0d 06 02 01 01 00 01 00 00 00

View File

@ -258,6 +258,10 @@ scoreAddOneThousand_skipZeroHundreds anop
ldx #P1_SCORE_THOUSANDS_OFFSET
jmp scoreAddOneToTile
checkHighScore entry
; TODO - Write this code
rtl
highScore dc i4'0'