| | | 1 | | //======================================================================= |
| | | 2 | | // WaitForNextSimulate.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 until the next Gravitas simulation frame. |
| | | 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 WaitForNextSimulate : 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 until the context advances to another simulation frame. |
| | | 25 | | /// </summary> |
| | | 26 | | public WaitForNextSimulate(GravitasWorldContext context) |
| | | 27 | | { |
| | 233 | 28 | | ChronicleGuard.ThrowIfNull(context, nameof(context)); |
| | | 29 | | |
| | 233 | 30 | | _context = context; |
| | 233 | 31 | | _lifetime = context.CaptureClockLifetime(); |
| | 233 | 32 | | _deadlineFrame = context.GetDeadlineFrame(1); |
| | 232 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | 19 | 36 | | public GravitasWorldContext Context => _context; |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public bool KeepWaiting |
| | | 40 | | { |
| | | 41 | | get |
| | | 42 | | { |
| | 225 | 43 | | _context.ValidateClockLifetime(_lifetime); |
| | 223 | 44 | | return _context.FrameCount < _deadlineFrame; |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | 1 | 49 | | public object? Current => null; |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | 2 | 52 | | public bool MoveNext() => KeepWaiting; |
| | | 53 | | |
| | | 54 | | /// <inheritdoc /> |
| | 1 | 55 | | public void Reset() { } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | 20 | 58 | | public void Dispose() { } |
| | | 59 | | } |