Prev: 33521 Up: Map Next: 33722
33537: Handle Bat
The handler for a bat, which has the sprite IDs 106 and 107. 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 9 of the entity, and BatFlightPattern holds the distance to fly on each axis for each step. The low bits of offset 8 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 33537 LD A,(60049) Fetch the room of the player from PlayerRoom.
33540 CP (IX+1) Jump to CountDownCreature if the bat is not in the same room as the player.
33543 JP NZ,CountDownCreature
33546 CALL StoreEntity Call StoreEntity to save the position of the bat.
33549 LD HL,24101 Point HL at the number of creatures at NumCreatures.
33552 INC (HL) Increment the number of creatures by one.
Check whether the bat has touched the player or been hit by a weapon.
33553 CALL CheckPlayerTouch Call CheckPlayerTouch to check whether the entity is touching the player.
33556 DEC E Jump to HandleTouchingCreature if the bat touched the player.
33557 JP Z,HandleTouchingCreature
33560 CALL CheckWeaponHit Call CheckWeaponHit to check whether the weapon has hit the entity.
33563 DEC E Jump to PopCreature if the bat was hit by a weapon.
33564 JP Z,PopCreature
33567 LD (IX+15),0 Write 0 to offset 15 of the bat, so it is not removed.
33571 LD DE,(24093) 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.
33575 LD A,(IX+9) Fetch the step of the flight pattern.
33578 INC A Increment A by one.
33579 AND %00001111 Keep only bits 0-3.
33581 LD (IX+9),A Write A back to the bat.
33584 JP NZ,HandlerBatlet_Move Jump to HandlerBatlet_Move unless the pattern has restarted.
33587 LD A,R Fetch a random number (from the refresh register).
33589 AND %00000111 Keep only bits 0-2.
33591 LD (IX+8),A Update the direction of the bat.
Move the bat vertically.
HandlerBatlet_Move 33594 CALL GetBatStep Call GetBatStep to get the table entry for this step.
33597 LD (IX+5),67 Set the attribute of the entity to INK:MAGENTA, PAPER:BLACK (BRIGHT).
33601 JR NZ,HandlerBatlet_MoveY Jump to HandlerBatlet_MoveY if the axes are not swapped.
33603 INC HL Move on to the second distance.
HandlerBatlet_MoveY 33604 LD A,(HL) Fetch the distance to fly.
33605 BIT 1,(IX+8) Jump to HandlerBatlet_AddY if the bat is moving down.
33609 JR NZ,HandlerBatlet_AddY
33611 NEG Reverse the distance, so that the bat flies up.
HandlerBatlet_AddY 33613 ADD A,(IX+4) Add the Y position of the bat.
33616 LD C,A Keep the new Y position in C.
33617 SUB 104 Subtract 104 (the vertical centre of the room).
33619 JR C,HandlerBatlet_CheckTop Jump to HandlerBatlet_CheckTop if the new position is above the centre.
33621 CP D Jump to HandlerBatlet_TurnUp if the new position is beyond the bottom wall.
33622 JR NC,HandlerBatlet_TurnUp
33624 JR HandlerBatlet_SetY Jump to HandlerBatlet_SetY to set the Y position of the bat.
HandlerBatlet_CheckTop 33626 NEG Make the distance from the centre positive.
33628 CP D Jump to HandlerBatlet_TurnDown if the new position is beyond the top wall.
33629 JR NC,HandlerBatlet_TurnDown
HandlerBatlet_SetY 33631 LD (IX+4),C Update the Y position of the bat.
Move the bat horizontally.
HandlerBatlet_Horizontal 33634 CALL GetBatStep Call GetBatStep to get the table entry for this step.
33637 JR Z,HandlerBatlet_MoveX Jump to HandlerBatlet_MoveX if the axes are swapped.
33639 INC HL Move on to the second distance.
HandlerBatlet_MoveX 33640 LD A,(HL) Fetch the distance to fly.
33641 BIT 0,(IX+8) Jump to HandlerBatlet_AddX if the bat is moving right.
33645 JR NZ,HandlerBatlet_AddX
33647 NEG Reverse the distance, so that the bat flies left.
HandlerBatlet_AddX 33649 ADD A,(IX+3) Add the X position of the bat.
33652 LD C,A Keep the new X position in C.
33653 SUB 88 Subtract 88 (the horizontal centre of the room).
33655 JR C,HandlerBatlet_CheckLeft Jump to HandlerBatlet_CheckLeft if the new position is left of the centre.
33657 CP E Jump to HandlerBatlet_TurnLeft if the new position is beyond the right wall.
33658 JR NC,HandlerBatlet_TurnLeft
33660 JR HandlerBatlet_SetX Jump to HandlerBatlet_SetX to set the X position of the bat.
HandlerBatlet_CheckLeft 33662 NEG Make the distance from the centre positive.
33664 CP E Jump to HandlerBatlet_TurnRight if the new position is beyond the left wall.
33665 JR NC,HandlerBatlet_TurnRight
HandlerBatlet_SetX 33667 LD (IX+3),C Update the X position of the bat.
Flap the wings, by changing the sprite every fourth step.
33670 LD A,(IX+9) Fetch the step of the flight pattern.
33673 RRA Divide the step by four, so that the wings flap every fourth step.
33674 RRA
33675 AND %00000001 Keep only bit 0.
33677 LD C,A Keep the wing frame in C.
33678 LD A,(IX+0) Fetch the sprite ID of the bat.
33681 AND %11111110 Reset bit 0.
33683 ADD A,C Add the wing frame to the sprite ID.
33684 LD (IX+0),A Update the sprite ID of the bat.
Remove the bat if the player is no longer in play, otherwise display it.
33687 LD A,(60048) Fetch the sprite ID of the player from Player.
33690 CP 49 Jump to PopCreature if the player is no longer in play (the sprite ID is 49 or more).
33692 JP NC,PopCreature
33695 JP HandlerCharacter_Display Jump to HandlerCharacter_Display to display the bat.
The bat has reached a wall so reverse its direction.
HandlerBatlet_TurnUp 33698 RES 1,(IX+8) Reset bit 1 of the direction of the bat, so it moves up.
33702 JR HandlerBatlet_Horizontal Jump to HandlerBatlet_Horizontal to move the bat horizontally.
HandlerBatlet_TurnDown 33704 SET 1,(IX+8) Set bit 1 of the direction of the bat, so it moves down.
33708 JR HandlerBatlet_Horizontal Jump to HandlerBatlet_Horizontal to move the bat horizontally.
HandlerBatlet_TurnRight 33710 SET 0,(IX+8) Set bit 0 of the direction of the bat, so it moves right.
33714 JR HandlerBatlet_SetX Jump to HandlerBatlet_SetX to set the X position of the bat.
HandlerBatlet_TurnLeft 33716 RES 0,(IX+8) Reset bit 0 of the direction of the bat, so it moves left.
33720 JR HandlerBatlet_SetX Jump to HandlerBatlet_SetX to set the X position of the bat.
Prev: 33521 Up: Map Next: 33722