move backend to subfolder

This commit is contained in:
Vinzenz Schroeter 2024-04-21 12:38:03 +02:00
parent d4d1f2f981
commit 8d09663eff
80 changed files with 98 additions and 88 deletions

View file

@ -0,0 +1,16 @@
namespace TanksServer.Models;
internal sealed class Bullet(Player tankOwner, FloatPosition position, double rotation, bool isExplosive, DateTime timeout) : IMapEntity
{
public Player Owner { get; } = tankOwner;
public double Rotation { get; } = rotation;
public FloatPosition Position { get; set; } = position;
public bool IsExplosive { get; } = isExplosive;
public DateTime Timeout { get; } = timeout;
public PixelBounds Bounds => new (Position.ToPixelPosition(), Position.ToPixelPosition());
}

View file

@ -0,0 +1,11 @@
using System.Diagnostics;
using TanksServer.GameLogic;
namespace TanksServer.Models;
[DebuggerDisplay("({X} | {Y})")]
internal readonly struct FloatPosition(double x, double y)
{
public double X { get; } = (x + MapService.PixelsPerRow) % MapService.PixelsPerRow;
public double Y { get; } = (y + MapService.PixelsPerColumn) % MapService.PixelsPerColumn;
}

View file

@ -0,0 +1,10 @@
namespace TanksServer.Models;
public class HostConfiguration
{
public bool EnableServicePointDisplay { get; set; } = true;
public int ServicePointDisplayMinFrameTimeMs { get; set; } = 25;
public int ClientDisplayMinFrameTimeMs { get; set; } = 25;
}

View file

@ -0,0 +1,8 @@
namespace TanksServer.Models;
internal interface IMapEntity
{
FloatPosition Position { get; set; }
PixelBounds Bounds { get; }
}

View file

@ -0,0 +1,6 @@
using System.Diagnostics;
namespace TanksServer.Models;
[DebuggerDisplay("{TopLeft}, {BottomRight}")]
internal record struct PixelBounds(PixelPosition TopLeft, PixelPosition BottomRight);

View file

@ -0,0 +1,17 @@
using System.Diagnostics;
using TanksServer.GameLogic;
namespace TanksServer.Models;
[DebuggerDisplay("({X} | {Y})")]
internal readonly struct PixelPosition(int x, int y)
{
public int X { get; } = (x + MapService.PixelsPerRow) % MapService.PixelsPerRow;
public int Y { get; } = (y + MapService.PixelsPerColumn) % MapService.PixelsPerColumn;
public void Deconstruct(out int x, out int y)
{
x = X;
y = Y;
}
}

View file

@ -0,0 +1,16 @@
using System.Text.Json.Serialization;
namespace TanksServer.Models;
internal sealed class Player(string name, Guid id)
{
public string Name => name;
[JsonIgnore] public Guid Id => id;
[JsonIgnore] public PlayerControls Controls { get; } = new();
public Scores Scores { get; } = new();
public DateTime LastInput { get; set; } = DateTime.Now;
}

View file

@ -0,0 +1,10 @@
namespace TanksServer.Models;
internal sealed class PlayerControls
{
public bool Forward { get; set; }
public bool Backward { get; set; }
public bool TurnLeft { get; set; }
public bool TurnRight { get; set; }
public bool Shoot { get; set; }
}

View file

@ -0,0 +1,42 @@
using TanksServer.GameLogic;
namespace TanksServer.Models;
internal static class PositionHelpers
{
public static PixelPosition GetPixelRelative(this PixelPosition position, short subX, short subY)
=> new(position.X + subX, position.Y + subY);
public static PixelPosition ToPixelPosition(this FloatPosition position)
=> new((int)Math.Round(position.X), (int)Math.Round(position.Y));
public static PixelPosition ToPixelPosition(this TilePosition position) => new(
(ushort)(position.X * MapService.TileSize),
(ushort)(position.Y * MapService.TileSize)
);
public static TilePosition ToTilePosition(this PixelPosition position) => new(
(ushort)(position.X / MapService.TileSize),
(ushort)(position.Y / MapService.TileSize)
);
public static FloatPosition ToFloatPosition(this PixelPosition position) => new(position.X, position.Y);
public static double Distance(this FloatPosition p1, FloatPosition p2)
=> Math.Sqrt(
Math.Pow(p1.X - p2.X, 2) +
Math.Pow(p1.Y - p2.Y, 2)
);
public static PixelBounds GetBoundsForCenter(this FloatPosition position, ushort size)
{
var sub = (short)(-size / 2d);
var add = (short)(size / 2d - 1);
var pixelPosition = position.ToPixelPosition();
return new PixelBounds(
pixelPosition.GetPixelRelative(sub, sub),
pixelPosition.GetPixelRelative(add, add)
);
}
}

View file

@ -0,0 +1,10 @@
using TanksServer.GameLogic;
namespace TanksServer.Models;
internal sealed class PowerUp(FloatPosition position): IMapEntity
{
public FloatPosition Position { get; set; } = position;
public PixelBounds Bounds => Position.GetBoundsForCenter(MapService.TileSize);
}

View file

@ -0,0 +1,22 @@
namespace TanksServer.Models;
internal sealed record class Scores(int Kills = 0, int Deaths = 0)
{
public int Kills { get; set; } = Kills;
public int Deaths { get; set; } = Deaths;
public double Ratio
{
get
{
if (Kills == 0)
return 0;
if (Deaths == 0)
return Kills;
return Kills / (double)Deaths;
}
}
public int WallsDestroyed { get; set; }
}

View file

@ -0,0 +1,34 @@
using System.Diagnostics;
using TanksServer.GameLogic;
namespace TanksServer.Models;
internal sealed class Tank(Player player, FloatPosition spawnPosition) : IMapEntity
{
private double _rotation;
public Player Owner { get; } = player;
public double Rotation
{
get => _rotation;
set
{
var newRotation = (value % 1d + 1d) % 1d;
Debug.Assert(newRotation is >= 0 and < 1);
_rotation = newRotation;
}
}
public DateTime NextShotAfter { get; set; }
public bool Moved { get; set; }
public FloatPosition Position { get; set; } = spawnPosition;
public PixelBounds Bounds => Position.GetBoundsForCenter(MapService.TileSize);
public int Orientation => (int)Math.Round(Rotation * 16) % 16;
public byte ExplosiveBullets { get; set; }
}

View file

@ -0,0 +1,11 @@
using System.Diagnostics;
using TanksServer.GameLogic;
namespace TanksServer.Models;
[DebuggerDisplay("({X} | {Y})")]
internal readonly struct TilePosition(ushort x, ushort y)
{
public ushort X { get; } = (ushort)((x + MapService.TilesPerRow) % MapService.TilesPerRow);
public ushort Y { get; } = (ushort)((y + MapService.TilesPerColumn) % MapService.TilesPerColumn);
}