340 lines
14 KiB
C#
340 lines
14 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 HashSet<Type> updateTypes = 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 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);
|
|
}
|
|
|
|
var updatesAttribute = GetType().GetCustomAttribute<Updates>(false);
|
|
if (updatesAttribute != null)
|
|
{
|
|
updateTypes = updatesAttribute.updateTypes;
|
|
if (sendTypes.Any())
|
|
{
|
|
throw new IllegalEngineAttributesException("Engine {0} sends Message(s) and updates Component(s)", GetType().Name);
|
|
}
|
|
}
|
|
}
|
|
|
|
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<Guid, TComponent>> ExistingComponentsOnEntity<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
if (!receiveTypes.Contains(typeof(ComponentMessage<TComponent>)))
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
|
|
return ReadMessages<ComponentMessage<TComponent>>().Where((message) => message.entity == entity).Select((message) => (message.componentID, message.component));
|
|
}
|
|
|
|
private IEnumerable<ValueTuple<Guid, TComponent>> PendingComponentsOnEntity<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
if (!receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>)))
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared pending Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
|
|
return ReadMessages<PendingComponentMessage<TComponent>>().Where((message) => message.entity == entity).Select((message) => (message.componentID, message.component));
|
|
}
|
|
|
|
protected IEnumerable<ValueTuple<Guid, TComponent>> GetPendingComponents<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
return PendingComponentsOnEntity<TComponent>(entity);
|
|
}
|
|
|
|
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 HasPendingComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
|
|
{
|
|
return GetPendingComponents<TComponent>(entity).Any();
|
|
}
|
|
|
|
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
|
|
{
|
|
if (!updateTypes.Contains(typeof(TComponent)))
|
|
{
|
|
throw new IllegalSendException("Engine {0} tried to update undeclared Component {1}", this.GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
|
|
componentManager.AddUpdateComponentOperation(componentID, newComponent);
|
|
}
|
|
|
|
protected void UpdateComponent<TComponent>(Guid componentID, TComponent newComponentValue) where TComponent : struct, IComponent
|
|
{
|
|
UpdateComponentInWorld(componentID, newComponentValue);
|
|
}
|
|
|
|
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}", this.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 IEnumerable<ValueTuple<Guid, TComponent>> ReadPendingComponents<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
if (!receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>)))
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared pending Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
|
|
return ReadMessages<PendingComponentMessage<TComponent>>().Select((message) => (message.componentID, message.component));
|
|
}
|
|
|
|
protected IEnumerable<ValueTuple<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
if (!receiveTypes.Contains(typeof(ComponentMessage<TComponent>)))
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", this.GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
|
|
return ReadMessages<ComponentMessage<TComponent>>().Select((message) => (message.componentID, message.component));
|
|
}
|
|
|
|
protected ValueTuple<Guid, TComponent> ReadPendingComponent<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return ReadPendingComponents<TComponent>().Single();
|
|
}
|
|
|
|
protected ValueTuple<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return ReadComponents<TComponent>().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 bool SomePendingComponent<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
if (!receiveTypes.Contains(typeof(PendingComponentMessage<TComponent>)))
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared pending Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
|
|
return ReadMessages<PendingComponentMessage<TComponent>>().Any();
|
|
}
|
|
|
|
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
if (!receiveTypes.Contains(typeof(ComponentMessage<TComponent>)))
|
|
{
|
|
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
|
|
return ReadMessages<ComponentMessage<TComponent>>().Any();
|
|
}
|
|
|
|
protected void Destroy(Guid entityID)
|
|
{
|
|
entityManager.MarkForDestroy(entityID);
|
|
}
|
|
|
|
protected void RemoveComponent(Guid componentID)
|
|
{
|
|
componentManager.MarkForRemoval(componentID);
|
|
}
|
|
}
|
|
}
|