encompass-cs/encompass-cs/Engine.cs

158 lines
5.5 KiB
C#
Raw Normal View History

2019-06-15 07:39:08 +00:00
using System;
using System.Reflection;
2019-06-15 00:51:06 +00:00
using System.Collections.Generic;
2019-06-17 01:11:35 +00:00
using System.Linq;
using Encompass.Exceptions;
2019-06-15 00:51:06 +00:00
2019-06-16 01:05:56 +00:00
namespace Encompass
{
public abstract class Engine
{
2019-06-29 05:07:48 +00:00
internal readonly List<Type> mutateComponentTypes = new List<Type>();
internal readonly List<Type> emitMessageTypes = new List<Type>();
internal readonly List<Type> readMessageTypes = new List<Type>();
2019-06-15 07:39:08 +00:00
2019-06-15 00:51:06 +00:00
private EntityManager entityManager;
private ComponentManager componentManager;
2019-06-16 01:55:35 +00:00
private MessageManager messageManager;
2019-06-15 00:51:06 +00:00
2019-06-20 17:46:15 +00:00
protected Engine()
2019-06-16 01:05:56 +00:00
{
2019-06-20 03:37:46 +00:00
var mutatesAttribute = GetType().GetCustomAttribute<Mutates>(false);
2019-06-16 01:05:56 +00:00
if (mutatesAttribute != null)
{
2019-06-15 07:39:08 +00:00
mutateComponentTypes = mutatesAttribute.mutateComponentTypes;
}
2019-06-16 01:55:35 +00:00
2019-06-20 03:37:46 +00:00
var emitsAttribute = GetType().GetCustomAttribute<Emits>(false);
2019-06-16 01:55:35 +00:00
if (emitsAttribute != null)
{
emitMessageTypes = emitsAttribute.emitMessageTypes;
}
2019-06-20 03:37:46 +00:00
var readsAttribute = GetType().GetCustomAttribute<Reads>(false);
2019-06-16 01:55:35 +00:00
if (readsAttribute != null)
{
readMessageTypes = readsAttribute.readMessageTypes;
}
2019-06-15 07:39:08 +00:00
}
2019-06-16 01:05:56 +00:00
internal void AssignEntityManager(EntityManager entityManager)
{
2019-06-15 00:51:06 +00:00
this.entityManager = entityManager;
}
2019-06-16 01:05:56 +00:00
internal void AssignComponentManager(ComponentManager componentManager)
{
2019-06-15 00:51:06 +00:00
this.componentManager = componentManager;
}
2019-06-16 01:55:35 +00:00
internal void AssignMessageManager(MessageManager messageManager)
{
this.messageManager = messageManager;
}
public abstract void Update(double dt);
2019-06-15 00:51:06 +00:00
2019-06-16 01:05:56 +00:00
protected Entity CreateEntity()
{
2019-06-20 03:37:46 +00:00
return entityManager.CreateEntity();
}
protected Entity GetEntity(Guid entityID)
{
return entityManager.GetEntity(entityID);
}
protected Guid GetEntityIDByComponentID(Guid componentID)
2019-06-20 03:37:46 +00:00
{
return componentManager.GetEntityIDByComponentID(componentID);
2019-06-15 00:51:06 +00:00
}
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<ValueTuple<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent
2019-06-16 01:05:56 +00:00
{
2019-06-20 03:37:46 +00:00
return componentManager.GetActiveComponentsByType<TComponent>();
2019-06-15 00:51:06 +00:00
}
protected ValueTuple<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent
2019-06-16 01:05:56 +00:00
{
2019-06-20 03:37:46 +00:00
return componentManager.GetActiveComponentByType<TComponent>();
2019-06-15 00:51:06 +00:00
}
2019-06-15 07:39:08 +00:00
internal void UpdateComponentInWorld<TComponent>(Guid componentID, TComponent newComponent) where TComponent : struct, IComponent
2019-06-16 01:05:56 +00:00
{
if (mutateComponentTypes.Contains(typeof(TComponent)))
{
2019-06-20 03:37:46 +00:00
componentManager.UpdateComponent(componentID, newComponent);
2019-06-16 01:05:56 +00:00
}
else
{
2019-06-15 18:40:42 +00:00
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
2019-06-16 01:05:56 +00:00
{
2019-06-20 03:37:46 +00:00
UpdateComponentInWorld(componentID, newComponentValue);
2019-06-15 07:39:08 +00:00
}
2019-06-16 01:55:35 +00:00
protected void EmitMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
{
if (emitMessageTypes.Contains(typeof(TMessage)))
{
2019-06-20 03:37:46 +00:00
messageManager.AddMessage(message);
2019-06-16 01:55:35 +00:00
}
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)))
{
2019-06-20 03:37:46 +00:00
return messageManager.GetMessagesByType<TMessage>();
2019-06-16 01:55:35 +00:00
}
else
{
throw new IllegalMessageReadException("Engine {0} tried to read undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
}
}
2019-06-17 01:11:35 +00:00
protected bool Some<TMessage>() where TMessage : struct, IMessage
{
if (readMessageTypes.Contains(typeof(TMessage)))
{
2019-06-20 17:46:15 +00:00
return messageManager.GetMessagesByType<TMessage>().Any();
2019-06-17 01:11:35 +00:00
}
else
{
throw new IllegalMessageReadException("Engine {0} tried to read undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
}
}
2019-06-20 03:37:46 +00:00
protected void Destroy(Guid entityID)
{
entityManager.MarkForDestroy(entityID);
}
2019-06-15 00:51:06 +00:00
}
}