public void ChangeGameState(GameStateEnum state) { if (currentGameState != null) { currentGameState.Exit(); } currentGameState = GetGameState(state); currentGameState.Enter(); }
/// <summary> /// 切换GameState /// </summary> /// <param name="force">false时, 如果当前状态为目标状态, 不执行切换, 直接返回当前状态</param> public T EnterState <T>(object userData = null, bool force = false) 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.PrepareExit(); } currentState.PrepareEnter(); if (oldState != null) { var oldStateType = oldState.StateType; oldState.Exit(); // 记录上次的状态 stateChangingContex.RecordLastState(oldStateType); } currentState.Enter(); if (oldState != null) { oldState.DoneExit(); oldState.Dispose(); GameObject.Destroy(oldState); } currentState.DoneEnter(); return(currentState as T); }
/// <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); }
/// <summary> /// シーンステートを変更する /// </summary> /// <param name="next">次のゲームシーンステート</param> private void ChangeState(GameStateBase next) { if (currentState != null) { currentState.Exit(); currentState.OnStateChanged = null; } // 次のステートがなかったらゲーム終了 if (next == null) { isGameFinished = true; currentState = null; return; } currentState = next; currentState.OnStateChanged = (state) => { ChangeState(state); }; currentState.Enter(); }