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