43 lines
1.1 KiB
C#
43 lines
1.1 KiB
C#
using System.Numerics;
|
|
using Robust.Shared.Map;
|
|
using Robust.Shared.Map.Components;
|
|
|
|
namespace Content.Shared.Random.Rules;
|
|
|
|
/// <summary>
|
|
/// Returns true if on a grid or in range of one.
|
|
/// </summary>
|
|
public sealed partial class GridInRangeRule : RulesRule
|
|
{
|
|
[DataField]
|
|
public float Range = 10f;
|
|
|
|
private List<Entity<MapGridComponent>> _grids = [];
|
|
|
|
public override bool Check(EntityManager entManager, EntityUid uid)
|
|
{
|
|
if (!entManager.TryGetComponent(uid, out TransformComponent? xform))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (xform.GridUid != null)
|
|
{
|
|
return !Inverted;
|
|
}
|
|
|
|
var transform = entManager.System<SharedTransformSystem>();
|
|
var mapManager = IoCManager.Resolve<IMapManager>();
|
|
|
|
var worldPos = transform.GetWorldPosition(xform);
|
|
var gridRange = new Vector2(Range, Range);
|
|
|
|
_grids.Clear();
|
|
mapManager.FindGridsIntersecting(xform.MapID, new Box2(worldPos - gridRange, worldPos + gridRange), ref _grids);
|
|
if (_grids.Count > 0)
|
|
return !Inverted;
|
|
|
|
return Inverted;
|
|
}
|
|
}
|