Files
crystall-punk-14/Content.Server/AI/Utils/Visibility.cs

38 lines
1.4 KiB
C#
Raw Normal View History

using System.Linq;
using Robust.Shared.Map;
namespace Content.Server.AI.Utils
{
public static class Visibility
{
// Should this be in robust or something? Fark it
2021-12-05 18:09:01 +01:00
public static IEnumerable<EntityUid> GetNearestEntities(EntityCoordinates grid, Type component, float range)
{
2021-12-08 17:04:21 +01:00
var entMan = IoCManager.Resolve<IEntityManager>();
var inRange = GetEntitiesInRange(grid, component, range).ToList();
2021-12-08 17:04:21 +01:00
var sortedInRange = inRange.OrderBy(o => (entMan.GetComponent<TransformComponent>(o).Coordinates.Position - grid.Position).Length);
return sortedInRange;
}
2021-12-05 18:09:01 +01:00
public static IEnumerable<EntityUid> GetEntitiesInRange(EntityCoordinates grid, Type component, float range)
{
var entityManager = IoCManager.Resolve<IEntityManager>();
foreach (var entity in entityManager.GetAllComponents(component).Select(c => c.Owner))
{
2021-12-08 17:04:21 +01:00
var transform = entityManager.GetComponent<TransformComponent>(entity);
2022-06-20 12:14:35 +12:00
if (transform.Coordinates.GetGridUid(entityManager) != grid.GetGridUid(entityManager))
{
continue;
}
2021-12-08 17:04:21 +01:00
if ((transform.Coordinates.Position - grid.Position).Length <= range)
{
yield return entity;
}
}
}
}
}