Example #1
0
    /// <summary>
    /// 清理掉所有如栈的状态
    /// </summary>
    public void ClearPushedStates()
    {
        while (stateStack.Count > 0)
        {
            GameStateBase state = stateStack.Pop();
            state.PrepareExit();

            var oldStateType = state.StateType;
            state.Exit();
            // 记录上次的状态
            stateChangingContex.RecordLastState(oldStateType);

            state.DonePushStack();
            state.Dispose();
            GameObject.Destroy(state);
        }
    }
Example #2
0
    /// <summary>
    /// 将oldState压栈但不销毁,新state作为当前的currentState]
    /// oldState 执行 preparePushStack等系列函数, currentState执行 prepareEnter系列函数
    /// </summary>
    public T PushStackState <T>(object userData, bool force) where T : GameStateBase
    {
        if (force == false)
        {
            if (currentState != null && typeof(T) == currentState.GetType())
            {
                return(currentState as T);
            }
        }

        GameStateBase oldState = currentState;

        currentState = rootGo.AddComponent <T>();
        currentState.MachineManager = this;
        currentState.Create(userData);

        if (oldState != null)
        {
            oldState.PreparePushStack();
        }
        currentState.PrepareEnter();

        if (oldState != null)
        {
            oldState.PushStack();
            stateStack.Push(oldState);
        }
        currentState.Enter();

        if (oldState != null)
        {
            oldState.DonePushStack();
        }
        currentState.DoneEnter();

        return(currentState as T);
    }