more consistent tick pacing
This commit is contained in:
parent
dc9ad21a3d
commit
190c0c3143
|
@ -1,11 +1,12 @@
|
||||||
|
using System.Diagnostics;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using TanksServer.TickSteps;
|
using TanksServer.TickSteps;
|
||||||
|
|
||||||
namespace TanksServer.Services;
|
namespace TanksServer;
|
||||||
|
|
||||||
internal sealed class GameTickService(
|
internal sealed class GameTickWorker(
|
||||||
IEnumerable<ITickStep> steps, IHostApplicationLifetime lifetime, ILogger<GameTickService> logger
|
IEnumerable<ITickStep> steps, IHostApplicationLifetime lifetime, ILogger<GameTickWorker> logger
|
||||||
) : IHostedService, IDisposable
|
) : IHostedService, IDisposable
|
||||||
{
|
{
|
||||||
private readonly CancellationTokenSource _cancellation = new();
|
private readonly CancellationTokenSource _cancellation = new();
|
||||||
|
@ -22,11 +23,16 @@ internal sealed class GameTickService(
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var sw = new Stopwatch();
|
||||||
while (!_cancellation.IsCancellationRequested)
|
while (!_cancellation.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
|
logger.LogTrace("since last frame: {}", sw.Elapsed);
|
||||||
|
sw.Restart();
|
||||||
|
|
||||||
foreach (var step in _steps)
|
foreach (var step in _steps)
|
||||||
await step.TickAsync();
|
await step.TickAsync();
|
||||||
await Task.Delay(1000 / 25);
|
|
||||||
|
await Task.Delay(TimeSpan.FromMilliseconds(1000 / 25) - sw.Elapsed);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
|
@ -88,7 +88,7 @@ internal static class Program
|
||||||
builder.Services.AddSingleton<LastFinishedFrameProvider>();
|
builder.Services.AddSingleton<LastFinishedFrameProvider>();
|
||||||
builder.Services.AddSingleton<SpawnQueueProvider>();
|
builder.Services.AddSingleton<SpawnQueueProvider>();
|
||||||
|
|
||||||
builder.Services.AddHostedService<GameTickService>();
|
builder.Services.AddHostedService<GameTickWorker>();
|
||||||
builder.Services.AddHostedService(sp => sp.GetRequiredService<ControlsServer>());
|
builder.Services.AddHostedService(sp => sp.GetRequiredService<ControlsServer>());
|
||||||
builder.Services.AddHostedService(sp => sp.GetRequiredService<ClientScreenServer>());
|
builder.Services.AddHostedService(sp => sp.GetRequiredService<ClientScreenServer>());
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue