// // Created by darman on 9/24/26. // #pragma once namespace SiegeDeck::Engine { // Fixed-timestep simulation time. Real time is fed in each frame, scaled by Speed(), // and handed out again as whole steps of StepDt(): // // sim.Accumulate(frame.Dt()); // while (sim.ConsumeStep()) // FixedUpdate(sim.StepDt()); // Render(sim.Alpha()); // // Speed 0 is the planning phase. 2x and 3x run more steps per frame, never bigger ones, // so fast projectiles can't skip past enemies at high speed. class SimClock { public: // maxStepsPerFrame caps catch-up work after a stall: past it, the backlog is dropped // and the simulation slows down instead of freezing the frame (the spiral of death). SimClock(double stepDt, int32 maxStepsPerFrame); void Accumulate(double realDt); // True if a whole step is available; consumes it and advances Time() and StepIndex(). bool ConsumeStep(); // 0 pauses. Negative values are treated as 0. void SetSpeed(double newSpeed); [[nodiscard]] double Speed() const { return speed; } [[nodiscard]] bool Paused() const { return speed == 0.0; } [[nodiscard]] double StepDt() const { return stepDt; } // How far between the previous and the current step the renderer is, in [0, 1). // Blend PrevTransform -> Transform3D by this. [[nodiscard]] double Alpha() const { return accumulator / stepDt; } // Simulated seconds and steps since construction. Stops advancing while paused. [[nodiscard]] double Time() const { return static_cast(stepIndex) * stepDt; } [[nodiscard]] uint64 StepIndex() const { return stepIndex; } private: double stepDt; double maxBacklog; double speed = 1.0; double accumulator = 0.0; uint64 stepIndex = 0; }; }