Files
crystall-punk-14/Content.Server/Coordinates/Helpers/SnapgridHelper.cs

52 lines
1.7 KiB
C#
Raw Normal View History

2021-04-01 14:34:35 -07:00
using System;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
2021-06-09 22:19:39 +02:00
namespace Content.Server.Coordinates.Helpers
{
public static class SnapgridHelper
{
2021-12-08 17:17:12 +01:00
public static void SnapToGrid(this EntityUid entity, IEntityManager? entMan = null, IMapManager? mapManager = null)
{
2021-12-08 17:17:12 +01:00
IoCManager.Resolve(ref entMan, ref mapManager);
var transform = entMan.GetComponent<TransformComponent>(entity);
transform.Coordinates = transform.Coordinates.SnapToGrid(entMan, mapManager);
}
2021-12-08 17:17:12 +01:00
public static EntityCoordinates SnapToGrid(this EntityCoordinates coordinates, IEntityManager? entMan = null, IMapManager? mapManager = null)
{
2021-12-08 17:17:12 +01:00
IoCManager.Resolve(ref entMan, ref mapManager);
2021-12-08 17:17:12 +01:00
var gridId = coordinates.GetGridId(entMan);
var tileSize = 1f;
if (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);
}
2021-04-01 14:34:35 -07:00
public static EntityCoordinates SnapToGrid(this EntityCoordinates coordinates, IMapGrid grid)
2021-04-01 14:34:35 -07:00
{
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;
2021-04-01 14:34:35 -07:00
return new EntityCoordinates(coordinates.EntityId, x, y);
}
}
}