有限状态机FSM,可以高效管理各种状态以及状态之间的切换,用过的都说好,其中StateMachine源码如下:
/** * Autor: Created by 李清风 on 2020-01-03. * Desc: 有限状态机 */ using System; using System.Collections.Generic; using UnityEngine; namespace GameCore { public delegate void ForeachStateFun(State varState, object varObj); /// State base abstract class. abstract public class State { private string mName; /// State type_. protected StateMachine mMachine; public StateMachine pMachine { get { return mMachine; } set { mMachine = value; } } public State(string name) { mName = name; } //get stateName. public string pName { get { return mName; } } /// update every frame; virtual public void update() { return; } /// state begin . virtual public void begin() { return; } /// state end. virtual public void end() { return; } } /// State mashine base abstract class. public class StateMachine { protected Dictionary<string, State> mStateCollect; ///state collect; /// protected State mLastState; //last state protected State mCurrentState; ///current state; protected State mNextState; public StateMachine() { mStateCollect = new Dictionary<string, State>(); mCurrentState = null; mNextState = null; } virtual public void update() { if (mCurrentState != null) { mCurrentState.update(); } if (mNextState != null) { if (mCurrentState != null) { mCurrentState.end(); } mCurrentState = mNextState; mNextState = null; mCurrentState.begin(); } } /// Destories the state of the all. protected void destoryAllState() { mStateCollect.Clear(); mCurrentState = null; } /// Finds the state. public State getState(string name) { State temstate = null; if (mStateCollect.TryGetValue(name, out temstate)) { return temstate; } return null; } /// Registers the state. public bool registerState(State state) { if (state == null) { return false; } string name = state.pName; if (mStateCollect.ContainsKey(name)) { return false; } state.pMachine = this; mStateCollect.Add(name, state); return true; } /// Unregisters the state. public bool unregisterState(string name) { State temstate = null; if (mStateCollect.TryGetValue(name, out temstate) == false) { ///state not exists; return false; } temstate.pMachine = null; mStateCollect.Remove(name); return true; } /// goto other state. virtual public bool setNextState(string name) { State targetState = getState(name); if (targetState == null) { ///state not exists; return false; } mLastState = mCurrentState; if (targetState == mNextState) { return false; } mNextState = targetState; return true; } public void Foreach(ForeachStateFun varFan, object varObj) { if (varFan == null) { return; } Dictionary<string, State>.Enumerator it = mStateCollect.GetEnumerator(); for (int i = 0; i < mStateCollect.Count; ++i) { it.MoveNext(); KeyValuePair<string, State> kvp = it.Current; if (kvp.Value == null) { continue; } varFan(kvp.Value, varObj); } } /// <summary> ///返回最近的马上就要执行的的状态,下一个状态机 ,如果没有返回空 /// </summary> /// <returns>The state.</returns> public State GetNextState() { return mNextState; } public State getCurrentState() { return mCurrentState; } public string getCurrentStateType() { State s = getCurrentState(); if (s != null) { return s.pName; } return null; } } }