Prev: 28802 Up: Map Next: 29072
28891: Update Frog
Used by the routine at Handler_Frogs.
Updates a single frog's animation and sound state. Frogs sit idle on their platform, then randomly jump upward through a series of animation frames. A croaking sound effect plays during the jump, rising in pitch on the way up and falling on the way down.
Input
B Platform Y position
C Platform X position (left frog)
HL Pointer to the frog's state data (jump timer, sound counters)
IX Pointer to the frog's sprite data
Check the jump timer to determine if the frog is mid-jump or idle.
Update_Frog 28891 LD A,(HL) Fetch the jump timer value from *HL.
28892 OR A
28893 JR Z,Set_Frog_Idle Jump to Set_Frog_Idle if the timer is zero (frog is idle).
28895 DEC (HL) Decrement the jump timer by one.
28896 JR Z,Set_Frog_Idle Jump to Set_Frog_Idle if the timer has now reached zero (jump complete).
Frog is mid-jump; cycle through animation frames 0-3.
28898 LD A,(IX+2) Increment the animation frame at *IX+2 wrapping at 3.
28901 INC A
28902 AND %00000011
28904 LD (IX+2),A
28907 JR Handle_Frog_Sound Jump to Handle_Frog_Sound.
Frog is idle; set the idle frame.
Set_Frog_Idle 28909 LD (IX+2),6 Write 6 to *IX+2 (idle animation frame).
Handle the frog's croak sound effect. The sound has two phases: ascending pitch (counter rises from 1 to 24), then descending pitch (counter falls back to 0).
Handle_Frog_Sound 28913 INC HL Advance to the ascending sound counter.
28914 LD A,(HL)
28915 OR A Jump to Check_Random_Croak if the sound counter is zero (no sound playing).
28916 JR Z,Check_Random_Croak
28918 INC HL Advance to the descending flag.
28919 LD A,(HL) Jump to Frog_Sound_Descending if the descending flag is non-zero (sound is descending).
28920 OR A
28921 JR NZ,Frog_Sound_Descending
Ascending phase: increment the pitch and output to the speaker.
28923 DEC HL Move back to the ascending counter.
28924 LD A,(HL) Calculate the speaker output from the counter: add 3 rotate left, mask and set bit 0 for the border.
28925 ADD A,3
28927 RLCA
28928 AND %00011000
28930 OR %00000001
28932 OUT (254),A Output to the speaker port.
28934 LD A,(HL) Fetch the ascending counter and increment it.
28935 INC A
28936 CP 25 Jump to Store_Ascending_Counter if the ascending counter has not reached 25.
28938 JR NZ,Store_Ascending_Counter
Ascending phase complete; switch to descending.
28940 XOR A Write 0 to clear the ascending counter.
28941 LD (HL),A
28942 INC HL Set the descending flag to 255.
28943 DEC A
28944 LD (HL),A
28945 DEC HL Move the pointer back to the ascending counter.
28946 JR Position_Frog_Sprite Jump to Position_Frog_Sprite.
Store_Ascending_Counter 28948 LD (HL),A Write the updated ascending counter back.
28949 JR Position_Frog_Sprite Jump to Position_Frog_Sprite.
Descending phase: decrement the pitch and output to the speaker.
Frog_Sound_Descending 28951 DEC HL Move back to the ascending counter (used as pitch value).
28952 LD A,(HL) Calculate the speaker output from the counter: rotate left, mask and set bit 0 for the border.
28953 RLCA
28954 AND %00011000
28956 OR %00000001
28958 OUT (254),A Output to the speaker port.
28960 DEC (HL) Decrement the counter; jump to Position_Frog_Sprite if it is non-zero.
28961 JR NZ,Position_Frog_Sprite
28963 INC HL Counter has reached zero; clear the descending flag and move back.
28964 LD (HL),0
28966 DEC HL
28967 JR Position_Frog_Sprite Jump to Position_Frog_Sprite.
No sound playing; randomly trigger a new croak if the frog is idle.
Check_Random_Croak 28969 LD A,R Read the R register and mask; jump to Position_Frog_Sprite if it does not equal 15 (random chance to croak).
28971 AND %00111111
28973 CP 15
28975 JR NZ,Position_Frog_Sprite
28977 LD A,(IX+2) Jump to Position_Frog_Sprite if the animation frame at *IX+2 is not 6 (frog must be idle to start a croak).
28980 CP 6
28982 JR NZ,Position_Frog_Sprite
28984 INC HL Advance to the descending flag; check if it is non-zero and move back.
28985 LD A,(HL)
28986 OR A
28987 DEC HL
28988 JR NZ,Start_Descending_Croak Jump to Start_Descending_Croak if the descending flag is non-zero.
28990 LD (HL),1 Write 1 to start the ascending sound counter.
28992 JR Position_Frog_Sprite Jump to Position_Frog_Sprite.
Start_Descending_Croak 28994 LD (HL),25 Write 25 to the ascending counter (start from maximum pitch).
Calculate the frog's screen position based on its jump state and facing direction.
Position_Frog_Sprite 28996 LD E,(HL) Load the ascending counter into E and descending flag into D.
28997 INC HL
28998 LD D,(HL)
28999 LD A,E Jump to Frog_On_Platform if the ascending counter is zero (frog is on the platform).
29000 OR A
29001 JR Z,Frog_On_Platform
Frog is mid-jump; calculate the Y position from the jump height table.
29003 DEC A Decrement the counter to form a zero-based table index.
29004 PUSH DE Stash DE on the stack.
29005 LD DE,29167 Look up the jump height from the table at Table_FrogJumpHeight.
29008 LD L,A
29009 LD H,0
29011 ADD HL,DE
29012 POP DE Restore DE from the stack.
29013 LD A,B Subtract from the platform Y position in B.
29014 SUB (HL)
29015 LD (IX+1),A Write it to *IX+1.
29018 LD A,E Calculate the X position: ascending counter * 4 + platform X.
29019 ADD A,A
29020 ADD A,A
29021 ADD A,C
29022 LD (IX+0),A Write it to *IX+0.
Sprite ID Sprite
40 udg45083-56x4
29025 LD (IX+3),40 Set the sprite frame to 40 (frog jumping right).
29029 BIT 7,D Return if bit 7 of D is not set (facing right).
29031 RET Z
Sprite ID Sprite
41 udg45115-56x4
29032 INC (IX+3) Increment the sprite frame to 41 (frog jumping left).
29035 RET Return.
Frog is on the platform; set its resting position.
Frog_On_Platform 29036 LD (IX+1),B Set the Y position to the platform Y from B.
29039 BIT 7,D Jump to Frog_Facing_Left if bit 7 of D is set (frog faces left).
29041 JR NZ,Frog_Facing_Left
29043 LD (IX+0),C Set the X position to the platform X from C.
Sprite ID Sprite
42 udg45147-56x4
29046 LD (IX+3),42 Set the sprite frame to 42 (frog sitting right).
Random_Tongue_Flick 29050 LD A,R Read the R register and mask.
29052 AND %00011111
29054 OR A Return if the result is non-zero (random chance to flick tongue).
29055 RET NZ
Sprite ID Sprite Sprite ID Sprite
Right Left
43 udg45179-56x4 45 udg45243-56x4
29056 INC (IX+3) Increment the sprite frame by one (tongue-out variant).
29059 RET Return.
Left-facing frog on platform.
Frog_Facing_Left 29060 LD A,C Set the X position to platform X plus 104.
29061 ADD A,104
29063 LD (IX+0),A
Sprite ID Sprite
44 udg45211-56x4
29066 LD (IX+3),44 Set the sprite frame to 44 (frog sitting left).
29070 JR Random_Tongue_Flick Jump to Random_Tongue_Flick.
Prev: 28802 Up: Map Next: 29072