Prev: C4A3 Up: Map Next: C56C
C4C8: Handle Turning
Used by the routine at HandlePlayerLegs.
Turns the player to the left or the right, if the controls ask for it. A turn takes the player one quarter of the way round, and the player has to wait for two frames after that before it can turn again.
There is a second way of controlling the player, which is for use with a joystick and is chosen by bit 3 of the control method in ControlMethod. That bit is never set, so it is never used. In this way, four of the control bits stand for the four directions that the player can face, and the player turns until it faces the one that is asked for, and then walks.
Input
C Controls
IX Record of the legs
Output
C Controls, with the walking bit set
HandleTurning C4C8 LD HL,$A709 Take the control method, ControlMethod.
C4CB LD A,(HL) Fetch the control method.
C4CC AND %00000110 Keep only the bits that choose a joystick.
C4CE JR Z,HandleTurning_Usual Jump to HandleTurning_Usual to turn the usual way if the keyboard is being used.
C4D0 BIT 3,(HL) Jump to HandleTurning_Usual to turn the usual way if bit 3 of the control method is not set.
C4D2 JR Z,HandleTurning_Usual
Turn towards the direction that is asked for. Return if the player has only just entered the room, or is not on the ground.
C4D4 LD A,(IX+$0C) Fetch the collision flags.
C4D7 AND %11110000 Keep only the countdown from entering the room.
C4D9 RET NZ Return if the player has only just entered the room.
C4DA BIT 2,(IX+$0C) Return if the player is not on the ground.
C4DE RET Z
C4DF BIT 0,C Jump to HandleTurning_TestRight if the bit for turning left is set.
C4E1 JR NZ,HandleTurning_TestRight
C4E3 BIT 2,C Jump to HandleTurning_AskForward if the bit for walking forward is set.
C4E5 JR NZ,HandleTurning_AskForward
HandleTurning_TestRight C4E7 BIT 1,C Jump to HandleTurning_AskRight if the bit for turning right is set.
C4E9 JR NZ,HandleTurning_AskRight
C4EB BIT 4,C Jump to HandleTurning_AskPickUp if the bit for picking up and dropping is set.
C4ED JR NZ,HandleTurning_AskPickUp
C4EF BIT 0,C Jump to HandleTurning_AskLeft if the bit for turning left is set.
C4F1 JR NZ,HandleTurning_AskLeft
C4F3 RES 2,C Clear the bit for walking forward.
C4F5 RET Return.
Turn towards the direction that the bit for walking forward stands for, which is direction 02.
HandleTurning_AskForward C4F6 CALL GetFacingDirection Call GetFacingDirection to find out which way the player is facing.
C4F9 CP $02 Compare it with the direction.
HandleTurning_Compare C4FB JR Z,HandleTurning_Walk Jump to HandleTurning_Walk to walk if the player is facing that way.
C4FD CPL Work out which way to turn from the direction.
HandleTurning_Choose C4FE AND %00000001 Keep only one bit.
C500 JR HandleTurning_Turn Jump to HandleTurning_Turn to turn the player.
Turn towards the direction that the bit for turning right stands for, which is direction 01.
HandleTurning_AskRight C502 CALL GetFacingDirection Call GetFacingDirection to find out which way the player is facing.
C505 CP $01 Compare it with the direction.
C507 JR HandleTurning_Compare Jump to HandleTurning_Compare to walk if the player is facing that way, and to turn if not.
Turn towards the direction that the bit for picking up and dropping stands for, which is direction 03.
HandleTurning_AskPickUp C509 CALL GetFacingDirection Call GetFacingDirection to find out which way the player is facing.
C50C CP $03 Compare it with the direction.
HandleTurning_WalkIfEqual C50E JR Z,HandleTurning_Walk Jump to HandleTurning_Walk to walk if the player is facing that way.
C510 JR HandleTurning_Choose Jump to HandleTurning_Choose to turn if not.
Turn towards the direction that the bit for turning left stands for, which is direction 00.
HandleTurning_AskLeft C512 CALL GetFacingDirection Call GetFacingDirection to find out which way the player is facing.
C515 AND A Test the direction.
C516 JR HandleTurning_WalkIfEqual Jump to HandleTurning_WalkIfEqual to walk if the player is facing that way, and to turn if not.
HandleTurning_Walk C518 SET 2,C Set the bit for walking forward.
C51A RET Return.
Turn the usual way. Wait first if the player has turned during the last two frames.
HandleTurning_Usual C51B LD A,(IX+$0D) Fetch the hit flags, whose lowest three bits hold the time left before the player can turn.
C51E AND %00000111 Keep only the countdown.
C520 JR Z,HandleTurning_TestControls Jump to HandleTurning_TestControls if there is nothing left to wait for.
C522 DEC (IX+$0D) Take one off the countdown.
C525 RET Return.
HandleTurning_TestControls C526 LD A,C Fetch the controls.
C527 AND %00000011 Keep only the bits for turning left and turning right.
C529 RET Z Return if neither is set.
C52A LD A,(IX+$0C) Fetch the collision flags.
C52D AND %11110000 Keep only the countdown from entering the room.
C52F RET NZ Return if the player has only just entered the room.
C530 BIT 3,(IX+$0C) Return if the player is jumping.
C534 RET NZ
C535 BIT 2,C Test the bit for walking forward, which makes no difference.
C537 JR NZ,HandleTurning_StartCountdown
HandleTurning_StartCountdown C539 LD A,(IX+$0D) Fetch the hit flags.
C53C OR %00000010 Set bit 1 of them, to start the countdown that stops the player from turning again at once.
C53E LD (IX+$0D),A Set them.
C541 BIT 1,C Test the bit for turning right.
HandleTurning_Turn C543 JR NZ,HandleTurning_Right Jump to HandleTurning_Right to turn right if it is set.
Turn left, which makes the direction go from 02 to 00, 03, 01 and back to 02. This is done by flipping the mirror flag of the legs, and the type as well if they were mirrored.
C545 BIT 6,(IX+$07) Jump to HandleTurning_FlipMirror if the legs are not mirrored.
C549 JR Z,HandleTurning_FlipMirror
HandleTurning_FlipType C54B LD A,(IX+$00) Fetch the type of the legs.
C54E XOR %00000100 Flip bit 2 of it.
C550 LD (IX+$00),A Write the flipped type of the legs.
HandleTurning_FlipMirror C553 LD A,(IX+$07) Fetch the flags.
C556 XOR %01000000 Flip the bit that mirrors the legs.
C558 LD (IX+$07),A Set them.
C55B LD A,(IX+$00) Set the type of the body to be eight more than the type of the legs.
C55E ADD A,$08
C560 LD (IX+$20),A
C563 RET Return.
Turn right, which makes the direction go from 02 to 01, 03, 00 and back to 02. This is done in the same way, but the type is flipped if the legs were not mirrored.
HandleTurning_Right C564 BIT 6,(IX+$07) Jump to HandleTurning_FlipType if the legs are not mirrored.
C568 JR Z,HandleTurning_FlipType
C56A JR HandleTurning_FlipMirror Jump to HandleTurning_FlipMirror.
View the equivalent code in;
Prev: C4A3 Up: Map Next: C56C