A C64 Game - Step 33
More refinement in this step. This step marks a design change in gameplay. Before the game was planned to be more survival based, with enemies that hunt you about. Beginning with this step the game play gets more Bubble Bobble like.
For one the zombies are changed. Their animation is enhanced and they learned to jump. Also, mummies now attack if they see the player (ie. look in his direction). Plus the left/right moving bat is changed to move diagonally.

Until now the jump variables were only available to the player. Now we rename the PLAYER_JUMP_xxx variables to SPRITE_JUMP_xxx and allow a range of all 8 sprites.
The diagonal moving bat is added. This is quite simple, we add a second direction variable (SPRITE_DIRECTION_Y) for the vertical direction. Copy over the left/right moving code and adjust it to vertical movement. Done!
;------------------------------------------------------------
;simply move diagonal
;------------------------------------------------------------
!zone BehaviourBatDiagonal
BehaviourBatDiagonal
lda DELAYED_GENERIC_COUNTER
and #$3
bne .NoAnimUpdate
inc SPRITE_ANIM_POS,x
lda SPRITE_ANIM_POS,x
and #$3
sta SPRITE_ANIM_POS,x
tay
lda BAT_ANIMATION,y
sta SPRITE_POINTER_BASE,x
.NoAnimUpdate
lda SPRITE_DIRECTION,x
beq .MoveRight
;move left
jsr ObjectMoveLeftBlocking
beq .ToggleDirection
jmp .MoveY
.MoveRight
jsr ObjectMoveRightBlocking
beq .ToggleDirection
jmp .MoveY
.ToggleDirection
lda SPRITE_DIRECTION,x
eor #1
sta SPRITE_DIRECTION,x
.MoveY
lda SPRITE_DIRECTION_Y,x
beq .MoveDown
;move up
jsr ObjectMoveUpBlocking
beq .ToggleDirectionY
rts
.MoveDown
jsr ObjectMoveDownBlocking
beq .ToggleDirectionY
rts
.ToggleDirectionY
lda SPRITE_DIRECTION_Y,x
eor #1
sta SPRITE_DIRECTION_Y,x
rts
For the mummy to react to the player we add this snippet in front of its behaviour code. Detecting the player is done the naive way. The player has to be at the same height (the same char y pos), and the mummy has to look in its direction.
If all these conditions are matched the mummy assumes its attack stance and hurries towards the player.
lda SPRITE_CHAR_POS_Y,x
cmp SPRITE_CHAR_POS_Y
bne .NoPlayerInSight
;player on same height
;looking at the player?
lda SPRITE_DIRECTION,x
beq .LookingRight
lda SPRITE_CHAR_POS_X,x
cmp SPRITE_CHAR_POS_X
bpl .LookingAtPlayer
jmp .NoPlayerInSight
.LookingRight
lda SPRITE_CHAR_POS_X,x
cmp SPRITE_CHAR_POS_X
bmi .LookingAtPlayer
jmp .NoPlayerInSight
.LookingAtPlayer
lda #SPRITE_MUMMY_ATTACK_R
clc
adc SPRITE_DIRECTION,x
sta SPRITE_POINTER_BASE,x
lda SPRITE_DIRECTION,x
beq .AttackRight
;attack to left
jsr ObjectMoveLeftBlocking
jsr ObjectMoveLeftBlocking
beq .ToggleDirection
rts
.AttackRight
;attack to left
jsr ObjectMoveRightBlocking
jsr ObjectMoveRightBlocking
beq .ToggleDirection
rts
.NoPlayerInSight
... old behaviour
Allowing the zombie to jump is pretty easy as well. We already have jumping code for the player. In this step we however just copy the code over. The actual jump code starts at the .jump label.
Increase the jump cointer, fetch the Y movement from a jump table and move the sprite upwards. Once the zombie reaches the end of the table (the peak of the jump) we simply let gravity set in.
!zone BehaviourZombie
BehaviourZombie
lda SPRITE_JUMP_POS,x
bne .IsJumping
jsr ObjectMoveDownBlocking
bne .Falling
.IsJumping
lda DELAYED_GENERIC_COUNTER
and #$3
beq .MovementUpdate
rts
.Falling
rts
.MovementUpdate
lda SPRITE_JUMP_POS,x
bne .UpdateJump
lda SPRITE_STATE,x
bne .OtherStates
jsr GenerateRandomNumber
cmp #17
beq .Jump
jmp .NormalWalk
.OtherStates
;collapsing?
cmp #128
beq .Collapsing1
cmp #129
beq .Collapsing2
cmp #131
bne .NotWakeUp1
jmp .WakeUp1
.NotWakeUp1
cmp #132
bne .NotWakeUp2
jmp .WakeUp2
.NotWakeUp2
cmp #130
bne .NotHidden
jmp .Hidden
.NotHidden
rts
.Jump
;start jump
lda #SPRITE_ZOMBIE_JUMP_R
clc
adc SPRITE_DIRECTION,x
sta SPRITE_POINTER_BASE,x
.UpdateJump
inc SPRITE_JUMP_POS,x
lda SPRITE_JUMP_POS,x
cmp #JUMP_TABLE_SIZE
bne .JumpOn
;jump done
lda #0
sta SPRITE_JUMP_POS,x
rts
.JumpOn
ldy SPRITE_JUMP_POS,x
lda JUMP_TABLE,y
bne .KeepJumping
rts
.KeepJumping
sta PARAM5
.JumpContinue
jsr ObjectMoveUpBlocking
beq .JumpBlocked
dec PARAM5
bne .JumpContinue
rts
.JumpBlocked
lda #0
sta SPRITE_JUMP_POS,x
rts
... common zombie behaviour
step33.zip