| | | 1 | | using FixedMathSharp; |
| | | 2 | | using System; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | |
| | | 5 | | namespace Trailblazer; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Stores deterministic fixed-step timing state for one Trailblazer runtime owner. |
| | | 9 | | /// </summary> |
| | | 10 | | internal sealed class TrailblazerClock |
| | | 11 | | { |
| | | 12 | | internal const int DefaultFrameRate = 32; |
| | | 13 | | |
| | 961 | 14 | | private int _frameRate = DefaultFrameRate; |
| | | 15 | | |
| | 961 | 16 | | private Fixed64 _deltaTime = Fixed64.One / (Fixed64)DefaultFrameRate; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets the fixed simulation frame rate. |
| | | 20 | | /// </summary> |
| | 78 | 21 | | public int FrameRate => _frameRate; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the fixed time step for each simulation frame. |
| | | 25 | | /// </summary> |
| | 6178 | 26 | | public Fixed64 DeltaTime => _deltaTime; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets the reciprocal of the current fixed time step. |
| | | 30 | | /// </summary> |
| | 4389 | 31 | | public Fixed64 InvDeltaTime => Fixed64.One / _deltaTime; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets the number of simulated frames. |
| | | 35 | | /// </summary> |
| | | 36 | | public int FrameCount { get; private set; } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets the total simulated time in seconds. |
| | | 40 | | /// </summary> |
| | | 41 | | public Fixed64 TotalTime { get; private set; } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets the accumulated visualization time since the last late-simulate reset. |
| | | 45 | | /// </summary> |
| | | 46 | | public Fixed64 AccumulatedTime { get; private set; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Gets whether the next visualization step should reset accumulated time. |
| | | 50 | | /// </summary> |
| | | 51 | | public bool ResetAccumulation { get; private set; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets the accumulated visualization time expressed in simulation frames. |
| | | 55 | | /// </summary> |
| | | 56 | | public Fixed64 ExpectedAccumulation { get; private set; } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Advances the fixed simulation frame. |
| | | 60 | | /// </summary> |
| | | 61 | | public void Simulate() |
| | | 62 | | { |
| | 2299 | 63 | | FrameCount++; |
| | 2299 | 64 | | TotalTime += _deltaTime; |
| | 2299 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Marks visualization accumulation for reset after a fixed simulation frame completes. |
| | | 69 | | /// </summary> |
| | | 70 | | public void LateSimulate() |
| | | 71 | | { |
| | 2 | 72 | | ResetAccumulation = true; |
| | 2 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Advances deterministic visualization accumulation by one fixed step. |
| | | 77 | | /// </summary> |
| | | 78 | | public void Visualize() |
| | | 79 | | { |
| | 5 | 80 | | if (ResetAccumulation) |
| | | 81 | | { |
| | 2 | 82 | | AccumulatedTime = Fixed64.Zero; |
| | 2 | 83 | | ResetAccumulation = false; |
| | | 84 | | } |
| | | 85 | | |
| | 5 | 86 | | AccumulatedTime += _deltaTime; |
| | 5 | 87 | | ExpectedAccumulation = AccumulatedTime / _deltaTime; |
| | 5 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Resets elapsed timing state while preserving the configured frame rate. |
| | | 92 | | /// </summary> |
| | | 93 | | public void Reset() |
| | | 94 | | { |
| | 25 | 95 | | FrameCount = 0; |
| | 25 | 96 | | TotalTime = Fixed64.Zero; |
| | 25 | 97 | | AccumulatedTime = Fixed64.Zero; |
| | 25 | 98 | | ExpectedAccumulation = Fixed64.Zero; |
| | 25 | 99 | | ResetAccumulation = false; |
| | 25 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Updates the fixed simulation frame rate. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="frameRate">The new frame rate. Must be greater than zero.</param> |
| | | 106 | | public void SetFrameRate(int frameRate) |
| | | 107 | | { |
| | 11 | 108 | | if (frameRate <= 0) |
| | | 109 | | { |
| | 2 | 110 | | throw new ArgumentOutOfRangeException( |
| | 2 | 111 | | nameof(frameRate), |
| | 2 | 112 | | frameRate, |
| | 2 | 113 | | "Frame rate must be greater than zero."); |
| | | 114 | | } |
| | | 115 | | |
| | 9 | 116 | | _frameRate = frameRate; |
| | 9 | 117 | | _deltaTime = Fixed64.One / (Fixed64)_frameRate; |
| | 9 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Calculates the frame index containing the specified fixed-point timestamp. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <param name="timestamp">The timestamp to resolve.</param> |
| | | 124 | | /// <returns>The zero-based frame index for the timestamp.</returns> |
| | | 125 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 126 | | public int GetFrameFromTime(Fixed64 timestamp) |
| | | 127 | | { |
| | 1 | 128 | | return (timestamp * InvDeltaTime).FloorToInt(); |
| | | 129 | | } |
| | | 130 | | } |