aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/src/Godosa.Core/Runs/FrameClock.cs
blob: eedb78d3d337d4aef80d4ce683a063cadc1edd84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
namespace Godosa.Core.Runs;

/// <summary>Game time per frame: deterministic → a fixed <see cref="Step"/> whatever the wall clock; otherwise the wall dt
/// clamped to [0, maxDt]. <see cref="Held"/> (e.g. a map still loading) → no game time and no frame counted.</summary>
public sealed class FrameClock(bool deterministic, double step = 1.0 / 60, double maxDt = 0.1)
{
    public bool Deterministic { get; } = deterministic;
    public double Step { get; } = step;
    public long Frame { get; private set; }
    public double Seconds { get; private set; }
    public bool Held { get; set; }

    /// <summary>The game dt of this frame (0 when held); counts the frame unless held.</summary>
    public double Next(double wallDt)
    {
        if (Held)
            return 0;
        var dt = Deterministic ? Step : Math.Clamp(wallDt, 0, maxDt);
        Frame++;
        Seconds += dt;
        return dt;
    }
}