1. 程式人生 > >狀態機代碼

狀態機代碼

machine abstract pda chang using htm div console event

拷貝來自:http://www.cnblogs.com/plateFace/p/5018686.html
using System;
namespace StateMachine2.State
{

    public enum AnimationState
    {
        Walk = 1,
        Dead,
    }

    public abstract class State
    {
        abstract public int GetStateId{ get; }
        abstract public void Enter(StateEvent data);
        abstract public void Execute(StateEvent data);
        abstract public void Exit(StateEvent data);
    }


    //狀態運行時候的參數
    public class StateEvent
    {
        public string data;
    }

    //行走狀態
    public class State_Walk : State
    {
        public const int ID = 1;

        public override int GetStateId
        {
            get
            {
                return ID;
            }
        }

        public override void Enter(StateEvent data)
        {
            Console.WriteLine("角色行走-進入");
        }

        public override void Execute(StateEvent data)
        {
            Console.WriteLine("角色行走-執行中");
        }

        public override void Exit(StateEvent data)
        {
            Console.WriteLine("角色行走-退出");
        }
    }

    //死亡狀態
    public class State_Dead : State
    {
        public const int ID = 2;
        public override int GetStateId
        {
            get
            {
                return ID;
            }
        }

        public override void Enter(StateEvent data)
        {
            Console.WriteLine("角色死亡-進入");
        }

        public override void Execute(StateEvent data)
        {
            Console.WriteLine("角色死亡-執行中");
        }

        public override void Exit(StateEvent data)
        {
            Console.WriteLine("角色死亡-退出");
        }
    }

    public class StateMachine
    {
        private State currentState = null;
        private State previousState = null;
        private StateEvent dataEvent = null;
        private bool isStop;

        public State CurrentState
        {
            get
            {
                return currentState;
            }
        }
        public State PreviousState
        {
            get
            {
                return previousState;
            }
        }
        public bool IsStop
        {
            get
            {
                return isStop;
            }

            set
            {
                isStop = value;
            }
        }

        private State GetState(AnimationState animationState)
        {
            switch (animationState)
            {
                case AnimationState.Walk: return new State_Walk();
                case AnimationState.Dead: return new State_Dead();
            }
            return new State_Walk();
        }

        public void ChangeState(AnimationState animationState, StateEvent data,StateEvent previousData = null)
        {
            ChangeState(GetState(animationState),data, previousData);
        }

        public void ChangeState(State state,StateEvent data, StateEvent previousData = null)
        {
            //如果切換的狀態就是本狀態,就退出
            if (currentState != null && state.GetStateId == currentState.GetStateId)
                return;

            //退出上一個狀態
            if (previousState != null)
                previousState.Exit(previousData);

            //設置進狀態,進入新狀態
            currentState = state;
            dataEvent = data;
            currentState.Enter(data);
        }




        public void Update()
        {
            if (currentState == null)
            {
                Console.WriteLine("當前沒有狀態可以執行");
                return;
            }
            else if (IsStop)
            {
                Console.WriteLine("狀態機已經停止");
                return;
            }
            else
            {
                currentState.Execute(dataEvent);
            }
        }
    }

}

using StateMachine2.State;
using System;

namespace StateMachine2
{
    class Program
    {
        static void Main(string[] args)
        {
            StateMachine sm = new StateMachine();
            sm.ChangeState(AnimationState.Walk, new StateEvent() { data = "行走需要的參數" });
            sm.Update();       //執行行走狀態 
            sm.Update();       //執行行走狀態     
            
            sm.ChangeState(AnimationState.Dead, new StateEvent() { data = "死亡需要的參數" });
            sm.Update();
            sm.IsStop = true;   //停止狀態機

            sm.Update();        //再次執行狀態


            Console.WriteLine("結束程序");
            Console.ReadLine();
        }
    }
}

狀態機代碼