< Summary

Information
Class: Trailblazer.TrailblazerClock
Assembly: Trailblazer
File(s): /home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Runtime/TrailblazerClock.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 130
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
get_FrameRate()100%11100%
get_DeltaTime()100%11100%
get_InvDeltaTime()100%11100%
Simulate()100%11100%
LateSimulate()100%11100%
Visualize()100%22100%
Reset()100%11100%
SetFrameRate(...)100%22100%
GetFrameFromTime(...)100%11100%

File(s)

/home/runner/work/Trailblazer/Trailblazer/src/Trailblazer/Runtime/TrailblazerClock.cs

#LineLine coverage
 1using FixedMathSharp;
 2using System;
 3using System.Runtime.CompilerServices;
 4
 5namespace Trailblazer;
 6
 7/// <summary>
 8/// Stores deterministic fixed-step timing state for one Trailblazer runtime owner.
 9/// </summary>
 10internal sealed class TrailblazerClock
 11{
 12    internal const int DefaultFrameRate = 32;
 13
 96114    private int _frameRate = DefaultFrameRate;
 15
 96116    private Fixed64 _deltaTime = Fixed64.One / (Fixed64)DefaultFrameRate;
 17
 18    /// <summary>
 19    /// Gets the fixed simulation frame rate.
 20    /// </summary>
 7821    public int FrameRate => _frameRate;
 22
 23    /// <summary>
 24    /// Gets the fixed time step for each simulation frame.
 25    /// </summary>
 617826    public Fixed64 DeltaTime => _deltaTime;
 27
 28    /// <summary>
 29    /// Gets the reciprocal of the current fixed time step.
 30    /// </summary>
 438931    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    {
 229963        FrameCount++;
 229964        TotalTime += _deltaTime;
 229965    }
 66
 67    /// <summary>
 68    /// Marks visualization accumulation for reset after a fixed simulation frame completes.
 69    /// </summary>
 70    public void LateSimulate()
 71    {
 272        ResetAccumulation = true;
 273    }
 74
 75    /// <summary>
 76    /// Advances deterministic visualization accumulation by one fixed step.
 77    /// </summary>
 78    public void Visualize()
 79    {
 580        if (ResetAccumulation)
 81        {
 282            AccumulatedTime = Fixed64.Zero;
 283            ResetAccumulation = false;
 84        }
 85
 586        AccumulatedTime += _deltaTime;
 587        ExpectedAccumulation = AccumulatedTime / _deltaTime;
 588    }
 89
 90    /// <summary>
 91    /// Resets elapsed timing state while preserving the configured frame rate.
 92    /// </summary>
 93    public void Reset()
 94    {
 2595        FrameCount = 0;
 2596        TotalTime = Fixed64.Zero;
 2597        AccumulatedTime = Fixed64.Zero;
 2598        ExpectedAccumulation = Fixed64.Zero;
 2599        ResetAccumulation = false;
 25100    }
 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    {
 11108        if (frameRate <= 0)
 109        {
 2110            throw new ArgumentOutOfRangeException(
 2111                nameof(frameRate),
 2112                frameRate,
 2113                "Frame rate must be greater than zero.");
 114        }
 115
 9116        _frameRate = frameRate;
 9117        _deltaTime = Fixed64.One / (Fixed64)_frameRate;
 9118    }
 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    {
 1128        return (timestamp * InvDeltaTime).FloorToInt();
 129    }
 130}