mirror of
https://github.com/WWhiteDreamProject/wwdpublic.git
synced 2026-04-18 05:59:03 +03:00
Uses the following Cherry-Picks: https://github.com/space-wizards/space-station-14/pull/26994 https://github.com/space-wizards/space-station-14/pull/26518 https://github.com/space-wizards/space-station-14/pull/26279 https://github.com/space-wizards/space-station-14/pull/24946 https://github.com/space-wizards/space-station-14/pull/27188 Requires: https://github.com/Simple-Station/Einstein-Engines/pull/535 https://github.com/Simple-Station/Einstein-Engines/pull/534 --------- Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Co-authored-by: Jake Huxell <JakeHuxell@pm.me> Co-authored-by: Tayrtahn <tayrtahn@gmail.com> Co-authored-by: 0x6273 <0x40@keemail.me> Co-authored-by: DEATHB4DEFEAT <zachcaffee@outlook.com>
47 lines
1.9 KiB
C#
47 lines
1.9 KiB
C#
using System.Numerics;
|
|
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 gridId = coordinates.GetGridUid(entMan);
|
|
|
|
if (gridId == null)
|
|
{
|
|
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);
|
|
}
|
|
|
|
var grid = entMan.GetComponent<MapGridComponent>(gridId.Value);
|
|
var tileSize = grid.TileSize;
|
|
var localPos = coordinates.WithEntityId(gridId.Value).Position;
|
|
var x = (int)Math.Floor(localPos.X / tileSize) + tileSize / 2f;
|
|
var y = (int)Math.Floor(localPos.Y / tileSize) + tileSize / 2f;
|
|
var gridPos = new EntityCoordinates(gridId.Value, new Vector2(x, y));
|
|
return gridPos.WithEntityId(coordinates.EntityId);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|