encompass-cs/encompass-cs/Engine.cs

125 lines
4.4 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;
2019-06-15 00:51:06 +00:00
2019-06-16 01:05:56 +00:00
namespace Encompass
{
public abstract class Engine
{
2019-06-17 00:56:36 +00:00
private readonly List<Type> mutateComponentTypes = new List<Type>();
private readonly List<Type> emitMessageTypes = new List<Type>();
private 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-16 01:05:56 +00:00
public Engine()
{
2019-06-15 07:39:08 +00:00
var mutatesAttribute = this.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
var emitsAttribute = this.GetType().GetCustomAttribute<Emits>(false);
if (emitsAttribute != null)
{
emitMessageTypes = emitsAttribute.emitMessageTypes;
}
var readsAttribute = this.GetType().GetCustomAttribute<Reads>(false);
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;
}
2019-06-15 00:51:06 +00:00
public abstract void Update(float dt);
2019-06-16 01:05:56 +00:00
protected Entity CreateEntity()
{
2019-06-15 00:51:06 +00:00
return this.entityManager.CreateEntity();
}
protected IEnumerable<KeyValuePair<Guid, TComponent>> ReadComponents<TComponent>() where TComponent : struct, IComponent
2019-06-16 01:05:56 +00:00
{
2019-06-15 00:51:06 +00:00
return this.componentManager.GetActiveComponentsByType<TComponent>();
}
protected KeyValuePair<Guid, TComponent> ReadComponent<TComponent>() where TComponent : struct, IComponent
2019-06-16 01:05:56 +00:00
{
2019-06-15 00:51:06 +00:00
return this.componentManager.GetActiveComponentByType<TComponent>();
}
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)))
{
this.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
{
this.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)))
{
this.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 this.messageManager.GetMessagesByType<TMessage>();
}
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)))
{
return messageManager.GetMessagesByType<TMessage>().Count() > 0;
}
else
{
throw new IllegalMessageReadException("Engine {0} tried to read undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
}
}
2019-06-15 00:51:06 +00:00
}
}