position is now center, socket improvements

This commit is contained in:
Vinzenz Schroeter 2024-04-13 14:07:14 +02:00
parent 40eba7a7c7
commit d4d0abd013
18 changed files with 134 additions and 106 deletions

View file

@ -1,10 +1,12 @@
namespace TanksServer.Models;
internal sealed class Bullet(Player tankOwner, FloatPosition position, double rotation): IMapEntity
internal sealed class Bullet(Player tankOwner, FloatPosition position, double rotation) : IMapEntity
{
public Player Owner { get; } = tankOwner;
public FloatPosition Position { get; set; } = position;
public double Rotation { get; set; } = rotation;
}
public FloatPosition Position { get; set; } = position;
public PixelBounds Bounds => new (Position.ToPixelPosition(), Position.ToPixelPosition());
}

View file

@ -3,4 +3,6 @@ namespace TanksServer.Models;
internal interface IMapEntity
{
FloatPosition Position { get; set; }
}
PixelBounds Bounds { get; }
}

View file

@ -0,0 +1,3 @@
namespace TanksServer.Models;
internal record struct PixelBounds(PixelPosition TopLeft, PixelPosition BottomRight);

View file

@ -10,8 +10,8 @@ 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)
(ushort)(position.X * MapService.TileSize + subX),
(ushort)(position.Y * MapService.TileSize + subY)
);
}
@ -23,21 +23,26 @@ internal static class PositionHelpers
}
public static PixelPosition ToPixelPosition(this FloatPosition position) => new(
x: (ushort)((int)position.X % MapService.PixelsPerRow),
y: (ushort)((int)position.Y % MapService.PixelsPerRow)
(ushort)((int)position.X % MapService.PixelsPerRow),
(ushort)((int)position.Y % MapService.PixelsPerRow)
);
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(
x: (ushort)(position.X / MapService.TileSize),
y: (ushort)(position.Y / MapService.TileSize)
(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(
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)
);
}
}

View file

@ -20,9 +20,21 @@ internal sealed class Tank(Player player, FloatPosition spawnPosition) : IMapEnt
}
}
public FloatPosition Position { get; set; } = spawnPosition;
public DateTime NextShotAfter { get; set; }
public bool Moved { get; set; }
}
public FloatPosition Position { get; set; } = spawnPosition;
public PixelBounds Bounds => GetBoundsForCenter(Position);
public static PixelBounds GetBoundsForCenter(FloatPosition position) => new(
new PixelPosition(
(ushort)(position.X - MapService.TileSize / 2d),
(ushort)(position.Y - MapService.TileSize / 2d)
), new PixelPosition(
(ushort)(position.X + MapService.TileSize / 2d - 1d),
(ushort)(position.Y + MapService.TileSize / 2d - 1d)
)
);
}