Game Development Reference
In-Depth Information
Now, our jump value has an initial timer value—this will be important (as you will
see shortly). Now, go into
onStateChange
in
PlayerStateListener
. In the
jump
portion of the
switch
case, change the bottom part to look like the following just
after
playerHasLanded = false
:
PlayerStateController.stateDelayTimer[
(int)PlayerStateController.playerStates.jump] = 0f;
We use the fact that the timer is
0f
as part of our checks. If the timer is
0f
, then it is
not running, and so we do not allow jumping. Specifically, the timer is not running
while the player is in the middle of jumping or in the middle of falling, giving them
all the time they need to land and start the timer again.
Having said that, you will also need to change the
falling
case statement in the
same
switch
condition. Make sure that it is like the following code:
PlayerStateController.stateDelayTimer[ (int)PlayerStateController.
playerStates.jump] = 0f;
Only one more change to make in this method—we need to add the code that starts
the timer backup. In the
landing
check of this same
switch
condition, add the
following line of code:
PlayerStateController.stateDelayTimer[(int)PlayerStateController.
playerStates.jump]= Time.time + 0.1f;
That line will cause jumping to be allowed again 0.1 seconds after landing occurs.
Once
nextAllowedJumpTime
is equal to
0f
, we can jump again.
Almost there!
Now for the meat of the code that will control this new "conditional abort"
functionality—at the very bottom of
PlayerStateListener
, add the following
method:
//checkIfAbortOnStateCondition allows us to do additional state
//verification, to see if there is any reason this state should
// not be allowed to begin.
bool checkIfAbortOnStateCondition(PlayerStateController.playerStates
newState)
{
bool returnVal = false;
switch(newState)
{