From a47439f027bd58b69c8356a1fe869c8b0fa96057 Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Sun, 1 Feb 2026 10:37:29 +0800
Subject: [PATCH] =?UTF-8?q?feat(coroutine):=20=E6=B7=BB=E5=8A=A0=E5=B8=A6?=
=?UTF-8?q?=E8=B6=85=E6=97=B6=E7=9A=84=E4=BA=8B=E4=BB=B6=E7=AD=89=E5=BE=85?=
=?UTF-8?q?=E5=8D=8F=E7=A8=8B=E6=8C=87=E4=BB=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 实现 WaitForEventWithTimeout 类用于带超时的事件等待
- 支持泛型事件类型参数化
- 提供超时检测和事件数据访问功能
- 实现 IYieldInstruction 接口支持协程执行
- 包含时间累加和状态更新逻辑
---
.../instructions/WaitForEventWithTimeout.cs | 61 +++++++++++++++++++
1 file changed, 61 insertions(+)
create mode 100644 GFramework.Core/coroutine/instructions/WaitForEventWithTimeout.cs
diff --git a/GFramework.Core/coroutine/instructions/WaitForEventWithTimeout.cs b/GFramework.Core/coroutine/instructions/WaitForEventWithTimeout.cs
new file mode 100644
index 0000000..6a410a2
--- /dev/null
+++ b/GFramework.Core/coroutine/instructions/WaitForEventWithTimeout.cs
@@ -0,0 +1,61 @@
+// Copyright (c) 2026 GeWuYou
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using GFramework.Core.Abstractions.coroutine;
+
+namespace GFramework.Core.coroutine.instructions;
+
+///
+/// 带超时的事件等待指令
+///
+/// 事件类型
+/// 要包装的事件等待指令
+/// 超时时间(秒)
+public sealed class WaitForEventWithTimeout(WaitForEvent waitForEvent, float timeout)
+ : IYieldInstruction
+{
+ private readonly WaitForEvent _waitForEvent =
+ waitForEvent ?? throw new ArgumentNullException(nameof(waitForEvent));
+
+ private float _elapsedTime;
+
+ ///
+ /// 获取是否已超时
+ ///
+ public bool IsTimeout => _elapsedTime >= timeout;
+
+ ///
+ /// 获取接收到的事件数据
+ ///
+ public TEvent? EventData => _waitForEvent.EventData;
+
+ ///
+ /// 获取指令是否已完成(事件已触发或超时)
+ ///
+ public bool IsDone => _waitForEvent.IsDone || IsTimeout;
+
+ ///
+ /// 更新指令状态
+ ///
+ /// 时间增量
+ public void Update(double deltaTime)
+ {
+ // 只有在事件未完成时才累加经过的时间
+ if (!_waitForEvent.IsDone)
+ {
+ _elapsedTime += (float)deltaTime;
+ }
+
+ _waitForEvent.Update(deltaTime);
+ }
+}
\ No newline at end of file