Example #1
0
        /// <summary>
        ///     Execute the supplied <paramref name="transition" />.
        /// </summary>
        /// <param name="transition">
        ///     Transition to be executed.
        /// </param>
        /// <returns>
        ///     <see langword="true" /> if a transition to a different state was performed
        ///     and the new state is not the exit state,
        ///     <see langword="false" /> if current state remains current.
        /// </returns>
        /// <remarks>
        ///     The method returns <see langword="false" /> when transitioning to the exit state
        ///     to prevent the state machine from looping directly over the enter state in the same
        ///     update tick to allow eventual parent state machines to move on to the next state.
        /// </remarks>
        private bool Execute([NotNull] ITransition transition)
        {
            IState targetState = transition.TargetState;

            if (targetState == _currentState)
            {
                return(false);
            }

            _currentState.Exit();

            transition.Execute(_currentState);
            Transitioning?.Invoke(_currentState, targetState);

            _currentState = targetState;
            _transitions.TryGetValue(_currentState, out _currentTransitions);

            _currentState.Enter();

            _completed = targetState == DefaultState.Exit;
            return(!_completed);
        }