Game Development Reference
In-Depth Information
Wait, did I collide with something?
Next, we need to set up some code so that this collider can tell the Player
object that a specific kind of collision happened. Create a new C# script called
PlayerColliderListener , attach it to the SceneryToggler object, and make
it look like the following code snippet:
using UnityEngine;
using System.Collections;
public class PlayerColliderListener : MonoBehaviour
{
public PlayerStateListener targetStateListener = null;
void OnTriggerEnter2D( Collider2D collidedObject )
{
switch(collidedObject.tag)
{
case "Platform":
// When the player lands on a platform, toggle the Landing state.
targetStateListener.onStateChange(PlayerStateController.
playerStates.landing);
break;
}
}
}
Assign the Player object to the SceneryToggler object's Target State Listener slot.
With this, there is one issue: the state system could get into a flow where a state
transition occurs, which takes the current state away from the jump even though
the player is still jumping. We need to make sure the jump state is still active while
the player is jumping, at least for this game. Therefore, we need to know if the player
has landed or not. For that, we'll just use a simple Boolean. This will serve as our
check as to whether we have or have not landed. It defaults to true because we can
only jump if we have already landed.
Go back to the jump case in the method onStateChange within
PlayerStateListener and wrap it in an if check with if(playerHasLanded) .
Finally, add a case for landing in onStateCycle , onStateChange ,
checkForValidStatePair , and checkIfAbortOnStateCondition . Make each
of those methods look like the following code snippet:
OnStateCycle:
case PlayerStateController.playerStates.landing:
 
Search WWH ::




Custom Search