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();
|
|
|
|
}
|
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
|
|
|
|
{
|
2019-06-15 00:51:06 +00:00
|
|
|
return this.componentManager.GetActiveComponentsByType<TComponent>();
|
|
|
|
}
|
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
protected TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
|
|
|
|
{
|
2019-06-15 00:51:06 +00:00
|
|
|
return this.componentManager.GetActiveComponentByType<TComponent>();
|
|
|
|
}
|
2019-06-15 07:39:08 +00:00
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
internal void UpdateComponentInWorld<TComponent>(TComponent originalComponent, TComponent newComponent) where TComponent : struct, IComponent
|
|
|
|
{
|
|
|
|
if (mutateComponentTypes.Contains(typeof(TComponent)))
|
|
|
|
{
|
2019-06-15 07:39:08 +00:00
|
|
|
this.componentManager.UpdateComponent(originalComponent, 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
protected void UpdateComponent<TComponent>(TComponent component, Func<TComponent, TComponent> updateFunction) where TComponent : struct, IComponent
|
|
|
|
{
|
2019-06-15 18:40:42 +00:00
|
|
|
var updatedComponent = updateFunction(component);
|
|
|
|
this.UpdateComponentInWorld(component, updatedComponent);
|
|
|
|
}
|
|
|
|
|
2019-06-16 01:05:56 +00:00
|
|
|
protected void UpdateComponents<TComponent>(IEnumerable<TComponent> components, Func<TComponent, TComponent> updateFunction) where TComponent : struct, IComponent
|
|
|
|
{
|
|
|
|
foreach (var component in components)
|
|
|
|
{
|
2019-06-15 18:40:42 +00:00
|
|
|
this.UpdateComponent(component, updateFunction);
|
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
|
|
|
}
|
|
|
|
}
|