forked from space-wizards/space-station-14
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnapgridHelper.cs
48 lines (36 loc) · 1.65 KB
/
SnapgridHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using Robust.Shared.Map;
namespace Content.Server.Coordinates.Helpers
{
public static class SnapgridHelper
{
public static void SnapToGrid(this EntityUid entity, IEntityManager? entMan = null, IMapManager? mapManager = null)
{
IoCManager.Resolve(ref entMan, ref mapManager);
var transform = entMan.GetComponent<TransformComponent>(entity);
transform.Coordinates = transform.Coordinates.SnapToGrid(entMan, mapManager);
}
public static EntityCoordinates SnapToGrid(this EntityCoordinates coordinates, IEntityManager? entMan = null, IMapManager? mapManager = null)
{
IoCManager.Resolve(ref entMan, ref mapManager);
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);
}
public static EntityCoordinates SnapToGrid(this EntityCoordinates coordinates, IMapGrid 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);
}
}
}