Advertisement
nbannister

StateMachine

Mar 7th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6.  
  7. public class StateMachine : MonoBehaviour {
  8.  
  9.     public List<State> statesList = new List<State> ();
  10.     public State StartingState;
  11.     protected State currentState;
  12.  
  13.     public void Start () {
  14.         SetState (StartingState);
  15.     }
  16.  
  17.     public State GetCurrentState { get { return currentState; } }
  18.  
  19.     /// <summary>
  20.     /// Switch the currentState to a specific State object
  21.     /// </summary>
  22.     /// <param name="state">
  23.     /// The state object to set as the currentState</param>
  24.     /// <returns>Whether the state was changed</returns>
  25.     public virtual bool SetState (State state) {
  26.         bool success = false;
  27.         if (state && state != currentState) {
  28.             State oldState = currentState;
  29.             currentState = state;
  30.             if (oldState)
  31.                 oldState.StateExit ();
  32.             currentState.StateEnter ();
  33.             success = true;
  34.         }
  35.         return success;
  36.     }
  37.  
  38.     public void setState (State state) {
  39.         SetState (state);
  40.     }
  41.  
  42.     /// <summary>
  43.     /// Switch the currentState to a State of a the given type.
  44.     /// </summary>
  45.     /// <typeparam name="StateType">
  46.     /// The type of state to use for the currentState</typeparam>
  47.     /// <returns>Whether the state was changed</returns>
  48.     public virtual bool SetState<StateType> () where StateType : State {
  49.         bool success = false;
  50.         //if the state can be found in the list of states
  51.         //already created, switch to the existing version
  52.         foreach (State state in statesList) {
  53.             if (state is StateType) {
  54.                 success = SetState (state);
  55.                 return success;
  56.             }
  57.         }
  58.         //if the state is not found in the list,
  59.         //see if it is on the gameobject.
  60.         State stateComponent = GetComponent<StateType> ();
  61.         if (stateComponent) {
  62.             stateComponent.Initialize (this);
  63.             statesList.Add (stateComponent);
  64.             success = SetState (stateComponent);
  65.             return success;
  66.         }
  67.         //if it is not on the gameobject,
  68.         //make a new instance
  69.         State newState = gameObject.AddComponent <StateType> ();
  70.         newState.Initialize (this);
  71.         statesList.Add (newState);
  72.         success = SetState (newState);
  73.  
  74.         return success;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement