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
|
|
|
|
{
|
2024-04-13 16:27:45 +02:00
|
|
|
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)
|
2024-04-16 20:24:29 +02:00
|
|
|
=> new((int)Math.Round(position.X), (int)Math.Round(position.Y));
|
2024-04-13 14:07:14 +02:00
|
|
|
|
|
|
|
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 18:35:36 +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-17 19:34:19 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
2024-04-13 14:07:14 +02:00
|
|
|
}
|