Prev: 6CA7 Up: Map Next: 6D38
6CB7: Handle Sugar
The handler for the sugar (IDs 08 and 09), which drifts about the screen. Byte 04 of the actor is the horizontal speed, and byte 05 is the vertical speed. Every 20 frames new random speeds are chosen (see SetRandomSpeeds); they are both either 02 or FE (-2). The sugar bounces off the sides and the top and bottom of the play area, and as it goes it also moves up and down in a wave, which is made from the frame counter in byte 07 of the actor.
While the sugar is still entering from the right of the screen (a horizontal co-ordinate of C4 or more) it can't hit Charlie or be hit.
Input
IX Actor
HandleSugar 6CB7 CALL StoreEntity Record the position of the sprite using StoreEntity.
6CBA LD HL,$5F1B Point HL at ActorsInPlay.
6CBD INC (HL) Increase the number of actors in play by one.
6CBE LD A,(IX+$01) Fetch the horizontal co-ordinate.
6CC1 CP $C4 Jump to HandleSugar_Move if the sugar is still entering from the right.
6CC3 JR NC,HandleSugar_Move
6CC5 CALL TestCharlieCollision Test whether the sugar has hit Charlie using TestCharlieCollision.
6CC8 DEC E Has Charlie been hit (is E now zero)?
6CC9 JP Z,LoseHat Jump to LoseHat if Charlie has been hit.
6CCC CALL TestFlourCollision Test whether a flour shot has hit the sugar using TestFlourCollision.
6CCF BIT 7,(IX+$00) Jump to HandleHitActor if the sugar has been hit.
6CD3 JP NZ,HandleHitActor
HandleSugar_Move 6CD6 INC (IX+$07) Increase the frame counter in byte 07 of the actor by one.
6CD9 LD (IX+$03),$44 Set the actor colour to 44 (green).
6CDD LD A,(IX+$07) Fetch the frame counter.
6CE0 AND %00011111 Keep only bits 0-4.
6CE2 JR Z,HandleSugar_NewSpeeds Jump to HandleSugar_NewSpeeds if the counter is a multiple of 20 to change speed.
6CE4 LD A,(IX+$01) Fetch the horizontal co-ordinate.
6CE7 ADD A,(IX+$04) Add the horizontal speed.
6CEA LD (IX+$01),A Update the horizontal co-ordinate.
6CED CP $C0 Jump to HandleSugar_RightEdge if the sugar has reached the right edge.
6CEF JR NC,HandleSugar_RightEdge
6CF1 CP $04 Jump to HandleSugar_LeftEdge if the sugar has reached the left edge.
6CF3 JR C,HandleSugar_LeftEdge
Make the offset for the wave from the frame counter. For each of the eight values of bits 0-2 of the counter it is 03 FC FD FE 00 01 02 and 03.
HandleSugar_Wave 6CF5 LD A,(IX+$07) Fetch the frame counter.
6CF8 AND %00000011 Keep only bits 0-1.
6CFA BIT 2,(IX+$07) Jump to HandleSugar_Vertical if bit 2 of the frame counter is set.
6CFE JR NZ,HandleSugar_Vertical
6D00 NEG Negate A.
6D02 XOR %00000011 Flip bits 0-1.
HandleSugar_Vertical 6D04 ADD A,(IX+$05) Add the vertical speed.
6D07 ADD A,(IX+$02) Add the vertical co-ordinate.
6D0A CP $20 Jump to HandleSugar_Top if the sugar has reached the top.
6D0C JR C,HandleSugar_Top
6D0E CP $A0 Jump to HandleSugar_Bottom if the sugar has reached the bottom.
6D10 JR NC,HandleSugar_Bottom
6D12 LD (IX+$02),A Update the vertical co-ordinate.
HandleSugar_Draw 6D15 CALL DisplayEntity Erase the sprite and draw it in its new position using DisplayEntity.
6D18 JP ColourSprite Jump to ColourSprite to colour the sprite.
The sugar has reached the right edge, so move it left.
HandleSugar_RightEdge 6D1B LD (IX+$04),$FE Set the horizontal speed to FE (-2, moving left).
6D1F JR HandleSugar_Wave Jump to HandleSugar_Wave.
The sugar has reached the left edge, so move it right.
HandleSugar_LeftEdge 6D21 LD (IX+$04),$02 Write 02 to the horizontal speed.
6D25 JR HandleSugar_Wave Jump to HandleSugar_Wave.
The sugar has reached the top, so move it down.
HandleSugar_Top 6D27 LD (IX+$05),$02 Write 02 to the vertical speed.
6D2B JR HandleSugar_Draw Jump to HandleSugar_Draw.
The sugar has reached the bottom, so move it up.
HandleSugar_Bottom 6D2D LD (IX+$05),$FE Write FE to the vertical speed.
6D31 JR HandleSugar_Draw Jump to HandleSugar_Draw.
Choose new speeds. A is the new vertical speed, so it is added to the vertical co-ordinate twice.
HandleSugar_NewSpeeds 6D33 CALL SetRandomSpeeds Set random horizontal and vertical speeds using SetRandomSpeeds.
6D36 JR HandleSugar_Vertical Jump to HandleSugar_Vertical.
Prev: 6CA7 Up: Map Next: 6D38