using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
using MoonTools.FastCollections;
namespace Encompass
{
///
/// Engines are the Encompass notion of an ECS System.
/// They are responsible for reading the World state, reading messages, emitting messages, and creating or mutating Entities and Components.
/// Engines run once per World Update.
///
public abstract class Engine : IEquatable
{
internal Guid ID;
internal readonly HashSet readTypes = new HashSet();
internal readonly HashSet readImmediateTypes = new HashSet();
internal readonly HashSet sendTypes = new HashSet();
internal readonly HashSet receiveTypes = new HashSet();
internal readonly HashSet writeTypes = new HashSet();
internal readonly HashSet writeImmediateTypes = new HashSet();
internal readonly HashSet queryWithTypes = new HashSet();
internal readonly HashSet queryWithoutTypes = new HashSet();
internal readonly Dictionary writePriorities = new Dictionary();
internal readonly int defaultWritePriority = 0;
///
/// If false, the Engine will ignore time dilation.
///
internal bool usesTimeDilation = true;
public bool TimeDilationActive { get => usesTimeDilation && timeManager.TimeDilationActive; }
private EntityManager entityManager;
private MessageManager messageManager;
private ComponentManager componentManager;
private TimeManager timeManager;
private TrackingManager trackingManager;
private EntitySetQuery entityQuery;
private HashSet _trackedEntities = new HashSet();
protected IEnumerable TrackedEntities
{
get
{
foreach (var entityID in _trackedEntities)
{
yield return entityManager.GetEntity(entityID);
}
}
}
protected Engine()
{
ID = Guid.NewGuid();
var sendsAttribute = GetType().GetCustomAttribute(false);
if (sendsAttribute != null)
{
sendTypes = sendsAttribute.sendTypes;
}
var activatesAttribute = GetType().GetCustomAttribute(false);
if (activatesAttribute != null)
{
writeImmediateTypes = activatesAttribute.writeImmediateTypes;
}
var defaultWritePriorityAttribute = GetType().GetCustomAttribute(false);
if (defaultWritePriorityAttribute != null)
{
defaultWritePriority = defaultWritePriorityAttribute.writePriority;
}
foreach (var writesAttribute in GetType().GetCustomAttributes(false))
{
writeTypes.UnionWith(writesAttribute.writeTypes);
writePriorities = new Dictionary[2] { writePriorities, writesAttribute.priorities }.SelectMany(dict => dict).ToDictionary(pair => pair.Key, pair => pair.Value);
}
var receivesAttribute = GetType().GetCustomAttribute(false);
if (receivesAttribute != null)
{
receiveTypes = receivesAttribute.receiveTypes;
}
var readsAttribute = GetType().GetCustomAttribute(false);
if (readsAttribute != null)
{
readTypes = readsAttribute.readTypes;
}
var readsImmediateAttribute = GetType().GetCustomAttribute(false);
if (readsImmediateAttribute != null)
{
readImmediateTypes = readsImmediateAttribute.readImmediateTypes;
}
var queryWithAttribute = GetType().GetCustomAttribute(false);
if (queryWithAttribute != null)
{
queryWithTypes = queryWithAttribute.queryWithTypes;
}
var queryWithoutAttribute = GetType().GetCustomAttribute(false);
if (queryWithoutAttribute != null)
{
queryWithoutTypes = queryWithoutAttribute.queryWithoutTypes;
}
}
public override bool Equals(object obj)
{
if (obj is Engine engine)
{
return Equals(engine);
}
return false;
}
public bool Equals(Engine other)
{
return other.ID == ID;
}
public override int GetHashCode()
{
return ID.GetHashCode();
}
internal void AssignEntityManager(EntityManager entityManager)
{
this.entityManager = entityManager;
}
internal void AssignComponentManager(ComponentManager componentManager)
{
this.componentManager = componentManager;
}
internal void AssignMessageManager(MessageManager messageManager)
{
this.messageManager = messageManager;
}
internal void AssignTimeManager(TimeManager timeManager)
{
this.timeManager = timeManager;
}
internal void AssignTrackingManager(TrackingManager trackingManager)
{
this.trackingManager = trackingManager;
}
///
/// Runs once per World update with the calculated delta-time.
///
/// The time in seconds that has elapsed since the previous frame.
public abstract void Update(double dt);
///
/// Creates and returns a new empty Entity.
///
protected Entity CreateEntity()
{
return entityManager.CreateEntity();
}
///
/// Returns true if an Entity with the specified ID exists.
///
protected bool EntityExists(Entity entity)
{
return entityManager.EntityExists(entity.ID);
}
///
/// Returns an Entity containing the specified Component type.
///
protected Entity ReadEntity() where TComponent : struct, IComponent
{
return entityManager.GetEntity(ReadComponentHelper().Item2);
}
///
/// Returns all Entities containing the specified Component type.
///
protected IEnumerable ReadEntities() where TComponent : struct, IComponent
{
foreach (var pair in ReadComponentsHelper())
{
yield return entityManager.GetEntity(pair.Item2);
}
}
// these next two are for the ComponentMessageEmitter only
internal IEnumerable ReadComponentsFromWorld() where TComponent : struct, IComponent
{
return componentManager.GetComponentsByType();
}
private IEnumerable<(TComponent, int)> ReadComponentsHelper() where TComponent : struct, IComponent
{
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
var existingRead = readTypes.Contains(typeof(TComponent));
if (existingRead && immediateRead)
{
return componentManager.ReadExistingAndImmediateComponentsByType();
}
else if (existingRead)
{
return componentManager.GetComponentsIncludingEntity();
}
else if (immediateRead)
{
return componentManager.ReadImmediateComponentsByType();
}
else
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
}
///
/// Returns all of the Components with the specified Component Type.
///
protected IEnumerable ReadComponents() where TComponent : struct, IComponent
{
foreach (var pair in ReadComponentsHelper())
{
yield return pair.Item1;
}
}
///
/// Returns all of the components of the specified type including an Entity reference for each Component.
///
protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity() where TComponent : struct, IComponent
{
foreach (var (component, id) in ReadComponentsHelper())
{
yield return (component, entityManager.GetEntity(id));
}
}
private (TComponent, int) ReadComponentHelper() where TComponent : struct, IComponent
{
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
var existingRead = readTypes.Contains(typeof(TComponent));
if (existingRead && immediateRead)
{
return componentManager.ReadFirstExistingOrImmediateComponentByType();
}
else if (existingRead)
{
return componentManager.ReadFirstExistingComponentByType();
}
else if (immediateRead)
{
return componentManager.ReadFirstImmediateComponentByType();
}
else
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
}
///
/// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned.
///
protected TComponent ReadComponent() where TComponent : struct, IComponent
{
return ReadComponentHelper().Item1;
}
///
/// Returns a component of the specified type including its Entity reference. If multiples exist, an arbitrary Component is returned.
///
protected (TComponent, Entity) ReadComponentIncludingEntity() where TComponent : struct, IComponent
{
var (component, id) = ReadComponentHelper();
return (component, entityManager.GetEntity(id));
}
///
/// Returns true if any Component with the specified Component Type exists.
///
protected bool SomeComponent() where TComponent : struct, IComponent
{
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
var existingRead = readTypes.Contains(typeof(TComponent));
if (existingRead && immediateRead)
{
return componentManager.SomeExistingOrImmediateComponent();
}
else if (existingRead)
{
return componentManager.SomeExistingComponent();
}
else if (immediateRead)
{
return componentManager.SomeImmediateComponent();
}
else
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
}
private TComponent GetComponentHelper(int entityID) where TComponent : struct, IComponent
{
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
var existingRead = readTypes.Contains(typeof(TComponent));
if (existingRead && immediateRead)
{
return componentManager.ReadImmediateOrExistingComponentByEntityAndType(entityID);
}
else if (existingRead)
{
return componentManager.ReadExistingComponentByEntityAndType(entityID);
}
else if (immediateRead)
{
return componentManager.ReadImmediateComponentByEntityAndType(entityID);
}
else
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
}
///
/// Returns a Component with the specified Type that exists on the Entity.
///
///
/// Thrown when the Entity does not have a Component of the specified Type
///
///
/// Thrown when the Engine does not declare that it reads the given Component Type.
///
protected TComponent GetComponent(Entity entity) where TComponent : struct, IComponent
{
return GetComponentHelper(entity.ID);
}
///
/// Returns true if the Entity has a Component of the given Type.
///
///
/// Thrown when the Engine does not declare that is Reads the given Component Type.
///
protected bool HasComponent(Entity entity) where TComponent : struct, IComponent
{
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
var existingRead = readTypes.Contains(typeof(TComponent));
if (immediateRead && existingRead)
{
return componentManager.HasExistingOrImmediateComponent(entity.ID);
}
else if (existingRead)
{
return componentManager.HasExistingComponent(entity.ID);
}
else if (immediateRead)
{
return componentManager.HasImmediateComponent(entity.ID);
}
else
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
}
///
/// Returns true if the Entity has a Component of the given Type.
///
///
/// Thrown when the Engine does not declare that is Reads the given Component Type.
///
protected bool HasComponent(Entity entity, Type type)
{
var immediateRead = readImmediateTypes.Contains(type);
var existingRead = readTypes.Contains(type);
if (immediateRead && existingRead)
{
return componentManager.HasExistingOrImmediateComponent(entity.ID, type);
}
else if (existingRead)
{
return componentManager.HasExistingComponent(entity.ID, type);
}
else if (immediateRead)
{
return componentManager.HasImmediateComponent(entity.ID, type);
}
else
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, type.Name);
}
}
///
/// Sets Component data for the specified Component Type on the specified Entity. If Component data for this Type already existed on the Entity, the component data is overwritten.
///
///
/// Thrown when the Engine does not declare that it Writes the given Component Type.
///
protected void SetComponent(Entity entity, TComponent component) where TComponent : struct, IComponent
{
var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : defaultWritePriority;
if (!writeTypes.Contains(typeof(TComponent)))
{
throw new IllegalWriteException("Engine {0} tried to update undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
bool written;
if (writeImmediateTypes.Contains(typeof(TComponent)))
{
written = componentManager.AddImmediateComponent(entity.ID, component, priority);
if (written)
{
trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
}
}
else
{
written = componentManager.UpdateComponent(entity.ID, component, priority);
}
if (!componentManager.HasExistingComponent(entity.ID))
{
trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
}
if (written && component is IDrawableComponent drawableComponent)
{
componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer);
}
}
///
/// Sends a Message.
///
///
/// Thrown when the Engine does not declare that it Sends the Message Type.
///
protected void SendMessage(TMessage message) where TMessage : struct, IMessage
{
if (!sendTypes.Contains(typeof(TMessage)))
{
throw new IllegalSendException("Engine {0} tried to send undeclared Message {1}", GetType().Name, typeof(TMessage).Name);
}
messageManager.AddMessage(message);
}
///
/// Sends a message after the specified number of seconds, respecting time dilation.
///
/// The time in seconds that will elapse before the message is sent.
protected void SendMessage(TMessage message, double time) where TMessage : struct, IMessage
{
messageManager.AddMessage(message, time);
}
///
/// Sends a message after the specified number of seconds, ignoring time dilation.
///
/// The time in seconds that will elapse before the message is sent.
protected void SendMessageIgnoringTimeDilation(TMessage message, double time) where TMessage : struct, IMessage
{
messageManager.AddMessageIgnoringTimeDilation(message, time);
}
///
/// Reads all messages of the specified Type.
///
///
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
///
protected IEnumerable ReadMessages() where TMessage : struct, IMessage
{
if (!receiveTypes.Contains(typeof(TMessage)))
{
throw new IllegalReadException("Engine {0} tried to read undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
}
return messageManager.GetMessagesByType();
}
///
/// Reads an arbitrary message of the specified Type.
///
///
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
///
protected TMessage ReadMessage() where TMessage : struct, IMessage
{
return messageManager.First();
}
///
/// Returns true if a Message of the specified Type has been sent this frame.
///
///
/// Thrown when the Engine does not declare that it Receives the specified Message Type.
///
protected bool SomeMessage() where TMessage : struct, IMessage
{
if (!receiveTypes.Contains(typeof(TMessage)))
{
throw new IllegalReadException("Engine {0} tried to read undeclared Message {1}", GetType().Name, typeof(TMessage).Name);
}
return messageManager.Any();
}
///
/// Destroys the specified Entity. This also removes all of the Components associated with the Entity.
/// Entity destruction takes place after all the Engines have been processed by World Update.
///
protected void Destroy(Entity entity)
{
entityManager.MarkForDestroy(entity.ID);
}
///
/// Destroys an arbitrary Entity containing a Component of the specified Type.
/// Entity destruction takes place after all the Engines have been processed by World Update.
///
protected void DestroyWith() where TComponent : struct, IComponent
{
Destroy(ReadEntity());
}
///
/// Destroys all Entities containing a Component of the specified Type.
/// Entity destruction takes place after all the Engines have been processed by World Update.
///
protected void DestroyAllWith() where TComponent : struct, IComponent
{
foreach (var entity in ReadEntities())
{
Destroy(entity);
}
}
///
/// Removes a Component with the specified type from the given Entity.
/// Note that the Engine must Read the Component type that is being removed.
/// If a Component with the specified type does not exist on the Entity, returns false and does not mutate the Entity.
///
protected void RemoveComponent(Entity entity) where TComponent : struct, IComponent
{
var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : defaultWritePriority;
if (!writeTypes.Contains(typeof(TComponent)))
{
throw new IllegalWriteException("Engine {0} tried to remove undeclared Component {1}. Declare with Writes attribute.", GetType().Name, typeof(TComponent).Name);
}
if (writeImmediateTypes.Contains(typeof(TComponent)))
{
if (componentManager.RemoveImmediate(entity.ID, priority))
{
trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
}
}
else
{
componentManager.Remove(entity.ID, priority);
}
if (componentManager.HasExistingComponent(entity.ID))
{
trackingManager.RegisterRemoval(entity.ID, typeof(TComponent));
}
}
///
/// Activates the Encompass time dilation system.
/// Engines that have the IgnoresTimeDilation property will ignore all time dilation.
/// If multiple time dilations are active they will be averaged.
///
/// The time dilation factor, which is multiplied by real delta time.
/// The time that will elapse before time is fully dilated, in real time.
/// The length of real time that time will be fully dilated.
/// The time that will elapse before time is fully undilated.
protected void ActivateTimeDilation(double factor, double easeInTime, double activeTime, double easeOutTime)
{
timeManager.ActivateTimeDilation(factor, easeInTime, activeTime, easeOutTime);
}
///
/// Activates the Encompass time dilation system.
/// Engines that have the IgnoresTimeDilation property will ignore all time dilation.
/// If multiple time dilations are active they will be averaged.
///
/// The time dilation factor, which is multiplied by real delta time.
/// The time that will elapse before time is fully dilated, in real time.
/// An easing function for the easing in of time dilation.
/// The length of real time that time will be fully dilated.
/// The time that will elapse before time is fully undilated.
protected void ActivateTimeDilation(double factor, double easeInTime, System.Func easeInFunction, double activeTime, double easeOutTime)
{
timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime);
}
///
/// Activates the Encompass time dilation system.
/// Engines that have the IgnoresTimeDilation property will ignore all time dilation.
/// If multiple time dilations are active they will be averaged.
///
/// The time dilation factor, which is multiplied by real delta time.
/// The time that will elapse before time is fully dilated, in real time.
/// The length of real time that time will be fully dilated.
/// The time that will elapse before time is fully undilated.
/// An easing function for the easing out of time dilation.
protected void ActivateTimeDilation(double factor, double easeInTime, double activeTime, double easeOutTime, System.Func easeOutFunction)
{
timeManager.ActivateTimeDilation(factor, easeInTime, activeTime, easeOutTime, easeOutFunction);
}
///
/// Activates the Encompass time dilation system.
/// Engines that have the IgnoresTimeDilation property will ignore all time dilation.
/// If multiple time dilations are active they will be averaged.
///
/// The time dilation factor, which is multiplied by real delta time.
/// The time that will elapse before time is fully dilated, in real time.
/// An easing function for the easing in of time dilation.
/// The length of real time that time will be fully dilated.
/// The time that will elapse before time is fully undilated.
/// An easing function for the easing out of time dilation.
protected void ActivateTimeDilation(double factor, double easeInTime, System.Func easeInFunction, double activeTime, double easeOutTime, System.Func easeOutFunction)
{
timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime, easeOutFunction);
}
///
/// Efficiently reads Messages of a given type that all reference the same Entity.
///
/// The Message subtype.
/// The entity that all messages in the IEnumerable refer to.
///
protected IEnumerable ReadMessagesWithEntity(Entity entity) where TMessage : struct, IMessage, IHasEntity
{
return messageManager.WithEntity(entity.ID);
}
protected bool SomeMessageWithEntity(Entity entity) where TMessage : struct, IMessage, IHasEntity
{
return messageManager.SomeWithEntity(entity.ID);
}
internal void CheckAndUpdateTracking(int entityID)
{
if (_trackedEntities.Contains(entityID) && !entityQuery.CheckEntity(entityID, componentManager.ExistingBits))
{
_trackedEntities.Remove(entityID);
}
else if (!_trackedEntities.Contains(entityID) && entityQuery.CheckEntity(entityID, componentManager.ExistingBits))
{
_trackedEntities.Add(entityID);
}
}
internal void ImmediateCheckAndUpdateTracking(int entityID)
{
if (_trackedEntities.Contains(entityID) && !entityQuery.ImmediateCheckEntity(entityID, componentManager.ImmediateBits, componentManager.ExistingBits))
{
_trackedEntities.Remove(entityID);
}
else if (!_trackedEntities.Contains(entityID) && entityQuery.ImmediateCheckEntity(entityID, componentManager.ImmediateBits, componentManager.ExistingBits))
{
_trackedEntities.Add(entityID);
}
}
///
/// Returns an empty EntitySetQuery. Can be modified and iterated over to obtain Entities that fit the given criteria.
///
internal void BuildEntityQuery()
{
var withMask = BitSet512.Zero;
foreach (var type in queryWithTypes)
{
withMask = withMask.Set(componentManager.TypeToIndex[type]);
}
var withoutMask = BitSet512.Zero;
foreach (var type in queryWithoutTypes)
{
withoutMask = withoutMask.Set(componentManager.TypeToIndex[type]);
}
var immediateMask = BitSet512.Zero;
foreach (var type in readImmediateTypes)
{
immediateMask = immediateMask.Set(componentManager.TypeToIndex[type]);
}
var existingMask = BitSet512.Zero;
foreach (var type in readTypes)
{
existingMask = existingMask.Set(componentManager.TypeToIndex[type]);
}
entityQuery = new EntitySetQuery(
withMask & immediateMask,
withMask & existingMask,
withoutMask & immediateMask,
withoutMask & existingMask,
~withMask
);
}
internal void RegisterDestroyedEntity(int entityID)
{
_trackedEntities.Remove(entityID);
}
}
}