infinite map

This commit is contained in:
Vinzenz Schroeter 2024-04-12 18:32:10 +02:00
parent 7213318838
commit 461a9139c2
20 changed files with 101 additions and 69 deletions

View file

@ -1,3 +1,9 @@
using TanksServer.GameLogic;
namespace TanksServer.Models;
internal readonly record struct FloatPosition(double X, double 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

@ -3,5 +3,4 @@ namespace TanksServer.Models;
internal interface IMapEntity
{
FloatPosition Position { get; set; }
double Rotation { get; set; }
}

View file

@ -1,3 +1,9 @@
using TanksServer.GameLogic;
namespace TanksServer.Models;
internal record struct PixelPosition(ushort X, ushort Y);
internal readonly struct PixelPosition(ushort x, ushort y)
{
public ushort X { get; } = (ushort)((x + MapService.PixelsPerRow) % MapService.PixelsPerRow);
public ushort Y { get; } = (ushort)((y + MapService.PixelsPerColumn) % MapService.PixelsPerColumn);
}

View file

@ -10,19 +10,25 @@ internal static class PositionHelpers
Debug.Assert(subX < 8);
Debug.Assert(subY < 8);
return new PixelPosition(
X: (ushort)(position.X * MapService.TileSize + subX),
Y: (ushort)(position.Y * MapService.TileSize + subY)
x: (ushort)(position.X * MapService.TileSize + subX),
y: (ushort)(position.Y * MapService.TileSize + subY)
);
}
public static PixelPosition GetPixelRelative(this PixelPosition position, byte subX, byte subY)
{
Debug.Assert(subX < 8);
Debug.Assert(subY < 8);
return new PixelPosition((ushort)(position.X + subX), (ushort)(position.Y + subY));
}
public static PixelPosition ToPixelPosition(this FloatPosition position) => new(
X: (ushort)((int)position.X % MapService.PixelsPerRow),
Y: (ushort)((int)position.Y % MapService.PixelsPerRow)
x: (ushort)((int)position.X % MapService.PixelsPerRow),
y: (ushort)((int)position.Y % MapService.PixelsPerRow)
);
public static TilePosition ToTilePosition(this PixelPosition position) => new(
X: position.X / MapService.TileSize,
Y: position.Y / MapService.TileSize
x: (ushort)(position.X / MapService.TileSize),
y: (ushort)(position.Y / MapService.TileSize)
);
}

View file

@ -1,20 +1,23 @@
using System.Diagnostics;
using TanksServer.GameLogic;
namespace TanksServer.Models;
internal sealed class Tank(Player player, FloatPosition spawnPosition): IMapEntity
internal sealed class Tank(Player player, FloatPosition spawnPosition) : IMapEntity
{
private double _rotation;
public Player Owner { get; } = player;
/// <summary>
/// Bounds: 0 (inclusive) .. 16 (exclusive)
/// </summary>
public double Rotation
{
get => _rotation;
set => _rotation = (value + 16d) % 16d;
set
{
var newRotation = (value % 1d + 1d) % 1d;
Debug.Assert(newRotation is >= 0 and < 1);
_rotation = newRotation;
}
}
public FloatPosition Position { get; set; } = spawnPosition;
@ -22,12 +25,4 @@ internal sealed class Tank(Player player, FloatPosition spawnPosition): IMapEnti
public DateTime NextShotAfter { get; set; }
public bool Moved { get; set; }
public (FloatPosition TopLeft, FloatPosition BottomRight) GetBounds()
{
return (
Position,
new FloatPosition(Position.X + MapService.TileSize , Position.Y + MapService.TileSize )
);
}
}
}

View file

@ -1,3 +1,9 @@
using TanksServer.GameLogic;
namespace TanksServer.Models;
internal record struct TilePosition(int X, int 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);
}