formatting
This commit is contained in:
parent
d4d0abd013
commit
1f0e6ba8fa
19 changed files with 88 additions and 79 deletions
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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] == '#';
|
||||
}
|
||||
|
|
|
@ -18,4 +18,4 @@ internal sealed class MoveBullets(BulletManager bullets, IOptions<TanksConfigura
|
|||
bullet.Position.Y - Math.Cos(angle) * config.Value.BulletSpeed
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace TanksServer.Graphics;
|
|||
internal sealed class LastFinishedFrameProvider
|
||||
{
|
||||
private PixelGrid? _lastFrame;
|
||||
|
||||
|
||||
public PixelGrid LastFrame
|
||||
{
|
||||
get => _lastFrame ?? throw new InvalidOperationException("first frame not yet drawn");
|
||||
|
|
|
@ -3,4 +3,4 @@ using System.Text.Json.Serialization;
|
|||
namespace TanksServer.Interactivity;
|
||||
|
||||
[JsonSerializable(typeof(Player))]
|
||||
internal sealed partial class AppSerializerContext: JsonSerializerContext;
|
||||
internal sealed partial class AppSerializerContext : JsonSerializerContext;
|
||||
|
|
|
@ -29,7 +29,7 @@ internal sealed class PlayerServer(ILogger<PlayerServer> logger, SpawnQueue spaw
|
|||
}
|
||||
|
||||
public IEnumerable<Player> GetAll() => _players.Values;
|
||||
|
||||
|
||||
private Player AddAndSpawn(string name)
|
||||
{
|
||||
var player = new Player(name);
|
||||
|
|
|
@ -8,19 +8,19 @@ namespace TanksServer.Interactivity;
|
|||
|
||||
internal sealed class SendToServicePointDisplay : ITickStep
|
||||
{
|
||||
private readonly LastFinishedFrameProvider _lastFinishedFrameProvider;
|
||||
private readonly Cp437Grid _scoresBuffer;
|
||||
private readonly PlayerServer _players;
|
||||
private readonly ILogger<SendToServicePointDisplay> _logger;
|
||||
private readonly IDisplayConnection _displayConnection;
|
||||
private PixelGrid? _lastSentFrame;
|
||||
|
||||
private DateTime _nextFailLog = DateTime.Now;
|
||||
|
||||
private const int ScoresWidth = 12;
|
||||
private const int ScoresHeight = 20;
|
||||
private const int ScoresPlayerRows = ScoresHeight - 5;
|
||||
|
||||
private readonly IDisplayConnection _displayConnection;
|
||||
private readonly LastFinishedFrameProvider _lastFinishedFrameProvider;
|
||||
private readonly ILogger<SendToServicePointDisplay> _logger;
|
||||
private readonly PlayerServer _players;
|
||||
private readonly Cp437Grid _scoresBuffer;
|
||||
|
||||
private PixelGrid? _lastSentFrame;
|
||||
private DateTime _nextFailLog = DateTime.Now;
|
||||
|
||||
public SendToServicePointDisplay(
|
||||
LastFinishedFrameProvider lastFinishedFrameProvider,
|
||||
PlayerServer players,
|
||||
|
@ -90,4 +90,4 @@ internal sealed class SendToServicePointDisplay : ITickStep
|
|||
for (; row < 17; row++)
|
||||
_scoresBuffer[row] = string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,12 +8,11 @@ internal sealed class Player(string name)
|
|||
|
||||
public Guid Id { get; } = Guid.NewGuid();
|
||||
|
||||
[JsonIgnore]
|
||||
public PlayerControls Controls { get; } = new();
|
||||
[JsonIgnore] public PlayerControls Controls { get; } = new();
|
||||
|
||||
public int Kills { get; set; }
|
||||
|
||||
|
||||
public int Deaths { get; set; }
|
||||
|
||||
public DateTime LastInput { get; set; } = DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue