393 lines
16 KiB
C#
393 lines
16 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Encompass.Exceptions;
|
|
|
|
namespace Encompass
|
|
{
|
|
public abstract class Engine
|
|
{
|
|
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()
|
|
{
|
|
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 writesAttribute = GetType().GetCustomAttribute<Writes>(false);
|
|
if (writesAttribute != null)
|
|
{
|
|
sendTypes.UnionWith(writesAttribute.writeTypes);
|
|
writePriorities = writesAttribute.priorities;
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public abstract void Update(double dt);
|
|
|
|
protected Entity CreateEntity()
|
|
{
|
|
return entityManager.CreateEntity();
|
|
}
|
|
|
|
protected bool EntityExists(Guid entityID)
|
|
{
|
|
return entityManager.EntityExists(entityID);
|
|
}
|
|
|
|
protected Entity GetEntity(Guid entityID)
|
|
{
|
|
return entityManager.GetEntity(entityID);
|
|
}
|
|
|
|
protected Guid GetEntityIDByComponentID(Guid componentID)
|
|
{
|
|
return componentMessageManager.GetEntityIDByComponentID(componentID);
|
|
}
|
|
|
|
protected Entity GetEntityByComponentID(Guid componentID)
|
|
{
|
|
return GetEntity(GetEntityIDByComponentID(componentID));
|
|
}
|
|
|
|
protected TComponent GetComponentByID<TComponent>(Guid componentID) where TComponent : struct, IComponent
|
|
{
|
|
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));
|
|
}
|
|
|
|
protected IEnumerable<ValueTuple<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);
|
|
}
|
|
}
|
|
|
|
protected ValueTuple<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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
protected ValueTuple<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);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
internal void UpdateComponentInWorld<TComponent>(Guid componentID, TComponent newComponent) where TComponent : struct, IComponent
|
|
{
|
|
componentManager.AddUpdateComponentOperation(componentID, newComponent);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
ComponentWriteMessage<TComponent> componentUpdateMessage;
|
|
componentUpdateMessage.componentID = componentID;
|
|
componentUpdateMessage.component = component;
|
|
SendMessage(componentUpdateMessage);
|
|
|
|
return componentID;
|
|
}
|
|
|
|
protected Guid SetComponent<TComponent>(Guid componentID, TComponent component) where TComponent : struct, IComponent
|
|
{
|
|
return SetComponent(GetEntityByComponentID(componentID), component);
|
|
}
|
|
|
|
protected Guid SetDrawComponent<TComponent>(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent
|
|
{
|
|
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);
|
|
}
|
|
|
|
ComponentWriteMessage<TComponent> componentUpdateMessage;
|
|
componentUpdateMessage.componentID = componentID;
|
|
componentUpdateMessage.component = component;
|
|
SendMessage(componentUpdateMessage);
|
|
|
|
return componentID;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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>();
|
|
}
|
|
|
|
protected TMessage ReadMessage<TMessage>() where TMessage : struct, IMessage
|
|
{
|
|
return ReadMessages<TMessage>().First();
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
protected void Destroy(Guid entityID)
|
|
{
|
|
entityManager.MarkForDestroy(entityID);
|
|
}
|
|
|
|
protected void RemoveComponent(Guid componentID)
|
|
{
|
|
componentManager.MarkForRemoval(componentID);
|
|
}
|
|
}
|
|
}
|