/// <summary> /// Transition to supplied <paramref name="targetState" />. /// </summary> /// <param name="targetState"> /// Target state to transition the state machine to. /// </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 TransitionTo([NotNull] IState targetState) { if (targetState == _currentState) { return(false); } _currentState.Exit(); _currentState = targetState; _currentState.Enter(); _currentTransitions = _transitions.TryGetValue(_currentState, out Transitions currentTransitions) ? currentTransitions : ExitTransitions.Instance; if (targetState != DefaultState.Exit) { return(true); } _completed = true; return(false); }
/// <summary> /// Initializes a new instance of the <see cref="StateMachine"/> class. /// </summary> public StateMachine() { _anyTransitions = GetTransitions(DefaultState.Any); _currentTransitions = GetTransitions(DefaultState.Enter); AddTransition(DefaultState.Exit, DefaultState.Enter); }