803 lines
34 KiB
C#
803 lines
34 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Encompass.Exceptions;
|
|
using MoonTools.FastCollections;
|
|
|
|
namespace Encompass
|
|
{
|
|
/// <summary>
|
|
/// Engines are the Encompass notion of an ECS System.
|
|
/// They are responsible for reading the World state, reading messages, emitting messages, and creating or mutating Entities and Components.
|
|
/// Engines run once per World Update.
|
|
/// </summary>
|
|
public abstract class Engine : IEquatable<Engine>
|
|
{
|
|
internal Guid ID;
|
|
|
|
internal readonly HashSet<Type> readTypes = new HashSet<Type>();
|
|
internal readonly HashSet<Type> readImmediateTypes = new HashSet<Type>();
|
|
internal readonly HashSet<Type> sendTypes = new HashSet<Type>();
|
|
internal readonly HashSet<Type> receiveTypes = new HashSet<Type>();
|
|
internal readonly HashSet<Type> writeTypes = new HashSet<Type>();
|
|
internal readonly HashSet<Type> writeImmediateTypes = new HashSet<Type>();
|
|
internal readonly HashSet<Type> queryWithTypes = new HashSet<Type>();
|
|
internal readonly HashSet<Type> queryWithoutTypes = new HashSet<Type>();
|
|
internal readonly Dictionary<Type, int> writePriorities = new Dictionary<Type, int>();
|
|
internal readonly int defaultWritePriority = 0;
|
|
|
|
/// <summary>
|
|
/// If false, the Engine will ignore time dilation.
|
|
/// </summary>
|
|
internal bool usesTimeDilation = true;
|
|
public bool TimeDilationActive { get => usesTimeDilation && timeManager.TimeDilationActive; }
|
|
|
|
private EntityManager entityManager;
|
|
private MessageManager messageManager;
|
|
private ComponentManager componentManager;
|
|
private TimeManager timeManager;
|
|
private TrackingManager trackingManager;
|
|
|
|
private EntitySetQuery entityQuery;
|
|
|
|
private HashSet<int> _trackedEntities = new HashSet<int>();
|
|
protected IEnumerable<Entity> TrackedEntities
|
|
{
|
|
get
|
|
{
|
|
foreach (var entityID in _trackedEntities)
|
|
{
|
|
yield return entityManager.GetEntity(entityID);
|
|
}
|
|
}
|
|
}
|
|
|
|
private HashSet<int> _newlyCreatedEntities = new HashSet<int>();
|
|
|
|
protected Engine()
|
|
{
|
|
ID = Guid.NewGuid();
|
|
|
|
var sendsAttribute = GetType().GetCustomAttribute<Sends>(false);
|
|
if (sendsAttribute != null)
|
|
{
|
|
sendTypes = sendsAttribute.sendTypes;
|
|
}
|
|
|
|
var activatesAttribute = GetType().GetCustomAttribute<WritesImmediate>(false);
|
|
if (activatesAttribute != null)
|
|
{
|
|
writeImmediateTypes = activatesAttribute.writeImmediateTypes;
|
|
}
|
|
|
|
var defaultWritePriorityAttribute = GetType().GetCustomAttribute<DefaultWritePriority>(false);
|
|
|
|
if (defaultWritePriorityAttribute != null)
|
|
{
|
|
defaultWritePriority = defaultWritePriorityAttribute.writePriority;
|
|
}
|
|
|
|
foreach (var writesAttribute in GetType().GetCustomAttributes<Writes>(false))
|
|
{
|
|
writeTypes.UnionWith(writesAttribute.writeTypes);
|
|
writePriorities = new Dictionary<Type, int>[2] { writePriorities, writesAttribute.priorities }.SelectMany(dict => dict).ToDictionary(pair => pair.Key, pair => pair.Value);
|
|
}
|
|
|
|
var receivesAttribute = GetType().GetCustomAttribute<Receives>(false);
|
|
if (receivesAttribute != null)
|
|
{
|
|
receiveTypes = receivesAttribute.receiveTypes;
|
|
}
|
|
|
|
var readsAttribute = GetType().GetCustomAttribute<Reads>(false);
|
|
if (readsAttribute != null)
|
|
{
|
|
readTypes = readsAttribute.readTypes;
|
|
}
|
|
|
|
var readsImmediateAttribute = GetType().GetCustomAttribute<ReadsImmediate>(false);
|
|
if (readsImmediateAttribute != null)
|
|
{
|
|
readImmediateTypes = readsImmediateAttribute.readImmediateTypes;
|
|
}
|
|
|
|
var queryWithAttribute = GetType().GetCustomAttribute<QueryWith>(false);
|
|
if (queryWithAttribute != null)
|
|
{
|
|
queryWithTypes = queryWithAttribute.queryWithTypes;
|
|
}
|
|
|
|
var queryWithoutAttribute = GetType().GetCustomAttribute<QueryWithout>(false);
|
|
if (queryWithoutAttribute != null)
|
|
{
|
|
queryWithoutTypes = queryWithoutAttribute.queryWithoutTypes;
|
|
}
|
|
}
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
if (obj is Engine engine)
|
|
{
|
|
return Equals(engine);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public bool Equals(Engine other)
|
|
{
|
|
return other.ID == ID;
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
return ID.GetHashCode();
|
|
}
|
|
|
|
internal void AssignEntityManager(EntityManager entityManager)
|
|
{
|
|
this.entityManager = entityManager;
|
|
}
|
|
|
|
internal void AssignComponentManager(ComponentManager componentManager)
|
|
{
|
|
this.componentManager = componentManager;
|
|
}
|
|
|
|
internal void AssignMessageManager(MessageManager messageManager)
|
|
{
|
|
this.messageManager = messageManager;
|
|
}
|
|
|
|
internal void AssignTimeManager(TimeManager timeManager)
|
|
{
|
|
this.timeManager = timeManager;
|
|
}
|
|
|
|
internal void AssignTrackingManager(TrackingManager trackingManager)
|
|
{
|
|
this.trackingManager = trackingManager;
|
|
}
|
|
|
|
internal void CheckMessageRead<TMessage>() where TMessage : struct, IMessage
|
|
{
|
|
if (!receiveTypes.Contains(typeof(TMessage)))
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
|
|
}
|
|
}
|
|
|
|
private bool EntityCreatedThisFrame(int entityID)
|
|
{
|
|
return _newlyCreatedEntities.Contains(entityID);
|
|
}
|
|
|
|
internal void ClearNewlyCreatedEntities()
|
|
{
|
|
_newlyCreatedEntities.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Runs once per World update with the calculated delta-time.
|
|
/// </summary>
|
|
/// <param name="dt">The time in seconds that has elapsed since the previous frame.</param>
|
|
public abstract void Update(double dt);
|
|
|
|
/// <summary>
|
|
/// Creates and returns a new empty Entity.
|
|
/// </summary>
|
|
protected Entity CreateEntity()
|
|
{
|
|
var entity = entityManager.CreateEntity();
|
|
_newlyCreatedEntities.Add(entity.ID);
|
|
return entity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if an Entity with the specified ID exists.
|
|
/// </summary>
|
|
protected bool EntityExists(Entity entity)
|
|
{
|
|
return entityManager.EntityExists(entity.ID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if an Entity with the specified ID exists.
|
|
/// </summary>
|
|
protected bool EntityExists(int entityID)
|
|
{
|
|
return entityManager.EntityExists(entityID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns an Entity containing the specified Component type.
|
|
/// </summary>
|
|
protected Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return entityManager.GetEntity(ReadComponentHelper<TComponent>().Item2);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all Entities containing the specified Component type.
|
|
/// </summary>
|
|
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
foreach (var pair in ReadComponentsHelper<TComponent>())
|
|
{
|
|
yield return entityManager.GetEntity(pair.Item2);
|
|
}
|
|
}
|
|
|
|
// these next two are for the ComponentMessageEmitter only
|
|
|
|
internal IEnumerable<TComponent> ReadComponentsFromWorld<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return componentManager.GetComponentsByType<TComponent>();
|
|
}
|
|
|
|
private IEnumerable<(TComponent, int)> ReadComponentsHelper<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
|
|
var existingRead = readTypes.Contains(typeof(TComponent));
|
|
if (existingRead && immediateRead)
|
|
{
|
|
return componentManager.ReadExistingAndImmediateComponentsByType<TComponent>();
|
|
}
|
|
else if (existingRead)
|
|
{
|
|
return componentManager.GetComponentsIncludingEntity<TComponent>();
|
|
}
|
|
else if (immediateRead)
|
|
{
|
|
return componentManager.ReadImmediateComponentsByType<TComponent>();
|
|
}
|
|
else
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all of the Components with the specified Component Type.
|
|
/// </summary>
|
|
protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
foreach (var pair in ReadComponentsHelper<TComponent>())
|
|
{
|
|
yield return pair.Item1;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all of the components of the specified type including an Entity reference for each Component.
|
|
/// </summary>
|
|
protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
foreach (var (component, id) in ReadComponentsHelper<TComponent>())
|
|
{
|
|
yield return (component, entityManager.GetEntity(id));
|
|
}
|
|
}
|
|
|
|
private (TComponent, int) ReadComponentHelper<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
|
|
var existingRead = readTypes.Contains(typeof(TComponent));
|
|
if (existingRead && immediateRead)
|
|
{
|
|
return componentManager.ReadFirstExistingOrImmediateComponentByType<TComponent>();
|
|
}
|
|
else if (existingRead)
|
|
{
|
|
return componentManager.ReadFirstExistingComponentByType<TComponent>();
|
|
}
|
|
else if (immediateRead)
|
|
{
|
|
return componentManager.ReadFirstImmediateComponentByType<TComponent>();
|
|
}
|
|
else
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned.
|
|
/// </summary>
|
|
protected TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return ReadComponentHelper<TComponent>().Item1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a component of the specified type including its Entity reference. If multiples exist, an arbitrary Component is returned.
|
|
/// </summary>
|
|
protected (TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
var (component, id) = ReadComponentHelper<TComponent>();
|
|
return (component, entityManager.GetEntity(id));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if any Component with the specified Component Type exists.
|
|
/// </summary>
|
|
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
|
|
var existingRead = readTypes.Contains(typeof(TComponent));
|
|
if (existingRead && immediateRead)
|
|
{
|
|
return componentManager.SomeExistingOrImmediateComponent<TComponent>();
|
|
}
|
|
else if (existingRead)
|
|
{
|
|
return componentManager.SomeExistingComponent<TComponent>();
|
|
}
|
|
else if (immediateRead)
|
|
{
|
|
return componentManager.SomeImmediateComponent<TComponent>();
|
|
}
|
|
else
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
}
|
|
|
|
private TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct, IComponent
|
|
{
|
|
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
|
|
var existingRead = readTypes.Contains(typeof(TComponent));
|
|
if (existingRead && immediateRead)
|
|
{
|
|
return componentManager.ReadImmediateOrExistingComponentByEntityAndType<TComponent>(entityID);
|
|
}
|
|
else if (existingRead)
|
|
{
|
|
return componentManager.ReadExistingComponentByEntityAndType<TComponent>(entityID);
|
|
}
|
|
else if (immediateRead)
|
|
{
|
|
return componentManager.ReadImmediateComponentByEntityAndType<TComponent>(entityID);
|
|
}
|
|
else
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a Component with the specified Type that exists on the Entity.
|
|
/// </summary>
|
|
/// <exception cref="Encompass.Exceptions.NoComponentOfTypeOnEntityException">
|
|
/// Thrown when the Entity does not have a Component of the specified Type
|
|
/// </exception>
|
|
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
|
/// Thrown when the Engine does not declare that it reads the given Component Type.
|
|
/// </exception>
|
|
protected TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
return GetComponentHelper<TComponent>(entity.ID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if the Entity has a Component of the given Type.
|
|
/// </summary>
|
|
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
|
/// Thrown when the Engine does not declare that is Reads the given Component Type.
|
|
/// </exception>
|
|
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
|
|
var existingRead = readTypes.Contains(typeof(TComponent));
|
|
|
|
if (immediateRead && existingRead)
|
|
{
|
|
return componentManager.HasExistingOrImmediateComponent<TComponent>(entity.ID);
|
|
}
|
|
else if (existingRead)
|
|
{
|
|
return componentManager.HasExistingComponent<TComponent>(entity.ID);
|
|
}
|
|
else if (immediateRead)
|
|
{
|
|
return componentManager.HasImmediateComponent<TComponent>(entity.ID);
|
|
}
|
|
else
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if the Entity has a Component of the given Type.
|
|
/// </summary>
|
|
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
|
/// Thrown when the Engine does not declare that is Reads the given Component Type.
|
|
/// </exception>
|
|
protected bool HasComponent(Entity entity, Type type)
|
|
{
|
|
var immediateRead = readImmediateTypes.Contains(type);
|
|
var existingRead = readTypes.Contains(type);
|
|
|
|
if (immediateRead && existingRead)
|
|
{
|
|
return componentManager.HasExistingOrImmediateComponent(entity.ID, type);
|
|
}
|
|
else if (existingRead)
|
|
{
|
|
return componentManager.HasExistingComponent(entity.ID, type);
|
|
}
|
|
else if (immediateRead)
|
|
{
|
|
return componentManager.HasImmediateComponent(entity.ID, type);
|
|
}
|
|
else
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, type.Name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets Component data for the specified Component Type on the specified Entity. If Component data for this Type already existed on the Entity, the component data is overwritten.
|
|
/// </summary>
|
|
/// <exception cref="Encompass.Exceptions.IllegalWriteException">
|
|
/// Thrown when the Engine does not declare that it Writes the given Component Type.
|
|
/// </exception>
|
|
protected void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
|
{
|
|
var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : defaultWritePriority;
|
|
|
|
if (!writeTypes.Contains(typeof(TComponent)))
|
|
{
|
|
throw new IllegalWriteException("Engine {0} tried to update undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
|
|
bool written;
|
|
if (writeImmediateTypes.Contains(typeof(TComponent)))
|
|
{
|
|
written = componentManager.AddImmediateComponent(entity.ID, component, priority);
|
|
if (written)
|
|
{
|
|
trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
written = componentManager.UpdateComponent(entity.ID, component, priority);
|
|
}
|
|
|
|
if (!componentManager.HasExistingComponent<TComponent>(entity.ID))
|
|
{
|
|
trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
|
|
}
|
|
|
|
if (written && component is IDrawableComponent drawableComponent)
|
|
{
|
|
componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// An alternative to SetComponent that can be used for new Entities and does not require setting write priority.
|
|
/// </summary>
|
|
/// <exception cref="Encompass.Exceptions.IllegalWriteException">
|
|
/// Thrown when the Engine does not declare that it Writes the given Component Type.
|
|
/// </exception>
|
|
protected void AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
|
{
|
|
if (!EntityCreatedThisFrame(entity.ID))
|
|
{
|
|
throw new IllegalWriteException("AddComponent used on Entity that was not created in this context. Use SetComponent instead.");
|
|
}
|
|
|
|
if (writeImmediateTypes.Contains(typeof(TComponent)))
|
|
{
|
|
componentManager.AddImmediateComponent(entity.ID, component);
|
|
trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
|
|
}
|
|
else
|
|
{
|
|
componentManager.AddComponent(entity.ID, component);
|
|
}
|
|
|
|
trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
|
|
|
|
if (component is IDrawableComponent drawableComponent)
|
|
{
|
|
componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sends a Message.
|
|
/// </summary>
|
|
/// <exception cref="Encompass.Exceptions.IllegalSendException">
|
|
/// Thrown when the Engine does not declare that it Sends the Message Type.
|
|
/// </exception>
|
|
protected void SendMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
|
|
{
|
|
if (!sendTypes.Contains(typeof(TMessage)))
|
|
{
|
|
throw new IllegalSendException("Engine {0} tried to send undeclared Message {1}", GetType().Name, typeof(TMessage).Name);
|
|
}
|
|
|
|
messageManager.AddMessage(message);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sends a message after the specified number of seconds, respecting time dilation.
|
|
/// </summary>
|
|
/// <param name="time">The time in seconds that will elapse before the message is sent.</param>
|
|
protected void SendMessage<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
|
|
{
|
|
messageManager.AddMessage(message, time);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sends a message after the specified number of seconds, ignoring time dilation.
|
|
/// </summary>
|
|
/// <param name="time">The time in seconds that will elapse before the message is sent.</param>
|
|
protected void SendMessageIgnoringTimeDilation<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
|
|
{
|
|
messageManager.AddMessageIgnoringTimeDilation(message, time);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads all messages of the specified Type.
|
|
/// </summary>
|
|
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
|
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
|
|
/// </exception>
|
|
protected IEnumerable<TMessage> ReadMessages<TMessage>() where TMessage : struct, IMessage
|
|
{
|
|
CheckMessageRead<TMessage>();
|
|
return messageManager.GetMessagesByType<TMessage>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads an arbitrary message of the specified Type.
|
|
/// </summary>
|
|
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
|
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
|
|
/// </exception>
|
|
protected TMessage ReadMessage<TMessage>() where TMessage : struct, IMessage
|
|
{
|
|
CheckMessageRead<TMessage>();
|
|
return messageManager.First<TMessage>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if a Message of the specified Type has been sent this frame.
|
|
/// </summary>
|
|
/// <exception cref="Encompass.Exceptions.IllegalReadException">
|
|
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
|
|
/// </exception>
|
|
protected bool SomeMessage<TMessage>() where TMessage : struct, IMessage
|
|
{
|
|
CheckMessageRead<TMessage>();
|
|
return messageManager.Any<TMessage>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Destroys the specified Entity. This also removes all of the Components associated with the Entity.
|
|
/// Entity destruction takes place after all the Engines have been processed by World Update.
|
|
/// </summary>
|
|
protected void Destroy(Entity entity)
|
|
{
|
|
entityManager.MarkForDestroy(entity.ID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Destroys an arbitrary Entity containing a Component of the specified Type.
|
|
/// Entity destruction takes place after all the Engines have been processed by World Update.
|
|
/// </summary>
|
|
protected void DestroyWith<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
Destroy(ReadEntity<TComponent>());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Destroys all Entities containing a Component of the specified Type.
|
|
/// Entity destruction takes place after all the Engines have been processed by World Update.
|
|
/// </summary>
|
|
protected void DestroyAllWith<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
foreach (var entity in ReadEntities<TComponent>())
|
|
{
|
|
Destroy(entity);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes a Component with the specified type from the given Entity.
|
|
/// Note that the Engine must Read the Component type that is being removed.
|
|
/// If a Component with the specified type does not exist on the Entity, returns false and does not mutate the Entity.
|
|
/// </summary>
|
|
protected void RemoveComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : defaultWritePriority;
|
|
|
|
if (!writeTypes.Contains(typeof(TComponent)))
|
|
{
|
|
throw new IllegalWriteException("Engine {0} tried to remove undeclared Component {1}. Declare with Writes attribute.", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
|
|
if (writeImmediateTypes.Contains(typeof(TComponent)))
|
|
{
|
|
if (componentManager.RemoveImmediate<TComponent>(entity.ID, priority))
|
|
{
|
|
trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
componentManager.Remove<TComponent>(entity.ID, priority);
|
|
}
|
|
|
|
if (componentManager.HasExistingComponent<TComponent>(entity.ID))
|
|
{
|
|
trackingManager.RegisterRemoval(entity.ID, typeof(TComponent));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Activates the Encompass time dilation system.
|
|
/// Engines that have the IgnoresTimeDilation property will ignore all time dilation.
|
|
/// If multiple time dilations are active they will be averaged.
|
|
/// </summary>
|
|
/// <param name="factor">The time dilation factor, which is multiplied by real delta time.</param>
|
|
/// <param name="easeInTime">The time that will elapse before time is fully dilated, in real time.</param>
|
|
/// <param name="activeTime">The length of real time that time will be fully dilated.</param>
|
|
/// <param name="easeOutTime">The time that will elapse before time is fully undilated.</param>
|
|
protected void ActivateTimeDilation(double factor, double easeInTime, double activeTime, double easeOutTime)
|
|
{
|
|
timeManager.ActivateTimeDilation(factor, easeInTime, activeTime, easeOutTime);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Activates the Encompass time dilation system.
|
|
/// Engines that have the IgnoresTimeDilation property will ignore all time dilation.
|
|
/// If multiple time dilations are active they will be averaged.
|
|
/// </summary>
|
|
/// <param name="factor">The time dilation factor, which is multiplied by real delta time.</param>
|
|
/// <param name="easeInTime">The time that will elapse before time is fully dilated, in real time.</param>
|
|
/// <param name="easeInFunction">An easing function for the easing in of time dilation.</param>
|
|
/// <param name="activeTime">The length of real time that time will be fully dilated.</param>
|
|
/// <param name="easeOutTime">The time that will elapse before time is fully undilated.</param>
|
|
protected void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime)
|
|
{
|
|
timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Activates the Encompass time dilation system.
|
|
/// Engines that have the IgnoresTimeDilation property will ignore all time dilation.
|
|
/// If multiple time dilations are active they will be averaged.
|
|
/// </summary>
|
|
/// <param name="factor">The time dilation factor, which is multiplied by real delta time.</param>
|
|
/// <param name="easeInTime">The time that will elapse before time is fully dilated, in real time.</param>
|
|
/// <param name="activeTime">The length of real time that time will be fully dilated.</param>
|
|
/// <param name="easeOutTime">The time that will elapse before time is fully undilated.</param>
|
|
/// <param name="easeOutFunction">An easing function for the easing out of time dilation.</param>
|
|
protected void ActivateTimeDilation(double factor, double easeInTime, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction)
|
|
{
|
|
timeManager.ActivateTimeDilation(factor, easeInTime, activeTime, easeOutTime, easeOutFunction);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Activates the Encompass time dilation system.
|
|
/// Engines that have the IgnoresTimeDilation property will ignore all time dilation.
|
|
/// If multiple time dilations are active they will be averaged.
|
|
/// </summary>
|
|
/// <param name="factor">The time dilation factor, which is multiplied by real delta time.</param>
|
|
/// <param name="easeInTime">The time that will elapse before time is fully dilated, in real time.</param>
|
|
/// <param name="easeInFunction">An easing function for the easing in of time dilation.</param>
|
|
/// <param name="activeTime">The length of real time that time will be fully dilated.</param>
|
|
/// <param name="easeOutTime">The time that will elapse before time is fully undilated.</param>
|
|
/// <param name="easeOutFunction">An easing function for the easing out of time dilation.</param>
|
|
protected void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction)
|
|
{
|
|
timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime, easeOutFunction);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Efficiently reads Messages of a given type that all reference the given Entity.
|
|
/// </summary>
|
|
/// <typeparam name="TMessage">The Message subtype.</typeparam>
|
|
/// <param name="entity">The entity that all messages in the IEnumerable refer to.</param>
|
|
/// <returns></returns>
|
|
protected IEnumerable<TMessage> ReadMessagesWithEntity<TMessage>(Entity entity) where TMessage : struct, IMessage, IHasEntity
|
|
{
|
|
CheckMessageRead<TMessage>();
|
|
return messageManager.WithEntity<TMessage>(entity.ID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Efficiently reads a single Message of a given type that references a given Entity.
|
|
/// It is recommended to use this method in conjunction with SomeMessageWithEntity to prevent errors.
|
|
/// </summary>
|
|
protected TMessage ReadMessageWithEntity<TMessage>(Entity entity) where TMessage : struct, IMessage, IHasEntity
|
|
{
|
|
CheckMessageRead<TMessage>();
|
|
return messageManager.WithEntitySingular<TMessage>(entity.ID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Efficiently checks if any Message of a given type referencing a given Entity exists.
|
|
/// </summary>
|
|
protected bool SomeMessageWithEntity<TMessage>(Entity entity) where TMessage : struct, IMessage, IHasEntity
|
|
{
|
|
CheckMessageRead<TMessage>();
|
|
return messageManager.SomeWithEntity<TMessage>(entity.ID);
|
|
}
|
|
|
|
internal void CheckAndUpdateTracking(int entityID)
|
|
{
|
|
if (_trackedEntities.Contains(entityID) && !entityQuery.CheckEntity(entityID, componentManager.ExistingBits))
|
|
{
|
|
_trackedEntities.Remove(entityID);
|
|
}
|
|
else if (!_trackedEntities.Contains(entityID) && entityQuery.CheckEntity(entityID, componentManager.ExistingBits))
|
|
{
|
|
_trackedEntities.Add(entityID);
|
|
}
|
|
}
|
|
|
|
internal void ImmediateCheckAndUpdateTracking(int entityID)
|
|
{
|
|
if (_trackedEntities.Contains(entityID) && !entityQuery.ImmediateCheckEntity(entityID, componentManager.ImmediateBits, componentManager.ExistingBits))
|
|
{
|
|
_trackedEntities.Remove(entityID);
|
|
}
|
|
else if (!_trackedEntities.Contains(entityID) && entityQuery.ImmediateCheckEntity(entityID, componentManager.ImmediateBits, componentManager.ExistingBits))
|
|
{
|
|
_trackedEntities.Add(entityID);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns an empty EntitySetQuery. Can be modified and iterated over to obtain Entities that fit the given criteria.
|
|
/// </summary>
|
|
internal void BuildEntityQuery()
|
|
{
|
|
var withMask = BitSet512.Zero;
|
|
foreach (var type in queryWithTypes)
|
|
{
|
|
withMask = withMask.Set(componentManager.TypeToIndex[type]);
|
|
}
|
|
|
|
var withoutMask = BitSet512.Zero;
|
|
foreach (var type in queryWithoutTypes)
|
|
{
|
|
withoutMask = withoutMask.Set(componentManager.TypeToIndex[type]);
|
|
}
|
|
|
|
var immediateMask = BitSet512.Zero;
|
|
foreach (var type in readImmediateTypes)
|
|
{
|
|
immediateMask = immediateMask.Set(componentManager.TypeToIndex[type]);
|
|
}
|
|
|
|
var existingMask = BitSet512.Zero;
|
|
foreach (var type in readTypes)
|
|
{
|
|
existingMask = existingMask.Set(componentManager.TypeToIndex[type]);
|
|
}
|
|
|
|
entityQuery = new EntitySetQuery(
|
|
withMask & immediateMask,
|
|
withMask & existingMask,
|
|
withoutMask & immediateMask,
|
|
withoutMask & existingMask,
|
|
~withMask
|
|
);
|
|
}
|
|
|
|
internal void RegisterDestroyedEntity(int entityID)
|
|
{
|
|
_trackedEntities.Remove(entityID);
|
|
}
|
|
}
|
|
}
|