public void Exit() { if (CurrentState == null) { return; } if (!Application.isPlaying) { foreach (Transform item in transform) { item.gameObject.SetActive(false); } return; } Log($"(-) {name} EXITED state: {CurrentState.name}"); int currentIndex = CurrentState.transform.GetSiblingIndex(); //no longer at first: if (currentIndex == 0) { OnFirstStateExited?.Invoke(); } //no longer at last: if (currentIndex == transform.childCount - 1) { OnLastStateExited?.Invoke(); } CurrentState.SetActive(false); OnStateExited?.Invoke(CurrentState); m_CurrentState = null; }
private void ChangeState(GameState newState) { if (CurrentState != null) { PreviousState = CurrentState; CurrentState.Exit(newState); OnStateExited?.Invoke(PreviousState); } CurrentState = newState; CurrentState.OnGameStateEnter += OnGameStateEntered; CurrentState.OnGameStateEnterFailed += OnFailToEnterGameState; CurrentState.Enter(); }
public void SetState(IState state) { if (currentState == state) { return; } currentState?.OnExit(); OnStateExited?.Invoke(currentState); currentState = state; currentState.OnEnter(); OnStateEntered?.Invoke(currentState); }
public IEnumerator GoTo(IState nextState, bool transitionOutPreviousState = true) { if (Transitioning) { yield break; } if (ReferenceEquals(nextState, Current)) { yield break; } Transitioning = true; var previous = Current; // Transition out of the current state if (previous != null && transitionOutPreviousState) { previous.OnTransitionOut?.Invoke(nextState); OnStateTransitionOut?.Invoke(previous, nextState); yield return(previous.TransitionOut(nextState)); previous.OnExited?.Invoke(nextState); previous.Exited(nextState); OnStateExited?.Invoke(previous, nextState); previous.Sleep(nextState); } Current = nextState; // Transition in to the next state nextState.Wake(); nextState.OnTransitionIn?.Invoke(previous); OnStateTransitionIn?.Invoke(nextState, previous); yield return(nextState.TransitionIn(previous)); nextState.OnEntered?.Invoke(previous); nextState.Entered(previous); OnStateEntered?.Invoke(nextState, previous); StateEntered(nextState); Transitioning = false; }
public async UniTask TransitionToState(T state) { if (CurrentTransition != null) { DebugLog("Warning: Statemachine already transitioning from " + CurrentTransition.From + " to " + CurrentTransition.To); return; } if (!firstTransition && state.Equals(CurrentState)) // depending on the type of T, currentState may already be set when we begin { DebugLog("Warning: Statemachine is already in state " + state); return; } CurrentTransition = new Transition <T>(CurrentState, state); // exiting if (!firstTransition) { CurrentTransition.Phase = TransitionPhase.EXITING_FROM; OnStateExiting?.Invoke(CurrentTransition); await states[CurrentTransition.From].OnExit(CurrentTransition.To); CurrentTransition.Phase = TransitionPhase.EXITED_FROM; OnStateExited?.Invoke(CurrentTransition); } // entering CurrentState = CurrentTransition.To; CurrentTransition.Phase = TransitionPhase.ENTERING_TO; OnStateEntering?.Invoke(CurrentTransition); await states[CurrentTransition.To].OnEnter(CurrentTransition.From); CurrentTransition.Phase = TransitionPhase.ENTERED_TO; Transition <T> prevTransition = CurrentTransition; CurrentTransition = null; OnStateEntered?.Invoke(prevTransition); firstTransition = false; }
private void StateMachineOnStateExited(IState obj) { OnStateExited?.Invoke(obj); }