mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 添加协程上下文、句柄、调度器和作用域管理类 - 实现协程等待指令包括 WaitForSeconds、WaitUntil 和 WaitWhile - 创建协程系统和全局协程作用域管理器 - 定义协程相关抽象接口 ICoroutineScheduler、ICoroutineScope 等 - 升级 Meziantou.Analyzer 依赖版本至 2.0.283 - 升级 Meziantou.Polyfill 依赖版本至 1.0.100
52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
using System.Collections;
|
|
using GFramework.Game.Abstractions.coroutine;
|
|
|
|
namespace GFramework.Game.coroutine;
|
|
|
|
public class CoroutineScheduler : ICoroutineScheduler
|
|
{
|
|
private readonly List<CoroutineHandle> _active = new();
|
|
private readonly List<CoroutineHandle> _toAdd = new();
|
|
private readonly HashSet<CoroutineHandle> _toRemove = new();
|
|
|
|
public int ActiveCount => _active.Count;
|
|
|
|
public void Update(float deltaTime)
|
|
{
|
|
if (_toAdd.Count > 0)
|
|
{
|
|
_active.AddRange(_toAdd);
|
|
_toAdd.Clear();
|
|
}
|
|
|
|
for (var i = _active.Count - 1; i >= 0; i--)
|
|
{
|
|
var c = _active[i];
|
|
|
|
if (!c.Context.Scope.IsActive)
|
|
{
|
|
c.Cancel();
|
|
_toRemove.Add(c);
|
|
continue;
|
|
}
|
|
|
|
if (!c.Update(deltaTime))
|
|
_toRemove.Add(c);
|
|
}
|
|
|
|
if (_toRemove.Count <= 0) return;
|
|
{
|
|
_active.RemoveAll(c => _toRemove.Contains(c));
|
|
_toRemove.Clear();
|
|
}
|
|
}
|
|
|
|
internal CoroutineHandle StartCoroutine(IEnumerator routine, CoroutineContext context)
|
|
{
|
|
var handle = new CoroutineHandle(routine, context);
|
|
_toAdd.Add(handle);
|
|
return handle;
|
|
}
|
|
|
|
internal void RemoveCoroutine(CoroutineHandle handle) => _toRemove.Add(handle);
|
|
} |