< 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: 135
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
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%11100%
GetFrameFromTime(...)100%11100%

File(s)

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

#LineLine coverage
 1//=======================================================================
 2// TrailblazerClock.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024-present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using System;
 9using System.Runtime.CompilerServices;
 10using FixedMathSharp;
 11
 12namespace Trailblazer;
 13
 14/// <summary>
 15/// Stores deterministic fixed-step timing state for one Trailblazer runtime owner.
 16/// </summary>
 17internal sealed class TrailblazerClock
 18{
 19    internal const int DefaultFrameRate = 32;
 20
 98921    private int _frameRate = DefaultFrameRate;
 22
 98923    private Fixed64 _deltaTime = Fixed64.One / (Fixed64)DefaultFrameRate;
 24
 25    /// <summary>
 26    /// Gets the fixed simulation frame rate.
 27    /// </summary>
 10028    public int FrameRate => _frameRate;
 29
 30    /// <summary>
 31    /// Gets the fixed time step for each simulation frame.
 32    /// </summary>
 641133    public Fixed64 DeltaTime => _deltaTime;
 34
 35    /// <summary>
 36    /// Gets the reciprocal of the current fixed time step.
 37    /// </summary>
 467038    public Fixed64 InvDeltaTime => Fixed64.One / _deltaTime;
 39
 40    /// <summary>
 41    /// Gets the number of simulated frames.
 42    /// </summary>
 43    public int FrameCount { get; private set; }
 44
 45    /// <summary>
 46    /// Gets the total simulated time in seconds.
 47    /// </summary>
 48    public Fixed64 TotalTime { get; private set; }
 49
 50    /// <summary>
 51    /// Gets the accumulated visualization time since the last late-simulate reset.
 52    /// </summary>
 53    public Fixed64 AccumulatedTime { get; private set; }
 54
 55    /// <summary>
 56    /// Gets whether the next visualization step should reset accumulated time.
 57    /// </summary>
 58    public bool ResetAccumulation { get; private set; }
 59
 60    /// <summary>
 61    /// Gets the accumulated visualization time expressed in simulation frames.
 62    /// </summary>
 63    public Fixed64 ExpectedAccumulation { get; private set; }
 64
 65    /// <summary>
 66    /// Advances the fixed simulation frame.
 67    /// </summary>
 68    public void Simulate()
 69    {
 1218570        FrameCount++;
 1218571        TotalTime += _deltaTime;
 1218572    }
 73
 74    /// <summary>
 75    /// Marks visualization accumulation for reset after a fixed simulation frame completes.
 76    /// </summary>
 77    public void LateSimulate()
 78    {
 279        ResetAccumulation = true;
 280    }
 81
 82    /// <summary>
 83    /// Advances deterministic visualization accumulation by one fixed step.
 84    /// </summary>
 85    public void Visualize()
 86    {
 587        if (ResetAccumulation)
 88        {
 289            AccumulatedTime = Fixed64.Zero;
 290            ResetAccumulation = false;
 91        }
 92
 593        AccumulatedTime += _deltaTime;
 594        ExpectedAccumulation = AccumulatedTime / _deltaTime;
 595    }
 96
 97    /// <summary>
 98    /// Resets elapsed timing state while preserving the configured frame rate.
 99    /// </summary>
 100    public void Reset()
 101    {
 22102        FrameCount = 0;
 22103        TotalTime = Fixed64.Zero;
 22104        AccumulatedTime = Fixed64.Zero;
 22105        ExpectedAccumulation = Fixed64.Zero;
 22106        ResetAccumulation = false;
 22107    }
 108
 109    /// <summary>
 110    /// Updates the fixed simulation frame rate.
 111    /// </summary>
 112    /// <param name="frameRate">The new frame rate. Must be greater than zero.</param>
 113    public void SetFrameRate(int frameRate)
 114    {
 11115        SwiftThrowHelper.ThrowIfArgumentOutOfRange(
 11116            frameRate <= 0,
 11117            actualValue: frameRate,
 11118            paramName: nameof(frameRate),
 11119            message: "Frame rate must be greater than zero.");
 120
 9121        _frameRate = frameRate;
 9122        _deltaTime = Fixed64.One / (Fixed64)_frameRate;
 9123    }
 124
 125    /// <summary>
 126    /// Calculates the frame index containing the specified fixed-point timestamp.
 127    /// </summary>
 128    /// <param name="timestamp">The timestamp to resolve.</param>
 129    /// <returns>The zero-based frame index for the timestamp.</returns>
 130    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 131    public int GetFrameFromTime(Fixed64 timestamp)
 132    {
 1133        return (timestamp * InvDeltaTime).FloorToInt();
 134    }
 135}