Prev: 6621 Up: Map Next: 6683
662B: Handler: Worm Collection and Delivery
Used by the routine at Run_Game_Frame.
Manages the worm collectibles in each room. Scans the room attribute data to find active worm tiles, animates them with a wiggling effect, and checks if Percy is at the correct position to pick one up. When Percy is carrying a worm, the worm-in-beak sprite is positioned relative to Percy. On level 06 checks if Percy is at the nest to deliver the worm to the baby chicks for bonus points.
Calculate Percy's tile map position from his pixel coordinates.
Handler_Worms 662B LD IX,$DAC0 Point IX at Percy_X_Position.
662F LD B,$08 Set B to 08.
6631 LD A,(IX+$01) Calculate the tile row from Percy's Y position: add 08 rotate left twice and mask with E0 to get the row component in E.
6634 ADD A,B
6635 RLCA
6636 RLCA
6637 AND %11100000
6639 LD E,A
663A LD A,(IX+$00) Calculate the tile column from Percy's X position: add 02 rotate right three times and mask with 1F then OR with the row component to form the full tile map offset in E.
663D INC A
663E INC A
663F RRCA
6640 RRCA
6641 RRCA
6642 AND %00011111
6644 OR E
6645 LD E,A
6646 LD A,($5FA5) If *PercyFacingDirection (facing direction) is zero (facing right), increment E by one to adjust the lookup position.
6649 OR A
664A JR NZ,Scan_Worms
664C INC E Increment the map offset by one.
Scan the room attribute entries to find active worm tiles.
Scan_Worms 664D LD HL,($5FB5) Load the room attribute pointer from *Room_Object_Data_Pointer into HL.
6650 LD A,($5FB1) Load the worm count from *CurrentLevel, increment it by one and store it in C.
6653 INC A
6654 LD C,A
Scan_Worms_Loop 6655 LD A,(HL) Jump to Skip_Collected_Worm if the current entry is 1E (worm already collected).
6656 CP $1E
6658 JR Z,Skip_Collected_Worm
665A INC HL Advance past the type byte.
665B LD A,(HL) Jump to Skip_Disabled_Worm if the next byte is 1F (the slot is disabled).
665C CP $1F
665E JR Z,Skip_Disabled_Worm
6660 INC HL Advance.
6661 LD A,(HL) Compare the entry index against C; jump to Skip_Invalid_Entry if it is greater or equal (not a valid worm).
6662 INC HL
6663 CP C
6664 JR NC,Skip_Invalid_Entry
6666 LD A,(HL) Jump to Handler_Worm_Found if the tile position matches E (worm found at Percy's location).
6667 CP E
6668 JR Z,Handler_Worm_Found
This entry point is used by the routine at Handler_Worm_Found.
No match at this entry; draw the worm wiggle animation at this tile position and move to the next entry.
Animate_Worm 666A PUSH HL Stash the room attribute pointer, worm counter and position on the stack.
666B PUSH BC
666C PUSH DE
666D LD L,(HL) Load the tile screen address low byte from *HL; set high byte to F0 to form a screen buffer address.
666E LD H,$F0
6670 CALL Handler_DrawWorm Call Handler_DrawWorm to draw the worm wiggle animation frame.
6673 POP DE Restore the position, worm counter and room attribute pointer from the stack.
6674 POP BC
6675 POP HL
This entry point is used by the routine at Handler_Worm_Found.
Next_Worm_Entry 6676 INC HL Advance to the next entry and loop back to Scan_Worms_Loop until all 08 entries are checked.
6677 DJNZ Scan_Worms_Loop
6679 JR Position_Worm_In_Beak Jump to Position_Worm_In_Beak.
Skip entries that are marked as collected or disabled.
Skip_Collected_Worm 667B INC HL Skip one extra byte for collected entries.
Skip_Disabled_Worm 667C INC HL Skip the remaining bytes of this entry.
667D INC HL
Skip_Invalid_Entry 667E INC HL Advance past this entry and loop back to Scan_Worms_Loop until all entries are checked.
667F DJNZ Scan_Worms_Loop
6681 JR Position_Worm_In_Beak Jump to Position_Worm_In_Beak.
Prev: 6621 Up: Map Next: 6683