Anchorable shared (#17422)
Co-authored-by: BenOwnby <ownbyb@appstate.edu> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
This commit is contained in:
42
Content.Shared/Coordinates/Helpers/SnapgridHelper.cs
Normal file
42
Content.Shared/Coordinates/Helpers/SnapgridHelper.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Map.Components;
|
||||
|
||||
namespace Content.Shared.Coordinates.Helpers
|
||||
{
|
||||
public static class SnapgridHelper
|
||||
{
|
||||
public static EntityCoordinates SnapToGrid(this EntityCoordinates coordinates, IEntityManager? entMan = null, IMapManager? mapManager = null)
|
||||
{
|
||||
IoCManager.Resolve(ref entMan, ref mapManager);
|
||||
|
||||
var gridIdOpt = coordinates.GetGridUid(entMan);
|
||||
|
||||
var tileSize = 1f;
|
||||
|
||||
if (gridIdOpt is EntityUid gridId && gridId.IsValid())
|
||||
{
|
||||
var grid = mapManager.GetGrid(gridId);
|
||||
tileSize = grid.TileSize;
|
||||
}
|
||||
|
||||
var localPos = coordinates.Position;
|
||||
|
||||
var x = (int)Math.Floor(localPos.X / tileSize) + tileSize / 2f;
|
||||
var y = (int)Math.Floor(localPos.Y / tileSize) + tileSize / 2f;
|
||||
|
||||
return new EntityCoordinates(coordinates.EntityId, x, y);
|
||||
}
|
||||
|
||||
public static EntityCoordinates SnapToGrid(this EntityCoordinates coordinates, MapGridComponent grid)
|
||||
{
|
||||
var tileSize = grid.TileSize;
|
||||
|
||||
var localPos = coordinates.Position;
|
||||
|
||||
var x = (int)Math.Floor(localPos.X / tileSize) + tileSize / 2f;
|
||||
var y = (int)Math.Floor(localPos.Y / tileSize) + tileSize / 2f;
|
||||
|
||||
return new EntityCoordinates(coordinates.EntityId, x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user