(๑•͈ᴗ•͈)❀送花给你

状态机

状态类继承的公共接口

    public interface IState
    {
        uint GetStateID();

        void OnEnter(StateMachine stateMachine, IState lastState, object param1, object param2);

        void OnLeave(IState nextState, object param1, object param2);

        void OnUpdate();

        void OnFixedUpdate();

        void OnLateUpdate();
    }

状态机

public class StateMachine
    {
        private Dictionary<uint, IState> mStateDic = null;

        private IState mCurrentState = null;

        public IState CurrentState
        {
            get
            {
                return mCurrentState;
            }
        }

        public uint CurrentID
        {
            get
            {
                return CurrentState == null ? 0 : mCurrentState.GetStateID();
            }
        }

        public StateMachine()
        {
            mStateDic = new Dictionary<uint, IState>();
            mCurrentState = null;
        }

        public bool RegisterState(IState state)
        {
            if(state==null)
            {
                return false;
            }
            if(mStateDic.ContainsKey(state.GetStateID()))
            {
                return false;
            }
            mStateDic.Add(state.GetStateID(), state);
            return true;
        }

        public bool RemoveState(uint stateId)
        {
            if(!mStateDic.ContainsKey(stateId))
            {
                return false;
            }
            if(mCurrentState!=null&&mCurrentState.GetStateID()==stateId)
            {
                return false;
            }
            mStateDic.Remove(stateId);
            return true;
        }

        public IState GetState(uint stateId)
        {
            IState state = null;
            mStateDic.TryGetValue(stateId, out state);
            return state;
        }

        public void StopState(object param1,object param2)
        {
            if(null==mCurrentState)
            {
                return;
            }
            mCurrentState.OnLeave(null, param1, param2);
            mCurrentState = null;
        }

        public BetweenSwitchState BetweenSwitchStateCallBack = null;

        /// <summary>
        /// 切换状态回调委托
        /// </summary>
        /// <param name="from">当前状态</param>
        /// <param name="to">要跳转的状态</param>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        public delegate void BetweenSwitchState(IState from, IState to, object param1, object param2);

        public bool SwitchState(uint newStateId,object param1,object param2)
        {
            if (mCurrentState != null && mCurrentState.GetStateID() == newStateId)
            {
                return false;
            }

            IState newState = GetState(newStateId);
            if(newState==null)
            {
                return false;
            }
            if(mCurrentState!=null)
            {
                mCurrentState.OnLeave(newState, param1, param2);
            }

            IState oldState = mCurrentState;
            mCurrentState = newState;

            if(BetweenSwitchStateCallBack!=null)
            {
                BetweenSwitchStateCallBack(oldState, mCurrentState, param1, param2);
            }

            newState.OnEnter(this, oldState, param1, param2);

            return true;
        }

        public void OnUpdate()
        {
            if(mCurrentState!=null)
            {
                mCurrentState.OnUpdate();
            }
        }

        public void OnFixedUpdate()
        {
            if(mCurrentState!=null)
            {
                mCurrentState.OnFixedUpdate();
            }
        }

        public void OnLateUpdate()
        {
            if(mCurrentState!=null)
            {
                mCurrentState.OnLateUpdate();
            }
        }
    }

 

posted @ 2020-03-02 21:07  胸前小红花  阅读(128)  评论(0)    收藏  举报