Game Development Reference
In-Depth Information
What we are going to do is add the ability for states to have a conditional check
and, if certain conditions are true, abort them from occurring. We are then going
to use that check to see if enough time has passed since the previous jump to allow
us to jump again. This will ensure that enough time has passed for the physics of
the previous landing to have finished resolving, without the player object being
somewhat stuck in the ground.
Let's first add the ability for us to know how many states there are in the
playerState enum. Open up PlayerStateController and change the bottom part
of the playerStates enum to look like the following code snippet:
kill,
resurrect,
_stateCount // Adding this to check the state count
The _stateCount variable will now display the actual number of states in the
enum. This works because the enum starts at 0. So, whatever the last entry is,
provided that the last entry is not an actual state itself, it will always read the correct
number of states.
Next, let's add a new line of code just below the same enum:
public static float[] stateDelayTimer = new
float[(int)playerStates._stateCount];
This array will be used to perform any timer checks on the states. Most of the states
will not have timers associated with them. However, this setup allows you to easily
add timers in the future if, for any reason, you have or want to do so.
Remember that at any time you can make your code more flexible for
the future; especially when it doesn't take any extra development time,
always make it more flexible. You will thank yourself five months from
now when you suddenly need to build a huge custom event into the
system in a weekend to meet a deadline.
Head on over to PlayerStateListener , and at the beginning, add the following
code to the existing Start method:
// Set up any specific starting values here
PlayerStateController.stateDelayTimer[
(int)PlayerStateController.playerStates.jump] = 1.0f;
 
Search WWH ::




Custom Search