Game Development Reference
In-Depth Information
if(horizontal < 0f)
{
if(onStateChange != null) onStateChange(PlayerStateController.
playerStates.left);
}
else
{
if(onStateChange != null) onStateChange(PlayerStateController.
playerStates.right);
}
}
else
{
if(onStateChange != null) onStateChange(PlayerStateController.
playerStates.idle);
}
}
}
Pro Tip
If you have a game with hundreds or even thousands of objects that
track events from one object, such as a player, then it would be advised
to use a singleton in those cases and have the other objects keep track of
the state of the player on their own. Otherwise, you can get a massive
load spike if you are loading thousands of events on a level load, which
would happen even if you are using a pooling system.
As you may have noticed, we listed out a number of states here. This makes up most
of the states we'll use in the game. Don't worry, we'll add some more as we go on,
which will show you how to add new states to the code.
This script also handles listening to the input keys. We're currently only listening to
the horizontal input. If it is negative, we are moving left, and if it is positive, we are
moving right. All of this is then managed by a simple Event and Delegate. This makes
sure all enemies and other objects in the world can be informed of state changes to
the player. All this does is open up numerous possibilities; we like possibilities.
 
Search WWH ::




Custom Search