player and entity physics improvements

This commit is contained in:
2025-10-01 00:40:47 +02:00
parent 11f76ca429
commit bd2c87ddd1
7 changed files with 444 additions and 105 deletions

View File

@@ -12,13 +12,24 @@ namespace Voxel
public readonly int Height = height;
public uint frames = 0;
public double timeElapsed = 0;
public event Action<float> Update;
public event Action<float, float> Update;
public event Action Tick;
private double _tickTime;
public const double TICK_LENGTH = 1.0 / 20.0; // 20 TPS
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
float deltaTime = (float)e.Time;
_tickTime += e.Time;
while (_tickTime >= TICK_LENGTH)
{
_tickTime -= TICK_LENGTH;
Tick(); // run exactly once per tick
}
if (Input.GetKey(Keys.Escape))
{
@@ -32,8 +43,6 @@ namespace Voxel
else
WindowState = WindowState.Normal;
}
Update.Invoke(deltaTime);
}
protected override void OnRenderFrame(FrameEventArgs e)
@@ -44,6 +53,8 @@ namespace Voxel
frames++;
timeElapsed += e.Time;
float alpha = (float)(_tickTime / Window.TICK_LENGTH);
float deltaTime = (float)e.Time;
if (timeElapsed >= 1)
{
Console.WriteLine("FPS: " + frames.ToString());
@@ -51,6 +62,8 @@ namespace Voxel
frames = 0;
}
Update.Invoke(deltaTime, alpha);
Renderer.Render();
SwapBuffers();