| | | 1 | | //======================================================================= |
| | | 2 | | // WaitForRealSeconds.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | |
| | | 10 | | namespace Gravitas.Support; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// A coroutine yield instruction that waits for a specified duration on the owning context's deterministic clock. |
| | | 14 | | /// </summary> |
| | | 15 | | public readonly struct WaitForRealSeconds : ILockedYieldInstruction |
| | | 16 | | { |
| | | 17 | | private readonly GravitasWorldContext _context; |
| | | 18 | | private readonly Fixed64 _targetTime; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Creates an instruction that waits for a nonnegative duration on the context's deterministic clock. |
| | | 22 | | /// </summary> |
| | | 23 | | public WaitForRealSeconds(GravitasWorldContext context, Fixed64 seconds) |
| | | 24 | | { |
| | 6 | 25 | | SwiftThrowHelper.ThrowIfNull(context, nameof(context)); |
| | 5 | 26 | | SwiftThrowHelper.ThrowIfArgument(seconds < Fixed64.Zero, nameof(seconds), "Wait duration cannot be negative."); |
| | | 27 | | |
| | 4 | 28 | | _context = context; |
| | 4 | 29 | | _targetTime = context.TotalTime + seconds; |
| | 4 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | 2 | 33 | | public GravitasWorldContext Context => _context; |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | 10 | 36 | | public bool KeepWaiting => _context.TotalTime < _targetTime; |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | 1 | 39 | | public object? Current => null; |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | 3 | 42 | | public bool MoveNext() => KeepWaiting; |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | 1 | 45 | | public void Reset() { } |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | 2 | 48 | | public void Dispose() { } |
| | | 49 | | } |