mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-01-02 03:18:23 -05:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
41 lines
874 B
C#
41 lines
874 B
C#
namespace Sandbox;
|
|
|
|
internal class FixedUpdate
|
|
{
|
|
/// <summary>
|
|
/// How many times a second FixedUpdate runs
|
|
/// </summary>
|
|
public float Frequency = 16;
|
|
|
|
public float Delta => 1.0f / Frequency;
|
|
|
|
/// <summary>
|
|
/// Accumulate frame time up until a maximum amount (maxSteps). While this value
|
|
/// is above the <see cref="Delta"/> time we will invoke a fixed update.
|
|
/// </summary>
|
|
private long step;
|
|
|
|
internal void Run( Action fixedUpdate, float time, int maxSteps )
|
|
{
|
|
var delta = Delta;
|
|
long curStep = (long)Math.Floor( time / delta );
|
|
|
|
// Clamp the steps so we never jump too many
|
|
step = long.Clamp( step, curStep - maxSteps, curStep );
|
|
|
|
if ( step == curStep )
|
|
return;
|
|
|
|
while ( step < curStep )
|
|
{
|
|
step++;
|
|
using var timeScope = Time.Scope( (step * delta), delta );
|
|
fixedUpdate();
|
|
}
|
|
|
|
// always end up to date
|
|
step = curStep;
|
|
|
|
}
|
|
}
|