追蹤
兔子也會的遊戲設計
關於部落格
一個推廣XNA與OGRE的園地,當然還有其他關於遊戲設計的主題,歡迎有興趣的同好交流指教
  • 104824

    累積人氣

  • 0

    今日人氣

    0

    追蹤人氣

狀態模式

//狀態模式
using System;

namespace State
{
    ///
    /// Client 的摘要描述。
    /// 主控台測試部分
    ///
    class Client
    {
        ///
        /// 應用程式的主進入點。
        ///
        [STAThread]
        static void Main(string[] args)
        {
            //建立主遊戲
            MainGame main = new MainGame();
            while (true)
            {
                //繪圖
                main.FrameRender();
                //運算
                main.FrameMove();
            }
        }
    }

    //主狀態介面
    interface MajorState
    {
        //每個狀態都有有兩個動作
        //負責運算
        void FrameMove();
        //負責繪圖
        void FrameRender();
    }

    //開始實做狀態介面

    //建立狀態
    class MS_Create : MajorState
    {
        //每個都有一個mainGame物件
        MainGame mainGame;
        public MS_Create(MainGame mainGame) { this.mainGame = mainGame; }
        public void FrameMove()
        {
            //改變狀態 取得mainGame的中的下個狀態然後再設給mainGame
            mainGame.SetCurrentState(mainGame.GetMainMenuState());
        }
        public void FrameRender()
        {
            Console.WriteLine("程式被建立n");
        }
    };

    //下面如法泡製
    //主選單狀態
    class MS_MainMenu : MajorState
    {
        MainGame mainGame;
        public MS_MainMenu(MainGame mainGame) { this.mainGame = mainGame; }
        public void FrameMove()
        {
            Console.ReadLine();
            mainGame.SetCurrentState(mainGame.GetMainGameState());
        }

        public void FrameRender()
        {
            Console.WriteLine("我在主選單n");
        }
    };
    //主遊戲狀態
    class MS_MainGame : MajorState
    {
        MainGame mainGame;
        public MS_MainGame(MainGame mainGame) { this.mainGame = mainGame; }
        public void FrameMove()
        {
            Console.ReadLine();
            mainGame.SetCurrentState(mainGame.GetDestoryState());
        }

        public void FrameRender()
        {
            Console.WriteLine("我在主遊戲n");
        }
    };

    //摧毀狀態
    class MS_Destory : MajorState
    {
        MainGame mainGame;
        public MS_Destory(MainGame mainGame) { this.mainGame = mainGame; }

        public void FrameMove()
        {
            Console.WriteLine("Press Enter To Continue....n");
            Console.ReadLine();
            Environment.Exit(0);

        }
        public void FrameRender()
        {
            Console.WriteLine("摧毀遊戲n");
            Console.ReadLine();

        }
    };

    //主遊戲的類別
    class MainGame
    {
        //先宣告所有狀態類別
        private MajorState ms_Create;
        private MajorState ms_MainMenu;
        private MajorState ms_MainGame;
        private MajorState ms_Destory;
        //記錄目前的狀態
        private MajorState currentState;
        public MainGame()
        {
            //把自己跟狀態綁定
            ms_Create = new MS_Create(this);
            ms_MainMenu = new MS_MainMenu(this);
            ms_MainGame = new MS_MainGame(this);
            ms_Destory = new MS_Destory(this);
            //一開始預設就是先建立狀態
            currentState = ms_Create;
        }
        //一旦呼叫FrameMove就執行當時狀態的FrameMove
        public void FrameMove()
        {
            currentState.FrameMove();
        }
        //一旦呼叫FrameRender就執行當時狀態的FrameRender
        public void FrameRender()
        {
            currentState.FrameRender();
        }
        //取得各個狀態
        public MajorState GetCreateState() { return ms_Create; }
        public MajorState GetMainMenuState() { return ms_MainMenu; }
        public MajorState GetMainGameState() { return ms_MainGame; }
        public MajorState GetDestoryState() { return ms_Destory; }
        //設定狀態
        public void SetCurrentState(MajorState state) { currentState = state; }

    };
}

相簿設定
標籤設定
相簿狀態