Prev: 82F1 Up: Map Next: 83BA
8301: Handle Bat
The handler for a bat, which has the sprite IDs 6A and 6B. If the player is in another room then CountDownCreature counts the bat down until it is removed. Otherwise the bat is counted, tested for touching the player (see CheckPlayerTouch) and for being hit by a weapon (see CheckWeaponHit), and then it flies. The step it is on in its flight pattern is held at offset 09 of the entity, and BatFlightPattern holds the distance to fly on each axis for each step. The low bits of offset 08 hold the direction: bit 0 is set when moving right, bit 1 is set when moving down and bit 2 swaps which of the two distances is used for each axis. The bat turns around at the walls.
Input
IX The bat entity
HandlerBatlet 8301 LD A,($EA91) Fetch the room of the player from PlayerRoom.
8304 CP (IX+$01) Jump to CountDownCreature if the bat is not in the same room as the player.
8307 JP NZ,CountDownCreature
830A CALL StoreEntity Call StoreEntity to save the position of the bat.
830D LD HL,$5E25 Point HL at the number of creatures at NumCreatures.
8310 INC (HL) Increment the number of creatures by one.
Check whether the bat has touched the player or been hit by a weapon.
8311 CALL CheckPlayerTouch Call CheckPlayerTouch to check whether the entity is touching the player.
8314 DEC E Jump to HandleTouchingCreature if the bat touched the player.
8315 JP Z,HandleTouchingCreature
8318 CALL CheckWeaponHit Call CheckWeaponHit to check whether the weapon has hit the entity.
831B DEC E Jump to PopCreature if the bat was hit by a weapon.
831C JP Z,PopCreature
831F LD (IX+$0F),$00 Write 00 to offset 0F of the bat, so it is not removed.
8323 LD DE,($5E1D) Fetch the size of the room from CurrentRoom_Width.
Move on to the next step of the pattern. A new direction is chosen every sixteen steps.
8327 LD A,(IX+$09) Fetch the step of the flight pattern.
832A INC A Increment A by one.
832B AND %00001111 Keep only bits 0-3.
832D LD (IX+$09),A Write A back to the bat.
8330 JP NZ,HandlerBatlet_Move Jump to HandlerBatlet_Move unless the pattern has restarted.
8333 LD A,R Fetch a random number (from the refresh register).
8335 AND %00000111 Keep only bits 0-2.
8337 LD (IX+$08),A Update the direction of the bat.
Move the bat vertically.
HandlerBatlet_Move 833A CALL GetBatStep Call GetBatStep to get the table entry for this step.
833D LD (IX+$05),$43 Set the attribute of the entity to INK:MAGENTA, PAPER:BLACK (BRIGHT).
8341 JR NZ,HandlerBatlet_MoveY Jump to HandlerBatlet_MoveY if the axes are not swapped.
8343 INC HL Move on to the second distance.
HandlerBatlet_MoveY 8344 LD A,(HL) Fetch the distance to fly.
8345 BIT 1,(IX+$08) Jump to HandlerBatlet_AddY if the bat is moving down.
8349 JR NZ,HandlerBatlet_AddY
834B NEG Reverse the distance, so that the bat flies up.
HandlerBatlet_AddY 834D ADD A,(IX+$04) Add the Y position of the bat.
8350 LD C,A Keep the new Y position in C.
8351 SUB $68 Subtract 68 (the vertical centre of the room).
8353 JR C,HandlerBatlet_CheckTop Jump to HandlerBatlet_CheckTop if the new position is above the centre.
8355 CP D Jump to HandlerBatlet_TurnUp if the new position is beyond the bottom wall.
8356 JR NC,HandlerBatlet_TurnUp
8358 JR HandlerBatlet_SetY Jump to HandlerBatlet_SetY to set the Y position of the bat.
HandlerBatlet_CheckTop 835A NEG Make the distance from the centre positive.
835C CP D Jump to HandlerBatlet_TurnDown if the new position is beyond the top wall.
835D JR NC,HandlerBatlet_TurnDown
HandlerBatlet_SetY 835F LD (IX+$04),C Update the Y position of the bat.
Move the bat horizontally.
HandlerBatlet_Horizontal 8362 CALL GetBatStep Call GetBatStep to get the table entry for this step.
8365 JR Z,HandlerBatlet_MoveX Jump to HandlerBatlet_MoveX if the axes are swapped.
8367 INC HL Move on to the second distance.
HandlerBatlet_MoveX 8368 LD A,(HL) Fetch the distance to fly.
8369 BIT 0,(IX+$08) Jump to HandlerBatlet_AddX if the bat is moving right.
836D JR NZ,HandlerBatlet_AddX
836F NEG Reverse the distance, so that the bat flies left.
HandlerBatlet_AddX 8371 ADD A,(IX+$03) Add the X position of the bat.
8374 LD C,A Keep the new X position in C.
8375 SUB $58 Subtract 58 (the horizontal centre of the room).
8377 JR C,HandlerBatlet_CheckLeft Jump to HandlerBatlet_CheckLeft if the new position is left of the centre.
8379 CP E Jump to HandlerBatlet_TurnLeft if the new position is beyond the right wall.
837A JR NC,HandlerBatlet_TurnLeft
837C JR HandlerBatlet_SetX Jump to HandlerBatlet_SetX to set the X position of the bat.
HandlerBatlet_CheckLeft 837E NEG Make the distance from the centre positive.
8380 CP E Jump to HandlerBatlet_TurnRight if the new position is beyond the left wall.
8381 JR NC,HandlerBatlet_TurnRight
HandlerBatlet_SetX 8383 LD (IX+$03),C Update the X position of the bat.
Flap the wings, by changing the sprite every fourth step.
8386 LD A,(IX+$09) Fetch the step of the flight pattern.
8389 RRA Divide the step by four, so that the wings flap every fourth step.
838A RRA
838B AND %00000001 Keep only bit 0.
838D LD C,A Keep the wing frame in C.
838E LD A,(IX+$00) Fetch the sprite ID of the bat.
8391 AND %11111110 Reset bit 0.
8393 ADD A,C Add the wing frame to the sprite ID.
8394 LD (IX+$00),A Update the sprite ID of the bat.
Remove the bat if the player is no longer in play, otherwise display it.
8397 LD A,($EA90) Fetch the sprite ID of the player from Player.
839A CP $31 Jump to PopCreature if the player is no longer in play (the sprite ID is 31 or more).
839C JP NC,PopCreature
839F JP HandlerCharacter_Display Jump to HandlerCharacter_Display to display the bat.
The bat has reached a wall so reverse its direction.
HandlerBatlet_TurnUp 83A2 RES 1,(IX+$08) Reset bit 1 of the direction of the bat, so it moves up.
83A6 JR HandlerBatlet_Horizontal Jump to HandlerBatlet_Horizontal to move the bat horizontally.
HandlerBatlet_TurnDown 83A8 SET 1,(IX+$08) Set bit 1 of the direction of the bat, so it moves down.
83AC JR HandlerBatlet_Horizontal Jump to HandlerBatlet_Horizontal to move the bat horizontally.
HandlerBatlet_TurnRight 83AE SET 0,(IX+$08) Set bit 0 of the direction of the bat, so it moves right.
83B2 JR HandlerBatlet_SetX Jump to HandlerBatlet_SetX to set the X position of the bat.
HandlerBatlet_TurnLeft 83B4 RES 0,(IX+$08) Reset bit 0 of the direction of the bat, so it moves left.
83B8 JR HandlerBatlet_SetX Jump to HandlerBatlet_SetX to set the X position of the bat.
Prev: 82F1 Up: Map Next: 83BA