| | | 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 | | public readonly struct WaitForFrames : ILockedYieldInstruction |
| | | 14 | | { |
| | | 15 | | private readonly GravitasWorldContext _context; |
| | | 16 | | private readonly int _startFrameCount; |
| | | 17 | | private readonly uint _numberOfFrames; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Creates an instruction that waits for a nonnegative number of simulation frames. |
| | | 21 | | /// </summary> |
| | | 22 | | public WaitForFrames(GravitasWorldContext context, int numberOfFrames) |
| | | 23 | | { |
| | 10 | 24 | | SwiftThrowHelper.ThrowIfNull(context, nameof(context)); |
| | 10 | 25 | | SwiftThrowHelper.ThrowIfNegative(numberOfFrames, nameof(numberOfFrames)); |
| | | 26 | | |
| | 9 | 27 | | _context = context; |
| | 9 | 28 | | _startFrameCount = context.FrameCount; |
| | 9 | 29 | | _numberOfFrames = (uint)numberOfFrames; |
| | 9 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | 6 | 33 | | public GravitasWorldContext Context => _context; |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public bool KeepWaiting => |
| | 11 | 37 | | unchecked((uint)(_context.FrameCount - _startFrameCount)) < _numberOfFrames; |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | 1 | 40 | | public object? Current => null; |
| | | 41 | | |
| | | 42 | | /// <inheritdoc /> |
| | 2 | 43 | | public bool MoveNext() => KeepWaiting; |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | 1 | 46 | | public void Reset() { } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | 7 | 49 | | public void Dispose() { } |
| | | 50 | | } |