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

@ -8,8 +8,6 @@ internal sealed class Bullet : IMapEntity
public required FloatPosition Position { get; set; }
public required bool IsExplosive { get; init; }
public required DateTime Timeout { get; init; }
public PixelBounds Bounds => new(Position.ToPixelPosition(), Position.ToPixelPosition());
@ -18,7 +16,5 @@ internal sealed class Bullet : IMapEntity
public required double Speed { get; set; }
public required double Acceleration { get; init; }
public required bool IsSmart { get; init; }
public required BulletStats Stats { get; init; }
}

View file

@ -1,38 +0,0 @@
using System.Text;
namespace TanksServer.Models;
[Flags]
internal enum MagazineType
{
Basic = 0,
Fast = 1 << 0,
Explosive = 1 << 1,
Smart = 1 << 2,
}
internal readonly record struct Magazine(MagazineType Type, byte UsedBullets, byte MaxBullets)
{
public bool Empty => UsedBullets >= MaxBullets;
public string ToDisplayString()
{
var sb = new StringBuilder();
if (Type.HasFlag(MagazineType.Fast))
sb.Append("» ");
if (Type.HasFlag(MagazineType.Explosive))
sb.Append("* ");
if (Type.HasFlag(MagazineType.Smart))
sb.Append("@ ");
sb.Append("[ ");
for (var i = 0; i < UsedBullets; i++)
sb.Append("\u25cb ");
for (var i = UsedBullets; i < MaxBullets; i++)
sb.Append("• ");
sb.Append(']');
return sb.ToString();
}
}

View file

@ -1,16 +0,0 @@
namespace TanksServer.Models;
internal record struct TankInfo(
int Orientation,
string Magazine,
PixelPosition Position,
bool Moving
);
internal record struct PlayerInfo(
string Name,
Scores Scores,
string Controls,
TankInfo? Tank,
int OpenConnections
);

View file

@ -4,8 +4,11 @@ namespace TanksServer.Models;
internal enum PowerUpType
{
MagazineType,
MagazineSize
MagazineSize,
BulletSpeed,
BulletAcceleration,
ExplosiveBullets,
SmartBullets,
}
internal sealed class PowerUp: IMapEntity
@ -15,6 +18,4 @@ internal sealed class PowerUp: IMapEntity
public PixelBounds Bounds => Position.GetBoundsForCenter(MapService.TileSize);
public required PowerUpType Type { get; init; }
public MagazineType? MagazineType { get; init; }
}

View file

@ -1,13 +1,14 @@
using System.Diagnostics;
using System.Text.Json.Serialization;
using TanksServer.GameLogic;
namespace TanksServer.Models;
internal sealed class Tank : IMapEntity
internal sealed class Tank(Player owner) : IMapEntity
{
private double _rotation;
public required Player Owner { get; init; }
[JsonIgnore] public Player Owner { get; } = owner;
public double Rotation
{
@ -26,11 +27,17 @@ internal sealed class Tank : IMapEntity
public required FloatPosition Position { get; set; }
public PixelBounds Bounds => Position.GetBoundsForCenter(MapService.TileSize);
[JsonIgnore] public PixelBounds Bounds => Position.GetBoundsForCenter(MapService.TileSize);
public int Orientation => (int)Math.Round(Rotation * 16) % 16;
public required Magazine Magazine { get; set; }
public int UsedBullets { get; set; }
public int MaxBullets { get; set; }
public DateTime ReloadingUntil { get; set; }
public required BulletStats BulletStats { get; set; }
}
internal sealed record class BulletStats(double Speed, double Acceleration, bool Explosive, bool Smart);