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 --- "Documents/\345\217\202\350\200\203.txt" | 2 + .../Assets/Scripts/Data/DataManager_Load.cs | 16 +- WorldlineKeepers/Assets/Scripts/Data/FileKey.cs | 22 ++ .../Assets/Scripts/Data/FileKey.cs.meta | 11 + WorldlineKeepers/Assets/Scripts/Data/Filelist.cs | 7 - .../Assets/Scripts/Stages/GameStageBase.cs | 2 +- .../Assets/Scripts/Stages/GameStageManager.cs | 7 +- .../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 - .../Assets/Scripts/Utils/AsyncUtils.cs | 17 ++ .../Assets/Scripts/Utils/AsyncUtils.cs.meta | 11 + 15 files changed, 377 insertions(+), 274 deletions(-) create mode 100644 "Documents/\345\217\202\350\200\203.txt" create mode 100644 WorldlineKeepers/Assets/Scripts/Data/FileKey.cs create mode 100644 WorldlineKeepers/Assets/Scripts/Data/FileKey.cs.meta 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 create mode 100644 WorldlineKeepers/Assets/Scripts/Utils/AsyncUtils.cs create mode 100644 WorldlineKeepers/Assets/Scripts/Utils/AsyncUtils.cs.meta diff --git "a/Documents/\345\217\202\350\200\203.txt" "b/Documents/\345\217\202\350\200\203.txt" new file mode 100644 index 0000000..87a4fc7 --- /dev/null +++ "b/Documents/\345\217\202\350\200\203.txt" @@ -0,0 +1,2 @@ +https://www.youtube.com/watch?v=1YOi66c1j1o&ab_channel=PFY%E7%8E%A9%E7%B5%A6%E4%BD%A0%E7%9C%8B +https://www.youtube.com/watch?v=gKlv10IO2D4&ab_channel=PFY%E7%8E%A9%E7%B5%A6%E4%BD%A0%E7%9C%8B \ No newline at end of file diff --git a/WorldlineKeepers/Assets/Scripts/Data/DataManager_Load.cs b/WorldlineKeepers/Assets/Scripts/Data/DataManager_Load.cs index 886446a..40b00a7 100644 --- a/WorldlineKeepers/Assets/Scripts/Data/DataManager_Load.cs +++ b/WorldlineKeepers/Assets/Scripts/Data/DataManager_Load.cs @@ -12,15 +12,25 @@ namespace WK.Data { /// - /// 异步加载所有数据 + /// 异步加载数据 /// /// - public IEnumerator AsyncLoadAllData() + public CoroutineHandle AsyncLoadAll() + { + return Timing.Instance.RunCoroutineOnInstance(AsyncLoadAllData()); + } + + private IEnumerator AsyncLoadAllData() { Load_Filelist(); yield return Timing.WaitForSeconds(StaticDefine.IntervalLoadFile); } + #region 加载 + + /// + /// fielist + /// private void Load_Filelist() { TextAsset text = ResourceManager.Instance.LoadAsset(StaticDefine.FileList); @@ -34,5 +44,7 @@ namespace WK.Data } } + #endregion + } } \ No newline at end of file diff --git a/WorldlineKeepers/Assets/Scripts/Data/FileKey.cs b/WorldlineKeepers/Assets/Scripts/Data/FileKey.cs new file mode 100644 index 0000000..1ab15b2 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Data/FileKey.cs @@ -0,0 +1,22 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace WK.Data +{ + + /// + /// 文件enum + /// + public enum EFileKey + { + none = 0, + + default_stats, + default_buffs, + default_items, + + all + } + +} diff --git a/WorldlineKeepers/Assets/Scripts/Data/FileKey.cs.meta b/WorldlineKeepers/Assets/Scripts/Data/FileKey.cs.meta new file mode 100644 index 0000000..1564c3d --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Data/FileKey.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 024b24790fd0abd4181e4b690d9fb4ee +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/Scripts/Data/Filelist.cs b/WorldlineKeepers/Assets/Scripts/Data/Filelist.cs index 094c1ea..dc96df9 100644 --- a/WorldlineKeepers/Assets/Scripts/Data/Filelist.cs +++ b/WorldlineKeepers/Assets/Scripts/Data/Filelist.cs @@ -19,13 +19,6 @@ namespace WK.Data Persistent = 2, } - public enum EFileKey - { - default_stats = 1, - default_buffs = 2, - default_items = 3, - } - /// /// 文件列表 /// diff --git a/WorldlineKeepers/Assets/Scripts/Stages/GameStageBase.cs b/WorldlineKeepers/Assets/Scripts/Stages/GameStageBase.cs index 430c2d8..19b3535 100644 --- a/WorldlineKeepers/Assets/Scripts/Stages/GameStageBase.cs +++ b/WorldlineKeepers/Assets/Scripts/Stages/GameStageBase.cs @@ -9,7 +9,7 @@ namespace WK public class GameStageBase : AsyncStatemachine.State { - protected GameStageManager manager = GameStageManager.Instance; + protected GameStageManager owner = GameStageManager.Instance; public override IEnumerator OnStart() { diff --git a/WorldlineKeepers/Assets/Scripts/Stages/GameStageManager.cs b/WorldlineKeepers/Assets/Scripts/Stages/GameStageManager.cs index 8c746a5..a28e56d 100644 --- a/WorldlineKeepers/Assets/Scripts/Stages/GameStageManager.cs +++ b/WorldlineKeepers/Assets/Scripts/Stages/GameStageManager.cs @@ -60,10 +60,10 @@ namespace WK { } - public void AsyncLoadStage(int stage, bool forceLoad = false, AsyncStatemachine.LoadStateComplete loadStateComplete = null) + public void AsyncLoadStage(EGameStage stage, bool forceLoad = false, AsyncStatemachine.LoadStateComplete loadStateComplete = null) { int curRunStage = m_Statemachine.GetCurStateID(); - if (!forceLoad && curRunStage == stages[stage]) + if (!forceLoad && curRunStage == stages[(int)stage]) { if (null != loadStateComplete) { @@ -75,11 +75,10 @@ namespace WK //LogHelper.LogEditorError("==> StageChange:" + curStage + " --> " + (EGameStage)stage); prevStage = curStage; - curStage = (EGameStage)stage; + curStage = stage; m_Statemachine.GotoState(stages[(int)stage], false, forceLoad, loadStateComplete); } - public EGameStage GetCurStage() { return curStage; 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: diff --git a/WorldlineKeepers/Assets/Scripts/Utils/AsyncUtils.cs b/WorldlineKeepers/Assets/Scripts/Utils/AsyncUtils.cs new file mode 100644 index 0000000..14bbc86 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Utils/AsyncUtils.cs @@ -0,0 +1,17 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using MovementEffects; + +namespace WK +{ + public static class AsyncUtils + { + public static CoroutineHandle AsyncAction(IEnumerator asyncAct) + { + var handle = Timing.Instance.RunCoroutineOnInstance(asyncAct); + return handle; + } + + } +} diff --git a/WorldlineKeepers/Assets/Scripts/Utils/AsyncUtils.cs.meta b/WorldlineKeepers/Assets/Scripts/Utils/AsyncUtils.cs.meta new file mode 100644 index 0000000..7540550 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Utils/AsyncUtils.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 59a14efd34714d1498d15731d63bfdfe +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- cgit v1.1-26-g67d0