using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using Robust.Shared.Physics;
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, Obsolete]
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, Obsolete]
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, Obsolete]
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, true).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, Obsolete]
public class TypeEntityQuery : IEntityQuery where T : IComponent
{
public bool Match(IEntity entity) => entity.HasComponent();
public IEnumerable Match(IEntityManager entityMan)
{
return entityMan.ComponentManager.EntityQuery(true).Select(component => component.Owner);
}
}
///
/// An entity query that will match all entities that intersect with the argument entity.
///
[PublicAPI, Obsolete]
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 physics))
{
return physics.Owner.Transform.MapID == entity.Transform.MapID && physics.GetWorldAABB().Contains(entity.Transform.WorldPosition);
}
return false;
}
public IEnumerable Match(IEntityManager entityMan)
{
return entityMan.GetEntities().Where(entity => Match(entity));
}
}
///
/// An entity query that will match entities that have all of the provided components.
///
[PublicAPI, Obsolete]
public class MultipleTypeEntityQuery : IEntityQuery
{
private readonly List ComponentTypes;
///
/// Constructs a new instance of MultipleTypeEntityQuery.
///
/// List of component types to match.
public MultipleTypeEntityQuery(List componentTypes)
{
foreach (var componentType in componentTypes)
{
DebugTools.Assert(typeof(IComponent).IsAssignableFrom(componentType), "componentType must inherit IComponent");
}
ComponentTypes = componentTypes;
}
///
public bool Match(IEntity entity)
{
foreach (var componentType in ComponentTypes)
{
if (!entity.HasComponent(componentType))
{
return false;
}
}
return true;
}
///
public IEnumerable Match(IEntityManager entityMan)
{
return entityMan.GetEntities(new TypeEntityQuery(ComponentTypes.First())).Where(entity => Match(entity));
}
}
}