Game Development Reference
In-Depth Information
Now, we need a script that listens to when the state changes and knows what to do
when this happens. Create another script called PlayerStateListener and make
that code look like the following code. The code is a rather large bit of code, and only
part of it is displayed here. Check out the entire code in the supplied code examples!
// Every cycle of the engine, process the current state.
void onStateCycle()
{
switch(currentState)
{
case PlayerStateController.playerStates.idle:
break;
case PlayerStateController.playerStates.left:
transform.Translate(newVector3((playerWalkSpeed * -1.0f) * Time.
deltaTime, 0.0f, 0.0f));
break;
case PlayerStateController.playerStates.right:
transform.Translate(newVector3(playerWalkSpeed * Time.deltaTime, 0.0f,
0.0f));
break;
}
}
// onStateChange is called when we make a change to the player's state
from anywhere within the game's code.
public void onStateChange(PlayerStateController.playerStatesnewState)
{
// If the current state and the new state are the same, abort - no
need to change to the state we're already in.
if(newState == currentState)
return;
// Check if the current state is allowed to transition into // this
state. If it's not, abort.
if(!checkForValidStatePair(newState))
return;
// Having reached here, we now know that this state change is //
allowed. So let's perform the necessary actions depending // on what
the new state is.
 
Search WWH ::




Custom Search