encompass-cs/encompass-cs/Engine.cs

162 lines
5.6 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 List<Type> writeTypes = new List<Type>();
internal readonly List<Type> readTypes = new List<Type>();
private EntityManager entityManager;
private ComponentManager componentManager;
private MessageManager messageManager;
protected Engine()
{
var emitsAttribute = GetType().GetCustomAttribute<Writes>(false);
if (emitsAttribute != null)
{
writeTypes = emitsAttribute.writeTypes;
}
var readsAttribute = GetType().GetCustomAttribute<Reads>(false);
if (readsAttribute != null)
{
readTypes = readsAttribute.readTypes;
}
}
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 (!readTypes.Contains(typeof(TComponent)))
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", this.GetType().Name, typeof(TComponent).Name);
}
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);
}
protected IEnumerable<ValueTuple<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent
{
if (!readTypes.Contains(typeof(TComponent)))
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", this.GetType().Name, typeof(TComponent).Name);
}
return componentManager.GetActiveComponentsByType<TComponent>();
}
protected ValueTuple<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent
{
if (!readTypes.Contains(typeof(TComponent)))
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", this.GetType().Name, typeof(TComponent).Name);
}
return componentManager.GetActiveComponentByType<TComponent>();
}
internal void UpdateComponentInWorld<TComponent>(Guid componentID, TComponent newComponent) where TComponent : struct, IComponent
{
if (!writeTypes.Contains(typeof(TComponent)))
{
throw new IllegalWriteException("Engine {0} tried to write undeclared Component {1}", this.GetType().Name, typeof(TComponent).Name);
}
componentManager.UpdateComponent(componentID, newComponent);
}
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 (!writeTypes.Contains(typeof(TMessage)))
{
throw new IllegalWriteException("Engine {0} tried to emit undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
}
messageManager.AddMessage(message);
}
protected IEnumerable<TMessage> ReadMessages<TMessage>() where TMessage : struct, IMessage
{
if (!readTypes.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 bool Some<TMessage>() where TMessage : struct, IMessage
{
if (!readTypes.Contains(typeof(TMessage)))
{
throw new IllegalReadException("Engine {0} tried to read undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
}
return messageManager.GetMessagesByType<TMessage>().Any();
}
protected void Destroy(Guid entityID)
{
entityManager.MarkForDestroy(entityID);
}
}
}