PathEnemyStates

From Headbone Creative
Revision as of 20:41, 16 April 2013 by Tyson Joehler (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

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,

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,

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.

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.

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.

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.

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.

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