Ejemplo n.º 1
0
        private void State(string stateName, Action defintion = null)
        {
            if (stateMap.ContainsKey(stateName))
            {
                Debug.Log("Duplicate state: " + stateName);
                return;
            }

            StateChartState parentState = stateStack.Peek();
            StateChartState state       = new StateChartState(stateName, parentState);

            parentState.subStates.Add(state);
            stateMap.Add(stateName, state);

            if (defintion != null)
            {
                stateStack.Push(state);
                defintion();
                stateStack.Pop();
                if (!hasUpdates && state.updateFn != null)
                {
                    hasUpdates = true;
                }
            }
        }
Ejemplo n.º 2
0
        public void Tick()
        {
            Queue <StateChartEvent> tmp = inactiveEventQueue;

            inactiveEventQueue = activeEventQueue;
            activeEventQueue   = tmp;

            while (inactiveEventQueue.Count > 0)
            {
                StateChartEvent      evt        = inactiveEventQueue.Dequeue();
                StateChartTransition transition = rootState.HandleEvent(evt.GetType(), evt);

                if (transition != null)
                {
                    GoToState(transition.targetState, transition.from);
                }
            }

            StateChartState ptr = rootState;

            while (ptr != null)
            {
                if (ptr.updateFn != null)
                {
                    ptr.updateFn();
                }

                ptr = ptr.activeSubstate;
            }
        }
Ejemplo n.º 3
0
 public void ExitChildren()
 {
     if (activeSubstate != null)
     {
         activeSubstate.Exit();
         activeSubstate = null;
     }
 }
Ejemplo n.º 4
0
 public StateChartState(string stateName, StateChartState parent)
 {
     id            = stateName;
     isActive      = false;
     isInitialized = false;
     subStates     = new List <StateChartState>();
     events        = new List <StateEventHandler>();
     transitions   = new List <StateTransitionHandler>();
     this.parent   = parent;
 }
Ejemplo n.º 5
0
        public string GetCurrentStateName()
        {
            StateChartState ptr = rootState;
            string          id  = String.Empty;

            while (ptr != null)
            {
                id  = ptr.id;
                ptr = ptr.activeSubstate;
            }

            return(id);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
 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;
 }
Ejemplo n.º 8
0
            public bool isParentOf(StateChartState other)
            {
                StateChartState ptr = other.parent;

                while (ptr != null)
                {
                    if (ptr == this)
                    {
                        return(true);
                    }
                    ptr = ptr.parent;
                }

                return(false);
            }
Ejemplo n.º 9
0
            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;
                }
            }
Ejemplo n.º 10
0
            public void Exit()
            {
                if (!isActive)
                {
                    return;
                }
                isActive = false;

                if (exitFn != null)
                {
                    exitFn();
                }

                if (activeSubstate != null)
                {
                    activeSubstate.Exit();
                    activeSubstate = null;
                }
            }
Ejemplo n.º 11
0
        public string GetStatePath()
        {
            stringBuilder.Length = 0;
            StateChartState ptr = rootState;

            while (ptr != null)
            {
                ptr = ptr.activeSubstate;

                if (ptr != null)
                {
                    stringBuilder.Append(ptr.id);

                    if (ptr.activeSubstate != null)
                    {
                        stringBuilder.Append(" -> ");
                    }
                }
            }
            return(stringBuilder.ToString());
        }
Ejemplo n.º 12
0
 public StateChartTransition(StateChartState from, string targetState)
 {
     this.from        = from;
     this.targetState = targetState;
 }