44 lines
1.7 KiB
C#
44 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Content.Server.AI.Components;
|
|
using Content.Shared.Physics;
|
|
using Robust.Shared.GameObjects;
|
|
using Robust.Shared.IoC;
|
|
using Robust.Shared.Map;
|
|
using Robust.Shared.Maths;
|
|
using Robust.Shared.Physics;
|
|
using Robust.Shared.Physics.Broadphase;
|
|
|
|
namespace Content.Server.AI.Utils
|
|
{
|
|
public static class Visibility
|
|
{
|
|
// Should this be in robust or something? Fark it
|
|
public static IEnumerable<IEntity> GetNearestEntities(EntityCoordinates grid, Type component, float range)
|
|
{
|
|
var inRange = GetEntitiesInRange(grid, component, range).ToList();
|
|
var sortedInRange = inRange.OrderBy(o => (IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(o.Uid).Coordinates.Position - grid.Position).Length);
|
|
|
|
return sortedInRange;
|
|
}
|
|
|
|
public static IEnumerable<IEntity> GetEntitiesInRange(EntityCoordinates grid, Type component, float range)
|
|
{
|
|
var entityManager = IoCManager.Resolve<IEntityManager>();
|
|
foreach (var entity in entityManager.GetAllComponents(component).Select(c => c.Owner))
|
|
{
|
|
if (IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(entity.Uid).Coordinates.GetGridId(entityManager) != grid.GetGridId(entityManager))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if ((IoCManager.Resolve<IEntityManager>().GetComponent<TransformComponent>(entity.Uid).Coordinates.Position - grid.Position).Length <= range)
|
|
{
|
|
yield return entity;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|