Finite-state machines can be improved in terms of having different layers or hierarchies. The principles are the same, but states are able to have their own internal finite-state machine, making them more flexible and scalable.
This recipe is based on top of the previous recipe, so it is important that we grasp and understand how the finite-state machine recipe works.
We will create a state that is capable of holding internal states, in order to develop multi-level hierarchical state machines:
StateHighLevel
class deriving from State
:using UnityEngine; using System.Collections; using System.Collections.Generic; public class StateHighLevel : State { }
public List<State> states; public State stateInitial; protected State stateCurrent;
public override void OnEnable() { if (stateCurrent == null) stateCurrent = stateInitial; stateCurrent.enabled = true; }
public override void OnDisable() { base.OnDisable(); stateCurrent.enabled = false; foreach (State s in states) { s.enabled = false; } }