< Summary

Information
Class: Gravitas.GravitasClock
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Runtime/GravitasClock.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 134
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/Gravitas/Gravitas/src/Gravitas/Runtime/GravitasClock.cs

#LineLine coverage
 1//=======================================================================
 2// GravitasClock.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
 8using FixedMathSharp;
 9using System.Runtime.CompilerServices;
 10
 11namespace Gravitas;
 12
 13/// <summary>
 14/// Stores deterministic fixed-step timing state for one Gravitas runtime owner.
 15/// </summary>
 16internal sealed class GravitasClock
 17{
 508318    private int _frameRate = PhysicsSettings.DefaultFrameRate;
 19
 508320    private Fixed64 _deltaTime = Fixed64.One / (Fixed64)PhysicsSettings.DefaultFrameRate;
 21
 22    /// <summary>
 23    /// Gets the fixed simulation frame rate.
 24    /// </summary>
 1609025    public int FrameRate => _frameRate;
 26
 27    /// <summary>
 28    /// Gets the fixed time step for each simulation frame.
 29    /// </summary>
 60987030    public Fixed64 DeltaTime => _deltaTime;
 31
 32    /// <summary>
 33    /// Gets the reciprocal of the current fixed time step.
 34    /// </summary>
 99935    public Fixed64 InvDeltaTime => Fixed64.One / _deltaTime;
 36
 37    /// <summary>
 38    /// Gets the number of simulated frames.
 39    /// </summary>
 40    public int FrameCount { get; private set; }
 41
 42    /// <summary>
 43    /// Gets the total simulated time in seconds.
 44    /// </summary>
 45    public Fixed64 TotalTime { get; private set; }
 46
 47    /// <summary>
 48    /// Gets the accumulated visualization time since the last late-simulate reset.
 49    /// </summary>
 50    public Fixed64 AccumulatedTime { get; private set; }
 51
 52    /// <summary>
 53    /// Gets whether the next visualization step should reset accumulated time.
 54    /// </summary>
 55    public bool ResetAccumulation { get; private set; }
 56
 57    /// <summary>
 58    /// Gets whether the current visualization step started from a reset.
 59    /// </summary>
 60    public bool ResetAccumulationThisVisualize { get; private set; }
 61
 62    /// <summary>
 63    /// Gets the accumulated visualization time expressed in simulation frames.
 64    /// </summary>
 65    public Fixed64 ExpectedAccumulation { get; private set; }
 66
 67    /// <summary>
 68    /// Advances the fixed simulation frame.
 69    /// </summary>
 70    public void Simulate()
 71    {
 232072        FrameCount++;
 232073        TotalTime += _deltaTime;
 232074    }
 75
 76    /// <summary>
 77    /// Marks visualization accumulation for reset after a fixed simulation frame completes.
 78    /// </summary>
 79    public void LateSimulate()
 80    {
 283281        ResetAccumulation = true;
 283282    }
 83
 84    /// <summary>
 85    /// Advances deterministic visualization accumulation by one fixed step.
 86    /// </summary>
 87    public void Visualize()
 88    {
 2989        ResetAccumulationThisVisualize = ResetAccumulation;
 2990        if (ResetAccumulation)
 91        {
 1992            AccumulatedTime = Fixed64.Zero;
 1993            ResetAccumulation = false;
 94        }
 95
 2996        AccumulatedTime += _deltaTime;
 2997        ExpectedAccumulation = FixedMath.Clamp01(AccumulatedTime / _deltaTime);
 2998    }
 99
 100    /// <summary>
 101    /// Resets elapsed timing state while preserving the configured frame rate.
 102    /// </summary>
 103    public void Reset()
 104    {
 31105        FrameCount = 0;
 31106        TotalTime = Fixed64.Zero;
 31107        AccumulatedTime = Fixed64.Zero;
 31108        ExpectedAccumulation = Fixed64.Zero;
 31109        ResetAccumulation = false;
 31110        ResetAccumulationThisVisualize = false;
 31111    }
 112
 113    /// <summary>
 114    /// Updates the fixed simulation frame rate.
 115    /// </summary>
 116    /// <param name="frameRate">The new frame rate. Must be within the supported physics settings range.</param>
 117    public void SetFrameRate(int frameRate)
 118    {
 1863119        PhysicsSettings.ThrowIfInvalidFrameRate(frameRate);
 1863120        _frameRate = frameRate;
 1863121        _deltaTime = Fixed64.One / (Fixed64)_frameRate;
 1863122    }
 123
 124    /// <summary>
 125    /// Calculates the frame index containing the specified fixed-point timestamp.
 126    /// </summary>
 127    /// <param name="timestamp">The timestamp to resolve.</param>
 128    /// <returns>The zero-based frame index for the timestamp.</returns>
 129    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 130    public int GetFrameFromTime(Fixed64 timestamp)
 131    {
 3132        return (timestamp * InvDeltaTime).FloorToInt();
 133    }
 134}