move upgrades to tank, serialize objects directly

This commit is contained in:
Vinzenz Schroeter 2024-05-08 00:29:33 +02:00
parent b1df817ece
commit 827b3a9330
16 changed files with 135 additions and 180 deletions

View file

@ -47,20 +47,7 @@ internal sealed class PlayerInfoConnection
private async ValueTask<IMemoryOwner<byte>?> GenerateMessageAsync()
{
var tank = _entityManager.GetCurrentTankOfPlayer(_player);
TankInfo? tankInfo = null;
if (tank != null)
{
var magazine = tank.ReloadingUntil > DateTime.Now ? "[ RELOADING ]" : tank.Magazine.ToDisplayString();
tankInfo = new TankInfo(tank.Orientation, magazine, tank.Position.ToPixelPosition(), tank.Moving);
}
var info = new PlayerInfo(
_player.Name,
_player.Scores,
_player.Controls.ToDisplayString(),
tankInfo,
_player.OpenConnections);
var info = new PlayerInfo(_player, _player.Controls.ToDisplayString(), tank);
_tempStream.Position = 0;
await JsonSerializer.SerializeAsync(_tempStream, info, AppSerializerContext.Default.PlayerInfo);
@ -85,3 +72,9 @@ internal sealed class PlayerInfoConnection
Interlocked.Exchange(ref _lastMessage, data)?.Dispose();
}
}
internal record struct PlayerInfo(
Player Player,
string Controls,
Tank? Tank
);