formatting

This commit is contained in:
Vinzenz Schroeter 2024-04-13 14:08:51 +02:00
parent d4d0abd013
commit 1f0e6ba8fa
19 changed files with 88 additions and 79 deletions

View file

@ -2,14 +2,11 @@ namespace TanksServer.GameLogic;
internal sealed class BulletManager
{
private readonly HashSet<Bullet> _bullets = new();
private readonly HashSet<Bullet> _bullets = [];
public void Spawn(Bullet bullet) => _bullets.Add(bullet);
public IEnumerable<Bullet> GetAll() => _bullets;
public void RemoveWhere(Predicate<Bullet> predicate)
{
_bullets.RemoveWhere(predicate);
}
public void RemoveWhere(Predicate<Bullet> predicate) => _bullets.RemoveWhere(predicate);
}

View file

@ -8,8 +8,6 @@ internal sealed class CollideBulletsWithMap(BulletManager bullets, MapService ma
return Task.CompletedTask;
}
private bool BulletHitsWall(Bullet bullet)
{
return map.IsCurrentlyWall(bullet.Position.ToPixelPosition().ToTilePosition());
}
private bool BulletHitsWall(Bullet bullet) =>
map.IsCurrentlyWall(bullet.Position.ToPixelPosition().ToTilePosition());
}

View file

@ -10,17 +10,29 @@ internal sealed class GameTickWorker(
) : IHostedService, IDisposable
{
private const int TicksPerSecond = 25;
private static readonly TimeSpan TickPacing = TimeSpan.FromMilliseconds((int)(1000 / TicksPerSecond));
private static readonly TimeSpan TickPacing = TimeSpan.FromMilliseconds(1000 / TicksPerSecond);
private readonly CancellationTokenSource _cancellation = new();
private readonly List<ITickStep> _steps = steps.ToList();
private Task? _run;
public void Dispose()
{
_cancellation.Dispose();
_run?.Dispose();
}
public Task StartAsync(CancellationToken cancellationToken)
{
_run = RunAsync();
return Task.CompletedTask;
}
public async Task StopAsync(CancellationToken cancellationToken)
{
await _cancellation.CancelAsync();
if (_run != null) await _run;
}
private async Task RunAsync()
{
try
@ -45,16 +57,4 @@ internal sealed class GameTickWorker(
lifetime.StopApplication();
}
}
public async Task StopAsync(CancellationToken cancellationToken)
{
await _cancellation.CancelAsync();
if (_run != null) await _run;
}
public void Dispose()
{
_cancellation.Dispose();
_run?.Dispose();
}
}
}

View file

@ -35,8 +35,5 @@ internal sealed class MapService
private char this[int tileX, int tileY] => _map[tileX + tileY * TilesPerRow];
public bool IsCurrentlyWall(TilePosition position)
{
return this[position.X, position.Y] == '#';
}
public bool IsCurrentlyWall(TilePosition position) => this[position.X, position.Y] == '#';
}

View file

@ -18,4 +18,4 @@ internal sealed class MoveBullets(BulletManager bullets, IOptions<TanksConfigura
bullet.Position.Y - Math.Cos(angle) * config.Value.BulletSpeed
);
}
}
}

View file

@ -11,10 +11,7 @@ internal sealed class SpawnQueue(
private readonly TimeSpan _spawnDelay = TimeSpan.FromMilliseconds(options.Value.SpawnDelayMs);
private readonly TimeSpan _idleTimeout = TimeSpan.FromMilliseconds(options.Value.IdleTimeoutMs);
public void EnqueueForImmediateSpawn(Player player)
{
_queue.Enqueue(player);
}
public void EnqueueForImmediateSpawn(Player player) => _queue.Enqueue(player);
public void EnqueueForDelayedSpawn(Player player)
{
@ -33,7 +30,7 @@ internal sealed class SpawnQueue(
_queue.Enqueue(player);
return false;
}
var now = DateTime.Now;
if (_spawnTimes.GetOrAdd(player, DateTime.MinValue) > now)
{
@ -44,4 +41,4 @@ internal sealed class SpawnQueue(
return true;
}
}
}