A C64 Game - Step 18
This time we add some enemy animation and a path based movement enemy type. The movement path is stored in a table of delta X and delta Y values. Values with the highest bit set are treated as negative.
The animation of the bat is also stored in a table (it's a simple ping pong loop).
Every objects get an animation delay (SPRITE_ANIM_DELAY), animation pos (SPRITE_ANIM_POS) and movement pos counter (SPRITE_MOVE_POS).
Remember, adding a new type means just adding the new constant and entries to the startup value tables.
If you wonder about the flickering white border on the bottom half: It's an easy way to see how long the actual per frame code runs. You'll notice more complex code taking quite a bit more time.
Here's a detailed look at the path code. It's actually pretty straight forward. Read the next byte. Check if the high bit is set and use the result to either move left/right. Rinse and repeat for Y.
!zone BehaviourBat8
BehaviourBat8
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
inc SPRITE_MOVE_POS,x
lda SPRITE_MOVE_POS,x
and #31
sta SPRITE_MOVE_POS,x
tay
lda PATH_8_DX,y
beq .NoXMoveNeeded
sta PARAM1
and #$80
beq .MoveRight
lda PARAM1
and #$7f
sta PARAM1
.MoveLeft
jsr MoveSpriteLeft
dec PARAM1
bne .MoveLeft
jmp .XMoveDone
.MoveRight
jsr MoveSpriteRight
dec PARAM1
bne .MoveRight
.NoXMoveNeeded
.XMoveDone
ldy SPRITE_MOVE_POS,x
lda PATH_8_DY,y
beq .NoYMoveNeeded
sta PARAM1
and #$80
beq .MoveDown
lda PARAM1
and #$7f
sta PARAM1
.MoveUp
jsr MoveSpriteUp
dec PARAM1
bne .MoveUp
rts
.MoveDown
jsr MoveSpriteDown
dec PARAM1
bne .MoveDown
.NoYMoveNeeded
rts
The tables themselves are handmade. For the planned path we just need to make sure we end up where we started:
PATH_8_DX
!byte $86
!byte $86
!byte $85
!byte $84
!byte $83
!byte $82
!byte $81
!byte 0
!byte 0
!byte 1
!byte 2
!byte 3
!byte 4
!byte 5
!byte 6
!byte 6
!byte 6
!byte 6
!byte 5
!byte 4
!byte 3
!byte 2
!byte 1
!byte 0
!byte 0
!byte $81
!byte $82
!byte $83
!byte $84
!byte $85
!byte $86
!byte $86
PATH_8_DY
!byte 0
!byte 1
!byte 2
!byte 3
!byte 4
!byte 5
!byte 6
!byte 6
!byte 6
!byte 6
!byte 5
!byte 4
!byte 3
!byte 2
!byte 1
!byte 0
!byte 0
!byte $81
!byte $82
!byte $83
!byte $84
!byte $85
!byte $86
!byte $86
!byte $86
!byte $86
!byte $85
!byte $84
!byte $83
!byte $82
!byte $81
!byte 0
step18.zip