679 lines
28 KiB
C#
679 lines
28 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Encompass.Exceptions;
|
|
|
|
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>
|
|
{
|
|
public Guid ID;
|
|
|
|
internal readonly HashSet<Type> sendTypes = new HashSet<Type>();
|
|
internal readonly HashSet<Type> receiveTypes = new HashSet<Type>();
|
|
internal readonly Dictionary<Type, int> writePriorities = new Dictionary<Type, int>();
|
|
|
|
private EntityManager entityManager;
|
|
private MessageManager messageManager;
|
|
private ComponentManager componentManager;
|
|
private ComponentMessageManager componentMessageManager;
|
|
|
|
protected Engine()
|
|
{
|
|
ID = Guid.NewGuid();
|
|
|
|
var sendsAttribute = GetType().GetCustomAttribute<Sends>(false);
|
|
if (sendsAttribute != null)
|
|
{
|
|
sendTypes = sendsAttribute.sendTypes;
|
|
}
|
|
|
|
var activatesAttribute = GetType().GetCustomAttribute<WritesPending>(false);
|
|
if (activatesAttribute != null)
|
|
{
|
|
sendTypes.UnionWith(activatesAttribute.writePendingTypes);
|
|
}
|
|
|
|
var writesAttributes = GetType().GetCustomAttributes<Writes>(false);
|
|
foreach (var writesAttribute in writesAttributes)
|
|
{
|
|
sendTypes.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)
|
|
{
|
|
receiveTypes.UnionWith(readsAttribute.readTypes);
|
|
}
|
|
|
|
var readsPendingAttribute = GetType().GetCustomAttribute<ReadsPending>(false);
|
|
if (readsPendingAttribute != null)
|
|
{
|
|
receiveTypes.UnionWith(readsPendingAttribute.readPendingTypes);
|
|
}
|
|
}
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
if (obj is Engine)
|
|
{
|
|
return this.Equals((Engine)obj);
|
|
}
|
|
|
|
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 AssignComponentMessageManager(ComponentMessageManager componentMessageManager)
|
|
{
|
|
this.componentMessageManager = componentMessageManager;
|
|
}
|
|
|
|
/// <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()
|
|
{
|
|
return entityManager.CreateEntity();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if an Entity with the specified ID exists.
|
|
/// </summary>
|
|
protected bool EntityExists(Guid entityID)
|
|
{
|
|
return entityManager.EntityExists(entityID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the Entity with the specified ID.
|
|
/// </summary>
|
|
/// <exception cref="Encompass.Exceptions.EntityNotFoundException">
|
|
/// Thrown when an Entity with the given ID does not exist.
|
|
/// </exception>
|
|
protected Entity GetEntity(Guid entityID)
|
|
{
|
|
return entityManager.GetEntity(entityID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the Entity ID associated with the specified Component Type and ID.
|
|
/// </summary>
|
|
protected Guid GetEntityIDByComponentID<TComponent>(Guid componentID) where TComponent : struct, IComponent
|
|
{
|
|
var pendingRead = receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>));
|
|
var existingRead = receiveTypes.Contains(typeof(ComponentMessage<TComponent>));
|
|
|
|
if (!pendingRead && !existingRead)
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
|
|
return componentMessageManager.GetEntityIDByComponentID(componentID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the Entity associated with the specified Component Type and ID.
|
|
/// </summary>
|
|
protected Entity GetEntityByComponentID<TComponent>(Guid componentID) where TComponent : struct, IComponent
|
|
{
|
|
return GetEntity(GetEntityIDByComponentID<TComponent>(componentID));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns an Entity containing the specified Component type.
|
|
/// </summary>
|
|
protected Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
var (id, component) = ReadComponent<TComponent>();
|
|
return GetEntityByComponentID<TComponent>(id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all Entities containing the specified Component type.
|
|
/// </summary>
|
|
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
foreach (var (id, component) in ReadComponents<TComponent>())
|
|
{
|
|
yield return GetEntityByComponentID<TComponent>(id);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the Component struct with the specified Component Type and ID.
|
|
/// </summary>
|
|
protected TComponent GetComponentByID<TComponent>(Guid componentID) where TComponent : struct, IComponent
|
|
{
|
|
var pendingRead = receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>));
|
|
var existingRead = receiveTypes.Contains(typeof(ComponentMessage<TComponent>));
|
|
|
|
if (!pendingRead && !existingRead)
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
|
|
if (componentMessageManager.GetComponentTypeByID(componentID) != typeof(TComponent))
|
|
{
|
|
throw new ComponentTypeMismatchException("Expected Component to be of type {0} but was actually of type {1}", typeof(TComponent).Name, componentMessageManager.GetComponentTypeByID(componentID).Name);
|
|
}
|
|
|
|
return (TComponent)componentMessageManager.GetComponentByID(componentID);
|
|
}
|
|
|
|
// these next two are for the ComponentMessageEmitter only
|
|
|
|
internal IEnumerable<(Guid, TComponent)> ReadComponentsFromWorld<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return componentManager.GetComponentsByType<TComponent>();
|
|
}
|
|
|
|
internal Entity ReadEntityFromWorld(Guid componentID)
|
|
{
|
|
return GetEntity(componentManager.GetEntityIDByComponentID(componentID));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all of the Components with the specified Component Type.
|
|
/// </summary>
|
|
protected IEnumerable<(Guid, TComponent)> ReadComponents<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
var pendingRead = receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>));
|
|
var existingRead = receiveTypes.Contains(typeof(ComponentMessage<TComponent>));
|
|
if (existingRead && pendingRead)
|
|
{
|
|
return componentMessageManager.ReadExistingAndPendingComponentsByType<TComponent>();
|
|
}
|
|
else if (existingRead)
|
|
{
|
|
return componentMessageManager.ReadExistingComponentsByType<TComponent>();
|
|
}
|
|
else if (pendingRead)
|
|
{
|
|
return componentMessageManager.ReadPendingComponentsByType<TComponent>();
|
|
}
|
|
else
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns all of the components of the specified type including an Entity reference for each Component.
|
|
/// </summary>
|
|
protected IEnumerable<(Guid, TComponent, Entity)> ReadComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return ReadComponents<TComponent>().Select((tuple) => (tuple.Item1, tuple.Item2, GetEntityByComponentID<TComponent>(tuple.Item1)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned.
|
|
/// </summary>
|
|
protected (Guid, TComponent) ReadComponent<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
var pendingRead = receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>));
|
|
var existingRead = receiveTypes.Contains(typeof(ComponentMessage<TComponent>));
|
|
if (existingRead && pendingRead)
|
|
{
|
|
return componentMessageManager.ReadFirstExistingOrPendingComponentByType<TComponent>();
|
|
}
|
|
else if (existingRead)
|
|
{
|
|
return componentMessageManager.ReadFirstExistingComponentByType<TComponent>();
|
|
}
|
|
else if (pendingRead)
|
|
{
|
|
return componentMessageManager.ReadFirstPendingComponentByType<TComponent>();
|
|
}
|
|
else
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a component of the specified type including its Entity reference. If multiples exist, an arbitrary Component is returned.
|
|
/// </summary>
|
|
protected (Guid, TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
var (id, component) = ReadComponent<TComponent>();
|
|
return (id, component, GetEntityByComponentID<TComponent>(id));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns true if any Component with the specified Component Type exists.
|
|
/// </summary>
|
|
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
var pendingRead = receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>));
|
|
var existingRead = receiveTypes.Contains(typeof(ComponentMessage<TComponent>));
|
|
if (existingRead && pendingRead)
|
|
{
|
|
return componentMessageManager.SomeExistingOrPendingComponent<TComponent>();
|
|
}
|
|
else if (existingRead)
|
|
{
|
|
return componentMessageManager.SomeExistingComponent<TComponent>();
|
|
}
|
|
else if (pendingRead)
|
|
{
|
|
return componentMessageManager.SomePendingComponent<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 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 (Guid, TComponent) GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
var pendingRead = receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>));
|
|
var existingRead = receiveTypes.Contains(typeof(ComponentMessage<TComponent>));
|
|
if (existingRead && pendingRead)
|
|
{
|
|
if (componentMessageManager.HasPendingComponent<TComponent>(entity))
|
|
{
|
|
return componentMessageManager.ReadPendingComponentByEntityAndType<TComponent>(entity);
|
|
}
|
|
else if (componentMessageManager.HasExistingComponent<TComponent>(entity))
|
|
{
|
|
return componentMessageManager.ReadExistingComponentByEntityAndType<TComponent>(entity);
|
|
}
|
|
else
|
|
{
|
|
throw new NoComponentOfTypeOnEntityException("No Component of type {0} exists on Entity {1}", typeof(TComponent).Name, entity.ID);
|
|
}
|
|
}
|
|
else if (existingRead)
|
|
{
|
|
return componentMessageManager.ReadExistingComponentByEntityAndType<TComponent>(entity);
|
|
}
|
|
else if (pendingRead)
|
|
{
|
|
return componentMessageManager.ReadPendingComponentByEntityAndType<TComponent>(entity);
|
|
}
|
|
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 (Guid, IComponent) GetComponent(Entity entity, Type type)
|
|
{
|
|
var pending = typeof(PendingComponentMessage<>).MakeGenericType(type);
|
|
var existing = typeof(ComponentMessage<>).MakeGenericType(type);
|
|
|
|
var pendingRead = receiveTypes.Contains(pending);
|
|
var existingRead = receiveTypes.Contains(existing);
|
|
|
|
if (existingRead && pendingRead)
|
|
{
|
|
if (componentMessageManager.HasPendingComponent(entity, pending))
|
|
{
|
|
return componentMessageManager.ReadPendingComponentByEntityAndType(entity, pending);
|
|
}
|
|
else if (componentMessageManager.HasExistingComponent(entity, existing))
|
|
{
|
|
return componentMessageManager.ReadExistingComponentByEntityAndType(entity, existing);
|
|
}
|
|
else
|
|
{
|
|
throw new NoComponentOfTypeOnEntityException("No Component of type {0} exists on Entity {1}", type.Name, entity.ID);
|
|
}
|
|
}
|
|
else if (existingRead)
|
|
{
|
|
return componentMessageManager.ReadExistingComponentByEntityAndType(entity, existing);
|
|
}
|
|
else if (pendingRead)
|
|
{
|
|
return componentMessageManager.ReadPendingComponentByEntityAndType(entity, pending);
|
|
}
|
|
else
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, type.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<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
var pendingRead = receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>));
|
|
var existingRead = receiveTypes.Contains(typeof(ComponentMessage<TComponent>));
|
|
|
|
if (pendingRead && existingRead)
|
|
{
|
|
return componentMessageManager.HasExistingOrPendingComponent<TComponent>(entity);
|
|
}
|
|
else if (existingRead)
|
|
{
|
|
return componentMessageManager.HasExistingComponent<TComponent>(entity);
|
|
}
|
|
else if (pendingRead)
|
|
{
|
|
return componentMessageManager.HasPendingComponent<TComponent>(entity);
|
|
}
|
|
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 pending = typeof(PendingComponentMessage<>).MakeGenericType(type);
|
|
var existing = typeof(ComponentMessage<>).MakeGenericType(type);
|
|
|
|
var pendingRead = receiveTypes.Contains(pending);
|
|
var existingRead = receiveTypes.Contains(existing);
|
|
|
|
if (pendingRead && existingRead)
|
|
{
|
|
return componentMessageManager.HasExistingOrPendingComponent(entity, type);
|
|
}
|
|
else if (existingRead)
|
|
{
|
|
return componentMessageManager.HasExistingComponent(entity, type);
|
|
}
|
|
else if (pendingRead)
|
|
{
|
|
return componentMessageManager.HasPendingComponent(entity, 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 Guid SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
|
|
{
|
|
var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : 0;
|
|
|
|
var componentID = componentManager.MarkComponentForWrite(entity, component, priority);
|
|
|
|
if (!sendTypes.Contains(typeof(ComponentWriteMessage<TComponent>)))
|
|
{
|
|
throw new IllegalWriteException("Engine {0} tried to update undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
|
|
if (sendTypes.Contains(typeof(PendingComponentMessage<TComponent>)))
|
|
{
|
|
PendingComponentMessage<TComponent> newComponentMessage;
|
|
newComponentMessage.entity = entity;
|
|
newComponentMessage.componentID = componentID;
|
|
newComponentMessage.component = component;
|
|
newComponentMessage.priority = priority;
|
|
SendPendingComponentMessage(newComponentMessage);
|
|
}
|
|
|
|
return componentID;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Overwrites Component struct data associated with the specified Component ID.
|
|
/// </summary>
|
|
protected Guid SetComponent<TComponent>(Guid componentID, TComponent component) where TComponent : struct, IComponent
|
|
{
|
|
return SetComponent(GetEntityByComponentID<TComponent>(componentID), component);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets Draw Component data for the specified Component Type on the specified Entity.
|
|
/// This method must be used for the Draw Component to be readable by an OrderedRenderer.
|
|
/// 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 Guid SetDrawComponent<TComponent>(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent, IDrawComponent
|
|
{
|
|
var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : 0;
|
|
|
|
var componentID = componentManager.MarkDrawComponentForWrite(entity, component, priority, layer);
|
|
|
|
if (!sendTypes.Contains(typeof(ComponentWriteMessage<TComponent>)))
|
|
{
|
|
throw new IllegalWriteException("Engine {0} tried to write undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
|
|
if (sendTypes.Contains(typeof(PendingComponentMessage<TComponent>)))
|
|
{
|
|
PendingComponentMessage<TComponent> newComponentMessage;
|
|
newComponentMessage.entity = entity;
|
|
newComponentMessage.componentID = componentID;
|
|
newComponentMessage.component = component;
|
|
newComponentMessage.priority = priority;
|
|
SendPendingComponentMessage(newComponentMessage);
|
|
}
|
|
|
|
return componentID;
|
|
}
|
|
|
|
/// <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.
|
|
/// </summary>
|
|
/// <param name="time">The time in seconds that will elapse before the message is sent.</param>
|
|
protected void SendMessageDelayed<TMessage>(TMessage message, double time) where TMessage : struct, IMessage
|
|
{
|
|
messageManager.AddMessageDelayed(message, time);
|
|
}
|
|
|
|
// unparameterized version to enable dynamic dispatch
|
|
protected void SendMessage(IMessage message)
|
|
{
|
|
var type = message.GetType();
|
|
|
|
if (!sendTypes.Contains(type) || !type.IsValueType)
|
|
{
|
|
throw new IllegalSendException("Engine {0} tried to send undeclared Message {1}", GetType().Name, type.Name);
|
|
}
|
|
|
|
messageManager.AddMessage(message);
|
|
}
|
|
|
|
internal void SendExistingComponentMessage<TComponent>(ComponentMessage<TComponent> message) where TComponent : struct, IComponent
|
|
{
|
|
componentMessageManager.AddExistingComponentMessage(message);
|
|
}
|
|
|
|
internal void SendPendingComponentMessage<TComponent>(PendingComponentMessage<TComponent> message) where TComponent : struct, IComponent
|
|
{
|
|
componentMessageManager.AddPendingComponentMessage(message);
|
|
}
|
|
|
|
/// <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
|
|
{
|
|
if (!receiveTypes.Contains(typeof(TMessage)))
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
|
|
}
|
|
|
|
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
|
|
{
|
|
return ReadMessages<TMessage>().First();
|
|
}
|
|
|
|
/// <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
|
|
{
|
|
if (!receiveTypes.Contains(typeof(TMessage)))
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Message {1}", GetType().Name, typeof(TMessage).Name);
|
|
}
|
|
|
|
return ReadMessages<TMessage>().Any();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Destroys the Entity with the specified ID. 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(Guid entityID)
|
|
{
|
|
entityManager.MarkForDestroy(entityID);
|
|
}
|
|
|
|
/// <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.
|
|
/// </summary>
|
|
protected void RemoveComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
var (componentID, _) = GetComponent<TComponent>(entity);
|
|
RemoveComponent(componentID);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes the Component with the specified ID from its Entity.
|
|
/// </summary>
|
|
protected void RemoveComponent(Guid componentID)
|
|
{
|
|
componentManager.MarkForRemoval(componentID);
|
|
}
|
|
}
|
|
}
|