2024-04-07 19:19:11 +02:00
|
|
|
using System.Diagnostics;
|
2024-04-10 19:25:45 +02:00
|
|
|
using TanksServer.GameLogic;
|
2024-04-07 19:19:11 +02:00
|
|
|
|
2024-04-10 19:25:45 +02:00
|
|
|
namespace TanksServer.Models;
|
2024-04-07 19:19:11 +02:00
|
|
|
|
|
|
|
internal static class PositionHelpers
|
|
|
|
{
|
|
|
|
public static PixelPosition GetPixelRelative(this TilePosition position, byte subX, byte subY)
|
|
|
|
{
|
|
|
|
Debug.Assert(subX < 8);
|
|
|
|
Debug.Assert(subY < 8);
|
|
|
|
return new PixelPosition(
|
2024-04-13 14:07:14 +02:00
|
|
|
(ushort)(position.X * MapService.TileSize + subX),
|
|
|
|
(ushort)(position.Y * MapService.TileSize + subY)
|
2024-04-07 19:19:11 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-04-12 18:32:10 +02:00
|
|
|
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));
|
|
|
|
}
|
2024-04-07 19:19:11 +02:00
|
|
|
|
|
|
|
public static PixelPosition ToPixelPosition(this FloatPosition position) => new(
|
2024-04-13 14:07:14 +02:00
|
|
|
(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)
|
2024-04-07 19:19:11 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
public static TilePosition ToTilePosition(this PixelPosition position) => new(
|
2024-04-13 14:07:14 +02:00
|
|
|
(ushort)(position.X / MapService.TileSize),
|
|
|
|
(ushort)(position.Y / MapService.TileSize)
|
2024-04-07 19:19:11 +02:00
|
|
|
);
|
2024-04-12 19:49:24 +02:00
|
|
|
|
|
|
|
public static FloatPosition ToFloatPosition(this PixelPosition position) => new(position.X, position.Y);
|
|
|
|
|
|
|
|
|
2024-04-13 14:07:14 +02:00
|
|
|
public static double Distance(this FloatPosition p1, FloatPosition p2) =>
|
|
|
|
Math.Sqrt(
|
2024-04-12 19:49:24 +02:00
|
|
|
Math.Pow(p1.X - p2.X, 2) +
|
|
|
|
Math.Pow(p1.Y - p2.Y, 2)
|
|
|
|
);
|
2024-04-13 14:07:14 +02:00
|
|
|
}
|