using Content.Shared.Physics;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.Interfaces.Physics;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Content.Shared.Maps
{
public static class TurfHelpers
{
///
/// Checks if a turf has something dense on it.
///
public static bool IsBlockedTurf(this TileRef turf, bool filterMobs)
{
var physics = IoCManager.Resolve();
var worldBox = GetWorldTileBox(turf);
var query = physics.GetCollidingEntities(turf.MapIndex, in worldBox);
foreach (var body in query)
{
if (body.CanCollide && body.Hard && (body.CollisionLayer & (int) CollisionGroup.Impassable) != 0)
return false;
if (filterMobs && (body.CollisionLayer & (int) CollisionGroup.MobMask) != 0)
return false;
}
return true;
}
///
/// Creates a box the size of a tile, at the same position in the world as the tile.
///
private static Box2 GetWorldTileBox(TileRef turf)
{
var map = IoCManager.Resolve();
var tileGrid = map.GetGrid(turf.GridIndex);
var tileBox = Box2.UnitCentered.Scale(tileGrid.TileSize);
return tileBox.Translated(tileGrid.GridTileToWorldPos(turf.GridIndices));
}
///
/// Creates a box the size of a tile.
///
private static Box2 GetTileBox(this TileRef turf)
{
var map = IoCManager.Resolve();
var tileGrid = map.GetGrid(turf.GridIndex);
return Box2.UnitCentered.Scale(tileGrid.TileSize);
}
}
}