-
Notifications
You must be signed in to change notification settings - Fork 4
Climbing the ladders (Part 9)
There are ladders in the background image. Player should be able to use them if they are there, right?
There's no plan of attack described in this part, because how hard could it be, to add simple ladder logic to this game? (hindsight: about 150 lines of code)
(here is a good spot to open the SpecBong.asm
and try to match the source with this text)
(you can also check the total difference between "Part 9" and "Part 8")
The structure S_LADDER_DATA
to keep ladder data around is defined to contain x
and y
coordinates of the top of the ladder (adjusted further by [-8, -16] to be actually directly comparable to player sprite [x, y] without further adjusting, adjusting the ladder position to match the player when the player stands visually in the middle of it right at its top). And the t
value which is height of the ladder in pixels (t as "tall").
We will use word variable Player1LadderData
to store the top y-coordinate of ladder and its height, when player is already hanging at some ladder, otherwise this will be zeroed.
Data of each ladder are defined under label LaddersData
(there are six ladders).
The sprite patterns drawn to display climbing-of-ladder and final climb at top of it are defined in the array LadderPatternData
which will map the y-delta of player toward the top of the ladder to particular element of the array, thus having the same pattern several times to time the animation of player as he moves up or down on it.
There will be new extension of the code handling player controls, this time when the player is not falling or jumping, there is one more test to see if he's hanging on the ladder already, diverting to completely new logic in such case. If the player is not hanging on the ladder, there are minor adjustment of the old code to reset the pattern to normal for more cases and preventing jump in direction when also up/down is pressed (jumping only straight up is possible then, to make the controls a bit more difficult).
In the part of code handling the regular running around, after the jump button test the directions up/down are checked too (before left/right), and if pressed, the "flag" byte of floor under right foot of player is checked, if it signals nearby ladder presence. If there is no ladder near, that's the end of player controls update, making him stand still in place.
With the bit flag signalling ladder nearby the whole array of ladders is processed and each element is check against current player position. If the player y coordinate is exactly the top or bottom of the ladder and the correct control (down for top, up for bottom) is pressed, there is final check testing the x coordinate alignment. If the player x coordinate is withing -2..+2 range of ladder position, the "hanging on ladder" state is set up (otherwise the routine just exits leaving the player standing as if there was no ladder signalled by floor).
Grabbing the ladder will align the x coordinate of player perfectly with the ladder and set the Player1LadderData
with current ladder data and follow into the code which is handling the "hanging on the ladder" state every frame.
While hanging on the ladder, every second frame (creating perception of 0.5px per frame speed of climbing) the y coordinate of player is adjusted by +-1 depending on the controls pressed. The new value is clamped to be in ladderY..ladderY+height range and set back to player sprite. The special case of being at the very top or bottom of ladder is detected while clamping the value and jumps further down the code to .atTopOrBottom
. But while in the "middle" of the ladder, the position of player is turned into array index into LadderPatternData
and pattern for particular position on ladder is fetched and set.
The code at .atTopOrBottom
does set "standing" pattern of player and checks also left/right control inputs, standing still if those are not pressed, otherwise clearing the "hanging on ladder" Player1LadderData
variable to make the player leave the ladder and falling through into the regular "running on platform" part of code.
And that's all, the player can now run around the stage, jump and climb ladders up and down.