using UnityEngine; using System.Collections; using System.Collections.Generic; using Newtonsoft.Json.Utilities; namespace WK { /// /// 一般状态机 /// public class BasicStatemachine { public delegate void StateEvent(); public delegate bool StateFinishChecker(); public class State { public BasicStatemachine owner; public int stateId; public virtual void BeginState() { //reserve function } public virtual void EndState() { //reserve function } //时间单位是毫秒 public virtual bool UpdateState(int deltaTimeMS) { return true; } } private Dictionary stateDic = new Dictionary(); private int currentStateId = -1; private int stateIdDistributor = 0; private int recordLastStateId = -1; public int RegisterState(State newState) { if (newState != null) { if (stateIdDistributor + 1 > int.MaxValue) { LogHelper.LogError("状态机添加状态失败:一个状态机中添加了过多的状态!"); return -1; } stateIdDistributor++; if (!stateDic.ContainsKey(stateIdDistributor)) { stateDic.Add(stateIdDistributor, newState); newState.owner = this; newState.stateId = stateIdDistributor; return stateIdDistributor; } } LogHelper.LogError("状态机添加状态失败:无效的新状态或新状态已存在!"); return -1; } public bool RemoveState(State toBeRemoveState) { if (toBeRemoveState != null) return RemoveState(toBeRemoveState.stateId); LogHelper.LogError("状态机删除状态失败:无效的状态!"); return false; } public bool RemoveState(int stateId) { if (stateDic.ContainsKey(stateId)) { stateDic.Remove(stateId); return true; } LogHelper.LogError("状态机删除状态失败:该状态不存在!"); return false; } public bool Begin(int beginStateId) { if (!HasBegin()) { ForceGoToState(beginStateId, false); return true; } return false; } public bool Stop() { if (HasBegin()) { ForceGoToState(-1); return true; } return false; } public bool GoToState(int newStateId, bool skipBeginFunc = false, bool forceLoad = false) { if (HasBegin()) { return ForceGoToState(newStateId, skipBeginFunc, forceLoad); } return false; } private bool ForceGoToState(int newStateId) { return ForceGoToState(newStateId, false); } private bool ForceGoToState(int newStateId, bool skipBeginFunc, bool bForce = false) { if (currentStateId != newStateId || bForce) { OnStateEnd(currentStateId); currentStateId = newStateId; if (!skipBeginFunc) OnStateBegin(currentStateId); return true; } else { return false; } } /// /// /// /// 剔除的state public void RecordCurrentState(int filterState) { if (currentStateId != filterState) { recordLastStateId = currentStateId; } } public void ClearRecordState() { recordLastStateId = -1; } public void RevertToRecordState(int fallbackState, bool skipBeginFunc, bool bForce = false) { if (recordLastStateId >= 0 && stateDic.ContainsKey(recordLastStateId)) { GoToState(recordLastStateId, skipBeginFunc, bForce); } else if (fallbackState >= 0 && stateDic.ContainsKey(fallbackState)) { GoToState(fallbackState, skipBeginFunc, bForce); } } //时间单位是毫秒 public void OnUpdate(int mSecDeltaTime) { if (HasBegin()) { UpdateState(currentStateId, mSecDeltaTime); } } public bool HasBegin() { if (currentStateId != -1 && stateDic.ContainsKey(currentStateId)) return true; else return false; } public bool IsInState(int stateId) { if (HasBegin()) { return currentStateId == stateId; } return false; } public int GetCurrentStateId() { return currentStateId; } public State GetState(int stateId) { if (stateDic.ContainsKey(stateId)) { return stateDic[stateId]; } return null; } public void Clean() { stateDic.Clear(); currentStateId = -1; stateIdDistributor = 0; recordLastStateId = -1; } /// /// 重置当前状态(置为-1) /// public void ResetCurrentState() { currentStateId = -1; } private void OnStateBegin(int stateId) { if (HasBegin()) { State state = GetState(stateId); if (state != null) state.BeginState(); } } private void OnStateEnd(int stateId) { if (HasBegin()) { State state = GetState(stateId); if (state != null) state.EndState(); } } //时间单位是毫秒 private void UpdateState(int stateId, int mSecDeltaTime) { if (HasBegin()) { State state = GetState(stateId); if (state != null) state.UpdateState(mSecDeltaTime); } } } }