< Summary

Information
Class: Gravitas.GravitasClock
Assembly: Gravitas
File(s): /home/runner/work/Gravitas/Gravitas/src/Gravitas/Runtime/GravitasClock.cs
Line coverage
100%
Covered lines: 36
Uncovered lines: 0
Coverable lines: 36
Total lines: 146
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%
get_FrameCount()100%11100%
get_ElapsedTime()100%11100%
GetDeadlineFrame(...)100%11100%
Simulate()100%11100%
LateSimulate()100%11100%
Visualize()100%22100%
Reset()100%11100%
SetFrameRate(...)100%11100%
GetFrameCountForDuration(...)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 System.Runtime.CompilerServices;
 9using Chronicler.Timing;
 10using FixedMathSharp;
 11using FixedMathSharp.Chronicler;
 12
 13namespace Gravitas;
 14
 15/// <summary>
 16/// Stores deterministic fixed-step timing state for one Gravitas runtime owner.
 17/// </summary>
 18internal sealed class GravitasClock
 19{
 1019120    private readonly ChronicleClock _timeline = new(FixedChronicleTime.FromFixed64(
 1019121        Fixed64.One / (Fixed64)PhysicsSettings.DefaultFrameRate));
 22
 23    internal object Lifetime { get; private set; } = new();
 24
 1019125    private int _frameRate = PhysicsSettings.DefaultFrameRate;
 26
 1019127    private Fixed64 _deltaTime = Fixed64.One / (Fixed64)PhysicsSettings.DefaultFrameRate;
 28
 1019129    private Fixed64 _invDeltaTime = Fixed64.One / (Fixed64.One / (Fixed64)PhysicsSettings.DefaultFrameRate);
 30
 31    /// <summary>
 32    /// Gets the fixed simulation frame rate.
 33    /// </summary>
 17319934    public int FrameRate => _frameRate;
 35
 36    /// <summary>
 37    /// Gets the fixed time step for each simulation frame.
 38    /// </summary>
 168993139    public Fixed64 DeltaTime => _deltaTime;
 40
 41    /// <summary>
 42    /// Gets the reciprocal of the current fixed time step.
 43    /// </summary>
 1926244    public Fixed64 InvDeltaTime => _invDeltaTime;
 45
 46    /// <summary>
 47    /// Gets the number of simulated frames.
 48    /// </summary>
 92697649    public long FrameCount => _timeline.FrameCount;
 50
 51    /// <summary>
 52    /// Gets the total simulated time in seconds.
 53    /// </summary>
 843854    public ChronicleTimestamp ElapsedTime => _timeline.ElapsedTime;
 55
 45756    internal long GetDeadlineFrame(long framesFromNow) => _timeline.GetDeadlineFrame(framesFromNow);
 57
 58    /// <summary>
 59    /// Gets the accumulated visualization time since the last late-simulate reset.
 60    /// </summary>
 61    public Fixed64 AccumulatedTime { get; private set; }
 62
 63    /// <summary>
 64    /// Gets whether the next visualization step should reset accumulated time.
 65    /// </summary>
 66    public bool ResetAccumulation { get; private set; }
 67
 68    /// <summary>
 69    /// Gets whether the current visualization step started from a reset.
 70    /// </summary>
 71    public bool ResetAccumulationThisVisualize { get; private set; }
 72
 73    /// <summary>
 74    /// Gets the accumulated visualization time expressed in simulation frames.
 75    /// </summary>
 76    public Fixed64 ExpectedAccumulation { get; private set; }
 77
 78    /// <summary>
 79    /// Advances the fixed simulation frame.
 80    /// </summary>
 81    public void Simulate()
 82    {
 7096983        _timeline.Advance();
 7096784    }
 85
 86    /// <summary>
 87    /// Marks visualization accumulation for reset after a fixed simulation frame completes.
 88    /// </summary>
 89    public void LateSimulate()
 90    {
 7131591        ResetAccumulation = true;
 7131592    }
 93
 94    /// <summary>
 95    /// Advances deterministic visualization accumulation by one fixed step.
 96    /// </summary>
 97    public void Visualize()
 98    {
 4999        ResetAccumulationThisVisualize = ResetAccumulation;
 49100        if (ResetAccumulation)
 101        {
 39102            AccumulatedTime = Fixed64.Zero;
 39103            ResetAccumulation = false;
 104        }
 105
 49106        AccumulatedTime += _deltaTime;
 49107        ExpectedAccumulation = FixedMath.Clamp01(AccumulatedTime / _deltaTime);
 49108    }
 109
 110    /// <summary>
 111    /// Resets elapsed timing state while preserving the configured frame rate.
 112    /// </summary>
 113    public void Reset()
 114    {
 113115        Lifetime = new object();
 113116        _timeline.Reset();
 113117        AccumulatedTime = Fixed64.Zero;
 113118        ExpectedAccumulation = Fixed64.Zero;
 113119        ResetAccumulation = false;
 113120        ResetAccumulationThisVisualize = false;
 113121    }
 122
 123    /// <summary>
 124    /// Updates the fixed simulation frame rate.
 125    /// </summary>
 126    /// <param name="frameRate">The new frame rate. Must be within the supported physics settings range.</param>
 127    public void SetFrameRate(int frameRate)
 128    {
 4226129        PhysicsSettings.ThrowIfInvalidFrameRate(frameRate);
 4226130        _frameRate = frameRate;
 4226131        _deltaTime = Fixed64.One / (Fixed64)_frameRate;
 4226132        _invDeltaTime = Fixed64.One / _deltaTime;
 4226133        _timeline.SetStepDuration(FixedChronicleTime.FromFixed64(_deltaTime));
 4226134    }
 135
 136    /// <summary>
 137    /// Counts complete steps at the current step size, not historical frame indices.
 138    /// </summary>
 139    /// <param name="duration">The nonnegative duration in seconds.</param>
 140    /// <returns>The number of complete steps in the duration.</returns>
 141    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 142    public long GetFrameCountForDuration(Fixed64 duration)
 143    {
 216144        return FixedChronicleTime.GetFrameCountForDuration(duration, _deltaTime);
 145    }
 146}