namespace SpaceGame.Api.Universe.Simulation; public sealed class SimulationHostedService(WorldService worldService) : BackgroundService { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { using var timer = new PeriodicTimer(TimeSpan.FromMilliseconds(200)); try { while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken)) { worldService.Tick(0.2f); } } catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) { } } }