Prev: CC31 Up: Map Next: CCDE
CC4B: Run Homing Creature
The handler of the object types 30, A0 and A4, which are creatures that chase the player. Each frame the creature speeds up a little towards the player along each axis, bounces off anything that it hits along x or y, and moves.
The speeds that the creature works out are kept in the stored speeds at 14 to 16 of its record in sixteenths, which gives it finer control than the whole numbers that ApplyGravityAndMove uses. The speeds that are used for moving it, in 09 to 0B, are the stored speeds rounded to whole numbers, which can be from FC to 04.
The lowest two bits of the type of the creature are the number of the picture of it, and are changed each frame to make it move.
Input
IX Object record
Move the creature and bounce off anything that it hits.
RunHomingCreature CC4B CALL SetSpriteOffset_Minus12_Minus6 Call SetSpriteOffset_Minus12_Minus6 to set the offsets of the sprite.
CC4E LD (IX+$04),$0A Set the half size of the creature along x to 0A.
CC52 LD (IX+$05),$0A Set the half size of the creature along y to 0A.
CC56 CALL ApplyGravityAndMove Call ApplyGravityAndMove to apply gravity to the creature and move it.
CC59 LD A,(IX+$0C) Fetch the collision flags.
CC5C AND %00000111 Keep the flags for hitting something along x, along y and landing.
CC5E JR Z,RunHomingCreature_Steer Jump to RunHomingCreature_Steer if the creature did not hit anything.
CC60 AND %00000001 Keep the flag for hitting something along x.
CC62 CALL NZ,ReverseSpeedX Call ReverseSpeedX to turn the creature around along x, if it did.
CC65 BIT 1,(IX+$0C) Test the flag for hitting something along y.
CC69 CALL NZ,ReverseSpeedY Call ReverseSpeedY to turn the creature around along y, if it did.
Steer towards the player along x, y and z.
RunHomingCreature_Steer CC6C LD A,($A770) Fetch the x position of the player.
CC6F SUB (IX+$01) Subtract the x position of the creature.
CC72 LD A,(IX+$14) Fetch the stored speed of the creature along x.
CC75 JR NC,RunHomingCreature_ForwardsX Jump to RunHomingCreature_ForwardsX to speed up forwards if the player is further along.
CC77 CALL SpeedUpBackwards Call SpeedUpBackwards to speed up backwards.
RunHomingCreature_StoreX CC7A LD (IX+$14),A Store the speed along x.
CC7D LD A,($A771) Fetch the y position of the player.
CC80 SUB (IX+$02) Subtract the y position of the creature.
CC83 LD A,(IX+$15) Fetch the stored speed of the creature along y.
CC86 JR NC,RunHomingCreature_ForwardsY Jump to RunHomingCreature_ForwardsY to speed up forwards if the player is further along.
CC88 CALL SpeedUpBackwards Call SpeedUpBackwards to speed up backwards.
RunHomingCreature_StoreY CC8B LD (IX+$15),A Store the speed along y.
CC8E LD A,($A792) Fetch the z position of the body of the player.
CC91 SUB (IX+$03) Subtract the z position of the creature.
CC94 LD A,(IX+$16) Fetch the stored speed of the creature along z.
CC97 JR NC,RunHomingCreature_ForwardsZ Jump to RunHomingCreature_ForwardsZ to speed up forwards if the player is higher.
CC99 CALL SpeedUpBackwards Call SpeedUpBackwards to speed up backwards.
RunHomingCreature_StoreZ CC9C LD (IX+$16),A Store the speed along z.
Round the stored speeds to whole numbers.
CC9F LD B,$03 Set a counter for the three speeds in B.
RunHomingCreature_RoundSpeeds CCA1 INC IX Move IX on by one byte, so that the stored speed is at 13 and the speed to move at is at 08, for each speed in turn.
CCA3 LD A,(IX+$13) Fetch the stored speed.
CCA6 ADD A,$08 Add half of the sixteen that is to be divided by, which rounds the result.
CCA8 SRA A Divide it by sixteen, keeping the sign.
CCAA SRA A
CCAC SRA A
CCAE SRA A
CCB0 LD (IX+$08),A Store it as the speed to move at.
CCB3 DJNZ RunHomingCreature_RoundSpeeds Jump to RunHomingCreature_RoundSpeeds until the three speeds have been set.
CCB5 DEC IX Put the record pointer back.
CCB7 DEC IX
CCB9 DEC IX
CCBB JR RunHomingCreature_Animate Jump to RunHomingCreature_Animate to change the picture of the creature.
Speed up forwards along the axis that the player is further along.
RunHomingCreature_ForwardsX CCBD CALL SpeedUpForwards Call SpeedUpForwards to speed up forwards.
CCC0 JR RunHomingCreature_StoreX Jump to RunHomingCreature_StoreX to store the speed.
RunHomingCreature_ForwardsY CCC2 CALL SpeedUpForwards Call SpeedUpForwards to speed up forwards.
CCC5 JR RunHomingCreature_StoreY Jump to RunHomingCreature_StoreY to store the speed.
RunHomingCreature_ForwardsZ CCC7 CALL SpeedUpForwards Call SpeedUpForwards to speed up forwards.
CCCA JR RunHomingCreature_StoreZ Jump to RunHomingCreature_StoreZ to store the speed.
Cycle the picture of the creature.
RunHomingCreature_Animate CCCC INC (IX+$10) Add one to the picture counter of the creature.
CCCF LD A,(IX+$10) Fetch it.
CCD2 AND %00000011 Keep the lowest two bits.
CCD4 LD B,A Put them in B.
CCD5 LD A,(IX+$00) Fetch the type of the creature.
CCD8 AND %11111100 Keep the type without its lowest two bits.
CCDA ADD A,B Add the picture number.
CCDB LD (IX+$00),A Set it as the type of the creature, which carries on with SetWipeAndDrawFlags.
Prev: CC31 Up: Map Next: CCDE