| | | 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 Chronicler.Timing; |
| | | 9 | | using FixedMathSharp; |
| | | 10 | | using FixedMathSharp.Chronicler; |
| | | 11 | | |
| | | 12 | | namespace Gravitas.Support; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// A coroutine yield instruction that waits for a specified duration on the owning context's deterministic clock. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// Uses a wide timestamp deadline, preserving the requested duration across frame-rate |
| | | 19 | | /// changes. Reading an instruction after context reset or disposal throws; reads never |
| | | 20 | | /// consume time. An unrepresentable deadline throws during construction. |
| | | 21 | | /// </remarks> |
| | | 22 | | public readonly struct WaitForRealSeconds : ILockedYieldInstruction |
| | | 23 | | { |
| | | 24 | | private readonly GravitasWorldContext _context; |
| | | 25 | | private readonly object _lifetime; |
| | | 26 | | private readonly ChronicleTimestamp _targetTime; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Creates an instruction that waits for a nonnegative duration on the context's deterministic clock. |
| | | 30 | | /// </summary> |
| | | 31 | | public WaitForRealSeconds(GravitasWorldContext context, Fixed64 seconds) |
| | | 32 | | { |
| | 221 | 33 | | ChronicleGuard.ThrowIfNull(context, nameof(context)); |
| | 220 | 34 | | ChronicleGuard.ThrowIfArgument(seconds < Fixed64.Zero, nameof(seconds), "Wait duration cannot be negative."); |
| | | 35 | | |
| | 219 | 36 | | _context = context; |
| | 219 | 37 | | _lifetime = context.CaptureClockLifetime(); |
| | 219 | 38 | | _targetTime = context.ElapsedTime + FixedChronicleTime.FromFixed64(seconds); |
| | 218 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <inheritdoc /> |
| | 3 | 42 | | public GravitasWorldContext Context => _context; |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public bool KeepWaiting |
| | | 46 | | { |
| | | 47 | | get |
| | | 48 | | { |
| | 435 | 49 | | _context.ValidateClockLifetime(_lifetime); |
| | 433 | 50 | | return _context.ElapsedTime < _targetTime; |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | 1 | 55 | | public object? Current => null; |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 3 | 58 | | public bool MoveNext() => KeepWaiting; |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | 1 | 61 | | public void Reset() { } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | 3 | 64 | | public void Dispose() { } |
| | | 65 | | } |