Prev: 30103 Up: Map Next: 30248
30106: Handler: UFO
Controls the UFO hazard. The UFO wanders randomly around the screen and attempts to abduct Percy. When it catches Percy, it teleports him to the UFO's position, plays a tractor beam sound effect, then drops him downward. The UFO cannot be stunned by Percy's egg, but a hit will cancel the egg.
Input
IX Pointer to the UFO sprite data
Check for respawn.
Handler_UFO 30106 LD A,(24507) Call Reset_UFO_Position if *RespawnFlag is set to reset the UFO.
30109 OR A
30110 CALL NZ,Reset_UFO_Position
Decrement the movement delay timer.
30113 LD HL,30329 Decrement *UFO_Movement_Delay.
30116 DEC (HL)
30117 JR NZ,Move_UFO Jump to Move_UFO if *UFO_Movement_Delay hasn't reached zero.
Movement delay expired; generate new random direction and delay.
New_UFO_Direction 30119 CALL Generate_Random_Number Call Generate_Random_Number to generate a random number.
30122 LD HL,30329 Point HL at UFO_Movement_Delay (UFO movement delay).
30125 PUSH AF Stash the random value on the stack.
30126 AND %00000111 Keep only bits 0-2.
30128 INC HL Increment HL by one to UFO_Direction.
30129 LD (HL),A Write A to *HL to set the new direction.
30130 DEC HL Move HL back to UFO_Movement_Delay.
30131 POP AF Restore the random value from the stack.
30132 RRCA Rotate the random value right three times.
30133 RRCA
30134 RRCA
30135 AND %00011111 Keep only bits 0-4.
30137 OR %00000100 Set bit 2 to produce a delay.
30139 LD (HL),A Write A to *UFO_Movement_Delay.
30140 PUSH HL Stash the UFO state data pointer on the stack.
Set a random value for the UFO speed between 1-3.
Generate_UFO_Speed 30141 CALL Generate_Random_Number Call Generate_Random_Number.
30144 AND %00000011 Keep only bits 0-1 to limit the random number to between 0-3.
30146 OR A Jump back to Generate_UFO_Speed to try again if the speed is zero.
30147 JR Z,Generate_UFO_Speed
30149 LD (30331),A Write the random speed to *UFO_Speed.
30152 POP HL Restore the UFO state data pointer from the stack.
Move the UFO in its current direction.
Move_UFO 30153 INC HL Load the direction from *HL+1 mask and double to form a jump table index.
30154 LD A,(HL)
30155 AND %00000111
30157 ADD A,A
30158 LD (27593),A Write the direction index to *27593.
30161 LD HL,30331 Point HL at UFO_Speed.
30164 LD C,(IX+0) Load the UFO's X position into C and Y position into B from *IX+0 and *IX+1.
30167 LD B,(IX+1)
30170 CALL Direction_Jump_Table Call Direction_Jump_Table to move the UFO in the current direction.
30173 JR C,New_UFO_Direction Jump to New_UFO_Direction if the move was invalid (carry set) and pick a new direction.
Update the UFO's animation frame, wrapping at 3.
30175 LD HL,30332 Point HL at UFO_Animation_Counter.
30178 LD A,(HL) Increment the animation counter, skip to Set_UFO_Frame if it's not yet at frame 3.
30179 INC A
30180 CP 3
30182 JR NZ,Set_UFO_Frame
30184 XOR A Reset the frame back to 0.
Set_UFO_Frame 30185 LD (HL),A Write the UFO frame to *UFO_Animation_Counter.
Sprite ID Sprite
79 udg46331-56x4
30186 ADD A,79 Add 79 as the base UFO frame and write it to *IX+3.
30188 LD (IX+3),A
30191 LD A,(30333) Toggle the sprite flicker flag at *UFO_Flicker_Flag between 0 and 1.
30194 INC A
30195 AND %00000001
30197 LD (30333),A
30200 INC A Write 1 to *IX+2 (sprite active value).
30201 LD (IX+2),A
Check if the UFO has recently abducted Percy.
30204 LD A,(30334) Jump to Check_UFO_Percy if *UFO_Abduction_Timer is zero.
30207 OR A
30208 JR Z,Check_UFO_Percy
30210 DEC A Decrement the abduction timer and write it back to *UFO_Abduction_Timer.
30211 LD (30334),A
30214 JR UFO_Abduct_Percy Jump to UFO_Abduct_Percy.
Check for collision with Percy.
Check_UFO_Percy 30216 LD IY,56000 Point IY at Percy_X_Position (Percy sprite data).
30220 CALL CheckSpriteCollision Call CheckSpriteCollision to check for collision with Percy.
30223 JR C,Check_UFO_Egg Jump to Check_UFO_Egg if carry is set (no collision).
UFO has caught Percy; set the abduction timer.
30225 LD A,100 Set 100 as the *UFO_Abduction_Timer value.
30227 LD (30334),A
30230 RET Return.
No collision with Percy; check if Percy's egg hits the UFO.
Check_UFO_Egg 30231 LD A,(24489) Return if *EggDropFlag is unset.
30234 OR A
30235 RET Z
30236 LD IY,56032 Point IY at Egg_X_Position (egg sprite data).
30240 CALL CheckEggCollision Call CheckEggCollision to check for egg collision with the UFO.
30243 RET C Return if carry is set (no collision).
30244 CALL CancelEggDrop Call CancelEggDrop.
30247 RET Return.
Prev: 30103 Up: Map Next: 30248