| | | 1 | | //======================================================================= |
| | | 2 | | // WaitForFrames.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 | | namespace Gravitas.Support; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// A coroutine yield instruction that waits for a specified number of Gravitas simulation frames. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Uses a checked deadline in the context's current clock lifetime. Reading an |
| | | 15 | | /// instruction after context reset or disposal throws; reads never consume frames. |
| | | 16 | | /// </remarks> |
| | | 17 | | public readonly struct WaitForFrames : ILockedYieldInstruction |
| | | 18 | | { |
| | | 19 | | private readonly GravitasWorldContext _context; |
| | | 20 | | private readonly object _lifetime; |
| | | 21 | | private readonly long _deadlineFrame; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates an instruction that waits for a nonnegative number of simulation frames. |
| | | 25 | | /// </summary> |
| | | 26 | | public WaitForFrames(GravitasWorldContext context, long numberOfFrames) |
| | | 27 | | { |
| | 225 | 28 | | ChronicleGuard.ThrowIfNull(context, nameof(context)); |
| | 225 | 29 | | ChronicleGuard.ThrowIfArgumentOutOfRange(numberOfFrames < 0, null, |
| | 225 | 30 | | nameof(numberOfFrames), "Wait frame count cannot be negative."); |
| | | 31 | | |
| | 224 | 32 | | _context = context; |
| | 224 | 33 | | _lifetime = context.CaptureClockLifetime(); |
| | 224 | 34 | | _deadlineFrame = context.GetDeadlineFrame(numberOfFrames); |
| | 223 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | 7 | 38 | | public GravitasWorldContext Context => _context; |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public bool KeepWaiting |
| | | 42 | | { |
| | | 43 | | get |
| | | 44 | | { |
| | 436 | 45 | | _context.ValidateClockLifetime(_lifetime); |
| | 434 | 46 | | return _context.FrameCount < _deadlineFrame; |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | 1 | 51 | | public object? Current => null; |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | 2 | 54 | | public bool MoveNext() => KeepWaiting; |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | 1 | 57 | | public void Reset() { } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | 8 | 60 | | public void Dispose() { } |
| | | 61 | | } |