Files
tbd-station-14/Content.Server/Utility/SnapgridHelper.cs
Acruid ca4fd649fe Massive Namespace Cleanup (#3120)
* Engine namespace changes.

* Automated remove redundant using statements.

* Simplified Graphics namespace.

* Apparently the container system stores full type names in the map file.😞 This updates those names.

* API Changes to LocalizationManager.LoadCulture.

* Update submodule to v0.3.2
2021-02-11 01:13:03 -08:00

40 lines
1.4 KiB
C#

using System;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
namespace Content.Server.Utility
{
public static class SnapgridHelper
{
public static void SnapToGrid(this IEntity entity, SnapGridOffset offset = SnapGridOffset.Center, IEntityManager entityManager = null, IMapManager mapManager = null)
{
entity.Transform.Coordinates = entity.Transform.Coordinates.SnapToGrid(offset, entityManager, mapManager);
}
public static EntityCoordinates SnapToGrid(this EntityCoordinates coordinates,
SnapGridOffset offset = SnapGridOffset.Center, IEntityManager entityManager = null, IMapManager mapManager = null)
{
entityManager ??= IoCManager.Resolve<IEntityManager>();
mapManager ??= IoCManager.Resolve<IMapManager>();
var gridId = coordinates.GetGridId(entityManager);
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 / (offset == SnapGridOffset.Center ? 2f : 0f);
var y = (int)Math.Floor(localPos.Y / tileSize) + tileSize / (offset == SnapGridOffset.Center ? 2f : 0f);
return new EntityCoordinates(coordinates.EntityId, x, y);
}
}
}