2023-07-08 14:08:32 +10:00
|
|
|
using System.Numerics;
|
2020-10-08 17:41:23 +02:00
|
|
|
using Robust.Shared.Map;
|
2022-11-22 13:12:04 +11:00
|
|
|
using Robust.Shared.Map.Components;
|
2020-10-08 17:41:23 +02:00
|
|
|
|
2023-06-29 08:35:54 -04:00
|
|
|
namespace Content.Shared.Coordinates.Helpers
|
2020-10-08 17:41:23 +02:00
|
|
|
{
|
|
|
|
|
public static class SnapgridHelper
|
|
|
|
|
{
|
2021-12-08 17:17:12 +01:00
|
|
|
public static EntityCoordinates SnapToGrid(this EntityCoordinates coordinates, IEntityManager? entMan = null, IMapManager? mapManager = null)
|
2020-10-08 17:41:23 +02:00
|
|
|
{
|
2021-12-08 17:17:12 +01:00
|
|
|
IoCManager.Resolve(ref entMan, ref mapManager);
|
2020-10-08 17:41:23 +02:00
|
|
|
|
2023-07-06 16:43:49 +12:00
|
|
|
var gridId = coordinates.GetGridUid(entMan);
|
2020-10-08 17:41:23 +02:00
|
|
|
|
2023-07-06 16:43:49 +12:00
|
|
|
if (gridId == null)
|
2020-10-08 17:41:23 +02:00
|
|
|
{
|
2023-07-06 16:43:49 +12:00
|
|
|
var xformSys = entMan.System<SharedTransformSystem>();
|
|
|
|
|
var mapPos = coordinates.ToMap(entMan, xformSys);
|
|
|
|
|
var mapX = (int)Math.Floor(mapPos.X) + 0.5f;
|
|
|
|
|
var mapY = (int)Math.Floor(mapPos.Y) + 0.5f;
|
|
|
|
|
mapPos = new MapCoordinates(new Vector2(mapX, mapY), mapPos.MapId);
|
|
|
|
|
return EntityCoordinates.FromMap(coordinates.EntityId, mapPos, xformSys);
|
2020-10-08 17:41:23 +02:00
|
|
|
}
|
|
|
|
|
|
2024-03-22 03:08:40 -04:00
|
|
|
var grid = entMan.GetComponent<MapGridComponent>(gridId.Value);
|
2023-07-06 16:43:49 +12:00
|
|
|
var tileSize = grid.TileSize;
|
|
|
|
|
var localPos = coordinates.WithEntityId(gridId.Value).Position;
|
2021-04-28 10:49:37 -07:00
|
|
|
var x = (int)Math.Floor(localPos.X / tileSize) + tileSize / 2f;
|
|
|
|
|
var y = (int)Math.Floor(localPos.Y / tileSize) + tileSize / 2f;
|
2023-07-06 16:43:49 +12:00
|
|
|
var gridPos = new EntityCoordinates(gridId.Value, new Vector2(x, y));
|
|
|
|
|
return gridPos.WithEntityId(coordinates.EntityId);
|
2020-10-08 17:41:23 +02:00
|
|
|
}
|
2021-04-01 14:34:35 -07:00
|
|
|
|
2022-11-22 13:12:04 +11:00
|
|
|
public static EntityCoordinates SnapToGrid(this EntityCoordinates coordinates, MapGridComponent grid)
|
2021-04-01 14:34:35 -07:00
|
|
|
{
|
|
|
|
|
var tileSize = grid.TileSize;
|
|
|
|
|
|
|
|
|
|
var localPos = coordinates.Position;
|
|
|
|
|
|
2021-04-28 10:49:37 -07:00
|
|
|
var x = (int)Math.Floor(localPos.X / tileSize) + tileSize / 2f;
|
|
|
|
|
var y = (int)Math.Floor(localPos.Y / tileSize) + tileSize / 2f;
|
2021-04-01 14:34:35 -07:00
|
|
|
|
|
|
|
|
return new EntityCoordinates(coordinates.EntityId, x, y);
|
|
|
|
|
}
|
2020-10-08 17:41:23 +02:00
|
|
|
}
|
|
|
|
|
}
|