A C64 Game - Step 32
A completely unspectacular step, but necessary every now and then anyhow: Bug fixing.
During the last few steps several bugs cropped up that are being addressed now:
o Items could appear outside the play area (in the border)
o On respawn/restart the shells are filled to the max
o On respawn the player now appears at the level start pos
o Autofire on game restart is now blocked
o All active items are removed when the player gets killed

On restarting the player we add these snippets:
The first half removes all active items, the second fills up the players' shells to the max. The last four lines set the player location to the level start position.
;remove all items
ldy #0
.RemoveItem
lda ITEM_ACTIVE,y
cmp #ITEM_NONE
beq .RemoveNextItem
lda #ITEM_NONE
sta ITEM_ACTIVE,y
jsr RemoveItemImage
.RemoveNextItem
iny
cpy #ITEM_COUNT
bne .RemoveItem
;refill shells
ldx #0
.RefillShellImage
lda #2
sta SCREEN_COLOR + 23 * 40 + 19,x
lda #7
sta SCREEN_COLOR + 24 * 40 + 19,x
inx
cpx PLAYER_SHELLS_MAX
bne .RefillShellImage
lda PLAYER_SHELLS_MAX
sta PLAYER_SHELLS
;respawn at correct position
lda PLAYER_START_POS_X
sta PARAM1
lda PLAYER_START_POS_Y
sta PARAM2
The item spawn code is enhanced with a simple check for the position to stay inside the wanted bounds.
!zone SpawnItem
SpawnItem
;find free item slot
ldy #0
.CheckNextItemSlot
lda ITEM_ACTIVE,y
cmp #ITEM_NONE
beq .FreeSlotFound
iny
cpy #ITEM_COUNT
bne .CheckNextItemSlot
rts
.FreeSlotFound
jsr GenerateRandomNumber
and #$1
sta ITEM_ACTIVE,y
sta PARAM1
lda #0
sta ITEM_TIME,y
lda SPRITE_CHAR_POS_X,x
sta ITEM_POS_X,y
;keep item in bounds
cmp #37
bmi .XIsOk
lda #37
sta ITEM_POS_X,y
.XIsOk
lda SPRITE_CHAR_POS_Y,x
sec
sbc #1
sta ITEM_POS_Y,y
cmp #21
bne .YIsOk
lda #20
sta ITEM_POS_X,y
.YIsOk
stx PARAM5
tya
tax
jsr PutItemImage
ldx PARAM5
rts
To be able to respawn the player at the level start pos we now store the object location during level creation:
...
lda PARAM3
sta SPRITE_ACTIVE,x
cmp #TYPE_PLAYER
bne .IsNotPlayer
lda PARAM1
sta PLAYER_START_POS_X
lda PARAM2
sta PLAYER_START_POS_Y
.IsNotPlayer
;PARAM1 and PARAM2 hold x,y already
jsr CalcSpritePosFromCharPos
...
step32.zip