Prev: 8ED7 Up: Map Next: 8F66
8EEF: Update Velocity
Used by the routine at ControlPlayer.
Changes the velocities of an entity that is controlled by ControlPlayer, and works out how many whole pixels it will move this frame. Each velocity (at offsets 06 and 07) has the change added, and is then limited to the fastest speed in either direction. The top four bits of the velocity are the pixels to move, which is GetWholePixels. While the steps of an automatic walk are being counted down by AutoWalkStep (the bottom four bits of offset 02) the pixels moved are 00 or 02 instead, depending on whether the velocity is below 10 with the sign of the velocity.
Input
DE The change in each velocity (X in E and Y in D)
HL The fastest velocity (X in L and Y in H)
IX The entity
Output
DE The pixels to move (X in E and Y in D)
UpdateVelocity 8EEF LD A,(IX+$02) Fetch offset 02 of the entity.
8EF2 AND %00001111 Keep only bits 0-3.
8EF4 JR NZ,UpdateVelocity_Walk Jump to UpdateVelocity_Walk if an automatic walk is under way.
Change the X velocity, and keep it within the fastest speed.
8EF6 LD A,E Fetch the change in the X velocity from E.
8EF7 ADD A,(IX+$06) Add the X velocity.
8EFA JP M,UpdateVelocity_NegativeX Jump to UpdateVelocity_NegativeX if the result is negative.
8EFD CP L Jump to UpdateVelocity_SetX if the X velocity is slower than the fastest X velocity.
8EFE JR C,UpdateVelocity_SetX
8F00 LD A,L Use the fastest X velocity.
UpdateVelocity_SetX 8F01 LD (IX+$06),A Update the X velocity.
Change the Y velocity, and keep it within the fastest speed.
8F04 LD A,D Fetch the change in the Y velocity from D.
8F05 ADD A,(IX+$07) Add the Y velocity.
8F08 JP M,UpdateVelocity_NegativeY Jump to UpdateVelocity_NegativeY if the result is negative.
8F0B CP H Jump to UpdateVelocity_SetY if the Y velocity is slower than the fastest Y velocity.
8F0C JR C,UpdateVelocity_SetY
8F0E LD A,H Use the fastest Y velocity.
UpdateVelocity_SetY 8F0F LD (IX+$07),A Update the Y velocity.
Get the whole pixels from the velocities.
8F12 LD A,(IX+$06) Fetch the X velocity.
8F15 CALL GetWholePixels Call GetWholePixels to convert the velocity to whole pixels.
8F18 LD E,A Keep the whole pixels of the X velocity in E.
8F19 LD A,(IX+$07) Fetch the Y velocity.
8F1C CALL GetWholePixels Call GetWholePixels to convert the velocity to whole pixels.
8F1F LD D,A Keep the whole pixels of the Y velocity in D.
8F20 RET Return.
The X velocity is negative, so limit it using its size.
UpdateVelocity_NegativeX 8F21 NEG Make the X velocity positive.
8F23 CP L Jump to UpdateVelocity_NegateX if the X velocity is slower than the fastest X velocity.
8F24 JR C,UpdateVelocity_NegateX
8F26 LD A,L Use the fastest X velocity.
UpdateVelocity_NegateX 8F27 NEG Make the X velocity negative again.
8F29 JR UpdateVelocity_SetX Jump to UpdateVelocity_SetX to set the X velocity.
The Y velocity is negative, so limit it using its size.
UpdateVelocity_NegativeY 8F2B NEG Make the Y velocity positive.
8F2D CP H Jump to UpdateVelocity_NegateY if the Y velocity is slower than the fastest Y velocity.
8F2E JR C,UpdateVelocity_NegateY
8F30 LD A,H Use the fastest Y velocity.
UpdateVelocity_NegateY 8F31 NEG Make the Y velocity negative again.
8F33 JR UpdateVelocity_SetY Jump to UpdateVelocity_SetY to set the Y velocity.
An automatic walk is under way, so move by two pixels on an axis if its velocity is 10 or more, or else not at all.
UpdateVelocity_Walk 8F35 LD A,(IX+$06) Fetch the X velocity.
8F38 AND A Jump to UpdateVelocity_WalkNegativeX if the X velocity is negative.
8F39 JP M,UpdateVelocity_WalkNegativeX
8F3C AND %11110000 Keep only bits 4-7.
8F3E JR Z,UpdateVelocity_WalkY Jump to UpdateVelocity_WalkY if the result is zero.
8F40 LD A,$02 A=02 (move two pixels right).
UpdateVelocity_WalkY 8F42 LD E,A Keep the X move in E.
8F43 LD A,(IX+$07) Fetch the Y velocity.
8F46 AND A Jump to UpdateVelocity_WalkNegativeY if the Y velocity is negative.
8F47 JP M,UpdateVelocity_WalkNegativeY
8F4A AND %11110000 Keep only bits 4-7.
8F4C JR Z,UpdateVelocity_WalkEnd Jump to UpdateVelocity_WalkEnd if the result is zero.
8F4E LD A,$02 A=02 (move two pixels down).
UpdateVelocity_WalkEnd 8F50 LD D,A Keep the Y move in D.
8F51 RET Return.
UpdateVelocity_WalkNegativeX 8F52 NEG Make the X velocity positive.
8F54 AND %11110000 Keep only bits 4-7.
8F56 JR Z,UpdateVelocity_WalkY Jump to UpdateVelocity_WalkY if the result is zero.
8F58 LD A,$FE A=-02 (move two pixels left).
8F5A JR UpdateVelocity_WalkY Jump to UpdateVelocity_WalkY to do the walk on the Y axis.
UpdateVelocity_WalkNegativeY 8F5C NEG Make the Y velocity positive.
8F5E AND %11110000 Keep only bits 4-7.
8F60 JR Z,UpdateVelocity_WalkEnd Jump to UpdateVelocity_WalkEnd if the result is zero.
8F62 LD A,$FE A=-02 (move two pixels up).
8F64 JR UpdateVelocity_WalkEnd Jump to UpdateVelocity_WalkEnd to finish the walk.
Prev: 8ED7 Up: Map Next: 8F66