コード例 #1
0
ファイル: StateMachine.cs プロジェクト: studentutu/coreutils
        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;
        }
コード例 #2
0
 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();
 }
コード例 #3
0
    public void SetState(IState state)
    {
        if (currentState == state)
        {
            return;
        }

        currentState?.OnExit();
        OnStateExited?.Invoke(currentState);
        currentState = state;
        currentState.OnEnter();

        OnStateEntered?.Invoke(currentState);
    }
コード例 #4
0
        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;
        }
コード例 #5
0
        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;
        }
コード例 #6
0
 private void StateMachineOnStateExited(IState obj)
 {
     OnStateExited?.Invoke(obj);
 }