235 lines
8.7 KiB
C#
235 lines
8.7 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>();
|
|
}
|
|
|
|
protected Guid AddComponent<TComponent>(Entity entity, TComponent component) 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);
|
|
}
|
|
|
|
return componentManager.AddComponent(entity.ID, component);
|
|
}
|
|
|
|
protected Guid AddDrawComponent<TComponent>(Entity entity, TComponent component, int layer = 0) 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);
|
|
}
|
|
|
|
return componentManager.AddDrawComponent(entity.ID, component, layer);
|
|
}
|
|
|
|
protected void ActivateComponent(Guid componentID)
|
|
{
|
|
var type = componentManager.GetComponentTypeByID(componentID);
|
|
if (!writeTypes.Contains(type))
|
|
{
|
|
throw new IllegalWriteException("Engine {0} tried to write undeclared Component {1}", this.GetType().Name, type.Name);
|
|
}
|
|
|
|
componentManager.Activate(componentID);
|
|
}
|
|
|
|
protected void DeactivateComponent(Guid componentID)
|
|
{
|
|
var type = componentManager.GetComponentTypeByID(componentID);
|
|
if (!writeTypes.Contains(type))
|
|
{
|
|
throw new IllegalWriteException("Engine {0} tried to write undeclared Component {1}", this.GetType().Name, type.Name);
|
|
}
|
|
|
|
componentManager.Deactivate(componentID);
|
|
}
|
|
|
|
protected IEnumerable<ValueTuple<Guid, TComponent>> GetComponents<TComponent>(Entity entity) 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.GetComponentsByEntityAndType<TComponent>(entity.ID);
|
|
}
|
|
|
|
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 componentManager.EntityHasComponentOfType<TComponent>(entity.ID);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
protected void RemoveComponent(Guid componentID)
|
|
{
|
|
var type = componentManager.GetComponentTypeByID(componentID);
|
|
if (!writeTypes.Contains(type))
|
|
{
|
|
throw new IllegalWriteException("Engine {0} tried to write undeclared Component {1}", this.GetType().Name, type.Name);
|
|
}
|
|
|
|
componentManager.Remove(componentID);
|
|
}
|
|
}
|
|
}
|