Prev: 6CDE Up: Map Next: 6D90
6CDF: Handle Butterfly
Used by the routine at Handler_RedBird.
Controls the bonus butterfly collectible. Butterflies flutter around specific rooms and can be collected by Percy for 02 bonus points. They appear only on certain room/level combinations defined in the table at Butterfly_Room_Table. When collected, bit 7 of the table entry is set to prevent recollection until the lives counter changes or Percy respawns.
Input
IX Pointer to the butterfly sprite data at Butterfly_X_Position
Set up the butterfly sprite and check if any collected butterflies need resetting.
Handle_Butterfly 6CDF LD IX,$DAE4 Point IX at Butterfly_X_Position (butterfly sprite data).
6CE3 LD (IX+$03),$00 Write 00 to *IX+03 (clear the butterfly sprite frame).
6CE7 LD A,($5FBE) Call Reset_Butterfly_Flags to reset collected flags if *Lives_Backup (lives backup) is unset.
6CEA OR A
6CEB CALL NZ,Reset_Butterfly_Flags
6CEE LD A,($5FB3) Fetch the lives display character from *LivesDisplayCharacter into B.
6CF1 LD B,A
6CF2 LD A,($6CC3) Call Reset_Butterfly_Flags to reset collected flags if *Lives_Display_Previous (previous lives count) does not equal B (lives count has changed).
6CF5 CP B
6CF6 CALL NZ,Reset_Butterfly_Flags
Search the butterfly table for an entry matching the current room and level.
6CF9 LD A,($5FC5) Load the current room from *CurrentRoom into C.
6CFC LD C,A
6CFD LD A,($5FB1) Load the current level from *CurrentLevel into B.
6D00 LD B,A
6D01 LD HL,$6CC6 Point HL at Butterfly_Room_Table (butterfly room/level table).
Search_Butterfly_Table 6D04 LD A,(HL) Fetch the room number from *HL.
6D05 CP $FF Return if the terminator byte has been found (end of table).
6D07 RET Z
6D08 CP C Jump to Skip_Butterfly_Entry_02 if the room number does not match C.
6D09 JR NZ,Skip_Butterfly_Entry_02
6D0B INC HL Advance to the level byte.
6D0C LD A,(HL) Fetch the level number from *HL.
6D0D CP B Jump to Skip_Butterfly_Entry_01 if the level does not match B.
6D0E JR NZ,Skip_Butterfly_Entry_01
6D10 BIT 7,(HL) Jump to Skip_Butterfly_Entry_01 if bit 7 of *HL is set (butterfly already collected).
6D12 JR NZ,Skip_Butterfly_Entry_01
6D14 JR Animate_Butterfly Jump to Animate_Butterfly (matching entry found).
Skip_Butterfly_Entry_02 6D16 INC HL Advance HL by two bytes to skip this entry.
Skip_Butterfly_Entry_01 6D17 INC HL Advance HL to skip this entry.
6D18 JR Search_Butterfly_Table Jump back to Search_Butterfly_Table.
Matching butterfly found; animate and move it.
Animate_Butterfly 6D1A PUSH HL Stash the table entry pointer on the stack.
6D1B LD A,$19 Set the border to yellow (19 output to port FE).
6D1D OUT ($FE),A
6D1F LD A,($5FBB) Call Randomise_Butterfly_Position to randomise the butterfly position if *RespawnFlag (respawn flag) is non-zero.
6D22 OR A
6D23 CALL NZ,Randomise_Butterfly_Position
Update the butterfly animation frame with a wing-flapping effect.
6D26 LD A,($6CDE) Fetch the butterfly colour from *Butterfly_Direction into B.
6D29 LD B,A
6D2A LD A,($6CDC) Toggle bit 0 of *Butterfly_Wing_Toggle (wing flap toggle) and write back.
6D2D XOR $01
6D2F LD ($6CDC),A
6D32 ADD A,$03 Add 03 to get the base butterfly frame.
6D34 BIT 2,B If bit 2 of B is set, jump to Set_Butterfly_Frame; otherwise add 02 for the alternate colour variant.
6D36 JR Z,Set_Butterfly_Frame
6D38 ADD A,$02
Set_Butterfly_Frame 6D3A LD (IX+$03),A Write the frame to *IX+03.
Decrement the movement delay and pick a new direction when it expires.
6D3D LD HL,$6CDD Point HL at Butterfly_Movement_Delay (butterfly movement delay).
6D40 DEC (HL) Decrement the delay counter.
6D41 JR NZ,Move_Butterfly Jump to Move_Butterfly if the delay has not reached zero.
Handle_Butterfly_0 6D43 CALL GenerateRandomNumber Call GenerateRandomNumber to generate a random number.
6D46 PUSH AF Stash the random value on the stack.
6D47 AND %00000111 Mask with 07 to get a direction.
6D49 LD ($6CDE),A Write the direction to *Butterfly_Direction.
6D4C POP AF Restore the random value from the stack.
6D4D LD HL,$6CDD Point HL at Butterfly_Movement_Delay (butterfly movement delay).
6D50 RRCA Rotate right three times.
6D51 RRCA
6D52 RRCA
6D53 AND %00001111 Mask with 0F.
6D55 OR %00000001 Set bit 0 to ensure a minimum delay of 01.
6D57 LD (HL),A Write the new delay to *HL.
Move the butterfly in its current direction.
Move_Butterfly 6D58 LD A,($6CDE) Fetch the direction from *Butterfly_Direction.
6D5B ADD A,A Double it to form a jump table index.
6D5C LD ($6BC9),A Write the direction index to *6BC9.
6D5F LD HL,$6CC5 Point HL at Butterfly_Movement_Boundary (butterfly movement boundary data).
6D62 LD C,(IX+$00) Load the butterfly's X position into C and Y position into B from *IX+00 and *IX+01.
6D65 LD B,(IX+$01)
6D68 CALL Direction_Jump_Table Call Direction_Jump_Table to move the butterfly in the current direction.
6D6B JR C,Handle_Butterfly_0 Jump to Handle_Butterfly_0 if the move was invalid (carry set) and pick a new direction.
Check if the butterfly has collided with Percy.
6D6D LD IY,$DAE4 Point IY at Butterfly_X_Position (butterfly sprite data).
6D71 LD IX,$DAC0 Point IX at Percy_X_Position (Percy sprite data).
6D75 CALL CheckEggCollision Call CheckEggCollision to check for collision with Percy.
6D78 JR C,Butterfly_No_Collision Jump to Butterfly_No_Collision if carry is set (no collision).
Butterfly collected by Percy.
Butterfly_Collected 6D7A POP HL Restore the table entry pointer from the stack.
6D7B SET 7,(HL) Set bit 7 of *HL to mark this butterfly as collected.
6D7D LD B,$60 Set the sound loop counter to 60 in B.
Butterfly_Sound_Loop 6D7F PUSH BC Stash the sound counter on the stack.
6D80 CALL UpdateEnergyBar_Refill_Shift Call UpdateEnergyBar_Refill_Shift to play one step of the collection sound.
6D83 POP BC Restore the sound counter from the stack.
6D84 DJNZ Butterfly_Sound_Loop Decrease the counter and loop back to Butterfly_Sound_Loop until the sound is complete.
6D86 LD A,$02 Call AddToScore to add 02 points to the score.
6D88 CALL AddToScore
6D8B LD B,$09 Set B to 09.
6D8D RET Return.
Butterfly_No_Collision 6D8E POP HL Restore the table entry pointer from the stack.
6D8F RET Return.
Prev: 6CDE Up: Map Next: 6D90