Prev: DD72 Up: Map Next: DDCF
DD99: Find Direction To Player
Used by the routine at UpdateDirection.
Works out which of the four directions an object has to face to move towards the player, using the axis along which the player is furthest away.
Input
IX Object
Output
A The direction (0 up, 1 right, 2 down and 3 left)
Find the axis along which the player is furthest away.
FindDirectionToPlayer DD99 LD HL,($BC8F) Work out the distance along the X axis from the object to the player.
DD9C LD E,(IX+$01)
DD9F LD D,(IX+$02)
DDA2 AND A
DDA3 SBC HL,DE
DDA5 PUSH HL Stash the signed distance on the stack.
DDA6 BIT 7,H Make it positive, and keep it in DE.
DDA8 CALL NZ,NegateHL
DDAB EX DE,HL
DDAC LD HL,($BC91) Work out the distance along the Y axis from the object to the player.
DDAF LD C,(IX+$03)
DDB2 LD B,(IX+$04)
DDB5 AND A
DDB6 SBC HL,BC
DDB8 PUSH HL Stash the signed distance on the stack.
DDB9 BIT 7,H Make it positive.
DDBB CALL NZ,NegateHL
DDBE AND A Compare it with the distance along the X axis, and start with the direction up the screen.
DDBF SBC HL,DE
DDC1 LD A,$00
DDC3 POP HL Restore the distances from the stack.
DDC4 POP DE
DDC5 JR NC,FindDirectionToPlayer_Sign Jump to FindDirectionToPlayer_Sign if the player is further along the Y axis.
DDC7 INC A Use the direction to the right, and use the distance along the X axis.
DDC8 EX DE,HL
Reverse the direction if the player is at a negative distance.
FindDirectionToPlayer_Sign DDC9 BIT 7,H Return if the player is not at a negative distance.
DDCB RET Z
DDCC ADD A,$02 Use the opposite direction, and return.
DDCE RET
Prev: DD72 Up: Map Next: DDCF