Prev: 52273 Up: Map Next: 52446
52299: Run Homing Creature
The handler of the object types 48, 160 and 164, 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 20 to 22 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 9 to 11, are the stored speeds rounded to whole numbers, which can be from 252 to 4.
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 52299 CALL SetSpriteOffset_Minus12_Minus6 Call SetSpriteOffset_Minus12_Minus6 to set the offsets of the sprite.
52302 LD (IX+4),10 Set the half size of the creature along x to 10.
52306 LD (IX+5),10 Set the half size of the creature along y to 10.
52310 CALL ApplyGravityAndMove Call ApplyGravityAndMove to apply gravity to the creature and move it.
52313 LD A,(IX+12) Fetch the collision flags.
52316 AND %00000111 Keep the flags for hitting something along x, along y and landing.
52318 JR Z,RunHomingCreature_Steer Jump to RunHomingCreature_Steer if the creature did not hit anything.
52320 AND %00000001 Keep the flag for hitting something along x.
52322 CALL NZ,ReverseSpeedX Call ReverseSpeedX to turn the creature around along x, if it did.
52325 BIT 1,(IX+12) Test the flag for hitting something along y.
52329 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 52332 LD A,(42864) Fetch the x position of the player.
52335 SUB (IX+1) Subtract the x position of the creature.
52338 LD A,(IX+20) Fetch the stored speed of the creature along x.
52341 JR NC,RunHomingCreature_ForwardsX Jump to RunHomingCreature_ForwardsX to speed up forwards if the player is further along.
52343 CALL SpeedUpBackwards Call SpeedUpBackwards to speed up backwards.
RunHomingCreature_StoreX 52346 LD (IX+20),A Store the speed along x.
52349 LD A,(42865) Fetch the y position of the player.
52352 SUB (IX+2) Subtract the y position of the creature.
52355 LD A,(IX+21) Fetch the stored speed of the creature along y.
52358 JR NC,RunHomingCreature_ForwardsY Jump to RunHomingCreature_ForwardsY to speed up forwards if the player is further along.
52360 CALL SpeedUpBackwards Call SpeedUpBackwards to speed up backwards.
RunHomingCreature_StoreY 52363 LD (IX+21),A Store the speed along y.
52366 LD A,(42898) Fetch the z position of the body of the player.
52369 SUB (IX+3) Subtract the z position of the creature.
52372 LD A,(IX+22) Fetch the stored speed of the creature along z.
52375 JR NC,RunHomingCreature_ForwardsZ Jump to RunHomingCreature_ForwardsZ to speed up forwards if the player is higher.
52377 CALL SpeedUpBackwards Call SpeedUpBackwards to speed up backwards.
RunHomingCreature_StoreZ 52380 LD (IX+22),A Store the speed along z.
Round the stored speeds to whole numbers.
52383 LD B,3 Set a counter for the three speeds in B.
RunHomingCreature_RoundSpeeds 52385 INC IX Move IX on by one byte, so that the stored speed is at 19 and the speed to move at is at 8, for each speed in turn.
52387 LD A,(IX+19) Fetch the stored speed.
52390 ADD A,8 Add half of the sixteen that is to be divided by, which rounds the result.
52392 SRA A Divide it by sixteen, keeping the sign.
52394 SRA A
52396 SRA A
52398 SRA A
52400 LD (IX+8),A Store it as the speed to move at.
52403 DJNZ RunHomingCreature_RoundSpeeds Jump to RunHomingCreature_RoundSpeeds until the three speeds have been set.
52405 DEC IX Put the record pointer back.
52407 DEC IX
52409 DEC IX
52411 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 52413 CALL SpeedUpForwards Call SpeedUpForwards to speed up forwards.
52416 JR RunHomingCreature_StoreX Jump to RunHomingCreature_StoreX to store the speed.
RunHomingCreature_ForwardsY 52418 CALL SpeedUpForwards Call SpeedUpForwards to speed up forwards.
52421 JR RunHomingCreature_StoreY Jump to RunHomingCreature_StoreY to store the speed.
RunHomingCreature_ForwardsZ 52423 CALL SpeedUpForwards Call SpeedUpForwards to speed up forwards.
52426 JR RunHomingCreature_StoreZ Jump to RunHomingCreature_StoreZ to store the speed.
Cycle the picture of the creature.
RunHomingCreature_Animate 52428 INC (IX+16) Add one to the picture counter of the creature.
52431 LD A,(IX+16) Fetch it.
52434 AND %00000011 Keep the lowest two bits.
52436 LD B,A Put them in B.
52437 LD A,(IX+0) Fetch the type of the creature.
52440 AND %11111100 Keep the type without its lowest two bits.
52442 ADD A,B Add the picture number.
52443 LD (IX+0),A Set it as the type of the creature, which carries on with SetWipeAndDrawFlags.
Prev: 52273 Up: Map Next: 52446