using Robust.Shared.Interfaces.GameObjects;
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.Utility;
namespace Robust.Shared.GameObjects
{
///
/// An entity query that will let all entities pass.
/// This is the same as matching ITransformComponent, but faster.
///
[PublicAPI]
public class AllEntityQuery : IEntityQuery
{
///
public bool Match(IEntity entity) => true;
///
public IEnumerable Match(IEntityManager entityMan)
{
return entityMan.GetEntities();
}
}
///
/// An entity query which will match entities based on a predicate.
/// If you only want a single type of Component, use TypeEntityQuery.
///
[PublicAPI]
public class PredicateEntityQuery : IEntityQuery
{
private readonly Predicate Predicate;
///
/// Constructs a new instance of PredicateEntityQuery.
///
///
public PredicateEntityQuery(Predicate predicate)
{
if (predicate == null)
throw new ArgumentNullException(nameof(predicate));
Predicate = predicate;
}
///
public bool Match(IEntity entity) => Predicate(entity);
///
public IEnumerable Match(IEntityManager entityMan)
{
return entityMan.GetEntities().Where(entity => Predicate(entity));
}
}
///
/// An entity query that will match one type of component.
/// This the fastest and most common query, and should be the default choice.
///
[PublicAPI]
public class TypeEntityQuery : IEntityQuery
{
private readonly Type ComponentType;
///
/// Constructs a new instance of TypeEntityQuery.
///
/// Type of the component to match.
public TypeEntityQuery(Type componentType)
{
DebugTools.Assert(typeof(IComponent).IsAssignableFrom(componentType), "componentType must inherit IComponent");
ComponentType = componentType;
}
///
public bool Match(IEntity entity) => entity.HasComponent(ComponentType);
///
public IEnumerable Match(IEntityManager entityMan)
{
return entityMan.ComponentManager.GetAllComponents(ComponentType).Select(component => component.Owner);
}
}
///
/// An entity query that will match one type of component.
/// This the fastest and most common query, and should be the default choice.
///
/// Type of component to match.
[PublicAPI]
public class TypeEntityQuery : IEntityQuery where T : IComponent
{
public bool Match(IEntity entity) => entity.HasComponent();
public IEnumerable Match(IEntityManager entityMan)
{
return entityMan.ComponentManager.GetAllComponents().Select(component => component.Owner);
}
}
///
/// An entity query that will match all entities that intersect with the argument entity.
///
[PublicAPI]
public class IntersectingEntityQuery : IEntityQuery
{
private readonly IEntity Entity;
///
/// Constructs a new instance of TypeEntityQuery.
///
/// Type of the component to match.
public IntersectingEntityQuery(IEntity entity)
{
Entity = entity;
}
///
public bool Match(IEntity entity)
{
if(Entity.TryGetComponent(out var collidable))
{
return collidable.MapID == entity.Transform.MapID && collidable.WorldAABB.Contains(entity.Transform.WorldPosition);
}
return false;
}
public IEnumerable Match(IEntityManager entityMan)
{
return entityMan.GetEntities().Where(entity => Match(entity));
}
}
}