mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 19:03:29 +08:00
- 实现了协程调度器和句柄管理机制 - 添加了多种等待指令包括延时、帧数、条件等待等 - 创建了协程辅助方法和扩展功能 - 集成了Godot引擎的时间源和生命周期管理 - 实现了协程的暂停、恢复、终止等控制功能 - 添加了协程标签管理和按标签批量操作功能 - 提供了与Godot节点生命周期绑定的取消机制
66 lines
1.8 KiB
C#
66 lines
1.8 KiB
C#
using GFramework.Core.Abstractions.coroutine;
|
|
using GFramework.Core.coroutine;
|
|
using Godot;
|
|
|
|
namespace GFramework.Godot.coroutine;
|
|
|
|
public static class CoroutineExtensions
|
|
{
|
|
/// <summary>
|
|
/// 启动协程的扩展方法
|
|
/// </summary>
|
|
public static CoroutineHandle RunCoroutine(
|
|
this IEnumerator<IYieldInstruction> coroutine,
|
|
Segment segment = Segment.Process,
|
|
string? tag = null)
|
|
{
|
|
return Timing.RunCoroutine(coroutine, segment, tag);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 让协程在指定节点被销毁时自动取消
|
|
/// </summary>
|
|
public static IEnumerator<IYieldInstruction> CancelWith(
|
|
this IEnumerator<IYieldInstruction> coroutine,
|
|
Node node)
|
|
{
|
|
while (Timing.IsNodeAlive(node) && coroutine.MoveNext())
|
|
yield return coroutine.Current;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 让协程在任一节点被销毁时自动取消
|
|
/// </summary>
|
|
public static IEnumerator<IYieldInstruction> CancelWith(
|
|
this IEnumerator<IYieldInstruction> coroutine,
|
|
Node node1,
|
|
Node node2)
|
|
{
|
|
while (Timing.IsNodeAlive(node1) &&
|
|
Timing.IsNodeAlive(node2) &&
|
|
coroutine.MoveNext())
|
|
yield return coroutine.Current;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 让协程在多个节点都被销毁时自动取消
|
|
/// </summary>
|
|
public static IEnumerator<IYieldInstruction> CancelWith(
|
|
this IEnumerator<IYieldInstruction> coroutine,
|
|
params Node[] nodes)
|
|
{
|
|
while (AllNodesAlive(nodes) && coroutine.MoveNext())
|
|
yield return coroutine.Current;
|
|
}
|
|
|
|
private static bool AllNodesAlive(Node[] nodes)
|
|
{
|
|
foreach (var node in nodes)
|
|
{
|
|
if (!Timing.IsNodeAlive(node))
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |