private void GoToState(string stateName, StateChartState from) { StateChartState target; if (stateMap.TryGetValue(stateName, out target)) { StateChartState ptr = target; List <StateChartState> stack = new List <StateChartState>(); if (from == target) { return; } // entering a descendent of currently active state if (from.isParentOf(target)) { while (ptr != from) { stack.Add(ptr); ptr = ptr.parent; } ptr = stack[0]; stack.RemoveAt(0); from.ExitChildren(); ptr.Enter(stack); } else { // root state is always active, no need for a null check while (!ptr.isActive) { stack.Add(ptr); ptr = ptr.parent; } ptr.ExitChildren(); stack.Reverse(); ptr = stack[0]; stack.RemoveAt(0); ptr.Enter(stack); } return; } Debug.Log("Unable to go to state: " + stateName); }
public StateChart(Action <StateChartBuilder> definition) { stateMap = new Dictionary <string, StateChartState>(); stateStack = new Stack <StateChartState>(); rootState = new StateChartState("Root", null); activeEventQueue = new Queue <StateChartEvent>(); inactiveEventQueue = new Queue <StateChartEvent>(); stateMap.Add(rootState.id, rootState); stateStack.Push(rootState); definition(new StateChartBuilder(this)); isDefined = true; stateStack.Pop(); rootState.Enter(null); stateStack = null; }
public void Enter(List <StateChartState> enterPath) { if (isActive) { return; } isActive = true; if (parent != null) { parent.activeSubstate = this; } if (!isInitialized) { isInitialized = true; if (initFn != null) { initFn(); initFn = null; } } if (enterFn != null) { enterFn(); } if (enterPath != null && enterPath.Count > 0) { activeSubstate = enterPath[enterPath.Count - 1]; enterPath.RemoveAt(enterPath.Count - 1); activeSubstate.Enter(enterPath); } else if (subStates.Count > 0) { activeSubstate = subStates[0]; activeSubstate.Enter(enterPath); } else { activeSubstate = null; } }