diff --git a/DungeonShooting_Godot/src/framework/activity/ActivityObject.cs b/DungeonShooting_Godot/src/framework/activity/ActivityObject.cs index a70d5cf..7b63117 100644 --- a/DungeonShooting_Godot/src/framework/activity/ActivityObject.cs +++ b/DungeonShooting_Godot/src/framework/activity/ActivityObject.cs @@ -811,84 +811,7 @@ //协程更新 if (_coroutineList != null) { - var pairs = _coroutineList.ToArray(); - for (var i = 0; i < pairs.Length; i++) - { - var item = pairs[i]; - var canNext = true; - - if (item.WaitType == CoroutineData.WaitTypeEnum.WaitForSeconds) //等待秒数 - { - if (!item.WaitForSeconds.NextStep(newDelta)) - { - canNext = false; - } - else - { - item.WaitType = CoroutineData.WaitTypeEnum.None; - item.WaitForSeconds = null; - } - } - else if (item.WaitType == CoroutineData.WaitTypeEnum.WaitForFixedProcess) //等待帧数 - { - if (!item.WaitForFixedProcess.NextStep()) - { - canNext = false; - } - else - { - item.WaitType = CoroutineData.WaitTypeEnum.None; - item.WaitForFixedProcess = null; - } - } - - if (canNext) - { - if (item.Enumerator.MoveNext()) //嵌套协程 - { - var next = item.Enumerator.Current; - if (next is IEnumerable enumerable) - { - if (item.EnumeratorStack == null) - { - item.EnumeratorStack = new Stack(); - } - - item.EnumeratorStack.Push(item.Enumerator); - item.Enumerator = enumerable.GetEnumerator(); - } - else if (next is IEnumerator enumerator) - { - if (item.EnumeratorStack == null) - { - item.EnumeratorStack = new Stack(); - } - - item.EnumeratorStack.Push(item.Enumerator); - item.Enumerator = enumerator; - } - else if (next is WaitForSeconds seconds) //等待秒数 - { - item.WaitFor(seconds); - } - else if (next is WaitForFixedProcess process) //等待帧数 - { - item.WaitFor(process); - } - } - else - { - if (item.EnumeratorStack == null || item.EnumeratorStack.Count == 0) - { - StopCoroutine(item.Id); - } - else - { - item.Enumerator = item.EnumeratorStack.Pop(); - } - } - } - } + ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, newDelta); } ProcessOver(newDelta); @@ -1206,22 +1129,7 @@ /// public long StartCoroutine(IEnumerator able) { - if (_coroutineList == null) - { - _coroutineList = new List(); - } - - var data = new CoroutineData(able); - _coroutineList.Add(data); - return data.Id; - } - - /// - /// 开启一个协程, 返回协程 id, 协程是在普通帧执行的, 支持: 协程嵌套, WaitForSeconds, WaitForFixedProcess - /// - public long StartCoroutine(IEnumerable able) - { - return StartCoroutine(able.GetEnumerator()); + return ProxyCoroutineHandler.ProxyStartCoroutine(ref _coroutineList, able); } /// @@ -1229,18 +1137,7 @@ /// public void StopCoroutine(long coroutineId) { - if (_coroutineList != null) - { - for (var i = 0; i < _coroutineList.Count; i++) - { - var item = _coroutineList[i]; - if (item.Id == coroutineId) - { - _coroutineList.RemoveAt(i); - return; - } - } - } + ProxyCoroutineHandler.ProxyStopCoroutine(ref _coroutineList, coroutineId); } /// @@ -1248,9 +1145,6 @@ /// public void StopAllCoroutine() { - if (_coroutineList != null) - { - _coroutineList.Clear(); - } + ProxyCoroutineHandler.ProxyStopAllCoroutine(ref _coroutineList); } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/activity/CoroutineData.cs b/DungeonShooting_Godot/src/framework/activity/CoroutineData.cs deleted file mode 100644 index b2f1225..0000000 --- a/DungeonShooting_Godot/src/framework/activity/CoroutineData.cs +++ /dev/null @@ -1,41 +0,0 @@ - -using System.Collections; -using System.Collections.Generic; - -public class CoroutineData -{ - private static long _id; - - public enum WaitTypeEnum - { - None, - WaitForSeconds, - WaitForFixedProcess, - } - - public readonly long Id; - public WaitTypeEnum WaitType = WaitTypeEnum.None; - public IEnumerator Enumerator; - public Stack EnumeratorStack; - - public WaitForSeconds WaitForSeconds; - public WaitForFixedProcess WaitForFixedProcess; - - public CoroutineData(IEnumerator enumerator) - { - Id = _id++; - Enumerator = enumerator; - } - - public void WaitFor(WaitForSeconds seconds) - { - WaitType = WaitTypeEnum.WaitForSeconds; - WaitForSeconds = seconds; - } - - public void WaitFor(WaitForFixedProcess process) - { - WaitType = WaitTypeEnum.WaitForFixedProcess; - WaitForFixedProcess = process; - } -} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/coroutine/CoroutineData.cs b/DungeonShooting_Godot/src/framework/coroutine/CoroutineData.cs new file mode 100644 index 0000000..0d5ed90 --- /dev/null +++ b/DungeonShooting_Godot/src/framework/coroutine/CoroutineData.cs @@ -0,0 +1,44 @@ + +using System.Collections; +using System.Collections.Generic; + +/// +/// 协程数据 +/// +public class CoroutineData +{ + private static long _id; + + public enum WaitTypeEnum + { + None, + WaitForSeconds, + WaitForFixedProcess, + } + + public readonly long Id; + public WaitTypeEnum WaitType = WaitTypeEnum.None; + public IEnumerator Enumerator; + public Stack EnumeratorStack; + + public WaitForSeconds WaitForSeconds; + public WaitForFixedProcess WaitForFixedProcess; + + public CoroutineData(IEnumerator enumerator) + { + Id = _id++; + Enumerator = enumerator; + } + + public void WaitFor(WaitForSeconds seconds) + { + WaitType = WaitTypeEnum.WaitForSeconds; + WaitForSeconds = seconds; + } + + public void WaitFor(WaitForFixedProcess process) + { + WaitType = WaitTypeEnum.WaitForFixedProcess; + WaitForFixedProcess = process; + } +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/coroutine/ProxyCoroutineHandler.cs b/DungeonShooting_Godot/src/framework/coroutine/ProxyCoroutineHandler.cs new file mode 100644 index 0000000..c9e1ecd --- /dev/null +++ b/DungeonShooting_Godot/src/framework/coroutine/ProxyCoroutineHandler.cs @@ -0,0 +1,140 @@ + +using System.Collections; +using System.Collections.Generic; + +/// +/// 协程代理类 +/// +public static class ProxyCoroutineHandler +{ + /// + /// 代理更新协程 + /// + public static void ProxyUpdateCoroutine(ref List coroutineList, float delta) + { + var pairs = coroutineList.ToArray(); + for (var i = 0; i < pairs.Length; i++) + { + var item = pairs[i]; + var canNext = true; + + if (item.WaitType == CoroutineData.WaitTypeEnum.WaitForSeconds) //等待秒数 + { + if (!item.WaitForSeconds.NextStep(delta)) + { + canNext = false; + } + else + { + item.WaitType = CoroutineData.WaitTypeEnum.None; + item.WaitForSeconds = null; + } + } + else if (item.WaitType == CoroutineData.WaitTypeEnum.WaitForFixedProcess) //等待帧数 + { + if (!item.WaitForFixedProcess.NextStep()) + { + canNext = false; + } + else + { + item.WaitType = CoroutineData.WaitTypeEnum.None; + item.WaitForFixedProcess = null; + } + } + + if (canNext) + { + if (item.Enumerator.MoveNext()) //嵌套协程 + { + var next = item.Enumerator.Current; + if (next is IEnumerable enumerable) + { + if (item.EnumeratorStack == null) + { + item.EnumeratorStack = new Stack(); + } + + item.EnumeratorStack.Push(item.Enumerator); + item.Enumerator = enumerable.GetEnumerator(); + } + else if (next is IEnumerator enumerator) + { + if (item.EnumeratorStack == null) + { + item.EnumeratorStack = new Stack(); + } + + item.EnumeratorStack.Push(item.Enumerator); + item.Enumerator = enumerator; + } + else if (next is WaitForSeconds seconds) //等待秒数 + { + item.WaitFor(seconds); + } + else if (next is WaitForFixedProcess process) //等待帧数 + { + item.WaitFor(process); + } + } + else + { + if (item.EnumeratorStack == null || item.EnumeratorStack.Count == 0) + { + ProxyStopCoroutine(ref coroutineList, item.Id); + } + else + { + item.Enumerator = item.EnumeratorStack.Pop(); + } + } + } + } + } + + /// + /// 代理协程, 开启一个协程, 返回协程 id, 协程是在普通帧执行的, 支持: 协程嵌套, WaitForSeconds, WaitForFixedProcess + /// + public static long ProxyStartCoroutine(ref List coroutineList, IEnumerator able) + { + if (coroutineList == null) + { + coroutineList = new List(); + } + + var data = new CoroutineData(able); + coroutineList.Add(data); + return data.Id; + } + + /// + /// 代理协程, 根据协程 id 停止协程 + /// + public static void ProxyStopCoroutine(ref List coroutineList, long coroutineId) + { + if (coroutineList != null) + { + for (var i = 0; i < coroutineList.Count; i++) + { + var item = coroutineList[i]; + if (item.Id == coroutineId) + { + coroutineList.RemoveAt(i); + return; + } + } + } + } + + /// + /// 代理协程, 停止所有协程 + /// + public static void ProxyStopAllCoroutine(ref List coroutineList) + { + if (coroutineList != null) + { + coroutineList.Clear(); + } + } + +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/ui/UiBase.cs b/DungeonShooting_Godot/src/framework/ui/UiBase.cs index 8a4cf5f..de19aca 100644 --- a/DungeonShooting_Godot/src/framework/ui/UiBase.cs +++ b/DungeonShooting_Godot/src/framework/ui/UiBase.cs @@ -1,3 +1,5 @@ +using System.Collections; +using System.Collections.Generic; using Godot; /// @@ -39,6 +41,9 @@ /// public bool IsDisposed { get; private set; } = false; + //开启的协程 + private List _coroutineList; + public UiBase(string uiName) { UiName = uiName; @@ -71,6 +76,13 @@ } /// + /// 每帧调用一次 + /// + public virtual void Process(float delta) + { + } + + /// /// 显示ui, 并传入参数 /// public void ShowUi(params object[] args) @@ -116,4 +128,40 @@ OnDisposeUi(); QueueFree(); } + + public sealed override void _Process(double delta) + { + var newDelta = (float)delta; + Process(newDelta); + + //协程更新 + if (_coroutineList != null) + { + ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, newDelta); + } + } + + /// + /// 开启一个协程, 返回协程 id, 协程是在普通帧执行的, 支持: 协程嵌套, WaitForSeconds, WaitForFixedProcess + /// + public long StartCoroutine(IEnumerator able) + { + return ProxyCoroutineHandler.ProxyStartCoroutine(ref _coroutineList, able); + } + + /// + /// 根据协程 id 停止协程 + /// + public void StopCoroutine(long coroutineId) + { + ProxyCoroutineHandler.ProxyStopCoroutine(ref _coroutineList, coroutineId); + } + + /// + /// 停止所有协程 + /// + public void StopAllCoroutine() + { + ProxyCoroutineHandler.ProxyStopAllCoroutine(ref _coroutineList); + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/ui/roomUI/RoomUIPanel.cs b/DungeonShooting_Godot/src/game/ui/roomUI/RoomUIPanel.cs index f1fdc34..e1dba97 100644 --- a/DungeonShooting_Godot/src/game/ui/roomUI/RoomUIPanel.cs +++ b/DungeonShooting_Godot/src/game/ui/roomUI/RoomUIPanel.cs @@ -1,4 +1,7 @@ +using System.Collections; +using Godot; + namespace UI.RoomUI; /// @@ -35,8 +38,8 @@ _gunBar.OnHide(); } - public override void _Process(double delta) + public override void Process(float delta) { - _gunBar.Process((float) delta); + _gunBar.Process(delta); } } \ No newline at end of file