From 5bb5da4caebfdc8627e331b4eb5c53457316ec44 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Thu, 18 May 2023 19:20:17 +0800 Subject: *misc --- .../Tools/Statemachine/BasicStatemachine.cs | 251 +++++++++++++++++++++ .../Tools/Statemachine/BasicStatemachine.cs.meta | 11 + .../Scripts/Tools/Statemachine/LiteStatemachine.cs | 23 ++ .../Tools/Statemachine/LiteStatemachine.cs.meta | 11 + .../Scripts/Tools/Statemachine/Statemachine.cs | 249 -------------------- .../Tools/Statemachine/Statemachine.cs.meta | 11 - 6 files changed, 296 insertions(+), 260 deletions(-) create mode 100644 WorldlineKeepers/Assets/Scripts/Tools/Statemachine/BasicStatemachine.cs create mode 100644 WorldlineKeepers/Assets/Scripts/Tools/Statemachine/BasicStatemachine.cs.meta create mode 100644 WorldlineKeepers/Assets/Scripts/Tools/Statemachine/LiteStatemachine.cs create mode 100644 WorldlineKeepers/Assets/Scripts/Tools/Statemachine/LiteStatemachine.cs.meta delete mode 100644 WorldlineKeepers/Assets/Scripts/Tools/Statemachine/Statemachine.cs delete mode 100644 WorldlineKeepers/Assets/Scripts/Tools/Statemachine/Statemachine.cs.meta (limited to 'WorldlineKeepers/Assets/Scripts/Tools/Statemachine') 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 +{ + /// + /// 一般状态机 + /// + 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); + } + } + } + +} \ No newline at end of file diff --git a/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/BasicStatemachine.cs.meta b/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/BasicStatemachine.cs.meta new file mode 100644 index 0000000..1f9dc33 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/BasicStatemachine.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 740b9ccdbc7196546acfadecbcbd71f0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/LiteStatemachine.cs b/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/LiteStatemachine.cs new file mode 100644 index 0000000..f119866 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/LiteStatemachine.cs @@ -0,0 +1,23 @@ +using JetBrains.Annotations; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace WK +{ + + /// + /// 轻量级状态机 + /// + public abstract class LiteStatemachine + { + + + public void GotoState(int target) + { + + } + + } + +} diff --git a/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/LiteStatemachine.cs.meta b/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/LiteStatemachine.cs.meta new file mode 100644 index 0000000..1243aac --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/LiteStatemachine.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a8fe740cda5abcf489e9188cdd7150ce +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/Statemachine.cs b/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/Statemachine.cs deleted file mode 100644 index b54b5e1..0000000 --- a/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/Statemachine.cs +++ /dev/null @@ -1,249 +0,0 @@ -using UnityEngine; -using System.Collections; -using System.Collections.Generic; -using Newtonsoft.Json.Utilities; - -namespace WK -{ - - public class BaseStateMachine - { - public delegate void StateEvent(); - public delegate bool StateFinishChecker(); - public class State - { - public BaseStateMachine 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); - } - } - } - -} \ No newline at end of file diff --git a/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/Statemachine.cs.meta b/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/Statemachine.cs.meta deleted file mode 100644 index 1f9dc33..0000000 --- a/WorldlineKeepers/Assets/Scripts/Tools/Statemachine/Statemachine.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 740b9ccdbc7196546acfadecbcbd71f0 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: -- cgit v1.1-26-g67d0