Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class StateTester : MonoBehaviour {
- protected virtual void Start(){
- InitStateSystem();
- }
- protected virtual void Update(){
- _activeState.Update();
- }
- #region State machine
- Dictionary<string, State> states; //we use this to link states to strings
- public string activeState {
- get; //other scripts can read our state string
- private set; //but not change it (which is what SetState() is for)
- //you could change this to set { SetState(value); } but I prefer not to
- }
- State _activeState;
- public const string STATE_A = "State A"; //we use constants for this largely to simply code
- public const string STATE_B = "State B"; //if we use the constants to set/change states, we don't have to worry about typos
- //this fills our dictionary, linking the strings to the local state class instances
- protected virtual void InitStateSystem(){
- //populate the dictionary with all our states
- states = new Dictionary<string, State>();
- states.Add(STATE_A, stateA);
- states.Add(STATE_B, stateB);
- //initialise our states
- stateA.Init(this);
- stateB.Init(this);
- //set initial state
- _activeState = stateA;
- activeState = STATE_A;
- }
- public virtual void SetState(string nextState){
- if(activeState == nextState) return; //abort if the state we're changing to is the same one we're running anyway
- State s;
- //try to find the string nextState in the dictionary, which also sets s if we find one
- if(!states.TryGetValue(nextState, out s)){
- //we could not find a state with that string in the dictionary, so early out without changing anything
- Debug.LogError("Unsupported state " + nextState + "!");
- return;
- }
- s.StartState();
- _activeState = s;
- activeState = nextState;
- }
- //this is the abstracted form of a state class
- public abstract class State {
- //common state parameters here
- protected StateTester owner; //this is what the state is controlling
- //add links to properties in the owner we want to easily read / edit, for example our Transform
- protected Transform transform { get { return owner.transform; } }
- //essentially the State's 'Start' function, but you have to call this manually
- public virtual void Init(StateTester a){
- owner = a;
- }
- //or whatever function every state is expected to have and be used for
- public abstract void Update();
- public abstract void StartState();
- }
- #endregion
- #region State A
- //this is our local instance of StateA, which will show up in the inspector
- public StateA stateA;
- [System.Serializable] //this tells Unity to save instances of this class
- public class StateA : State {
- //properties unique to State A here
- public float spinSpeed = 3;
- public override void StartState () {
- Debug.Log("I'm state A! See me spin!");
- }
- public override void Update () {
- transform.Rotate(0, Time.deltaTime * spinSpeed, 0);
- }
- }
- #endregion
- #region State B
- //this is our local instance of StateB, which will show up in the inspector
- public StateB stateB;
- [System.Serializable] //this tells Unity to save instances of this class
- public class StateB : State {
- //properties unique to State B here
- public float flySpeed = 4;
- public override void StartState () {
- Debug.Log("I'm state B! TO THE MOOOOON!");
- }
- public override void Update () {
- transform.Translate(0, Time.deltaTime * flySpeed, 0);
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement