Example #1
0
        internal Transition UpdateTransitions(StateEventArgs eventArgs)
        {
            Transition firingTransition = null;

            // Update sub-state transitions first.
            if (_parallelSubStates != null)
            {
                foreach (var stateCollection in _parallelSubStates)
                {
                    firingTransition = stateCollection.UpdateTransitions(eventArgs);
                    if (firingTransition != null && !stateCollection.ContainsRecursive(firingTransition.TargetState))
                    {
                        // The transition target is outside the sub-state collection.
                        // More parallel transition updates are only allowed for internal transitions.
                        break;
                    }

                    // There was no transition or the transition was an internal transition.
                    // We can check the next parallel state set.
                }
            }

            // Abort if a transition was performed.
            if (firingTransition != null)
            {
                return(firingTransition);
            }

            // Check transitions of this state.
            foreach (var transition in Transitions)
            {
                if (transition.TargetState == null)
                {
                    throw new InvalidOperationException("TargetState of transition must not be null.");
                }
                //if (transition.TargetState.StateMachine != StateMachine)
                //  throw new InvalidOperationException("TargetState of transition must not belong to different state machine.");

                // Check transition.
                bool fired = transition.Update(eventArgs.DeltaTime);
                if (fired)
                {
                    // Get state collection that contains this state and the target state.
                    var stateCollection = StateCollection.GetCollection(this, transition.TargetState);

                    // Exit states.
                    stateCollection.ExitState(transition, eventArgs);

                    // Execute transition action.
                    transition.OnAction(eventArgs);

                    // Enter states.
                    stateCollection.EnterState(transition, eventArgs);

                    // Do not update other transitions after first transition has fired.
                    return(transition);
                }
            }

            return(null);
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StateMachine"/> class.
 /// </summary>
 public StateMachine()
 {
     States = new StateCollection();
     //States.StateMachine = this;
 }