Difference between revisions of "PathEnemyStates"

From Headbone Creative
Jump to: navigation, search
(Initial page, includes API information and categories)
 
(Constructor: Updated title)
 
Line 13: Line 13:
 
== abstract public class NPCMovementBaseState : FSMState<Transition, StateID> ==
 
== abstract public class NPCMovementBaseState : FSMState<Transition, StateID> ==
 
Because both of the states in this state machine move the NPC, I created a base class that handles the common behaviors of movement and rotation for both states.  
 
Because both of the states in this state machine move the NPC, I created a base class that handles the common behaviors of movement and rotation for both states.  
=== Constructor ===
+
=== public NPCMovementBaseState(PathEnemyFSM _stateMachine) ===
 
This class requires an instance of PathEnemyFSM to be passed in when the state is created. This is required so the state can access the variables that configure movement speed and style, which are stored on the PathEnemyFSM, since that class derives from [http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html MonoBehaviour] and can be attached to [http://docs.unity3d.com/Documentation/ScriptReference/GameObject.html GameObject]s, and this class can't be directly added to a GameObject.  
 
This class requires an instance of PathEnemyFSM to be passed in when the state is created. This is required so the state can access the variables that configure movement speed and style, which are stored on the PathEnemyFSM, since that class derives from [http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html MonoBehaviour] and can be attached to [http://docs.unity3d.com/Documentation/ScriptReference/GameObject.html GameObject]s, and this class can't be directly added to a GameObject.  
  
 
The PathEnemyFSM passed into the constructor is stored in the protected class member ''fsm''.
 
The PathEnemyFSM passed into the constructor is stored in the protected class member ''fsm''.
 +
 
=== void MoveToTarget(GameObject npc, GameObject target) ===
 
=== void MoveToTarget(GameObject npc, GameObject target) ===
 
This function is implemented here because both states will need to be able to move the npc towards a target object. It uses the ''fsm'' class member to determine how the movement should happen.  
 
This function is implemented here because both states will need to be able to move the npc towards a target object. It uses the ''fsm'' class member to determine how the movement should happen.  

Latest revision as of 20:41, 16 April 2013

This contains the states that implement the behaviors of the PathEnemyFSM. It requires the FSMSystem and FSMState classes, and was created to demonstrate how to use those classes in a real-world example.

Contents

[edit] enum Transition

This enum is required by the Finite State Machine framework. It defines all possible transitions in the state machine. This state machine is relatively simple, so it only requires two transitions:

  SawPlayer,
  LostPlayer,

[edit] enum StateID

All of the states in the state machine, in addition to having state classes that implement their behavior, must also be defined in this StateID enum. This is to simplify adding transitions, and allows the programmer to define states and their transitions in easily readable terms. This state machine is relatively simple, with only two states:

  ChasingPlayer,
  FollowingPath,

[edit] abstract public class NPCMovementBaseState : FSMState<Transition, StateID>

Because both of the states in this state machine move the NPC, I created a base class that handles the common behaviors of movement and rotation for both states.

[edit] public NPCMovementBaseState(PathEnemyFSM _stateMachine)

This class requires an instance of PathEnemyFSM to be passed in when the state is created. This is required so the state can access the variables that configure movement speed and style, which are stored on the PathEnemyFSM, since that class derives from MonoBehaviour and can be attached to GameObjects, and this class can't be directly added to a GameObject.

The PathEnemyFSM passed into the constructor is stored in the protected class member fsm.

[edit] void MoveToTarget(GameObject npc, GameObject target)

This function is implemented here because both states will need to be able to move the npc towards a target object. It uses the fsm class member to determine how the movement should happen.

[edit] void RotateTowardTarget (GameObject npc, GameObject target)

This function is implemented here because both states will need to be able to rotate the npc towards a target object. It uses the fsm class member to determine how the movement should happen.

[edit] public class ChasePlayerState : NPCMovementBaseState

This class implements the behavior of chasing the player.

  • In Reason(), it checks if the npc is out of leash range (if leashing is enabled). It also checks if the player is out of chase range. If either case is true, the state fires the LostPlayer transition, switching the state machine back to the FollowPathState.
  • In Act(), this class moves the npc towards the player using the function defined in NPCMovementBaseState.

[edit] public class FollowPathState : NPCMovementBaseState

This class implements the behavior of following the pre-defined path.

  • In Reason(), it checks if the player is within both the chaseStartDistance range, and the leash range (if leashing is enabled). If so, it fires the SawPlayer transition, switching to the ChasePlayerState.
  • In Act(), this class moves the npc towards the current waypoint on the path, using the function defined in NPCMovementBaseState.
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox