Beispiel #1
0
        /// <summary>
        ///     Starts this instance.
        /// </summary>
        public void Start( )
        {
            try
            {
                if (_isStarted)
                {
                    return;
                }

                _isStarted = true;

                if (_stateMap.Transitions.Count > 0)
                {
                    ActiveStates = _stateMap.GetInitialStates( );
                }

                if (StartEvent != null)
                {
                    StartEvent.Execute(StateContainer, _stateMap);
                }

                foreach (IState state in ActiveStates)
                {
                    if (state.Enter != null)
                    {
                        state.Enter.CallOnEach(x => x.Execute(StateContainer, _stateMap));
                    }
                }
            }
            catch (Exception ex)
            {
                throw new StateMachineException(ex.Message);
            }
        }
Beispiel #2
0
        /// <summary>
        ///     Starts the specified initial state.
        /// </summary>
        /// <param name="initialState"> The initial state. </param>
        public void Start(IState initialState)
        {
            try
            {
                if (_isStarted)
                {
                    return;
                }

                _isStarted = true;

                if (!_stateMap.States.Contains(initialState))
                {
                    throw new StateMachineException("The specified start state does not exist in the state map");
                }

                ActiveStates.Add(initialState);

                if (StartEvent != null)
                {
                    StartEvent.Execute(StateContainer, _stateMap);
                }

                foreach (IState state in ActiveStates)
                {
                    if (state.Enter != null)
                    {
                        state.Enter.CallOnEach(x => x.Execute(StateContainer, _stateMap));
                    }
                }
            }
            catch (Exception ex)
            {
                throw new StateMachineException(ex.Message);
            }
        }