158 lines
5.5 KiB
C#
158 lines
5.5 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Encompass.Exceptions;
|
|
|
|
namespace Encompass
|
|
{
|
|
public abstract class Engine
|
|
{
|
|
private readonly List<Type> mutateComponentTypes = new List<Type>();
|
|
private readonly List<Type> emitMessageTypes = new List<Type>();
|
|
private readonly List<Type> readMessageTypes = new List<Type>();
|
|
|
|
private EntityManager entityManager;
|
|
private ComponentManager componentManager;
|
|
private MessageManager messageManager;
|
|
|
|
protected Engine()
|
|
{
|
|
var mutatesAttribute = GetType().GetCustomAttribute<Mutates>(false);
|
|
if (mutatesAttribute != null)
|
|
{
|
|
mutateComponentTypes = mutatesAttribute.mutateComponentTypes;
|
|
}
|
|
|
|
var emitsAttribute = GetType().GetCustomAttribute<Emits>(false);
|
|
if (emitsAttribute != null)
|
|
{
|
|
emitMessageTypes = emitsAttribute.emitMessageTypes;
|
|
}
|
|
|
|
var readsAttribute = GetType().GetCustomAttribute<Reads>(false);
|
|
if (readsAttribute != null)
|
|
{
|
|
readMessageTypes = readsAttribute.readMessageTypes;
|
|
}
|
|
}
|
|
|
|
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 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))
|
|
{
|
|
return (TComponent)componentManager.GetComponentByID(componentID);
|
|
}
|
|
else
|
|
{
|
|
throw new ComponentTypeMismatchException("Expected Component to be of type {0} but was actually of type {1}", typeof(TComponent).Name, componentManager.GetComponentTypeByID(componentID).Name);
|
|
}
|
|
}
|
|
|
|
protected IEnumerable<KeyValuePair<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return componentManager.GetActiveComponentsByType<TComponent>();
|
|
}
|
|
|
|
protected KeyValuePair<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent
|
|
{
|
|
return componentManager.GetActiveComponentByType<TComponent>();
|
|
}
|
|
|
|
internal void UpdateComponentInWorld<TComponent>(Guid componentID, TComponent newComponent) where TComponent : struct, IComponent
|
|
{
|
|
if (mutateComponentTypes.Contains(typeof(TComponent)))
|
|
{
|
|
componentManager.UpdateComponent(componentID, newComponent);
|
|
}
|
|
else
|
|
{
|
|
throw new IllegalComponentMutationException("Engine {0} tried to mutate undeclared Component {1}", this.GetType().Name, typeof(TComponent).Name);
|
|
}
|
|
}
|
|
|
|
protected void UpdateComponent<TComponent>(Guid componentID, TComponent newComponentValue) where TComponent : struct, IComponent
|
|
{
|
|
UpdateComponentInWorld(componentID, newComponentValue);
|
|
}
|
|
|
|
protected void EmitMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
|
|
{
|
|
if (emitMessageTypes.Contains(typeof(TMessage)))
|
|
{
|
|
messageManager.AddMessage(message);
|
|
}
|
|
else
|
|
{
|
|
throw new IllegalMessageEmitException("Engine {0} tried to emit undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
|
|
}
|
|
}
|
|
|
|
protected IEnumerable<TMessage> ReadMessages<TMessage>() where TMessage : struct, IMessage
|
|
{
|
|
if (readMessageTypes.Contains(typeof(TMessage)))
|
|
{
|
|
return messageManager.GetMessagesByType<TMessage>();
|
|
}
|
|
else
|
|
{
|
|
throw new IllegalMessageReadException("Engine {0} tried to read undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
|
|
}
|
|
}
|
|
|
|
protected bool Some<TMessage>() where TMessage : struct, IMessage
|
|
{
|
|
if (readMessageTypes.Contains(typeof(TMessage)))
|
|
{
|
|
return messageManager.GetMessagesByType<TMessage>().Any();
|
|
}
|
|
else
|
|
{
|
|
throw new IllegalMessageReadException("Engine {0} tried to read undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
|
|
}
|
|
}
|
|
|
|
protected void Destroy(Guid entityID)
|
|
{
|
|
entityManager.MarkForDestroy(entityID);
|
|
}
|
|
}
|
|
}
|