encompass-cs/encompass-cs/Engine.cs

315 lines
12 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>();
private EntityManager entityManager;
private ComponentManager componentManager;
private MessageManager messageManager;
protected Engine()
{
var sendsAttribute = GetType().GetCustomAttribute<Sends>(false);
if (sendsAttribute != null)
{
sendTypes = sendsAttribute.sendTypes;
}
var activatesAttribute = GetType().GetCustomAttribute<Activates>(false);
if (activatesAttribute != null)
{
sendTypes.UnionWith(activatesAttribute.activateTypes);
}
var updatesAttribute = GetType().GetCustomAttribute<Updates>(false);
if (updatesAttribute != null)
{
sendTypes.UnionWith(updatesAttribute.updateTypes);
}
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;
}
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 componentManager.GetEntityIDByComponentID(componentID);
}
protected Entity GetEntityByComponentID(Guid componentID)
{
return GetEntity(GetEntityIDByComponentID(componentID));
}
protected TComponent GetComponentByID<TComponent>(Guid componentID) where TComponent : struct, IComponent
{
if (componentManager.GetComponentTypeByID(componentID) != typeof(TComponent))
{
throw new ComponentTypeMismatchException("Expected Component to be of type {0} but was actually of type {1}", typeof(TComponent).Name, componentManager.GetComponentTypeByID(componentID).Name);
}
return (TComponent)componentManager.GetComponentByID(componentID);
}
internal IEnumerable<ValueTuple<Entity, Guid, TComponent>> ReadComponentsFromWorld<TComponent>() where TComponent : struct, IComponent
{
return componentManager.GetActiveComponentsByType<TComponent>().Select((triple) => (GetEntity(triple.Item1), triple.Item2, triple.Item3));
}
protected Guid AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent
{
if (!sendTypes.Contains(typeof(PendingComponentMessage<TComponent>)))
{
throw new IllegalActivateException("Engine {0} tried to activate undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
var componentID = componentManager.AddComponent(entity.ID, component);
PendingComponentMessage<TComponent> componentMessage;
componentMessage.entity = entity;
componentMessage.componentID = componentID;
componentMessage.component = component;
SendMessage(componentMessage);
return componentID;
}
protected Guid AddDrawComponent<TComponent>(Entity entity, TComponent component, int layer = 0) where TComponent : struct, IComponent
{
if (!sendTypes.Contains(typeof(PendingComponentMessage<TComponent>)))
{
throw new IllegalActivateException("Engine {0} tried to activate undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
var componentID = componentManager.AddDrawComponent(entity.ID, component, layer);
PendingComponentMessage<TComponent> newComponentMessage;
newComponentMessage.entity = entity;
newComponentMessage.componentID = componentID;
newComponentMessage.component = component;
SendMessage(newComponentMessage);
return componentID;
}
protected void ActivateComponent<TComponent>(Guid componentID) where TComponent : struct, IComponent
{
if (!sendTypes.Contains(typeof(PendingComponentMessage<TComponent>)))
{
throw new IllegalActivateException("Engine {0} tried to activate undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
var entity = GetEntity(componentManager.GetEntityIDByComponentID(componentID));
var component = GetComponentByID<TComponent>(componentID);
PendingComponentMessage<TComponent> newComponentMessage;
newComponentMessage.entity = entity;
newComponentMessage.componentID = componentID;
newComponentMessage.component = component;
SendMessage(newComponentMessage);
componentManager.Activate(componentID); // TODO: actually delay this to end of frame, make sure to update after activate
}
protected void DeactivateComponent(Guid componentID)
{
componentManager.MarkForDeactivation(componentID);
}
private IEnumerable<ValueTuple<Entity, Guid, TComponent>> ExistingComponents<TComponent>() where TComponent : struct, IComponent
{
return ReadMessages<ComponentMessage<TComponent>>().Select((message) => (message.entity, message.componentID, message.component));
}
private IEnumerable<ValueTuple<Entity, Guid, TComponent>> PendingComponents<TComponent>() where TComponent : struct, IComponent
{
return ReadMessages<PendingComponentMessage<TComponent>>().Select((message) => (message.entity, message.componentID, message.component));
}
private IEnumerable<ValueTuple<Entity, Guid, TComponent>> ReadComponentMessages<TComponent>() where TComponent : struct, IComponent
{
var pendingRead = receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>));
var existingRead = receiveTypes.Contains(typeof(ComponentMessage<TComponent>));
if (existingRead && pendingRead)
{
return ExistingComponents<TComponent>().Union(PendingComponents<TComponent>());
}
else if (existingRead)
{
return ExistingComponents<TComponent>();
}
else if (pendingRead)
{
return PendingComponents<TComponent>();
}
else
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
}
private IEnumerable<ValueTuple<Guid, TComponent>> ExistingComponentsOnEntity<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return ReadComponentMessages<TComponent>().Where((triple) => triple.Item1 == entity).Select((triple) => (triple.Item2, triple.Item3));
}
private IEnumerable<ValueTuple<Guid, TComponent>> PendingComponentsOnEntity<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return ReadComponentMessages<TComponent>().Where((triple) => triple.Item1 == entity).Select((triple) => (triple.Item2, triple.Item3));
}
protected IEnumerable<ValueTuple<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent
{
return ReadComponentMessages<TComponent>().Select((triple) => (triple.Item2, triple.Item3));
}
protected ValueTuple<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent
{
return ReadComponents<TComponent>().Single();
}
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
{
return ReadComponentMessages<TComponent>().Any();
}
protected IEnumerable<ValueTuple<Guid, TComponent>> GetComponents<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return ExistingComponentsOnEntity<TComponent>(entity);
}
protected ValueTuple<Guid, TComponent> GetPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return PendingComponentsOnEntity<TComponent>(entity).First();
}
protected ValueTuple<Guid, TComponent> GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return GetComponents<TComponent>(entity).First();
}
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return GetComponents<TComponent>(entity).Any();
}
internal void UpdateComponentInWorld<TComponent>(Guid componentID, TComponent newComponent) where TComponent : struct, IComponent
{
componentManager.AddUpdateComponentOperation(componentID, newComponent);
}
protected void UpdateComponent<TComponent>(Guid componentID, TComponent newComponentValue) where TComponent : struct, IComponent
{
if (!sendTypes.Contains(typeof(ComponentUpdateMessage<TComponent>)))
{
throw new IllegalUpdateException("Engine {0} tried to update undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
ComponentUpdateMessage<TComponent> componentUpdateMessage;
componentUpdateMessage.componentID = componentID;
componentUpdateMessage.component = newComponentValue;
SendMessage(componentUpdateMessage);
}
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 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>().Single();
}
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);
}
}
}