Data-oriented Construction System (#2152)
- Powerful - Data-oriented - Approved by PJB - Powered by node graphs and AI pathfinding - Coded by the same nerd who brought you atmos Co-authored-by: Exp <theexp111@gmail.com>
This commit is contained in:
committed by
GitHub
parent
a6647e8de1
commit
745401a41e
43
Content.Server/Utility/SnapgridHelper.cs
Normal file
43
Content.Server/Utility/SnapgridHelper.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.Components.Transform;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Interfaces.Map;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Maths;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user