diff options
author | chai <215380520@qq.com> | 2023-05-18 19:20:17 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-05-18 19:20:17 +0800 |
commit | 5bb5da4caebfdc8627e331b4eb5c53457316ec44 (patch) | |
tree | a8dd5d9eea5c563f68f30da397cb890978cbd10a /WorldlineKeepers/Assets/Scripts/Tools/Statemachine/BasicStatemachine.cs | |
parent | c66fe8c43a368eb4b23aa1e3104019aabf9e2274 (diff) |
*misc
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Tools/Statemachine/BasicStatemachine.cs')
-rw-r--r-- | WorldlineKeepers/Assets/Scripts/Tools/Statemachine/BasicStatemachine.cs | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/BasicStatemachine.cs b/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/BasicStatemachine.cs new file mode 100644 index 0000000..15ab82c --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/BasicStatemachine.cs @@ -0,0 +1,251 @@ +using UnityEngine; +using System.Collections; +using System.Collections.Generic; +using Newtonsoft.Json.Utilities; + +namespace WK +{ + /// <summary> + /// 一般状态机 + /// </summary> + 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<int/*state id*/, State> stateDic = new Dictionary<int/*state id*/, State>(); + 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; + } + } + + /// <summary> + /// + /// </summary> + /// <param name="filterState">剔除的state</param> + 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; + } + + /// <summary> + /// 重置当前状态(置为-1) + /// </summary> + 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); + } + } + } + +}
\ No newline at end of file |