Merge pull request 'Storage Optimizations' (#3) from component_store_optimize into master
continuous-integration/drone/push Build is passing Details

pull/5/head
cosmonaut 2020-03-25 19:08:04 +00:00
commit 8f200fd56f
46 changed files with 1135 additions and 1078 deletions

View File

@ -5,11 +5,11 @@ namespace Encompass
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
public class DefaultWritePriority : Attribute public class DefaultWritePriority : Attribute
{ {
public int writePriority; public int WritePriority { get; }
public DefaultWritePriority(int writePriority) public DefaultWritePriority(int writePriority)
{ {
this.writePriority = writePriority; WritePriority = writePriority;
} }
} }
} }

View File

@ -8,20 +8,13 @@ namespace Encompass
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
public class QueryWith : Attribute public class QueryWith : Attribute
{ {
public readonly HashSet<Type> queryWithTypes = new HashSet<Type>(); public readonly HashSet<Type> QueryWithTypes = new HashSet<Type>();
public QueryWith(params Type[] queryWithTypes) public QueryWith(params Type[] queryWithTypes)
{ {
foreach (var queryWithType in queryWithTypes) foreach (var queryWithType in queryWithTypes)
{ {
var isComponent = queryWithType.GetInterfaces().Contains(typeof(IComponent)); QueryWithTypes.Add(queryWithType);
if (!isComponent)
{
throw new IllegalReadTypeException("{0} must be a Component", queryWithType.Name);
}
this.queryWithTypes.Add(queryWithType);
} }
} }
} }

View File

@ -8,20 +8,13 @@ namespace Encompass
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
public class QueryWithout : Attribute public class QueryWithout : Attribute
{ {
public readonly HashSet<Type> queryWithoutTypes = new HashSet<Type>(); public readonly HashSet<Type> QueryWithoutTypes = new HashSet<Type>();
public QueryWithout(params Type[] queryWithoutTypes) public QueryWithout(params Type[] queryWithoutTypes)
{ {
foreach (var type in queryWithoutTypes) foreach (var type in queryWithoutTypes)
{ {
var isComponent = type.GetInterfaces().Contains(typeof(IComponent)); QueryWithoutTypes.Add(type);
if (!isComponent)
{
throw new IllegalReadTypeException("{0} must be a Component", type.Name);
}
this.queryWithoutTypes.Add(type);
} }
} }
} }

View File

@ -8,20 +8,13 @@ namespace Encompass
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
public class Reads : Attribute public class Reads : Attribute
{ {
public readonly HashSet<Type> readTypes = new HashSet<Type>(); public readonly HashSet<Type> ReadTypes = new HashSet<Type>();
public Reads(params Type[] readTypes) public Reads(params Type[] readTypes)
{ {
foreach (var readType in readTypes) foreach (var readType in readTypes)
{ {
var isComponent = readType.GetInterfaces().Contains(typeof(IComponent)); ReadTypes.Add(readType);
if (!isComponent)
{
throw new IllegalReadTypeException("{0} must be a Component", readType.Name);
}
this.readTypes.Add(readType);
} }
} }
} }

View File

@ -1,27 +1,18 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass namespace Encompass
{ {
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
public class ReadsImmediate : Attribute public class ReadsImmediate : Attribute
{ {
public readonly HashSet<Type> readImmediateTypes = new HashSet<Type>(); public readonly HashSet<Type> ReadImmediateTypes = new HashSet<Type>();
public ReadsImmediate(params Type[] readImmediateTypes) public ReadsImmediate(params Type[] readImmediateTypes)
{ {
foreach (var readImmediateType in readImmediateTypes) foreach (var readImmediateType in readImmediateTypes)
{ {
var isComponent = readImmediateType.GetInterfaces().Contains(typeof(IComponent)); ReadImmediateTypes.Add(readImmediateType);
if (!isComponent)
{
throw new IllegalReadTypeException("{0} must be a Component", readImmediateType.Name);
}
this.readImmediateTypes.Add(readImmediateType);
} }
} }
} }

View File

@ -8,7 +8,7 @@ namespace Encompass
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
public class Receives : Attribute public class Receives : Attribute
{ {
public readonly HashSet<Type> receiveTypes; public readonly HashSet<Type> ReceiveTypes;
public Receives(params Type[] receiveTypes) public Receives(params Type[] receiveTypes)
{ {
@ -21,7 +21,7 @@ namespace Encompass
} }
} }
this.receiveTypes = new HashSet<Type>(receiveTypes); ReceiveTypes = new HashSet<Type>(receiveTypes);
} }
} }
} }

View File

@ -8,7 +8,7 @@ namespace Encompass
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
public class Sends : Attribute public class Sends : Attribute
{ {
public readonly HashSet<Type> sendTypes; public readonly HashSet<Type> SendTypes;
public Sends(params Type[] sendTypes) public Sends(params Type[] sendTypes)
{ {
@ -21,7 +21,7 @@ namespace Encompass
} }
} }
this.sendTypes = new HashSet<Type>(sendTypes); this.SendTypes = new HashSet<Type>(sendTypes);
} }
} }
} }

View File

@ -8,33 +8,21 @@ namespace Encompass
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class Writes : Attribute public class Writes : Attribute
{ {
public readonly HashSet<Type> writeTypes = new HashSet<Type>(); public readonly HashSet<Type> WriteTypes = new HashSet<Type>();
public Dictionary<Type, int> priorities = new Dictionary<Type, int>(); public readonly Dictionary<Type, int> Priorities = new Dictionary<Type, int>();
public Writes(params Type[] writeTypes) public Writes(params Type[] writeTypes)
{ {
foreach (var writeType in writeTypes) foreach (var writeType in writeTypes)
{ {
var isComponent = writeType.GetInterfaces().Contains(typeof(IComponent)); WriteTypes.Add(writeType);
if (!isComponent)
{
throw new IllegalWriteTypeException("{0} must be a Component", writeType.Name);
}
this.writeTypes.Add(writeType);
} }
} }
public Writes(Type writeType, int priority) public Writes(Type writeType, int priority)
{ {
var isComponent = writeType.GetInterfaces().Contains(typeof(IComponent)); WriteTypes.Add(writeType);
if (!isComponent) Priorities.Add(writeType, priority);
{
throw new IllegalWriteTypeException("{0} must be a Component", writeType.Name);
}
writeTypes.Add(writeType);
priorities.Add(writeType, priority);
} }
} }
} }

View File

@ -1,26 +1,18 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using Encompass.Exceptions;
namespace Encompass namespace Encompass
{ {
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]
public class WritesImmediate : Attribute public class WritesImmediate : Attribute
{ {
public readonly HashSet<Type> writeImmediateTypes = new HashSet<Type>(); public readonly HashSet<Type> WriteImmediateTypes = new HashSet<Type>();
public WritesImmediate(params Type[] writeImmediateTypes) public WritesImmediate(params Type[] writeImmediateTypes)
{ {
foreach (var writeImmediateType in writeImmediateTypes) foreach (var writeImmediateType in writeImmediateTypes)
{ {
var isComponent = writeImmediateType.GetInterfaces().Contains(typeof(IComponent)); WriteImmediateTypes.Add(writeImmediateType);
if (!isComponent)
{
throw new IllegalWriteImmediateTypeException("{0} must be a Component", writeImmediateType.Name);
}
this.writeImmediateTypes.Add(writeImmediateType);
} }
} }
} }

View File

@ -6,49 +6,49 @@ namespace Encompass
{ {
internal class ComponentBitSet internal class ComponentBitSet
{ {
Dictionary<int, BitSet512> entities = new Dictionary<int, BitSet512>(); private readonly Dictionary<int, BitSet512> _entities = new Dictionary<int, BitSet512>();
Dictionary<Type, int> TypeToIndex { get; } private readonly Dictionary<Type, int> _typeToIndex;
public ComponentBitSet(Dictionary<Type, int> typeToIndex) public ComponentBitSet(Dictionary<Type, int> typeToIndex)
{ {
TypeToIndex = typeToIndex; _typeToIndex = typeToIndex;
} }
public void Clear() public void Clear()
{ {
entities.Clear(); _entities.Clear();
} }
public void AddEntity(int entityID) public void AddEntity(int entityID)
{ {
entities.Add(entityID, BitSet512.Zero); _entities.Add(entityID, BitSet512.Zero);
} }
public void Set<TComponent>(int entityID) where TComponent : struct, IComponent public void Set<TComponent>(int entityID) where TComponent : struct
{ {
if (!entities.ContainsKey(entityID)) { AddEntity(entityID); } if (!_entities.ContainsKey(entityID)) { AddEntity(entityID); }
entities[entityID] = entities[entityID].Set(TypeToIndex[typeof(TComponent)]); _entities[entityID] = _entities[entityID].Set(_typeToIndex[typeof(TComponent)]);
} }
public void RemoveComponent<TComponent>(int entityID) where TComponent : struct, IComponent public void RemoveComponent<TComponent>(int entityID) where TComponent : struct
{ {
if (entities.ContainsKey(entityID)) if (_entities.ContainsKey(entityID))
{ {
entities[entityID] = entities[entityID].UnSet(TypeToIndex[typeof(TComponent)]); _entities[entityID] = _entities[entityID].UnSet(_typeToIndex[typeof(TComponent)]);
} }
} }
public void RemoveEntity(int entityID) public void RemoveEntity(int entityID)
{ {
if (entities.ContainsKey(entityID)) if (_entities.ContainsKey(entityID))
{ {
entities.Remove(entityID); _entities.Remove(entityID);
} }
} }
public BitSet512 EntityBitArray(int entityID) public BitSet512 EntityBitArray(int entityID)
{ {
return entities.ContainsKey(entityID) ? entities[entityID] : BitSet512.Zero; return _entities.ContainsKey(entityID) ? _entities[entityID] : BitSet512.Zero;
} }
} }
} }

View File

@ -3,39 +3,48 @@ using System.Collections.Generic;
namespace Encompass namespace Encompass
{ {
internal class ComponentDeltaStore : ComponentStore internal class ComponentDeltaStore
{ {
private readonly ComponentStore _store;
private readonly Dictionary<Type, Replayer> _replayers = new Dictionary<Type, Replayer>(); private readonly Dictionary<Type, Replayer> _replayers = new Dictionary<Type, Replayer>();
private readonly HashSet<Replayer> _currentReplayers = new HashSet<Replayer>(); private readonly HashSet<Replayer> _currentReplayers = new HashSet<Replayer>();
public IEnumerable<Replayer> CurrentReplayers { get { return _currentReplayers; } } public IEnumerable<Replayer> CurrentReplayers { get { return _currentReplayers; } }
public ComponentDeltaStore(Dictionary<Type, int> typeToIndex) : base(typeToIndex) public ComponentDeltaStore(Dictionary<Type, int> typeToIndex)
{ {
_store = new ComponentStore(typeToIndex);
} }
public override void RegisterComponentType<TComponent>() public void RegisterComponentType<TComponent>() where TComponent : struct
{ {
base.RegisterComponentType<TComponent>(); _store.RegisterComponentType<TComponent>();
if (!_replayers.ContainsKey(typeof(TComponent))) if (!_replayers.ContainsKey(typeof(TComponent)))
{ {
_replayers.Add(typeof(TComponent), new Replayer<TComponent>(this)); _replayers.Add(typeof(TComponent), new Replayer<TComponent>(this));
} }
} }
public override void Set<TComponent>(int entityID, TComponent component) public ref readonly TComponent GetComponent<TComponent>(int entityID) where TComponent : struct
{ {
base.Set(entityID, component); return ref _store.Get<TComponent>(entityID);
}
public void Set<TComponent>(int entityID, in TComponent component) where TComponent : struct
{
_store.Set(entityID, component);
RegisterComponentType<TComponent>();
var replayer = _replayers[typeof(TComponent)]; var replayer = _replayers[typeof(TComponent)];
_currentReplayers.Add(replayer); _currentReplayers.Add(replayer);
replayer.UnMarkRemoval(entityID); replayer.UnMarkRemoval(entityID);
} }
public override bool Set<TComponent>(int entityID, TComponent component, int priority) public bool Set<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct
{ {
var result = base.Set(entityID, component, priority); var result = _store.Set(entityID, component, priority);
if (result) if (result)
{ {
RegisterComponentType<TComponent>();
var replayer = _replayers[typeof(TComponent)]; var replayer = _replayers[typeof(TComponent)];
_currentReplayers.Add(replayer); _currentReplayers.Add(replayer);
replayer.UnMarkRemoval(entityID); replayer.UnMarkRemoval(entityID);
@ -43,11 +52,12 @@ namespace Encompass
return result; return result;
} }
public override bool Remove<TComponent>(int entityID, int priority) public bool Remove<TComponent>(int entityID, int priority) where TComponent : struct
{ {
var result = base.Remove<TComponent>(entityID, priority); var result = _store.Remove<TComponent>(entityID, priority);
if (result) if (result)
{ {
RegisterComponentType<TComponent>();
var replayer = _replayers[typeof(TComponent)]; var replayer = _replayers[typeof(TComponent)];
_currentReplayers.Add(replayer); _currentReplayers.Add(replayer);
replayer.MarkRemoval(entityID); replayer.MarkRemoval(entityID);
@ -55,18 +65,23 @@ namespace Encompass
return result; return result;
} }
public override void Remove(int entityID) public void Remove(int entityID)
{ {
base.Remove(entityID); _store.Remove(entityID);
foreach (var replayer in CurrentReplayers) foreach (var replayer in CurrentReplayers)
{ {
replayer.MarkRemoval(entityID); replayer.MarkRemoval(entityID);
} }
} }
public override void ClearAll() public Span<Entity> AllEntities<TComponent>() where TComponent : struct
{ {
base.ClearAll(); return _store.AllEntities<TComponent>();
}
public void ClearAll()
{
_store.ClearAll();
foreach (var replayer in _currentReplayers) foreach (var replayer in _currentReplayers)
{ {
replayer.Clear(); replayer.Clear();

View File

@ -4,46 +4,42 @@ using System.Collections.Generic;
namespace Encompass namespace Encompass
{ {
internal class ComponentStore internal sealed class ComponentStore
{ {
private Dictionary<Type, TypedComponentStore> Stores = new Dictionary<Type, TypedComponentStore>(512); private Dictionary<Type, TypedComponentStore> _stores = new Dictionary<Type, TypedComponentStore>(512);
public ComponentBitSet ComponentBitSet { get; private set; } public ComponentBitSet ComponentBitSet { get; private set; }
private readonly Dictionary<Type, int> _typeToIndex;
public ComponentStore(Dictionary<Type, int> typeToIndex) public ComponentStore(Dictionary<Type, int> typeToIndex)
{ {
_typeToIndex = typeToIndex;
ComponentBitSet = new ComponentBitSet(typeToIndex); ComponentBitSet = new ComponentBitSet(typeToIndex);
} }
public IEnumerable<(Type, TypedComponentStore)> StoresEnumerable() public void RegisterComponentType<TComponent>() where TComponent : struct
{ {
foreach (var entry in Stores) if (!_stores.ContainsKey(typeof(TComponent)))
{
yield return (entry.Key, entry.Value);
}
}
public virtual void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
{
if (!Stores.ContainsKey(typeof(TComponent)))
{ {
var store = new TypedComponentStore<TComponent>(); var store = new TypedComponentStore<TComponent>();
Stores.Add(typeof(TComponent), store); _stores.Add(typeof(TComponent), store);
} }
if (!_typeToIndex.ContainsKey(typeof(TComponent))) { _typeToIndex.Add(typeof(TComponent), _typeToIndex.Count); }
} }
private TypedComponentStore<TComponent> Lookup<TComponent>() where TComponent : struct, IComponent private TypedComponentStore<TComponent> Lookup<TComponent>() where TComponent : struct
{ {
return Stores[typeof(TComponent)] as TypedComponentStore<TComponent>; return _stores[typeof(TComponent)] as TypedComponentStore<TComponent>;
} }
public bool Has<TComponent>(int entityID) where TComponent : struct, IComponent public bool Has<TComponent>(int entityID) where TComponent : struct
{ {
return Lookup<TComponent>().Has(entityID); return Lookup<TComponent>().Has(entityID);
} }
public bool Has(Type type, int entityID) public bool Has(Type type, int entityID)
{ {
return Stores.ContainsKey(type) && Stores[type].Has(entityID); return _stores[type].Has(entityID);
} }
public BitSet512 EntityBitArray(int entityID) public BitSet512 EntityBitArray(int entityID)
@ -51,18 +47,28 @@ namespace Encompass
return ComponentBitSet.EntityBitArray(entityID); return ComponentBitSet.EntityBitArray(entityID);
} }
public TComponent Get<TComponent>(int entityID) where TComponent : struct, IComponent public ref TComponent Get<TComponent>(int entityID) where TComponent : struct
{ {
return Lookup<TComponent>().Get(entityID); return ref Lookup<TComponent>().Get(entityID);
} }
public virtual void Set<TComponent>(int entityID, TComponent component) where TComponent : struct, IComponent public ref readonly TComponent Singular<TComponent>() where TComponent : struct
{
return ref Lookup<TComponent>().Singular();
}
public ref readonly Entity SingularEntity<TComponent>() where TComponent : struct
{
return ref Lookup<TComponent>().SingularEntity();
}
public void Set<TComponent>(int entityID, in TComponent component) where TComponent : struct
{ {
Lookup<TComponent>().Set(entityID, component); Lookup<TComponent>().Set(entityID, component);
ComponentBitSet.Set<TComponent>(entityID); ComponentBitSet.Set<TComponent>(entityID);
} }
public virtual bool Set<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct, IComponent public bool Set<TComponent>(int entityID, in TComponent component, int priority) where TComponent : struct
{ {
if (Lookup<TComponent>().Set(entityID, component, priority)) if (Lookup<TComponent>().Set(entityID, component, priority))
{ {
@ -72,7 +78,7 @@ namespace Encompass
return false; return false;
} }
public virtual bool Remove<TComponent>(int entityID, int priority) where TComponent : struct, IComponent public bool Remove<TComponent>(int entityID, int priority) where TComponent : struct
{ {
if (Lookup<TComponent>().Remove(entityID, priority)) if (Lookup<TComponent>().Remove(entityID, priority))
{ {
@ -82,59 +88,53 @@ namespace Encompass
return false; return false;
} }
public void ForceRemove<TComponent>(int entityID) where TComponent : struct, IComponent public void ForceRemove<TComponent>(int entityID) where TComponent : struct
{ {
Lookup<TComponent>().ForceRemove(entityID); Lookup<TComponent>().ForceRemove(entityID);
ComponentBitSet.RemoveComponent<TComponent>(entityID); ComponentBitSet.RemoveComponent<TComponent>(entityID);
} }
public virtual void Remove(int entityID) public void Remove(int entityID)
{ {
foreach (var entry in Stores.Values) foreach (var entry in _stores.Values)
{ {
entry.ForceRemove(entityID); entry.ForceRemove(entityID);
} }
ComponentBitSet.RemoveEntity(entityID); ComponentBitSet.RemoveEntity(entityID);
} }
public bool Any<TComponent>() where TComponent : struct, IComponent public bool Any<TComponent>() where TComponent : struct
{ {
return Lookup<TComponent>().Count > 0; return Lookup<TComponent>().Count > 0;
} }
public IEnumerable<(int, Type, IComponent)> AllInterfaceTyped() public Span<TComponent> All<TComponent>() where TComponent : struct
{ {
foreach (var store in Stores.Values) return Lookup<TComponent>().AllComponents();
{
foreach (var thing in store.AllInterfaceTyped())
{
yield return thing;
}
}
} }
public IEnumerable<(TComponent, int)> All<TComponent>() where TComponent : struct, IComponent public Span<Entity> AllEntities<TComponent>() where TComponent : struct
{ {
return Lookup<TComponent>().All(); return Lookup<TComponent>().AllEntities();
} }
public void Clear<TComponent>() where TComponent : struct, IComponent public void Clear<TComponent>() where TComponent : struct
{ {
Lookup<TComponent>().Clear(); Lookup<TComponent>().Clear();
} }
public virtual void ClearAllPriorities() public void ClearAllPriorities()
{ {
foreach (var store in Stores.Values) foreach (var store in _stores.Values)
{ {
store.ClearPriorities(); store.ClearPriorities();
} }
} }
public virtual void ClearAll() public void ClearAll()
{ {
ComponentBitSet.Clear(); ComponentBitSet.Clear();
foreach (var store in Stores.Values) foreach (var store in _stores.Values)
{ {
store.Clear(); store.Clear();
} }
@ -142,7 +142,7 @@ namespace Encompass
public void SwapWith(ComponentStore other) public void SwapWith(ComponentStore other)
{ {
(Stores, other.Stores) = (other.Stores, Stores); (_stores, other._stores) = (other._stores, _stores);
(ComponentBitSet, other.ComponentBitSet) = (other.ComponentBitSet, ComponentBitSet); (ComponentBitSet, other.ComponentBitSet) = (other.ComponentBitSet, ComponentBitSet);
} }

View File

@ -5,40 +5,40 @@ namespace Encompass
{ {
internal class MessageStore internal class MessageStore
{ {
private Dictionary<Type, TypedMessageStore> Stores = new Dictionary<Type, TypedMessageStore>(512); private readonly Dictionary<Type, TypedMessageStore> _stores = new Dictionary<Type, TypedMessageStore>(512);
private void RegisterMessageType<TMessage>() where TMessage : struct, IMessage private void RegisterMessageType<TMessage>() where TMessage : struct, IMessage
{ {
Stores.Add(typeof(TMessage), new TypedMessageStore<TMessage>()); _stores.Add(typeof(TMessage), new TypedMessageStore<TMessage>());
} }
private TypedMessageStore<TMessage> Lookup<TMessage>() where TMessage : struct, IMessage private TypedMessageStore<TMessage> Lookup<TMessage>() where TMessage : struct, IMessage
{ {
if (!Stores.ContainsKey(typeof(TMessage))) { RegisterMessageType<TMessage>(); } if (!_stores.ContainsKey(typeof(TMessage))) { RegisterMessageType<TMessage>(); }
return Stores[typeof(TMessage)] as TypedMessageStore<TMessage>; return _stores[typeof(TMessage)] as TypedMessageStore<TMessage>;
} }
public void AddMessage<TMessage>(TMessage message) where TMessage : struct, IMessage public void AddMessage<TMessage>(in TMessage message) where TMessage : struct, IMessage
{ {
Lookup<TMessage>().Add(message); Lookup<TMessage>().Add(message);
} }
public void AddMessage<TMessage>(TMessage message, double time) where TMessage : struct, IMessage public void AddMessage<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{ {
Lookup<TMessage>().Add(message, time); Lookup<TMessage>().Add(message, time);
} }
public void AddMessageIgnoringTimeDilation<TMessage>(TMessage message, double time) where TMessage : struct, IMessage public void AddMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{ {
Lookup<TMessage>().AddIgnoringTimeDilation(message, time); Lookup<TMessage>().AddIgnoringTimeDilation(message, time);
} }
public TMessage First<TMessage>() where TMessage : struct, IMessage public ref readonly TMessage First<TMessage>() where TMessage : struct, IMessage
{ {
return Lookup<TMessage>().First(); return ref Lookup<TMessage>().First();
} }
public IEnumerable<TMessage> All<TMessage>() where TMessage : struct, IMessage public Span<TMessage> All<TMessage>() where TMessage : struct, IMessage
{ {
return Lookup<TMessage>().All(); return Lookup<TMessage>().All();
} }
@ -53,6 +53,11 @@ namespace Encompass
return Lookup<TMessage>().WithEntity(entityID); return Lookup<TMessage>().WithEntity(entityID);
} }
public ref readonly TMessage FirstWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage
{
return ref Lookup<TMessage>().FirstWithEntity(entityID);
}
public bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity public bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
{ {
return Lookup<TMessage>().SomeWithEntity(entityID); return Lookup<TMessage>().SomeWithEntity(entityID);
@ -60,7 +65,7 @@ namespace Encompass
public void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta) public void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta)
{ {
foreach (var store in Stores.Values) foreach (var store in _stores.Values)
{ {
store.ProcessDelayedMessages(dilatedDelta, realtimeDelta); store.ProcessDelayedMessages(dilatedDelta, realtimeDelta);
} }
@ -68,7 +73,7 @@ namespace Encompass
public void ClearAll() public void ClearAll()
{ {
foreach (var store in Stores.Values) foreach (var store in _stores.Values)
{ {
store.Clear(); store.Clear();
} }

View File

@ -2,7 +2,7 @@
namespace Encompass namespace Encompass
{ {
internal abstract class Replayer internal abstract class Replayer
{ {
public abstract void Replay(ComponentStore store); public abstract void Replay(ComponentStore store);
public abstract void MarkRemoval(int entityID); public abstract void MarkRemoval(int entityID);
@ -10,7 +10,7 @@ namespace Encompass
public abstract void Clear(); public abstract void Clear();
} }
internal class Replayer<TComponent> : Replayer where TComponent : struct, IComponent internal class Replayer<TComponent> : Replayer where TComponent : struct
{ {
private readonly ComponentDeltaStore _deltaStore; private readonly ComponentDeltaStore _deltaStore;
private readonly HashSet<int> _removals = new HashSet<int>(); private readonly HashSet<int> _removals = new HashSet<int>();
@ -22,9 +22,10 @@ namespace Encompass
public override void Replay(ComponentStore store) public override void Replay(ComponentStore store)
{ {
foreach (var (component, entityID) in _deltaStore.All<TComponent>()) foreach (ref readonly var entity in _deltaStore.AllEntities<TComponent>())
{ {
store.Set(entityID, component); ref readonly var component = ref _deltaStore.GetComponent<TComponent>(entity.ID);
store.Set(entity.ID, component);
} }
foreach (var entityID in _removals) foreach (var entityID in _removals)

View File

@ -1,12 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Encompass namespace Encompass
{ {
internal abstract class TypedComponentStore internal abstract class TypedComponentStore
{ {
public abstract int Count { get; } public abstract int Count { get; }
public abstract IEnumerable<(int, Type, IComponent)> AllInterfaceTyped();
public abstract bool Has(int entity); public abstract bool Has(int entity);
public abstract bool Remove(int entity, int priority); public abstract bool Remove(int entity, int priority);
public abstract void ForceRemove(int entity); public abstract void ForceRemove(int entity);
@ -14,43 +14,72 @@ namespace Encompass
public abstract void ClearPriorities(); public abstract void ClearPriorities();
} }
internal class TypedComponentStore<TComponent> : TypedComponentStore where TComponent : struct, IComponent internal class TypedComponentStore<TComponent> : TypedComponentStore where TComponent : struct
{ {
private readonly Dictionary<int, TComponent> store = new Dictionary<int, TComponent>(128); private int _nextID = 0;
private readonly Dictionary<int, int> priorities = new Dictionary<int, int>(128); private readonly Dictionary<int, int> _entityIDToStorageIndex = new Dictionary<int, int>(128);
private readonly Dictionary<int, int> _priorities = new Dictionary<int, int>(128);
private Entity[] _storageIndexToEntities = new Entity[128];
private TComponent[] _components = new TComponent[128];
public override int Count { get => store.Count; } public override int Count { get => _entityIDToStorageIndex.Count; }
public TComponent Get(int entityID) public ref TComponent Get(int entityID)
{ {
if (!store.ContainsKey(entityID)) { throw new Exceptions.NoComponentOfTypeOnEntityException("No component of type {0} exists on Entity with ID {1}", typeof(TComponent), entityID); } if (!_entityIDToStorageIndex.ContainsKey(entityID)) { throw new Exceptions.NoComponentOfTypeOnEntityException("No component of type {0} exists on Entity with ID {1}", typeof(TComponent), entityID); }
return store[entityID]; return ref _components[_entityIDToStorageIndex[entityID]];
} }
public void Set(int entityID, TComponent component) public ref TComponent Singular()
{ {
store[entityID] = component; return ref _components[0];
} }
public bool Set(int entityID, TComponent component, int priority) public ref Entity SingularEntity()
{ {
if (!priorities.ContainsKey(entityID) || priority < priorities[entityID]) return ref _storageIndexToEntities[0];
}
public void Set(int entityID, in TComponent component)
{
InternalSet(entityID, component);
}
public bool Set(int entityID, in TComponent component, int priority)
{
if (!_priorities.ContainsKey(entityID) || priority <= _priorities[entityID]) // if priorities are equal that means it's the same engine
{ {
store[entityID] = component; InternalSet(entityID, component);
priorities[entityID] = priority; _priorities[entityID] = priority;
return true; return true;
} }
return false; return false;
} }
private void InternalSet(int entityID, in TComponent component)
{
if (!_entityIDToStorageIndex.ContainsKey(entityID))
{
var index = _nextID++;
if (index >= _components.Length)
{
System.Array.Resize(ref _components, _components.Length * 2);
System.Array.Resize(ref _storageIndexToEntities, _storageIndexToEntities.Length * 2);
}
_entityIDToStorageIndex[entityID] = index;
_storageIndexToEntities[index] = new Entity(entityID);
}
_components[_entityIDToStorageIndex[entityID]] = component;
}
public override bool Remove(int entityID, int priority) public override bool Remove(int entityID, int priority)
{ {
if (!priorities.ContainsKey(entityID) || priority < priorities[entityID]) if (!_priorities.ContainsKey(entityID) || priority <= _priorities[entityID]) // if priorities are equal that means it's the same engine
{ {
priorities[entityID] = priority; _priorities[entityID] = priority;
store.Remove(entityID); ForceRemove(entityID);
priorities.Remove(entityID);
return true; return true;
} }
@ -59,40 +88,53 @@ namespace Encompass
public override void ForceRemove(int entityID) public override void ForceRemove(int entityID)
{ {
store.Remove(entityID); if (_entityIDToStorageIndex.ContainsKey(entityID))
priorities.Remove(entityID); {
var storageIndex = _entityIDToStorageIndex[entityID];
_entityIDToStorageIndex.Remove(entityID);
_priorities.Remove(entityID);
// move a component into the hole to maintain contiguous memory
if (_nextID > 1 && storageIndex != _nextID - 1)
{
var lastStorageIndex = _nextID - 1;
ref readonly var lastEntity = ref _storageIndexToEntities[lastStorageIndex];
_entityIDToStorageIndex[lastEntity.ID] = storageIndex;
_storageIndexToEntities[storageIndex] = lastEntity;
_components[storageIndex] = _components[lastStorageIndex];
}
_nextID--;
}
} }
public override bool Has(int entityID) public override bool Has(int entityID)
{ {
return store.ContainsKey(entityID); return _entityIDToStorageIndex.ContainsKey(entityID);
} }
public override void Clear() public override void Clear()
{ {
store.Clear(); _nextID = 0;
priorities.Clear(); _entityIDToStorageIndex.Clear();
_priorities.Clear();
} }
public override void ClearPriorities() public override void ClearPriorities()
{ {
priorities.Clear(); _priorities.Clear();
} }
public IEnumerable<(TComponent, int)> All() public Span<Entity> AllEntities()
{ {
foreach (var kvp in store) return new Span<Entity>(_storageIndexToEntities, 0, _nextID);
{
yield return (kvp.Value, kvp.Key);
}
} }
public override IEnumerable<(int, Type, IComponent)> AllInterfaceTyped() public Span<TComponent> AllComponents()
{ {
foreach (var kvp in store) return new Span<TComponent>(_components, 0, _nextID);
{
yield return (kvp.Key, typeof(TComponent), (IComponent)kvp.Value);
}
} }
} }
} }

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Encompass namespace Encompass
@ -10,98 +11,117 @@ namespace Encompass
internal class TypedMessageStore<TMessage> : TypedMessageStore where TMessage : struct, IMessage internal class TypedMessageStore<TMessage> : TypedMessageStore where TMessage : struct, IMessage
{ {
private readonly List<TMessage> store = new List<TMessage>(128); // messages are placed in a contiguous region
private readonly List<(TMessage, double)> delayedStore = new List<(TMessage, double)>(128); // so we can return the collection as a Span
private readonly List<(TMessage, double)> delayedStoreIgnoringTimeDilation = new List<(TMessage, double)>(128); private int _nextIndex = 0;
private readonly Dictionary<int, List<TMessage>> entityToMessage = new Dictionary<int, List<TMessage>>(); private TMessage[] _store = new TMessage[128];
private readonly List<(TMessage, double)> _delayedStore = new List<(TMessage, double)>(128);
private readonly List<(TMessage, double)> _delayedStoreIgnoringTimeDilation = new List<(TMessage, double)>(128);
private readonly Dictionary<int, List<int>> _entityToIndices = new Dictionary<int, List<int>>();
public override void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta) public override void ProcessDelayedMessages(double dilatedDelta, double realtimeDelta)
{ {
for (int i = delayedStore.Count - 1; i >= 0; i--) for (var i = _delayedStore.Count - 1; i >= 0; i--)
{ {
var (message, time) = delayedStore[i]; var (message, time) = _delayedStore[i];
var updatedTime = time - dilatedDelta; var updatedTime = time - dilatedDelta;
if (updatedTime <= 0) if (updatedTime <= 0)
{ {
Add(message); Add(message);
delayedStore.RemoveAt(i); _delayedStore.RemoveAt(i);
} }
else else
{ {
delayedStore[i] = (message, updatedTime); _delayedStore[i] = (message, updatedTime);
} }
} }
for (int i = delayedStoreIgnoringTimeDilation.Count - 1; i >= 0; i--) for (var i = _delayedStoreIgnoringTimeDilation.Count - 1; i >= 0; i--)
{ {
var (message, time) = delayedStoreIgnoringTimeDilation[i]; var (message, time) = _delayedStoreIgnoringTimeDilation[i];
var updatedTime = time - realtimeDelta; var updatedTime = time - realtimeDelta;
if (updatedTime <= 0) if (updatedTime <= 0)
{ {
Add(message); Add(message);
delayedStoreIgnoringTimeDilation.RemoveAt(i); _delayedStoreIgnoringTimeDilation.RemoveAt(i);
} }
else else
{ {
delayedStoreIgnoringTimeDilation[i] = (message, updatedTime); _delayedStoreIgnoringTimeDilation[i] = (message, updatedTime);
} }
} }
} }
public void Add(TMessage message) public void Add(in TMessage message)
{ {
store.Add(message); var index = _nextIndex++;
if (index >= _store.Length)
{
Array.Resize(ref _store, _store.Length * 2);
}
_store[index] = message;
if (message is IHasEntity entityMessage) if (message is IHasEntity entityMessage)
{ {
var entityID = entityMessage.Entity.ID; var entityID = entityMessage.Entity.ID;
if (!entityToMessage.ContainsKey(entityID)) { entityToMessage.Add(entityID, new List<TMessage>()); } if (!_entityToIndices.ContainsKey(entityID)) { _entityToIndices.Add(entityID, new List<int>()); }
entityToMessage[entityID].Add(message); _entityToIndices[entityID].Add(index);
} }
} }
public void Add(TMessage message, double time) public void Add(in TMessage message, double time)
{ {
delayedStore.Add((message, time)); _delayedStore.Add((message, time));
} }
public void AddIgnoringTimeDilation(TMessage message, double time) public void AddIgnoringTimeDilation(in TMessage message, double time)
{ {
delayedStoreIgnoringTimeDilation.Add((message, time)); _delayedStoreIgnoringTimeDilation.Add((message, time));
} }
public TMessage First() public ref readonly TMessage First()
{ {
return store[0]; return ref _store[0];
} }
public bool Any() public bool Any()
{ {
return store.Count > 0; return _nextIndex != 0;
} }
public IEnumerable<TMessage> All() public Span<TMessage> All()
{ {
return store; return new Span<TMessage>(_store, 0, _nextIndex);
} }
public IEnumerable<TMessage> WithEntity(int entityID) public IEnumerable<TMessage> WithEntity(int entityID)
{ {
return entityToMessage.ContainsKey(entityID) ? entityToMessage[entityID] : System.Linq.Enumerable.Empty<TMessage>(); if (_entityToIndices.ContainsKey(entityID))
{
foreach (var index in _entityToIndices[entityID])
{
yield return _store[index];
}
}
}
public ref readonly TMessage FirstWithEntity(int entityID)
{
return ref _store[_entityToIndices[entityID][0]];
} }
public bool SomeWithEntity(int entityID) public bool SomeWithEntity(int entityID)
{ {
return entityToMessage.ContainsKey(entityID) && entityToMessage[entityID].Count > 0; return _entityToIndices.ContainsKey(entityID) && _entityToIndices[entityID].Count > 0;
} }
public override void Clear() public override void Clear()
{ {
store.Clear(); _nextIndex = 0;
foreach (var set in entityToMessage.Values) foreach (var set in _entityToIndices.Values)
{ {
set.Clear(); set.Clear();
} }

View File

@ -1,272 +1,289 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using MoonTools.FastCollections;
namespace Encompass namespace Encompass
{ {
internal class ComponentManager internal class ComponentManager
{ {
private readonly DrawLayerManager drawLayerManager; private readonly DrawLayerManager _drawLayerManager;
private readonly ComponentStore existingComponentStore; private readonly ComponentStore _existingComponentStore;
private readonly ComponentStore immediateComponentStore; private readonly ComponentStore _immediateComponentStore;
private readonly ComponentDeltaStore replayStore; private readonly ComponentDeltaStore _replayStore;
private ComponentStore upToDateComponentStore; private readonly ComponentStore _upToDateComponentStore;
public Dictionary<Type, int> TypeToIndex { get; } public Dictionary<Type, int> TypeToIndex { get; }
private readonly HashSet<int> entitiesMarkedForRemoval = new HashSet<int>(); private readonly HashSet<int> _entitiesMarkedForRemoval = new HashSet<int>();
internal ComponentBitSet ImmediateBits { get { return immediateComponentStore.ComponentBitSet; } } internal ComponentBitSet ImmediateBits { get { return _immediateComponentStore.ComponentBitSet; } }
internal ComponentBitSet ExistingBits { get { return existingComponentStore.ComponentBitSet; } } internal ComponentBitSet ExistingBits { get { return _existingComponentStore.ComponentBitSet; } }
public ComponentManager(DrawLayerManager drawLayerManager, Dictionary<Type, int> typeToIndex) public ComponentManager(DrawLayerManager drawLayerManager, Dictionary<Type, int> typeToIndex)
{ {
this.drawLayerManager = drawLayerManager; this._drawLayerManager = drawLayerManager;
existingComponentStore = new ComponentStore(typeToIndex); _existingComponentStore = new ComponentStore(typeToIndex);
immediateComponentStore = new ComponentStore(typeToIndex); _immediateComponentStore = new ComponentStore(typeToIndex);
replayStore = new ComponentDeltaStore(typeToIndex); _replayStore = new ComponentDeltaStore(typeToIndex);
upToDateComponentStore = new ComponentStore(typeToIndex); _upToDateComponentStore = new ComponentStore(typeToIndex);
TypeToIndex = typeToIndex; TypeToIndex = typeToIndex;
} }
public void RegisterComponentType<TComponent>() where TComponent : struct, IComponent public void RegisterComponentType<TComponent>() where TComponent : struct
{ {
existingComponentStore.RegisterComponentType<TComponent>(); _existingComponentStore.RegisterComponentType<TComponent>();
immediateComponentStore.RegisterComponentType<TComponent>(); _immediateComponentStore.RegisterComponentType<TComponent>();
replayStore.RegisterComponentType<TComponent>(); _replayStore.RegisterComponentType<TComponent>();
upToDateComponentStore.RegisterComponentType<TComponent>(); _upToDateComponentStore.RegisterComponentType<TComponent>();
} }
internal void SetExistingComponentStore(ComponentStore componentStore) internal void SetExistingComponentStore(ComponentStore componentStore)
{ {
existingComponentStore.SwapWith(componentStore); _existingComponentStore.SwapWith(componentStore);
} }
internal void SetUpToDateComponentStore(ComponentStore componentStore) internal void SetUpToDateComponentStore(ComponentStore componentStore)
{ {
upToDateComponentStore.SwapWith(componentStore); _upToDateComponentStore.SwapWith(componentStore);
} }
internal void RegisterDrawableComponent<TComponent>(int entityID, TComponent component, int layer) where TComponent : struct, IComponent internal void RegisterDrawableComponent<TComponent>(int entityID, int layer) where TComponent : struct
{ {
drawLayerManager.RegisterComponentWithLayer(entityID, component, layer); _drawLayerManager.RegisterComponentWithLayer<TComponent>(entityID, layer);
} }
internal void WriteComponents() internal void WriteComponents()
{ {
existingComponentStore.UpdateUsing(replayStore); _existingComponentStore.UpdateUsing(_replayStore);
existingComponentStore.ClearAllPriorities(); _existingComponentStore.ClearAllPriorities();
upToDateComponentStore.ClearAllPriorities(); _upToDateComponentStore.ClearAllPriorities();
immediateComponentStore.ClearAll(); _immediateComponentStore.ClearAll();
replayStore.ClearAll(); _replayStore.ClearAll();
} }
internal bool AddImmediateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct, IComponent internal bool AddImmediateComponent<TComponent>(int entityID, in TComponent component, int priority) where TComponent : struct
{ {
if (immediateComponentStore.Set(entityID, component, priority)) if (_immediateComponentStore.Set(entityID, component, priority))
{ {
replayStore.Set(entityID, component); _replayStore.Set(entityID, component);
upToDateComponentStore.Set(entityID, component); _upToDateComponentStore.Set(entityID, component);
return true; return true;
} }
return false; return false;
} }
internal void AddImmediateComponent<TComponent>(int entityID, TComponent component) where TComponent : struct, IComponent internal void AddImmediateComponent<TComponent>(int entityID, in TComponent component) where TComponent : struct
{ {
immediateComponentStore.Set(entityID, component); _immediateComponentStore.Set(entityID, component);
replayStore.Set(entityID, component); _replayStore.Set(entityID, component);
upToDateComponentStore.Set(entityID, component); _upToDateComponentStore.Set(entityID, component);
} }
internal bool UpdateComponent<TComponent>(int entityID, TComponent component, int priority) where TComponent : struct, IComponent internal bool UpdateComponent<TComponent>(int entityID, in TComponent component, int priority) where TComponent : struct
{ {
var result = upToDateComponentStore.Set(entityID, component, priority); var result = _upToDateComponentStore.Set(entityID, component, priority);
if (result) if (result)
{ {
replayStore.Set(entityID, component); _replayStore.Set(entityID, component);
} }
return result; return result;
} }
internal void AddComponent<TComponent>(int entityID, TComponent component) where TComponent : struct, IComponent internal void AddComponent<TComponent>(int entityID, in TComponent component) where TComponent : struct
{ {
upToDateComponentStore.Set(entityID, component); _upToDateComponentStore.Set(entityID, component);
replayStore.Set(entityID, component); _replayStore.Set(entityID, component);
} }
// existing or immediate reads // existing or immediate reads
internal IEnumerable<(TComponent, int)> ReadExistingAndImmediateComponentsByType<TComponent>() where TComponent : struct, IComponent internal Span<TComponent> ReadExistingAndImmediateComponentsByType<TComponent>() where TComponent : struct
{ {
return upToDateComponentStore.All<TComponent>(); return _upToDateComponentStore.All<TComponent>();
} }
internal (TComponent, int) ReadFirstExistingOrImmediateComponentByType<TComponent>() where TComponent : struct, IComponent internal ref readonly TComponent ExistingOrImmediateSingular<TComponent>() where TComponent : struct
{ {
if (!SomeExistingOrImmediateComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); } return ref _upToDateComponentStore.Singular<TComponent>();
var enumerator = ReadExistingAndImmediateComponentsByType<TComponent>().GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
} }
internal bool SomeExistingOrImmediateComponent<TComponent>() where TComponent : struct, IComponent internal Span<Entity> GetExistingAndImmediateEntities<TComponent>() where TComponent : struct
{ {
return upToDateComponentStore.Any<TComponent>(); return _upToDateComponentStore.AllEntities<TComponent>();
}
internal ref readonly Entity ExistingOrImmediateSingularEntity<TComponent>() where TComponent : struct
{
return ref _upToDateComponentStore.SingularEntity<TComponent>();
}
internal bool SomeExistingOrImmediateComponent<TComponent>() where TComponent : struct
{
return _upToDateComponentStore.Any<TComponent>();
} }
// existing reads // existing reads
internal (TComponent, int) ReadFirstExistingComponentByType<TComponent>() where TComponent : struct, IComponent internal Span<TComponent> GetExistingComponents<TComponent>() where TComponent : struct
{ {
if (!SomeExistingComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); } return _existingComponentStore.All<TComponent>();
var enumerator = GetComponentsIncludingEntity<TComponent>().GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
} }
internal bool SomeExistingComponent<TComponent>() where TComponent : struct, IComponent internal ref readonly TComponent ExistingSingular<TComponent>() where TComponent : struct
{ {
return existingComponentStore.Any<TComponent>(); return ref _existingComponentStore.Singular<TComponent>();
}
internal Span<Entity> GetExistingEntities<TComponent>() where TComponent : struct
{
return _existingComponentStore.AllEntities<TComponent>();
}
internal ref readonly Entity ExistingSingularEntity<TComponent>() where TComponent : struct
{
return ref _existingComponentStore.SingularEntity<TComponent>();
}
internal bool SomeExistingComponent<TComponent>() where TComponent : struct
{
return _existingComponentStore.Any<TComponent>();
} }
// immediate reads // immediate reads
internal IEnumerable<(TComponent, int)> ReadImmediateComponentsByType<TComponent>() where TComponent : struct, IComponent internal Span<TComponent> ReadImmediateComponentsByType<TComponent>() where TComponent : struct
{ {
return immediateComponentStore.All<TComponent>(); return _immediateComponentStore.All<TComponent>();
} }
internal (TComponent, int) ReadFirstImmediateComponentByType<TComponent>() where TComponent : struct, IComponent internal ref readonly TComponent ImmediateSingular<TComponent>() where TComponent : struct
{ {
if (!SomeImmediateComponent<TComponent>()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); } return ref _immediateComponentStore.Singular<TComponent>();
var enumerator = ReadImmediateComponentsByType<TComponent>().GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
} }
internal bool SomeImmediateComponent<TComponent>() where TComponent : struct, IComponent internal Span<Entity> GetImmediateEntities<TComponent>() where TComponent : struct
{ {
return immediateComponentStore.Any<TComponent>(); return _immediateComponentStore.AllEntities<TComponent>();
}
internal ref readonly Entity ImmediateSingularEntity<TComponent>() where TComponent : struct
{
return ref _immediateComponentStore.SingularEntity<TComponent>();
}
internal bool SomeImmediateComponent<TComponent>() where TComponent : struct
{
return _immediateComponentStore.Any<TComponent>();
} }
// component getters // component getters
internal TComponent ReadImmediateOrExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct, IComponent internal ref TComponent ReadImmediateOrExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{ {
return upToDateComponentStore.Get<TComponent>(entityID); return ref _upToDateComponentStore.Get<TComponent>(entityID);
} }
internal TComponent ReadExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct, IComponent internal ref TComponent ReadExistingComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{ {
return existingComponentStore.Get<TComponent>(entityID); return ref _existingComponentStore.Get<TComponent>(entityID);
} }
internal TComponent ReadImmediateComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct, IComponent internal ref TComponent ReadImmediateComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{ {
return immediateComponentStore.Get<TComponent>(entityID); return ref _immediateComponentStore.Get<TComponent>(entityID);
} }
// has checkers // has checkers
internal bool HasExistingOrImmediateComponent<TComponent>(int entityID) where TComponent : struct, IComponent internal bool HasExistingOrImmediateComponent<TComponent>(int entityID) where TComponent : struct
{ {
return upToDateComponentStore.Has<TComponent>(entityID); return _upToDateComponentStore.Has<TComponent>(entityID);
} }
internal bool HasExistingOrImmediateComponent(int entityID, Type type) internal bool HasExistingOrImmediateComponent(int entityID, Type type)
{ {
return upToDateComponentStore.Has(type, entityID); return _upToDateComponentStore.Has(type, entityID);
} }
internal bool HasExistingComponent<TComponent>(int entityID) where TComponent : struct, IComponent internal bool HasExistingComponent<TComponent>(int entityID) where TComponent : struct
{ {
return existingComponentStore.Has<TComponent>(entityID); return _existingComponentStore.Has<TComponent>(entityID);
} }
internal bool HasExistingComponent(int entityID, Type type) internal bool HasExistingComponent(int entityID, Type type)
{ {
return existingComponentStore.Has(type, entityID); return _existingComponentStore.Has(type, entityID);
} }
internal bool HasImmediateComponent<TComponent>(int entityID) where TComponent : struct, IComponent internal bool HasImmediateComponent<TComponent>(int entityID) where TComponent : struct
{ {
return immediateComponentStore.Has<TComponent>(entityID); return _immediateComponentStore.Has<TComponent>(entityID);
} }
internal bool HasImmediateComponent(int entityID, Type type) internal bool HasImmediateComponent(int entityID, Type type)
{ {
return immediateComponentStore.Has(type, entityID); return _immediateComponentStore.Has(type, entityID);
} }
internal IEnumerable<(TComponent, int)> GetComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent internal Span<TComponent> GetComponentsByType<TComponent>() where TComponent : struct
{ {
return existingComponentStore.All<TComponent>(); return _existingComponentStore.All<TComponent>();
} }
internal IEnumerable<TComponent> GetComponentsByType<TComponent>() where TComponent : struct, IComponent internal ref readonly TComponent GetComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{ {
foreach (var pair in existingComponentStore.All<TComponent>()) return ref _existingComponentStore.Get<TComponent>(entityID);
{
yield return pair.Item1;
}
} }
internal TComponent GetComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct, IComponent internal bool EntityHasComponentOfType<TComponent>(int entityID) where TComponent : struct
{ {
return existingComponentStore.Get<TComponent>(entityID); return _existingComponentStore.Has<TComponent>(entityID);
}
internal bool EntityHasComponentOfType<TComponent>(int entityID) where TComponent : struct, IComponent
{
return existingComponentStore.Has<TComponent>(entityID);
} }
internal void MarkAllComponentsOnEntityForRemoval(int entityID) internal void MarkAllComponentsOnEntityForRemoval(int entityID)
{ {
entitiesMarkedForRemoval.Add(entityID); _entitiesMarkedForRemoval.Add(entityID);
} }
internal void RemoveMarkedComponents() internal void RemoveMarkedComponents()
{ {
foreach (var entityID in entitiesMarkedForRemoval) foreach (var entityID in _entitiesMarkedForRemoval)
{ {
existingComponentStore.Remove(entityID); _existingComponentStore.Remove(entityID);
immediateComponentStore.Remove(entityID); _immediateComponentStore.Remove(entityID);
replayStore.Remove(entityID); _replayStore.Remove(entityID);
upToDateComponentStore.Remove(entityID); _upToDateComponentStore.Remove(entityID);
drawLayerManager.UnRegisterEntityWithLayer(entityID); _drawLayerManager.UnRegisterEntityWithLayer(entityID);
} }
entitiesMarkedForRemoval.Clear(); _entitiesMarkedForRemoval.Clear();
} }
public bool RemoveImmediate<TComponent>(int entityID, int priority) where TComponent : struct, IComponent public bool RemoveImmediate<TComponent>(int entityID, int priority) where TComponent : struct
{ {
if (immediateComponentStore.Remove<TComponent>(entityID, priority)) if (_immediateComponentStore.Remove<TComponent>(entityID, priority))
{ {
replayStore.Remove<TComponent>(entityID, priority); _replayStore.Remove<TComponent>(entityID, priority);
upToDateComponentStore.Remove<TComponent>(entityID, priority); _upToDateComponentStore.Remove<TComponent>(entityID, priority);
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID); _drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID);
return true; return true;
} }
return false; return false;
} }
public void Remove<TComponent>(int entityID, int priority) where TComponent : struct, IComponent public void Remove<TComponent>(int entityID, int priority) where TComponent : struct
{ {
if (upToDateComponentStore.Remove<TComponent>(entityID, priority)) if (_upToDateComponentStore.Remove<TComponent>(entityID, priority))
{ {
replayStore.Remove<TComponent>(entityID, priority); _replayStore.Remove<TComponent>(entityID, priority);
drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID); _drawLayerManager.UnRegisterComponentWithLayer<TComponent>(entityID);
} }
} }
public bool UpToDateEntityIsEmpty(int entityID) public bool UpToDateEntityIsEmpty(int entityID)
{ {
return upToDateComponentStore.EntityBitArray(entityID).AllFalse(); return _upToDateComponentStore.EntityBitArray(entityID).AllFalse();
} }
} }
} }

View File

@ -7,112 +7,126 @@ namespace Encompass
{ {
internal class DrawLayerManager internal class DrawLayerManager
{ {
private readonly SortedList<int, int> layerOrder = new SortedList<int, int>(); private readonly SortedList<int, int> _layerOrder = new SortedList<int, int>();
private readonly Dictionary<int, ComponentStore> layerIndexToComponentStore = new Dictionary<int, ComponentStore>(512); private readonly Dictionary<int, Dictionary<Type, HashSet<int>>> _layerIndexToTypeToID = new Dictionary<int, Dictionary<Type, HashSet<int>>>();
private readonly Dictionary<int, HashSet<GeneralRenderer>> layerIndexToGeneralRenderers = new Dictionary<int, HashSet<GeneralRenderer>>(512); private readonly Dictionary<int, HashSet<GeneralRenderer>> _layerIndexToGeneralRenderers = new Dictionary<int, HashSet<GeneralRenderer>>(512);
private readonly Dictionary<Type, Dictionary<int, int>> typeToEntityToLayer = new Dictionary<Type, Dictionary<int, int>>(512); private readonly Dictionary<Type, Dictionary<int, int>> _typeToEntityToLayer = new Dictionary<Type, Dictionary<int, int>>(512);
private Dictionary<Type, int> typeToIndex; public IEnumerable<int> LayerOrder { get { return _layerOrder.Values; } }
public IEnumerable<int> LayerOrder { get { return layerOrder.Values; } }
public DrawLayerManager(Dictionary<Type, int> typeToIndex) public DrawLayerManager()
{ {
this.typeToIndex = typeToIndex;
RegisterDrawLayer(0); RegisterDrawLayer(0);
} }
public void RegisterDrawLayer(int layer) public void RegisterDrawLayer(int layer)
{ {
if (!layerIndexToComponentStore.ContainsKey(layer)) if (!_layerIndexToTypeToID.ContainsKey(layer))
{ {
layerOrder.Add(layer, layer); _layerOrder.Add(layer, layer);
layerIndexToGeneralRenderers.Add(layer, new HashSet<GeneralRenderer>()); _layerIndexToGeneralRenderers.Add(layer, new HashSet<GeneralRenderer>());
layerIndexToComponentStore.Add(layer, new ComponentStore(typeToIndex)); _layerIndexToTypeToID.Add(layer, new Dictionary<Type, HashSet<int>>());
} }
} }
public void RegisterOrderedDrawable<TComponent>() where TComponent : struct, IComponent public void RegisterOrderedDrawable<TComponent>() where TComponent : struct
{ {
if (!typeToEntityToLayer.ContainsKey(typeof(TComponent))) if (!_typeToEntityToLayer.ContainsKey(typeof(TComponent)))
{ {
typeToEntityToLayer.Add(typeof(TComponent), new Dictionary<int, int>(128)); _typeToEntityToLayer.Add(typeof(TComponent), new Dictionary<int, int>(128));
} }
foreach (var pair in layerIndexToComponentStore) foreach (var pair in _layerIndexToTypeToID)
{ {
pair.Value.RegisterComponentType<TComponent>(); if (!pair.Value.ContainsKey(typeof(TComponent)))
{
pair.Value.Add(typeof(TComponent), new HashSet<int>());
}
} }
} }
public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer) public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
{ {
RegisterDrawLayer(layer); RegisterDrawLayer(layer);
var set = layerIndexToGeneralRenderers[layer]; var set = _layerIndexToGeneralRenderers[layer];
set.Add(renderer); set.Add(renderer);
} }
public void UnregisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer) public void RegisterComponentWithLayer<TComponent>(int entityID, int layer) where TComponent : struct
{ {
if (layerIndexToGeneralRenderers.ContainsKey(layer)) if (!_layerIndexToTypeToID.ContainsKey(layer))
{
layerIndexToGeneralRenderers[layer].Remove(renderer);
}
}
public void AdjustRendererLayer(GeneralRenderer renderer, int oldLayer, int newLayer)
{
UnregisterGeneralRendererWithLayer(renderer, oldLayer);
RegisterGeneralRendererWithLayer(renderer, newLayer);
}
public void RegisterComponentWithLayer<TComponent>(int entityID, TComponent component, int layer) where TComponent : struct, IComponent
{
if (!layerIndexToComponentStore.ContainsKey(layer))
{ {
throw new UndefinedLayerException("Layer {0} is not defined. Use WorldBuilder.RegisterDrawLayer to register the layer.", layer); throw new UndefinedLayerException("Layer {0} is not defined. Use WorldBuilder.RegisterDrawLayer to register the layer.", layer);
} }
if (typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID)) { UnRegisterComponentWithLayer<TComponent>(entityID); } if (_typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID))
{
if (_typeToEntityToLayer[typeof(TComponent)][entityID] != layer)
{
UnRegisterComponentWithLayer<TComponent>(entityID);
var set = layerIndexToComponentStore[layer]; var set = _layerIndexToTypeToID[layer][typeof(TComponent)];
set.Set<TComponent>(entityID, component); set.Add(entityID);
typeToEntityToLayer[typeof(TComponent)].Add(entityID, layer); _typeToEntityToLayer[typeof(TComponent)].Add(entityID, layer);
}
}
else
{
var set = _layerIndexToTypeToID[layer][typeof(TComponent)];
set.Add(entityID);
_typeToEntityToLayer[typeof(TComponent)].Add(entityID, layer);
}
} }
public void UnRegisterComponentWithLayer<TComponent>(int entityID) where TComponent : struct, IComponent public void UnRegisterComponentWithLayer<TComponent>(int entityID) where TComponent : struct
{ {
if (!typeToEntityToLayer.ContainsKey(typeof(TComponent))) { return; } if (!_typeToEntityToLayer.ContainsKey(typeof(TComponent))) { return; }
if (typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID)) if (_typeToEntityToLayer[typeof(TComponent)].ContainsKey(entityID))
{ {
var layer = typeToEntityToLayer[typeof(TComponent)][entityID]; var layer = _typeToEntityToLayer[typeof(TComponent)][entityID];
layerIndexToComponentStore[layer].ForceRemove<TComponent>(entityID); _layerIndexToTypeToID[layer][typeof(TComponent)].Remove(entityID);
} }
typeToEntityToLayer[typeof(TComponent)].Remove(entityID); _typeToEntityToLayer[typeof(TComponent)].Remove(entityID);
} }
public void UnRegisterEntityWithLayer(int entityID) public void UnRegisterEntityWithLayer(int entityID)
{ {
foreach (var store in layerIndexToComponentStore.Values) foreach (var store in _layerIndexToTypeToID.Values)
{ {
store.Remove(entityID); foreach (var typeToSet in store)
{
var type = typeToSet.Key;
var set = typeToSet.Value;
if (set.Contains(entityID))
{
_typeToEntityToLayer[type].Remove(entityID);
set.Remove(entityID);
}
}
} }
} }
public IEnumerable<GeneralRenderer> GeneralRenderersByLayer(int layer) public IEnumerable<GeneralRenderer> GeneralRenderersByLayer(int layer)
{ {
return layerIndexToGeneralRenderers.ContainsKey(layer) ? return _layerIndexToGeneralRenderers.ContainsKey(layer) ?
layerIndexToGeneralRenderers[layer] : _layerIndexToGeneralRenderers[layer] :
Enumerable.Empty<GeneralRenderer>(); Enumerable.Empty<GeneralRenderer>();
} }
public IEnumerable<(int, Type, IComponent)> AllInLayer(int layer) public IEnumerable<(int, Type)> AllInLayer(int layer)
{ {
return layerIndexToComponentStore.ContainsKey(layer) ? foreach (var kvp in _layerIndexToTypeToID[layer])
layerIndexToComponentStore[layer].AllInterfaceTyped() : {
Enumerable.Empty<(int, Type, IComponent)>(); foreach (var id in kvp.Value)
{
yield return (id, kvp.Key);
}
}
} }
} }
} }

View File

@ -14,104 +14,104 @@ namespace Encompass
/// </summary> /// </summary>
public abstract class Engine : IEquatable<Engine> public abstract class Engine : IEquatable<Engine>
{ {
internal Guid ID; internal Guid _id;
internal readonly HashSet<Type> readTypes = new HashSet<Type>(); internal readonly HashSet<Type> ReadTypes = new HashSet<Type>();
internal readonly HashSet<Type> readImmediateTypes = new HashSet<Type>(); internal readonly HashSet<Type> ReadImmediateTypes = new HashSet<Type>();
internal readonly HashSet<Type> sendTypes = new HashSet<Type>(); internal readonly HashSet<Type> SendTypes = new HashSet<Type>();
internal readonly HashSet<Type> receiveTypes = new HashSet<Type>(); internal readonly HashSet<Type> ReceiveTypes = new HashSet<Type>();
internal readonly HashSet<Type> writeTypes = new HashSet<Type>(); internal readonly HashSet<Type> WriteTypes = new HashSet<Type>();
internal readonly HashSet<Type> writeImmediateTypes = new HashSet<Type>(); internal readonly HashSet<Type> WriteImmediateTypes = new HashSet<Type>();
internal readonly HashSet<Type> queryWithTypes = new HashSet<Type>(); internal readonly HashSet<Type> QueryWithTypes = new HashSet<Type>();
internal readonly HashSet<Type> queryWithoutTypes = new HashSet<Type>(); internal readonly HashSet<Type> QueryWithoutTypes = new HashSet<Type>();
internal readonly Dictionary<Type, int> writePriorities = new Dictionary<Type, int>(); internal readonly Dictionary<Type, int> WritePriorities = new Dictionary<Type, int>();
internal readonly int defaultWritePriority = 0; internal readonly int DefaultWritePriority = 0;
/// <summary> /// <summary>
/// If false, the Engine will ignore time dilation. /// If false, the Engine will ignore time dilation.
/// </summary> /// </summary>
internal bool usesTimeDilation = true; internal bool _usesTimeDilation = true;
public bool TimeDilationActive { get => usesTimeDilation && timeManager.TimeDilationActive; } public bool TimeDilationActive { get => _usesTimeDilation && _timeManager.TimeDilationActive; }
private EntityManager entityManager; private EntityManager _entityManager;
private MessageManager messageManager; private MessageManager _messageManager;
private ComponentManager componentManager; private ComponentManager _componentManager;
private TimeManager timeManager; private TimeManager _timeManager;
private TrackingManager trackingManager; private TrackingManager _trackingManager;
private EntitySetQuery entityQuery; private EntitySetQuery _entityQuery;
private HashSet<int> _trackedEntities = new HashSet<int>(); private readonly HashSet<int> _trackedEntities = new HashSet<int>();
protected IEnumerable<Entity> TrackedEntities protected IEnumerable<Entity> TrackedEntities
{ {
get get
{ {
foreach (var entityID in _trackedEntities) foreach (var entityID in _trackedEntities)
{ {
yield return entityManager.GetEntity(entityID); yield return _entityManager.GetEntity(entityID);
} }
} }
} }
private HashSet<int> _newlyCreatedEntities = new HashSet<int>(); private readonly HashSet<int> _newlyCreatedEntities = new HashSet<int>();
protected Engine() protected Engine()
{ {
ID = Guid.NewGuid(); _id = Guid.NewGuid();
var sendsAttribute = GetType().GetCustomAttribute<Sends>(false); var sendsAttribute = GetType().GetCustomAttribute<Sends>(false);
if (sendsAttribute != null) if (sendsAttribute != null)
{ {
sendTypes = sendsAttribute.sendTypes; SendTypes = sendsAttribute.SendTypes;
} }
var activatesAttribute = GetType().GetCustomAttribute<WritesImmediate>(false); var activatesAttribute = GetType().GetCustomAttribute<WritesImmediate>(false);
if (activatesAttribute != null) if (activatesAttribute != null)
{ {
writeImmediateTypes = activatesAttribute.writeImmediateTypes; WriteImmediateTypes = activatesAttribute.WriteImmediateTypes;
} }
var defaultWritePriorityAttribute = GetType().GetCustomAttribute<DefaultWritePriority>(false); var defaultWritePriorityAttribute = GetType().GetCustomAttribute<DefaultWritePriority>(false);
if (defaultWritePriorityAttribute != null) if (defaultWritePriorityAttribute != null)
{ {
defaultWritePriority = defaultWritePriorityAttribute.writePriority; DefaultWritePriority = defaultWritePriorityAttribute.WritePriority;
} }
foreach (var writesAttribute in GetType().GetCustomAttributes<Writes>(false)) foreach (var writesAttribute in GetType().GetCustomAttributes<Writes>(false))
{ {
writeTypes.UnionWith(writesAttribute.writeTypes); WriteTypes.UnionWith(writesAttribute.WriteTypes);
writePriorities = new Dictionary<Type, int>[2] { writePriorities, writesAttribute.priorities }.SelectMany(dict => dict).ToDictionary(pair => pair.Key, pair => pair.Value); WritePriorities = new Dictionary<Type, int>[2] { WritePriorities, writesAttribute.Priorities }.SelectMany(dict => dict).ToDictionary(pair => pair.Key, pair => pair.Value);
} }
var receivesAttribute = GetType().GetCustomAttribute<Receives>(false); var receivesAttribute = GetType().GetCustomAttribute<Receives>(false);
if (receivesAttribute != null) if (receivesAttribute != null)
{ {
receiveTypes = receivesAttribute.receiveTypes; ReceiveTypes = receivesAttribute.ReceiveTypes;
} }
var readsAttribute = GetType().GetCustomAttribute<Reads>(false); var readsAttribute = GetType().GetCustomAttribute<Reads>(false);
if (readsAttribute != null) if (readsAttribute != null)
{ {
readTypes = readsAttribute.readTypes; ReadTypes = readsAttribute.ReadTypes;
} }
var readsImmediateAttribute = GetType().GetCustomAttribute<ReadsImmediate>(false); var readsImmediateAttribute = GetType().GetCustomAttribute<ReadsImmediate>(false);
if (readsImmediateAttribute != null) if (readsImmediateAttribute != null)
{ {
readImmediateTypes = readsImmediateAttribute.readImmediateTypes; ReadImmediateTypes = readsImmediateAttribute.ReadImmediateTypes;
} }
var queryWithAttribute = GetType().GetCustomAttribute<QueryWith>(false); var queryWithAttribute = GetType().GetCustomAttribute<QueryWith>(false);
if (queryWithAttribute != null) if (queryWithAttribute != null)
{ {
queryWithTypes = queryWithAttribute.queryWithTypes; QueryWithTypes = queryWithAttribute.QueryWithTypes;
} }
var queryWithoutAttribute = GetType().GetCustomAttribute<QueryWithout>(false); var queryWithoutAttribute = GetType().GetCustomAttribute<QueryWithout>(false);
if (queryWithoutAttribute != null) if (queryWithoutAttribute != null)
{ {
queryWithoutTypes = queryWithoutAttribute.queryWithoutTypes; QueryWithoutTypes = queryWithoutAttribute.QueryWithoutTypes;
} }
} }
@ -127,42 +127,42 @@ namespace Encompass
public bool Equals(Engine other) public bool Equals(Engine other)
{ {
return other.ID == ID; return other._id == _id;
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return ID.GetHashCode(); return HashCode.Combine(_id);
} }
internal void AssignEntityManager(EntityManager entityManager) internal void AssignEntityManager(EntityManager entityManager)
{ {
this.entityManager = entityManager; _entityManager = entityManager;
} }
internal void AssignComponentManager(ComponentManager componentManager) internal void AssignComponentManager(ComponentManager componentManager)
{ {
this.componentManager = componentManager; _componentManager = componentManager;
} }
internal void AssignMessageManager(MessageManager messageManager) internal void AssignMessageManager(MessageManager messageManager)
{ {
this.messageManager = messageManager; _messageManager = messageManager;
} }
internal void AssignTimeManager(TimeManager timeManager) internal void AssignTimeManager(TimeManager timeManager)
{ {
this.timeManager = timeManager; _timeManager = timeManager;
} }
internal void AssignTrackingManager(TrackingManager trackingManager) internal void AssignTrackingManager(TrackingManager trackingManager)
{ {
this.trackingManager = trackingManager; _trackingManager = trackingManager;
} }
internal void CheckMessageRead<TMessage>() where TMessage : struct, IMessage internal void CheckMessageRead<TMessage>() where TMessage : struct, IMessage
{ {
if (!receiveTypes.Contains(typeof(TMessage))) if (!ReceiveTypes.Contains(typeof(TMessage)))
{ {
throw new IllegalReadException("Engine {0} tried to read undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name); throw new IllegalReadException("Engine {0} tried to read undeclared Message {1}", this.GetType().Name, typeof(TMessage).Name);
} }
@ -189,7 +189,7 @@ namespace Encompass
/// </summary> /// </summary>
protected Entity CreateEntity() protected Entity CreateEntity()
{ {
var entity = entityManager.CreateEntity(); var entity = _entityManager.CreateEntity();
_newlyCreatedEntities.Add(entity.ID); _newlyCreatedEntities.Add(entity.ID);
return entity; return entity;
} }
@ -197,9 +197,9 @@ namespace Encompass
/// <summary> /// <summary>
/// Returns true if an Entity with the specified ID exists. /// Returns true if an Entity with the specified ID exists.
/// </summary> /// </summary>
protected bool EntityExists(Entity entity) protected bool EntityExists(in Entity entity)
{ {
return entityManager.EntityExists(entity.ID); return _entityManager.EntityExists(entity.ID);
} }
/// <summary> /// <summary>
@ -207,50 +207,52 @@ namespace Encompass
/// </summary> /// </summary>
protected bool EntityExists(int entityID) protected bool EntityExists(int entityID)
{ {
return entityManager.EntityExists(entityID); return _entityManager.EntityExists(entityID);
}
/// <summary>
/// Returns an Entity containing the specified Component type.
/// </summary>
protected Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
{
return entityManager.GetEntity(ReadComponentHelper<TComponent>().Item2);
} }
/// <summary> /// <summary>
/// Returns all Entities containing the specified Component type. /// Returns all Entities containing the specified Component type.
/// </summary> /// </summary>
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent protected Span<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
{ {
foreach (var pair in ReadComponentsHelper<TComponent>()) var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
{ var existingRead = ReadTypes.Contains(typeof(TComponent));
yield return entityManager.GetEntity(pair.Item2);
}
}
// these next two are for the ComponentMessageEmitter only
internal IEnumerable<TComponent> ReadComponentsFromWorld<TComponent>() where TComponent : struct, IComponent
{
return componentManager.GetComponentsByType<TComponent>();
}
private IEnumerable<(TComponent, int)> ReadComponentsHelper<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
var existingRead = readTypes.Contains(typeof(TComponent));
if (existingRead && immediateRead) if (existingRead && immediateRead)
{ {
return componentManager.ReadExistingAndImmediateComponentsByType<TComponent>(); return _componentManager.GetExistingAndImmediateEntities<TComponent>();
} }
else if (existingRead) else if (existingRead)
{ {
return componentManager.GetComponentsIncludingEntity<TComponent>(); return _componentManager.GetExistingEntities<TComponent>();
} }
else if (immediateRead) else if (immediateRead)
{ {
return componentManager.ReadImmediateComponentsByType<TComponent>(); return _componentManager.GetImmediateEntities<TComponent>();
}
else
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
}
/// <summary>
/// Returns an Entity containing the specified Component type.
/// </summary>
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
if (existingRead && immediateRead)
{
return ref _componentManager.ExistingOrImmediateSingularEntity<TComponent>();
}
else if (existingRead)
{
return ref _componentManager.ExistingSingularEntity<TComponent>();
}
else if (immediateRead)
{
return ref _componentManager.ImmediateSingularEntity<TComponent>();
} }
else else
{ {
@ -261,40 +263,21 @@ namespace Encompass
/// <summary> /// <summary>
/// Returns all of the Components with the specified Component Type. /// Returns all of the Components with the specified Component Type.
/// </summary> /// </summary>
protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent protected Span<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
{ {
foreach (var pair in ReadComponentsHelper<TComponent>()) var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
{ var existingRead = ReadTypes.Contains(typeof(TComponent));
yield return pair.Item1;
}
}
/// <summary>
/// Returns all of the components of the specified type including an Entity reference for each Component.
/// </summary>
protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent
{
foreach (var (component, id) in ReadComponentsHelper<TComponent>())
{
yield return (component, entityManager.GetEntity(id));
}
}
private (TComponent, int) ReadComponentHelper<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
var existingRead = readTypes.Contains(typeof(TComponent));
if (existingRead && immediateRead) if (existingRead && immediateRead)
{ {
return componentManager.ReadFirstExistingOrImmediateComponentByType<TComponent>(); return _componentManager.ReadExistingAndImmediateComponentsByType<TComponent>();
} }
else if (existingRead) else if (existingRead)
{ {
return componentManager.ReadFirstExistingComponentByType<TComponent>(); return _componentManager.GetExistingComponents<TComponent>();
} }
else if (immediateRead) else if (immediateRead)
{ {
return componentManager.ReadFirstImmediateComponentByType<TComponent>(); return _componentManager.ReadImmediateComponentsByType<TComponent>();
} }
else else
{ {
@ -305,38 +288,21 @@ namespace Encompass
/// <summary> /// <summary>
/// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned. /// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned.
/// </summary> /// </summary>
protected TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
{ {
return ReadComponentHelper<TComponent>().Item1; var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
} var existingRead = ReadTypes.Contains(typeof(TComponent));
/// <summary>
/// Returns a component of the specified type including its Entity reference. If multiples exist, an arbitrary Component is returned.
/// </summary>
protected (TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : struct, IComponent
{
var (component, id) = ReadComponentHelper<TComponent>();
return (component, entityManager.GetEntity(id));
}
/// <summary>
/// Returns true if any Component with the specified Component Type exists.
/// </summary>
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
{
var immediateRead = readImmediateTypes.Contains(typeof(TComponent));
var existingRead = readTypes.Contains(typeof(TComponent));
if (existingRead && immediateRead) if (existingRead && immediateRead)
{ {
return componentManager.SomeExistingOrImmediateComponent<TComponent>(); return ref _componentManager.ExistingOrImmediateSingular<TComponent>();
} }
else if (existingRead) else if (existingRead)
{ {
return componentManager.SomeExistingComponent<TComponent>(); return ref _componentManager.ExistingSingular<TComponent>();
} }
else if (immediateRead) else if (immediateRead)
{ {
return componentManager.SomeImmediateComponent<TComponent>(); return ref _componentManager.ImmediateSingular<TComponent>();
} }
else else
{ {
@ -344,21 +310,46 @@ namespace Encompass
} }
} }
private TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct, IComponent /// <summary>
/// Returns true if any Component with the specified Component Type exists.
/// </summary>
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
{ {
var immediateRead = readImmediateTypes.Contains(typeof(TComponent)); var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = readTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent));
if (existingRead && immediateRead) if (existingRead && immediateRead)
{ {
return componentManager.ReadImmediateOrExistingComponentByEntityAndType<TComponent>(entityID); return _componentManager.SomeExistingOrImmediateComponent<TComponent>();
} }
else if (existingRead) else if (existingRead)
{ {
return componentManager.ReadExistingComponentByEntityAndType<TComponent>(entityID); return _componentManager.SomeExistingComponent<TComponent>();
} }
else if (immediateRead) else if (immediateRead)
{ {
return componentManager.ReadImmediateComponentByEntityAndType<TComponent>(entityID); return _componentManager.SomeImmediateComponent<TComponent>();
}
else
{
throw new IllegalReadException("Engine {0} tried to read undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
}
}
private ref TComponent GetComponentHelper<TComponent>(int entityID) where TComponent : struct, IComponent
{
var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = ReadTypes.Contains(typeof(TComponent));
if (existingRead && immediateRead)
{
return ref _componentManager.ReadImmediateOrExistingComponentByEntityAndType<TComponent>(entityID);
}
else if (existingRead)
{
return ref _componentManager.ReadExistingComponentByEntityAndType<TComponent>(entityID);
}
else if (immediateRead)
{
return ref _componentManager.ReadImmediateComponentByEntityAndType<TComponent>(entityID);
} }
else else
{ {
@ -375,9 +366,9 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException"> /// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that it reads the given Component Type. /// Thrown when the Engine does not declare that it reads the given Component Type.
/// </exception> /// </exception>
protected TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent protected ref readonly TComponent GetComponent<TComponent>(in Entity entity) where TComponent : struct, IComponent
{ {
return GetComponentHelper<TComponent>(entity.ID); return ref GetComponentHelper<TComponent>(entity.ID);
} }
/// <summary> /// <summary>
@ -386,22 +377,22 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException"> /// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that is Reads the given Component Type. /// Thrown when the Engine does not declare that is Reads the given Component Type.
/// </exception> /// </exception>
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct, IComponent protected bool HasComponent<TComponent>(in Entity entity) where TComponent : struct, IComponent
{ {
var immediateRead = readImmediateTypes.Contains(typeof(TComponent)); var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent));
var existingRead = readTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent));
if (immediateRead && existingRead) if (immediateRead && existingRead)
{ {
return componentManager.HasExistingOrImmediateComponent<TComponent>(entity.ID); return _componentManager.HasExistingOrImmediateComponent<TComponent>(entity.ID);
} }
else if (existingRead) else if (existingRead)
{ {
return componentManager.HasExistingComponent<TComponent>(entity.ID); return _componentManager.HasExistingComponent<TComponent>(entity.ID);
} }
else if (immediateRead) else if (immediateRead)
{ {
return componentManager.HasImmediateComponent<TComponent>(entity.ID); return _componentManager.HasImmediateComponent<TComponent>(entity.ID);
} }
else else
{ {
@ -415,22 +406,22 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException"> /// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that is Reads the given Component Type. /// Thrown when the Engine does not declare that is Reads the given Component Type.
/// </exception> /// </exception>
protected bool HasComponent(Entity entity, Type type) protected bool HasComponent(in Entity entity, Type type)
{ {
var immediateRead = readImmediateTypes.Contains(type); var immediateRead = ReadImmediateTypes.Contains(type);
var existingRead = readTypes.Contains(type); var existingRead = ReadTypes.Contains(type);
if (immediateRead && existingRead) if (immediateRead && existingRead)
{ {
return componentManager.HasExistingOrImmediateComponent(entity.ID, type); return _componentManager.HasExistingOrImmediateComponent(entity.ID, type);
} }
else if (existingRead) else if (existingRead)
{ {
return componentManager.HasExistingComponent(entity.ID, type); return _componentManager.HasExistingComponent(entity.ID, type);
} }
else if (immediateRead) else if (immediateRead)
{ {
return componentManager.HasImmediateComponent(entity.ID, type); return _componentManager.HasImmediateComponent(entity.ID, type);
} }
else else
{ {
@ -444,37 +435,37 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalWriteException"> /// <exception cref="Encompass.Exceptions.IllegalWriteException">
/// Thrown when the Engine does not declare that it Writes the given Component Type. /// Thrown when the Engine does not declare that it Writes the given Component Type.
/// </exception> /// </exception>
protected void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent protected void SetComponent<TComponent>(in Entity entity, in TComponent component) where TComponent : struct, IComponent
{ {
var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : defaultWritePriority; var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority;
if (!writeTypes.Contains(typeof(TComponent))) if (!WriteTypes.Contains(typeof(TComponent)))
{ {
throw new IllegalWriteException("Engine {0} tried to update undeclared Component {1}", GetType().Name, typeof(TComponent).Name); throw new IllegalWriteException("Engine {0} tried to update undeclared Component {1}", GetType().Name, typeof(TComponent).Name);
} }
bool written; bool written;
if (writeImmediateTypes.Contains(typeof(TComponent))) if (WriteImmediateTypes.Contains(typeof(TComponent)))
{ {
written = componentManager.AddImmediateComponent(entity.ID, component, priority); written = _componentManager.AddImmediateComponent(entity.ID, component, priority);
if (written) if (written)
{ {
trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent)); _trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
} }
} }
else else
{ {
written = componentManager.UpdateComponent(entity.ID, component, priority); written = _componentManager.UpdateComponent(entity.ID, component, priority);
} }
if (!componentManager.HasExistingComponent<TComponent>(entity.ID)) if (!_componentManager.HasExistingComponent<TComponent>(entity.ID))
{ {
trackingManager.RegisterAddition(entity.ID, typeof(TComponent)); _trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
} }
if (written && component is IDrawableComponent drawableComponent) if (written && component is IDrawableComponent drawableComponent)
{ {
componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer); _componentManager.RegisterDrawableComponent<TComponent>(entity.ID, drawableComponent.Layer);
} }
} }
@ -484,28 +475,28 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalWriteException"> /// <exception cref="Encompass.Exceptions.IllegalWriteException">
/// Thrown when the Engine does not declare that it Writes the given Component Type. /// Thrown when the Engine does not declare that it Writes the given Component Type.
/// </exception> /// </exception>
protected void AddComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent protected void AddComponent<TComponent>(in Entity entity, in TComponent component) where TComponent : struct, IComponent
{ {
if (!EntityCreatedThisFrame(entity.ID)) if (!EntityCreatedThisFrame(entity.ID))
{ {
throw new IllegalWriteException("AddComponent used on Entity that was not created in this context. Use SetComponent instead."); throw new IllegalWriteException("AddComponent used on Entity that was not created in this context. Use SetComponent instead.");
} }
if (writeImmediateTypes.Contains(typeof(TComponent))) if (WriteImmediateTypes.Contains(typeof(TComponent)))
{ {
componentManager.AddImmediateComponent(entity.ID, component); _componentManager.AddImmediateComponent(entity.ID, component);
trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent)); _trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
} }
else else
{ {
componentManager.AddComponent(entity.ID, component); _componentManager.AddComponent(entity.ID, component);
} }
trackingManager.RegisterAddition(entity.ID, typeof(TComponent)); _trackingManager.RegisterAddition(entity.ID, typeof(TComponent));
if (component is IDrawableComponent drawableComponent) if (component is IDrawableComponent drawableComponent)
{ {
componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer); _componentManager.RegisterDrawableComponent<TComponent>(entity.ID, drawableComponent.Layer);
} }
} }
@ -515,32 +506,32 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalSendException"> /// <exception cref="Encompass.Exceptions.IllegalSendException">
/// Thrown when the Engine does not declare that it Sends the Message Type. /// Thrown when the Engine does not declare that it Sends the Message Type.
/// </exception> /// </exception>
protected void SendMessage<TMessage>(TMessage message) where TMessage : struct, IMessage protected void SendMessage<TMessage>(in TMessage message) where TMessage : struct, IMessage
{ {
if (!sendTypes.Contains(typeof(TMessage))) if (!SendTypes.Contains(typeof(TMessage)))
{ {
throw new IllegalSendException("Engine {0} tried to send undeclared Message {1}", GetType().Name, typeof(TMessage).Name); throw new IllegalSendException("Engine {0} tried to send undeclared Message {1}", GetType().Name, typeof(TMessage).Name);
} }
messageManager.AddMessage(message); _messageManager.AddMessage(message);
} }
/// <summary> /// <summary>
/// Sends a message after the specified number of seconds, respecting time dilation. /// Sends a message after the specified number of seconds, respecting time dilation.
/// </summary> /// </summary>
/// <param name="time">The time in seconds that will elapse before the message is sent.</param> /// <param name="time">The time in seconds that will elapse before the message is sent.</param>
protected void SendMessage<TMessage>(TMessage message, double time) where TMessage : struct, IMessage protected void SendMessage<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{ {
messageManager.AddMessage(message, time); _messageManager.AddMessage(message, time);
} }
/// <summary> /// <summary>
/// Sends a message after the specified number of seconds, ignoring time dilation. /// Sends a message after the specified number of seconds, ignoring time dilation.
/// </summary> /// </summary>
/// <param name="time">The time in seconds that will elapse before the message is sent.</param> /// <param name="time">The time in seconds that will elapse before the message is sent.</param>
protected void SendMessageIgnoringTimeDilation<TMessage>(TMessage message, double time) where TMessage : struct, IMessage protected void SendMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{ {
messageManager.AddMessageIgnoringTimeDilation(message, time); _messageManager.AddMessageIgnoringTimeDilation(message, time);
} }
/// <summary> /// <summary>
@ -549,10 +540,10 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException"> /// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that it Receives the specified Message Type. /// Thrown when the Engine does not declare that it Receives the specified Message Type.
/// </exception> /// </exception>
protected IEnumerable<TMessage> ReadMessages<TMessage>() where TMessage : struct, IMessage protected Span<TMessage> ReadMessages<TMessage>() where TMessage : struct, IMessage
{ {
CheckMessageRead<TMessage>(); CheckMessageRead<TMessage>();
return messageManager.GetMessagesByType<TMessage>(); return _messageManager.GetMessagesByType<TMessage>();
} }
/// <summary> /// <summary>
@ -561,10 +552,10 @@ namespace Encompass
/// <exception cref="Encompass.Exceptions.IllegalReadException"> /// <exception cref="Encompass.Exceptions.IllegalReadException">
/// Thrown when the Engine does not declare that it Receives the specified Message Type. /// Thrown when the Engine does not declare that it Receives the specified Message Type.
/// </exception> /// </exception>
protected TMessage ReadMessage<TMessage>() where TMessage : struct, IMessage protected ref readonly TMessage ReadMessage<TMessage>() where TMessage : struct, IMessage
{ {
CheckMessageRead<TMessage>(); CheckMessageRead<TMessage>();
return messageManager.First<TMessage>(); return ref _messageManager.First<TMessage>();
} }
/// <summary> /// <summary>
@ -576,16 +567,16 @@ namespace Encompass
protected bool SomeMessage<TMessage>() where TMessage : struct, IMessage protected bool SomeMessage<TMessage>() where TMessage : struct, IMessage
{ {
CheckMessageRead<TMessage>(); CheckMessageRead<TMessage>();
return messageManager.Any<TMessage>(); return _messageManager.Any<TMessage>();
} }
/// <summary> /// <summary>
/// Destroys the specified Entity. This also removes all of the Components associated with the Entity. /// 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. /// Entity destruction takes place after all the Engines have been processed by World Update.
/// </summary> /// </summary>
protected void Destroy(Entity entity) protected void Destroy(in Entity entity)
{ {
entityManager.MarkForDestroy(entity.ID); _entityManager.MarkForDestroy(entity.ID);
} }
/// <summary> /// <summary>
@ -614,30 +605,30 @@ namespace Encompass
/// Note that the Engine must Read the Component type that is being removed. /// 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. /// If a Component with the specified type does not exist on the Entity, returns false and does not mutate the Entity.
/// </summary> /// </summary>
protected void RemoveComponent<TComponent>(Entity entity) where TComponent : struct, IComponent protected void RemoveComponent<TComponent>(in Entity entity) where TComponent : struct, IComponent
{ {
var priority = writePriorities.ContainsKey(typeof(TComponent)) ? writePriorities[typeof(TComponent)] : defaultWritePriority; var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority;
if (!writeTypes.Contains(typeof(TComponent))) 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); 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 (WriteImmediateTypes.Contains(typeof(TComponent)))
{ {
if (componentManager.RemoveImmediate<TComponent>(entity.ID, priority)) if (_componentManager.RemoveImmediate<TComponent>(entity.ID, priority))
{ {
trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent)); _trackingManager.ImmediateUpdateTracking(entity.ID, typeof(TComponent));
} }
} }
else else
{ {
componentManager.Remove<TComponent>(entity.ID, priority); _componentManager.Remove<TComponent>(entity.ID, priority);
} }
if (componentManager.HasExistingComponent<TComponent>(entity.ID)) if (_componentManager.HasExistingComponent<TComponent>(entity.ID))
{ {
trackingManager.RegisterRemoval(entity.ID, typeof(TComponent)); _trackingManager.RegisterRemoval(entity.ID, typeof(TComponent));
} }
} }
@ -652,7 +643,7 @@ namespace Encompass
/// <param name="easeOutTime">The time that will elapse before time is fully undilated.</param> /// <param name="easeOutTime">The time that will elapse before time is fully undilated.</param>
protected void ActivateTimeDilation(double factor, double easeInTime, double activeTime, double easeOutTime) protected void ActivateTimeDilation(double factor, double easeInTime, double activeTime, double easeOutTime)
{ {
timeManager.ActivateTimeDilation(factor, easeInTime, activeTime, easeOutTime); _timeManager.ActivateTimeDilation(factor, easeInTime, activeTime, easeOutTime);
} }
/// <summary> /// <summary>
@ -667,7 +658,7 @@ namespace Encompass
/// <param name="easeOutTime">The time that will elapse before time is fully undilated.</param> /// <param name="easeOutTime">The time that will elapse before time is fully undilated.</param>
protected void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime) protected void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime)
{ {
timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime); _timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime);
} }
/// <summary> /// <summary>
@ -682,7 +673,7 @@ namespace Encompass
/// <param name="easeOutFunction">An easing function for the easing out of time dilation.</param> /// <param name="easeOutFunction">An easing function for the easing out of time dilation.</param>
protected void ActivateTimeDilation(double factor, double easeInTime, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction) protected void ActivateTimeDilation(double factor, double easeInTime, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction)
{ {
timeManager.ActivateTimeDilation(factor, easeInTime, activeTime, easeOutTime, easeOutFunction); _timeManager.ActivateTimeDilation(factor, easeInTime, activeTime, easeOutTime, easeOutFunction);
} }
/// <summary> /// <summary>
@ -698,7 +689,7 @@ namespace Encompass
/// <param name="easeOutFunction">An easing function for the easing out of time dilation.</param> /// <param name="easeOutFunction">An easing function for the easing out of time dilation.</param>
protected void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction) protected void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction)
{ {
timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime, easeOutFunction); _timeManager.ActivateTimeDilation(factor, easeInTime, easeInFunction, activeTime, easeOutTime, easeOutFunction);
} }
/// <summary> /// <summary>
@ -707,38 +698,38 @@ namespace Encompass
/// <typeparam name="TMessage">The Message subtype.</typeparam> /// <typeparam name="TMessage">The Message subtype.</typeparam>
/// <param name="entity">The entity that all messages in the IEnumerable refer to.</param> /// <param name="entity">The entity that all messages in the IEnumerable refer to.</param>
/// <returns></returns> /// <returns></returns>
protected IEnumerable<TMessage> ReadMessagesWithEntity<TMessage>(Entity entity) where TMessage : struct, IMessage, IHasEntity protected IEnumerable<TMessage> ReadMessagesWithEntity<TMessage>(in Entity entity) where TMessage : struct, IMessage, IHasEntity
{ {
CheckMessageRead<TMessage>(); CheckMessageRead<TMessage>();
return messageManager.WithEntity<TMessage>(entity.ID); return _messageManager.WithEntity<TMessage>(entity.ID);
} }
/// <summary> /// <summary>
/// Efficiently reads a single Message of a given type that references a given Entity. /// Efficiently reads a single Message of a given type that references a given Entity.
/// It is recommended to use this method in conjunction with SomeMessageWithEntity to prevent errors. /// It is recommended to use this method in conjunction with SomeMessageWithEntity to prevent errors.
/// </summary> /// </summary>
protected TMessage ReadMessageWithEntity<TMessage>(Entity entity) where TMessage : struct, IMessage, IHasEntity protected ref readonly TMessage ReadMessageWithEntity<TMessage>(in Entity entity) where TMessage : struct, IMessage, IHasEntity
{ {
CheckMessageRead<TMessage>(); CheckMessageRead<TMessage>();
return messageManager.WithEntitySingular<TMessage>(entity.ID); return ref _messageManager.WithEntitySingular<TMessage>(entity.ID);
} }
/// <summary> /// <summary>
/// Efficiently checks if any Message of a given type referencing a given Entity exists. /// Efficiently checks if any Message of a given type referencing a given Entity exists.
/// </summary> /// </summary>
protected bool SomeMessageWithEntity<TMessage>(Entity entity) where TMessage : struct, IMessage, IHasEntity protected bool SomeMessageWithEntity<TMessage>(in Entity entity) where TMessage : struct, IMessage, IHasEntity
{ {
CheckMessageRead<TMessage>(); CheckMessageRead<TMessage>();
return messageManager.SomeWithEntity<TMessage>(entity.ID); return _messageManager.SomeWithEntity<TMessage>(entity.ID);
} }
internal void CheckAndUpdateTracking(int entityID) internal void CheckAndUpdateTracking(int entityID)
{ {
if (_trackedEntities.Contains(entityID) && !entityQuery.CheckEntity(entityID, componentManager.ExistingBits)) if (_trackedEntities.Contains(entityID) && !_entityQuery.CheckEntity(entityID, _componentManager.ExistingBits))
{ {
_trackedEntities.Remove(entityID); _trackedEntities.Remove(entityID);
} }
else if (!_trackedEntities.Contains(entityID) && entityQuery.CheckEntity(entityID, componentManager.ExistingBits)) else if (!_trackedEntities.Contains(entityID) && _entityQuery.CheckEntity(entityID, _componentManager.ExistingBits))
{ {
_trackedEntities.Add(entityID); _trackedEntities.Add(entityID);
} }
@ -746,11 +737,11 @@ namespace Encompass
internal void ImmediateCheckAndUpdateTracking(int entityID) internal void ImmediateCheckAndUpdateTracking(int entityID)
{ {
if (_trackedEntities.Contains(entityID) && !entityQuery.ImmediateCheckEntity(entityID, componentManager.ImmediateBits, componentManager.ExistingBits)) if (_trackedEntities.Contains(entityID) && !_entityQuery.ImmediateCheckEntity(entityID, _componentManager.ImmediateBits, _componentManager.ExistingBits))
{ {
_trackedEntities.Remove(entityID); _trackedEntities.Remove(entityID);
} }
else if (!_trackedEntities.Contains(entityID) && entityQuery.ImmediateCheckEntity(entityID, componentManager.ImmediateBits, componentManager.ExistingBits)) else if (!_trackedEntities.Contains(entityID) && _entityQuery.ImmediateCheckEntity(entityID, _componentManager.ImmediateBits, _componentManager.ExistingBits))
{ {
_trackedEntities.Add(entityID); _trackedEntities.Add(entityID);
} }
@ -762,30 +753,34 @@ namespace Encompass
internal void BuildEntityQuery() internal void BuildEntityQuery()
{ {
var withMask = BitSet512.Zero; var withMask = BitSet512.Zero;
foreach (var type in queryWithTypes) foreach (var type in QueryWithTypes)
{ {
withMask = withMask.Set(componentManager.TypeToIndex[type]); if (!_componentManager.TypeToIndex.ContainsKey(type)) { _componentManager.TypeToIndex.Add(type, _componentManager.TypeToIndex.Count); }
withMask = withMask.Set(_componentManager.TypeToIndex[type]);
} }
var withoutMask = BitSet512.Zero; var withoutMask = BitSet512.Zero;
foreach (var type in queryWithoutTypes) foreach (var type in QueryWithoutTypes)
{ {
withoutMask = withoutMask.Set(componentManager.TypeToIndex[type]); if (!_componentManager.TypeToIndex.ContainsKey(type)) { _componentManager.TypeToIndex.Add(type, _componentManager.TypeToIndex.Count); }
withoutMask = withoutMask.Set(_componentManager.TypeToIndex[type]);
} }
var immediateMask = BitSet512.Zero; var immediateMask = BitSet512.Zero;
foreach (var type in readImmediateTypes) foreach (var type in ReadImmediateTypes)
{ {
immediateMask = immediateMask.Set(componentManager.TypeToIndex[type]); if (!_componentManager.TypeToIndex.ContainsKey(type)) { _componentManager.TypeToIndex.Add(type, _componentManager.TypeToIndex.Count); }
immediateMask = immediateMask.Set(_componentManager.TypeToIndex[type]);
} }
var existingMask = BitSet512.Zero; var existingMask = BitSet512.Zero;
foreach (var type in readTypes) foreach (var type in ReadTypes)
{ {
existingMask = existingMask.Set(componentManager.TypeToIndex[type]); if (!_componentManager.TypeToIndex.ContainsKey(type)) { _componentManager.TypeToIndex.Add(type, _componentManager.TypeToIndex.Count); }
existingMask = existingMask.Set(_componentManager.TypeToIndex[type]);
} }
entityQuery = new EntitySetQuery( _entityQuery = new EntitySetQuery(
withMask & immediateMask, withMask & immediateMask,
withMask & existingMask, withMask & existingMask,
withoutMask & immediateMask, withoutMask & immediateMask,

View File

@ -13,10 +13,10 @@ namespace Encompass
var readsAttribute = GetType().GetCustomAttribute<Reads>(false); var readsAttribute = GetType().GetCustomAttribute<Reads>(false);
if (readsAttribute != null) if (readsAttribute != null)
{ {
readsAttribute.readTypes.Add(typeof(TMessage)); readsAttribute.ReadTypes.Add(typeof(TMessage));
} }
receiveTypes.Add(typeof(TMessage)); ReceiveTypes.Add(typeof(TMessage));
} }
public override void Update(double dt) public override void Update(double dt)

View File

@ -37,7 +37,7 @@ namespace Encompass
public override int GetHashCode() public override int GetHashCode()
{ {
return ID.GetHashCode(); return HashCode.Combine(ID);
} }
} }
} }

View File

@ -5,48 +5,48 @@ namespace Encompass
{ {
internal class EntityManager internal class EntityManager
{ {
private readonly int entityCapacity; private readonly int _entityCapacity;
private readonly IDManager idManager = new IDManager(); private readonly IDManager _idManager = new IDManager();
private readonly HashSet<int> IDs = new HashSet<int>(); private readonly HashSet<int> _ids = new HashSet<int>();
private readonly HashSet<int> entitiesMarkedForDestroy = new HashSet<int>(); private readonly HashSet<int> _entitiesMarkedForDestroy = new HashSet<int>();
private readonly ComponentManager componentManager; private readonly ComponentManager _componentManager;
public IEnumerable<int> EntityIDs public IEnumerable<int> EntityIDs
{ {
get { return IDs; } get { return _ids; }
} }
public EntityManager(ComponentManager componentManager, int entityCapacity) public EntityManager(ComponentManager componentManager, int entityCapacity)
{ {
this.componentManager = componentManager; _componentManager = componentManager;
this.entityCapacity = entityCapacity; _entityCapacity = entityCapacity;
} }
private int NextID() private int NextID()
{ {
return idManager.NextID(); return _idManager.NextID();
} }
public Entity CreateEntity() public Entity CreateEntity()
{ {
if (IDs.Count < entityCapacity) if (_ids.Count < _entityCapacity)
{ {
var id = NextID(); var id = NextID();
var entity = new Entity(id); var entity = new Entity(id);
IDs.Add(id); _ids.Add(id);
return entity; return entity;
} }
else else
{ {
throw new EntityOverflowException("The number of entities has exceeded the entity capacity of {0}", entityCapacity); throw new EntityOverflowException("The number of entities has exceeded the entity capacity of {0}", _entityCapacity);
} }
} }
public bool EntityExists(int id) public bool EntityExists(int id)
{ {
return IDs.Contains(id); return _ids.Contains(id);
} }
public Entity GetEntity(int id) public Entity GetEntity(int id)
@ -61,20 +61,20 @@ namespace Encompass
public void MarkForDestroy(int entityID) public void MarkForDestroy(int entityID)
{ {
entitiesMarkedForDestroy.Add(entityID); _entitiesMarkedForDestroy.Add(entityID);
} }
public void DestroyMarkedEntities(IEnumerable<Engine> engines) public void DestroyMarkedEntities(IEnumerable<Engine> engines)
{ {
foreach (var entityID in entitiesMarkedForDestroy) foreach (var entityID in _entitiesMarkedForDestroy)
{ {
foreach (var engine in engines) { engine.RegisterDestroyedEntity(entityID); } foreach (var engine in engines) { engine.RegisterDestroyedEntity(entityID); }
componentManager.MarkAllComponentsOnEntityForRemoval(entityID); _componentManager.MarkAllComponentsOnEntityForRemoval(entityID);
IDs.Remove(entityID); _ids.Remove(entityID);
idManager.Free(entityID); _idManager.Free(entityID);
} }
entitiesMarkedForDestroy.Clear(); _entitiesMarkedForDestroy.Clear();
} }
// NOTE: this is very suboptimal // NOTE: this is very suboptimal
@ -82,7 +82,7 @@ namespace Encompass
{ {
foreach (var id in EntityIDs) foreach (var id in EntityIDs)
{ {
if (componentManager.UpToDateEntityIsEmpty(id)) if (_componentManager.UpToDateEntityIsEmpty(id))
{ {
MarkForDestroy(id); MarkForDestroy(id);
} }

View File

@ -1,12 +0,0 @@
using System;
namespace Encompass.Exceptions
{
public class IllegalReadTypeException : Exception
{
public IllegalReadTypeException(
string format,
params object[] args
) : base(string.Format(format, args)) { }
}
}

View File

@ -1,7 +0,0 @@
namespace Encompass
{
/// <summary>
/// Structs that implement IComponent are considered to be Components.
/// </summary>
public interface IComponent { }
}

View File

@ -4,25 +4,25 @@ namespace Encompass
{ {
internal class IDManager internal class IDManager
{ {
int nextID = 0; int _nextID = 0;
private Stack<int> availableIDs = new Stack<int>(); private readonly Stack<int> _availableIDs = new Stack<int>();
public int NextID() public int NextID()
{ {
if (availableIDs.Count > 0) if (_availableIDs.Count > 0)
{ {
return availableIDs.Pop(); return _availableIDs.Pop();
} }
else else
{ {
return nextID++; return _nextID++;
} }
} }
public void Free(int ID) public void Free(int id)
{ {
availableIDs.Push(ID); _availableIDs.Push(id);
} }
} }
} }

View File

@ -0,0 +1,4 @@
namespace Encompass
{
public interface IComponent { }
}

View File

@ -2,6 +2,6 @@ namespace Encompass
{ {
public interface IDrawableComponent public interface IDrawableComponent
{ {
int Layer { get; set; } int Layer { get; }
} }
} }

View File

@ -1,72 +1,71 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Encompass namespace Encompass
{ {
internal class MessageManager internal class MessageManager
{ {
private readonly TimeManager timeManager; private readonly TimeManager _timeManager;
private readonly MessageStore messageStore = new MessageStore(); private readonly MessageStore _messageStore = new MessageStore();
public MessageManager(TimeManager timeManager) public MessageManager(TimeManager timeManager)
{ {
this.timeManager = timeManager; _timeManager = timeManager;
} }
internal void AddMessage<TMessage>(TMessage message) where TMessage : struct, IMessage internal void AddMessage<TMessage>(in TMessage message) where TMessage : struct, IMessage
{ {
messageStore.AddMessage(message); _messageStore.AddMessage(message);
} }
internal void AddMessage<TMessage>(TMessage message, double time) where TMessage : struct, IMessage internal void AddMessage<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{ {
messageStore.AddMessage(message, time); _messageStore.AddMessage(message, time);
} }
internal void AddMessageIgnoringTimeDilation<TMessage>(TMessage message, double time) where TMessage : struct, IMessage internal void AddMessageIgnoringTimeDilation<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{ {
messageStore.AddMessageIgnoringTimeDilation(message, time); _messageStore.AddMessageIgnoringTimeDilation(message, time);
} }
internal void ClearMessages() internal void ClearMessages()
{ {
messageStore.ClearAll(); _messageStore.ClearAll();
} }
internal void ProcessDelayedMessages(double dt) internal void ProcessDelayedMessages(double dt)
{ {
messageStore.ProcessDelayedMessages(dt * timeManager.TimeDilationFactor, dt); _messageStore.ProcessDelayedMessages(dt * _timeManager.TimeDilationFactor, dt);
} }
internal IEnumerable<TMessage> GetMessagesByType<TMessage>() where TMessage : struct, IMessage internal Span<TMessage> GetMessagesByType<TMessage>() where TMessage : struct, IMessage
{ {
return messageStore.All<TMessage>(); return _messageStore.All<TMessage>();
} }
internal bool Any<TMessage>() where TMessage : struct, IMessage internal bool Any<TMessage>() where TMessage : struct, IMessage
{ {
return messageStore.Any<TMessage>(); return _messageStore.Any<TMessage>();
} }
internal TMessage First<TMessage>() where TMessage : struct, IMessage internal ref readonly TMessage First<TMessage>() where TMessage : struct, IMessage
{ {
return messageStore.First<TMessage>(); return ref _messageStore.First<TMessage>();
} }
internal IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity internal IEnumerable<TMessage> WithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
{ {
return messageStore.WithEntity<TMessage>(entityID); return _messageStore.WithEntity<TMessage>(entityID);
} }
internal TMessage WithEntitySingular<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity internal ref readonly TMessage WithEntitySingular<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
{ {
var enumerator = messageStore.WithEntity<TMessage>(entityID).GetEnumerator(); return ref _messageStore.FirstWithEntity<TMessage>(entityID);
enumerator.MoveNext();
return enumerator.Current;
} }
internal bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity internal bool SomeWithEntity<TMessage>(int entityID) where TMessage : struct, IMessage, IHasEntity
{ {
return messageStore.SomeWithEntity<TMessage>(entityID); return _messageStore.SomeWithEntity<TMessage>(entityID);
} }
} }
} }

View File

@ -5,40 +5,40 @@ namespace Encompass
{ {
internal class RenderManager internal class RenderManager
{ {
private readonly EntityManager entityManager; private readonly EntityManager _entityManager;
private readonly DrawLayerManager drawLayerManager; private readonly DrawLayerManager _drawLayerManager;
private readonly Dictionary<Type, Action<Entity, IComponent>> drawComponentTypeToOrderedRenderer = new Dictionary<Type, Action<Entity, IComponent>>(256); private readonly Dictionary<Type, Action<Entity>> _drawComponentTypeToOrderedRenderer = new Dictionary<Type, Action<Entity>>(256);
public RenderManager(EntityManager entityManager, DrawLayerManager drawLayerManager) public RenderManager(EntityManager entityManager, DrawLayerManager drawLayerManager)
{ {
this.entityManager = entityManager; _entityManager = entityManager;
this.drawLayerManager = drawLayerManager; _drawLayerManager = drawLayerManager;
} }
public void RegisterOrderedRenderer<TComponent>(Action<Entity, IComponent> renderAction) where TComponent : struct, IComponent public void RegisterOrderedRenderer<TComponent>(Action<Entity> renderAction) where TComponent : struct
{ {
drawComponentTypeToOrderedRenderer.Add(typeof(TComponent), renderAction); _drawComponentTypeToOrderedRenderer.Add(typeof(TComponent), renderAction);
drawLayerManager.RegisterOrderedDrawable<TComponent>(); _drawLayerManager.RegisterOrderedDrawable<TComponent>();
} }
public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer) public void RegisterGeneralRendererWithLayer(GeneralRenderer renderer, int layer)
{ {
drawLayerManager.RegisterGeneralRendererWithLayer(renderer, layer); _drawLayerManager.RegisterGeneralRendererWithLayer(renderer, layer);
} }
public void Draw() public void Draw()
{ {
foreach (var layer in drawLayerManager.LayerOrder) foreach (var layer in _drawLayerManager.LayerOrder)
{ {
var generalRendererSet = drawLayerManager.GeneralRenderersByLayer(layer); var generalRendererSet = _drawLayerManager.GeneralRenderersByLayer(layer);
foreach (var (entityID, componentType, component) in drawLayerManager.AllInLayer(layer)) foreach (var (entityID, componentType) in _drawLayerManager.AllInLayer(layer))
{ {
if (drawComponentTypeToOrderedRenderer.ContainsKey(componentType)) if (_drawComponentTypeToOrderedRenderer.ContainsKey(componentType))
{ {
var internalRenderAction = drawComponentTypeToOrderedRenderer[componentType]; var internalRenderAction = _drawComponentTypeToOrderedRenderer[componentType];
internalRenderAction(entityManager.GetEntity(entityID), component); internalRenderAction(_entityManager.GetEntity(entityID));
} }
} }

View File

@ -1,75 +1,56 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
namespace Encompass namespace Encompass
{ {
public abstract class Renderer public abstract class Renderer
{ {
internal EntityManager entityManager; internal EntityManager _entityManager;
internal ComponentManager componentManager; internal ComponentManager _componentManager;
internal void AssignEntityManager(EntityManager entityManager) internal void AssignEntityManager(EntityManager entityManager)
{ {
this.entityManager = entityManager; _entityManager = entityManager;
} }
internal void AssignComponentManager(ComponentManager componentManager) internal void AssignComponentManager(ComponentManager componentManager)
{ {
this.componentManager = componentManager; _componentManager = componentManager;
} }
protected IEnumerable<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent protected Span<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
{ {
foreach (var pair in ReadComponentsIncludingEntity<TComponent>()) return _componentManager.GetExistingEntities<TComponent>();
{
yield return pair.Item2;
}
} }
protected Entity ReadEntity<TComponent>() where TComponent : struct, IComponent protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
{ {
return ReadComponentIncludingEntity<TComponent>().Item2; return ref _componentManager.ExistingSingularEntity<TComponent>();
} }
protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent protected Span<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
{ {
return componentManager.GetComponentsByType<TComponent>(); return _componentManager.GetComponentsByType<TComponent>();
} }
protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity<TComponent>() where TComponent : struct, IComponent protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
{ {
foreach (var (component, id) in componentManager.GetComponentsIncludingEntity<TComponent>()) return ref _componentManager.ExistingSingular<TComponent>();
{
yield return (component, entityManager.GetEntity(id));
}
} }
protected TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent protected ref readonly TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
var enumerator = ReadComponents<TComponent>().GetEnumerator(); return ref _componentManager.GetComponentByEntityAndType<TComponent>(entity.ID);
enumerator.MoveNext();
return enumerator.Current;
}
protected (TComponent, Entity) ReadComponentIncludingEntity<TComponent>() where TComponent : struct, IComponent
{
var enumerator = ReadComponentsIncludingEntity<TComponent>().GetEnumerator();
enumerator.MoveNext();
return enumerator.Current;
}
protected TComponent GetComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{
return componentManager.GetComponentByEntityAndType<TComponent>(entity.ID);
} }
protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct, IComponent protected bool HasComponent<TComponent>(Entity entity) where TComponent : struct, IComponent
{ {
return componentManager.EntityHasComponentOfType<TComponent>(entity.ID); return _componentManager.EntityHasComponentOfType<TComponent>(entity.ID);
} }
protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent protected bool SomeComponent<TComponent>() where TComponent : struct, IComponent
{ {
return componentManager.SomeExistingComponent<TComponent>(); return _componentManager.SomeExistingComponent<TComponent>();
} }
} }
} }

View File

@ -7,11 +7,12 @@ namespace Encompass
/// </summary> /// </summary>
public abstract class OrderedRenderer<TComponent> : Renderer where TComponent : struct, IComponent, IDrawableComponent public abstract class OrderedRenderer<TComponent> : Renderer where TComponent : struct, IComponent, IDrawableComponent
{ {
public abstract void Render(Entity entity, TComponent drawComponent); public abstract void Render(Entity entity, in TComponent drawComponent);
internal void InternalRender(Entity entity, IComponent component) internal void InternalRender(Entity entity)
{ {
Render(entity, (TComponent)component); ref readonly var component = ref GetComponent<TComponent>(entity);
Render(entity, component);
} }
} }
} }

View File

@ -1,35 +1,56 @@
using System;
namespace Encompass namespace Encompass
{ {
internal struct TimeDilationData internal struct TimeDilationData
{ {
public double elapsedTime; private readonly double _factor;
public double easeInTime;
public System.Func<double, double, double, double, double> easeInFunction;
public double activeTime;
public double easeOutTime;
public System.Func<double, double, double, double, double> easeOutFunction;
public double factor;
public double Factor public double Factor
{ {
get get
{ {
if (elapsedTime < easeInTime) if (ElapsedTime < EaseInTime)
{ {
return easeInFunction(elapsedTime, 1, factor - 1, easeInTime); return EaseInFunction(ElapsedTime, 1, _factor - 1, EaseInTime);
} }
else if (elapsedTime < easeInTime + activeTime) else if (ElapsedTime < EaseInTime + ActiveTime)
{ {
return factor; return _factor;
} }
else if (elapsedTime < easeInTime + activeTime + easeOutTime) else if (ElapsedTime < EaseInTime + ActiveTime + EaseOutTime)
{ {
var elapsedOutTime = elapsedTime - easeInTime - activeTime; var elapsedOutTime = ElapsedTime - EaseInTime - ActiveTime;
return easeOutFunction(elapsedOutTime, factor, 1 - factor, easeOutTime); return EaseOutFunction(elapsedOutTime, _factor, 1 - _factor, EaseOutTime);
} }
return 1; return 1;
} }
} }
public double ElapsedTime { get; set; }
public double EaseInTime { get; }
public Func<double, double, double, double, double> EaseInFunction { get; }
public double ActiveTime { get; }
public double EaseOutTime { get; }
public Func<double, double, double, double, double> EaseOutFunction { get; }
public TimeDilationData(
double factor,
double easeInTime,
Func<double, double, double, double, double> easeInfunction,
double activeTime,
double easeOutTime,
Func<double, double, double, double, double> easeOutFunction
)
{
_factor = factor;
EaseInTime = easeInTime;
EaseInFunction = easeInfunction;
ActiveTime = activeTime;
EaseOutTime = easeOutTime;
EaseOutFunction = easeOutFunction;
ElapsedTime = 0;
}
} }
} }

View File

@ -4,7 +4,7 @@ namespace Encompass
{ {
internal class TimeManager internal class TimeManager
{ {
private readonly List<TimeDilationData> timeDilationDatas = new List<TimeDilationData>(32); private readonly List<TimeDilationData> _timeDilationDatas = new List<TimeDilationData>(32);
private double Linear(double t, double b, double c, double d) private double Linear(double t, double b, double c, double d)
{ {
@ -15,36 +15,36 @@ namespace Encompass
{ {
get get
{ {
if (timeDilationDatas.Count == 0) { return 1; } if (_timeDilationDatas.Count == 0) { return 1; }
var average = 0.0; var average = 0.0;
foreach (var data in timeDilationDatas) foreach (var data in _timeDilationDatas)
{ {
average += data.Factor; average += data.Factor;
} }
return average / timeDilationDatas.Count; return average / _timeDilationDatas.Count;
} }
} }
public bool TimeDilationActive public bool TimeDilationActive
{ {
get => timeDilationDatas.Count != 0; get => _timeDilationDatas.Count != 0;
} }
public void Update(double dt) public void Update(double dt)
{ {
for (var i = timeDilationDatas.Count - 1; i >= 0; i--) for (var i = _timeDilationDatas.Count - 1; i >= 0; i--)
{ {
var data = timeDilationDatas[i]; var data = _timeDilationDatas[i];
data.elapsedTime += dt; data.ElapsedTime += dt;
if (data.elapsedTime > data.easeInTime + data.activeTime + data.easeOutTime) if (data.ElapsedTime > data.EaseInTime + data.ActiveTime + data.EaseOutTime)
{ {
timeDilationDatas.RemoveAt(i); _timeDilationDatas.RemoveAt(i);
} }
else else
{ {
timeDilationDatas[i] = data; _timeDilationDatas[i] = data;
} }
} }
} }
@ -66,16 +66,15 @@ namespace Encompass
public void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction) public void ActivateTimeDilation(double factor, double easeInTime, System.Func<double, double, double, double, double> easeInFunction, double activeTime, double easeOutTime, System.Func<double, double, double, double, double> easeOutFunction)
{ {
timeDilationDatas.Add(new TimeDilationData _timeDilationDatas.Add(new TimeDilationData
{ (
elapsedTime = 0, factor,
easeInTime = easeInTime, easeInTime,
easeInFunction = easeInFunction, easeInFunction,
activeTime = activeTime, activeTime,
easeOutTime = easeOutTime, easeOutTime,
easeOutFunction = easeOutFunction, easeOutFunction
factor = factor ));
});
} }
} }
} }

View File

@ -5,13 +5,13 @@ namespace Encompass
{ {
internal class TrackingManager internal class TrackingManager
{ {
private Dictionary<Type, HashSet<Engine>> _immediateComponentTypesToEngines = new Dictionary<Type, HashSet<Engine>>(); private readonly Dictionary<Type, HashSet<Engine>> _immediateComponentTypesToEngines = new Dictionary<Type, HashSet<Engine>>();
private Dictionary<Type, HashSet<Engine>> _componentTypesToEngines = new Dictionary<Type, HashSet<Engine>>(); private readonly Dictionary<Type, HashSet<Engine>> _componentTypesToEngines = new Dictionary<Type, HashSet<Engine>>();
private HashSet<(int, Type)> _additions = new HashSet<(int, Type)>(); private readonly HashSet<(int, Type)> _additions = new HashSet<(int, Type)>();
private HashSet<(int, Type)> _removals = new HashSet<(int, Type)>(); private readonly HashSet<(int, Type)> _removals = new HashSet<(int, Type)>();
private HashSet<(int, Engine)> _pairsToCheck = new HashSet<(int, Engine)>(); private readonly HashSet<(int, Engine)> _pairsToCheck = new HashSet<(int, Engine)>();
public void RegisterComponentTypeToEngine(Type type, Engine engine) public void RegisterComponentTypeToEngine(Type type, Engine engine)
{ {

View File

@ -2,23 +2,24 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
namespace Encompass namespace Encompass
{ {
internal class UberEngine : Engine internal class UberEngine : Engine
{ {
private IEnumerable<Type> _componentTypes; private readonly IEnumerable<Type> _componentTypes;
private IEnumerable<Type> _messageTypes; private readonly IEnumerable<Type> _messageTypes;
public Entity Entity { get; private set; } public Entity Entity { get; private set; }
public UberEngine(IEnumerable<Type> componentTypes, IEnumerable<Type> messageTypes) public UberEngine(IEnumerable<Type> componentTypes, IEnumerable<Type> messageTypes)
{ {
_componentTypes = componentTypes; _componentTypes = componentTypes;
_messageTypes = messageTypes; _messageTypes = messageTypes;
readTypes.UnionWith(componentTypes); ReadTypes.UnionWith(componentTypes);
writeTypes.UnionWith(componentTypes); WriteTypes.UnionWith(componentTypes);
sendTypes.UnionWith(messageTypes); SendTypes.UnionWith(messageTypes);
receiveTypes.UnionWith(messageTypes); ReceiveTypes.UnionWith(messageTypes);
} }
public void Write() public void Write()
@ -39,17 +40,15 @@ namespace Encompass
foreach (var type in _componentTypes) foreach (var type in _componentTypes)
{ {
CallGenericMethod(type, "ReadComponent", null); CallGenericMethod(type, "ReadComponent", null);
CallGenericMethod(type, "ReadComponentIncludingEntity", null); CallGenericWrappedMethod(type, "ReadComponentsWrapper", null);
CallGenericMethod(type, "ReadComponents", null);
CallGenericMethod(type, "ReadComponentsIncludingEntity", null);
CallGenericMethod(type, "ReadEntity", null); CallGenericMethod(type, "ReadEntity", null);
CallGenericMethod(type, "ReadEntities", null); CallGenericWrappedMethod(type, "ReadEntitiesWrapper", null);
CallGenericMethod(type, "GetComponent", new Type[] { typeof(Entity) }, new object[] { Entity }); CallGenericMethod(type, "GetComponent", new object[] { Entity });
CallGenericMethod(type, "HasComponent", new Type[] { typeof(Entity) }, new object[] { Entity }); CallGenericMethod(type, "HasComponent", 1, new object[] { Entity });
CallGenericMethod(type, "SomeComponent", null); CallGenericMethod(type, "SomeComponent", null);
CallGenericMethod(type, "DestroyWith", null); CallGenericMethod(type, "DestroyWith", null);
CallGenericMethod(type, "DestroyAllWith", null); CallGenericMethod(type, "DestroyAllWith", null);
CallGenericMethod(type, "RemoveComponent", new Type[] { typeof(Entity) }, new object[] { Entity }); CallGenericMethod(type, "RemoveComponent", new object[] { Entity });
} }
foreach (var type in _messageTypes) foreach (var type in _messageTypes)
@ -58,20 +57,49 @@ namespace Encompass
CallGenericMethod(type, "SendMessage", 1, new object[] { Activator.CreateInstance(type) }); CallGenericMethod(type, "SendMessage", 1, new object[] { Activator.CreateInstance(type) });
CallGenericMethod(type, "SendMessage", 2, new object[] { Activator.CreateInstance(type), 1 }); CallGenericMethod(type, "SendMessage", 2, new object[] { Activator.CreateInstance(type), 1 });
CallGenericMethod(type, "ReadMessage", null); CallGenericMethod(type, "ReadMessage", null);
CallGenericMethod(type, "ReadMessages", null);
CallGenericWrappedMethod(type, "ReadMessagesWrapper", null);
CallGenericMethod(type, "SomeMessage", null); CallGenericMethod(type, "SomeMessage", null);
if (typeof(IHasEntity).IsAssignableFrom(type)) if (typeof(IHasEntity).IsAssignableFrom(type))
{ {
CallGenericMethod(type, "ReadMessagesWithEntity", new Type[] { typeof(Entity) }, new object[] { Entity }); CallGenericMethod(type, "ReadMessagesWithEntity", new object[] { Entity });
CallGenericMethod(type, "ReadMessageWithEntity", new object[] { Entity });
CallGenericMethod(type, "SomeMessageWithEntity", new object[] { Entity });
} }
} }
} }
// we can't reflect invoke on Span returns right now... so we have non-return wrapper methods
protected void ReadComponentsWrapper<TComponent>() where TComponent : struct, IComponent
{
ReadComponents<TComponent>();
}
protected void ReadMessagesWrapper<TMessage>() where TMessage : struct, IMessage
{
ReadMessages<TMessage>();
}
protected void ReadEntitiesWrapper<TComponent>() where TComponent : struct, IComponent
{
ReadEntities<TComponent>();
}
// trying to use PrepareMethod because we can't reflect invoke methods that return a span...
private void CallGenericMethod(Type type, string methodName, object[] parameters) private void CallGenericMethod(Type type, string methodName, object[] parameters)
{ {
var readComponentMethod = typeof(Engine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance); var readComponentMethod = typeof(Engine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type); var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
genericReadComponentMethod.Invoke(this, parameters); genericReadComponentMethod.Invoke(this, parameters);
// RuntimeHelpers.PrepareMethod(genericReadComponentMethod.MethodHandle);
}
private void CallGenericWrappedMethod(Type type, string methodName, object[] parameters)
{
var readComponentMethod = typeof(UberEngine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
genericReadComponentMethod.Invoke(this, parameters);
} }
private void CallGenericMethod(Type type, string methodName, Type[] types, object[] parameters) private void CallGenericMethod(Type type, string methodName, Type[] types, object[] parameters)
@ -79,6 +107,7 @@ namespace Encompass
var readComponentMethod = typeof(Engine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, types, null); var readComponentMethod = typeof(Engine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance, null, types, null);
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type); var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
genericReadComponentMethod.Invoke(this, parameters); genericReadComponentMethod.Invoke(this, parameters);
// RuntimeHelpers.PrepareMethod(genericReadComponentMethod.MethodHandle);
} }
private void CallGenericMethod(Type type, string methodName, int argumentNum, object[] parameters) private void CallGenericMethod(Type type, string methodName, int argumentNum, object[] parameters)
@ -86,6 +115,7 @@ namespace Encompass
var method = typeof(Engine).GetRuntimeMethods().Where(m => m.Name == methodName && m.GetParameters().Length == argumentNum).First(); var method = typeof(Engine).GetRuntimeMethods().Where(m => m.Name == methodName && m.GetParameters().Length == argumentNum).First();
var genericMethod = method.MakeGenericMethod(type); var genericMethod = method.MakeGenericMethod(type);
genericMethod.Invoke(this, parameters); genericMethod.Invoke(this, parameters);
// RuntimeHelpers.PrepareMethod(genericMethod.MethodHandle);
} }
} }
} }

View File

@ -6,7 +6,7 @@ namespace Encompass
{ {
class UberRenderer : Renderer class UberRenderer : Renderer
{ {
private IEnumerable<Type> _componentTypes; private readonly IEnumerable<Type> _componentTypes;
private Entity _entity; private Entity _entity;
public UberRenderer(IEnumerable<Type> componentTypes) public UberRenderer(IEnumerable<Type> componentTypes)
@ -19,27 +19,43 @@ namespace Encompass
_entity = entity; _entity = entity;
} }
// can't reflect invoke on Span returns...
public void Render() public void Render()
{ {
foreach (var type in _componentTypes) foreach (var type in _componentTypes)
{ {
CallGenericMethod(type, "ReadEntities", null); CallGenericWrappedMethod(type, "ReadEntitiesWrapper", null);
CallGenericMethod(type, "ReadEntity", null); CallGenericMethod(type, "ReadEntity", null);
CallGenericWrappedMethod(type, "ReadComponentsWrapper", null);
CallGenericMethod(type, "ReadComponent", null); CallGenericMethod(type, "ReadComponent", null);
CallGenericMethod(type, "ReadComponentIncludingEntity", null);
CallGenericMethod(type, "ReadComponents", null);
CallGenericMethod(type, "ReadComponentsIncludingEntity", null);
CallGenericMethod(type, "GetComponent", new object[] { _entity }); CallGenericMethod(type, "GetComponent", new object[] { _entity });
CallGenericMethod(type, "HasComponent", new object[] { _entity }); CallGenericMethod(type, "HasComponent", new object[] { _entity });
CallGenericMethod(type, "SomeComponent", null); CallGenericMethod(type, "SomeComponent", null);
} }
} }
protected void ReadEntitiesWrapper<TComponent>() where TComponent : struct, IComponent
{
ReadEntities<TComponent>();
}
protected void ReadComponentsWrapper<TComponent>() where TComponent : struct, IComponent
{
ReadComponents<TComponent>();
}
private void CallGenericMethod(Type type, string methodName, object[] parameters) private void CallGenericMethod(Type type, string methodName, object[] parameters)
{ {
var readComponentMethod = typeof(Renderer).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance); var readComponentMethod = typeof(Renderer).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type); var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
genericReadComponentMethod.Invoke(this, parameters); genericReadComponentMethod.Invoke(this, parameters);
} }
private void CallGenericWrappedMethod(Type type, string methodName, object[] parameters)
{
var readComponentMethod = typeof(UberRenderer).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
var genericReadComponentMethod = readComponentMethod.MakeGenericMethod(type);
genericReadComponentMethod.Invoke(this, parameters);
}
} }
} }

View File

@ -7,13 +7,13 @@ namespace Encompass
/// </summary> /// </summary>
public class World public class World
{ {
private readonly List<Engine> enginesInOrder; private readonly List<Engine> _enginesInOrder;
private readonly EntityManager entityManager; private readonly EntityManager _entityManager;
private readonly ComponentManager componentManager; private readonly ComponentManager _componentManager;
private readonly TrackingManager trackingManager; private readonly TrackingManager _trackingManager;
private readonly MessageManager messageManager; private readonly MessageManager _messageManager;
private readonly TimeManager timeManager; private readonly TimeManager _timeManager;
private readonly RenderManager renderManager; private readonly RenderManager _renderManager;
internal World( internal World(
List<Engine> enginesInOrder, List<Engine> enginesInOrder,
@ -25,13 +25,13 @@ namespace Encompass
RenderManager renderManager RenderManager renderManager
) )
{ {
this.enginesInOrder = enginesInOrder; _enginesInOrder = enginesInOrder;
this.entityManager = entityManager; _entityManager = entityManager;
this.componentManager = componentManager; _componentManager = componentManager;
this.trackingManager = trackingManager; _trackingManager = trackingManager;
this.messageManager = messageManager; _messageManager = messageManager;
this.timeManager = timeManager; _timeManager = timeManager;
this.renderManager = renderManager; _renderManager = renderManager;
} }
/// <summary> /// <summary>
@ -40,15 +40,15 @@ namespace Encompass
/// <param name="dt">The time in seconds that has passed since the previous frame.</param> /// <param name="dt">The time in seconds that has passed since the previous frame.</param>
public void Update(double dt) public void Update(double dt)
{ {
trackingManager.UpdateTracking(); _trackingManager.UpdateTracking();
messageManager.ProcessDelayedMessages(dt); _messageManager.ProcessDelayedMessages(dt);
timeManager.Update(dt); _timeManager.Update(dt);
foreach (var engine in enginesInOrder) foreach (var engine in _enginesInOrder)
{ {
if (engine.usesTimeDilation) if (engine._usesTimeDilation)
{ {
engine.Update(dt * timeManager.TimeDilationFactor); engine.Update(dt * _timeManager.TimeDilationFactor);
} }
else else
{ {
@ -58,12 +58,12 @@ namespace Encompass
engine.ClearNewlyCreatedEntities(); engine.ClearNewlyCreatedEntities();
} }
messageManager.ClearMessages(); _messageManager.ClearMessages();
entityManager.PruneEmptyEntities(); _entityManager.PruneEmptyEntities();
entityManager.DestroyMarkedEntities(enginesInOrder); _entityManager.DestroyMarkedEntities(_enginesInOrder);
componentManager.RemoveMarkedComponents(); _componentManager.RemoveMarkedComponents();
componentManager.WriteComponents(); _componentManager.WriteComponents();
} }
/// <summary> /// <summary>
@ -71,7 +71,7 @@ namespace Encompass
/// </summary> /// </summary>
public void Draw() public void Draw()
{ {
renderManager.Draw(); _renderManager.Draw();
} }
} }
} }

View File

@ -18,43 +18,43 @@ namespace Encompass
/// </remarks> /// </remarks>
public class WorldBuilder public class WorldBuilder
{ {
private readonly int entityCapacity; private readonly int _entityCapacity;
private readonly List<Engine> engines = new List<Engine>(); private readonly List<Engine> _engines = new List<Engine>();
private readonly DirectedGraph<Engine, Unit> engineGraph = GraphBuilder.DirectedGraph<Engine>(); private readonly DirectedGraph<Engine, Unit> _engineGraph = GraphBuilder.DirectedGraph<Engine>();
private readonly ComponentStore startingExistingComponentStore; private readonly ComponentStore _startingExistingComponentStore;
private readonly ComponentStore startingUpToDateComponentStore; private readonly ComponentStore _startingUpToDateComponentStore;
private readonly ComponentManager componentManager; private readonly ComponentManager _componentManager;
private readonly EntityManager entityManager; private readonly EntityManager _entityManager;
private readonly MessageManager messageManager; private readonly MessageManager _messageManager;
private readonly TimeManager timeManager; private readonly TimeManager _timeManager;
private readonly DrawLayerManager drawLayerManager; private readonly DrawLayerManager _drawLayerManager;
private readonly RenderManager renderManager; private readonly RenderManager _renderManager;
private readonly TrackingManager trackingManager; private readonly TrackingManager _trackingManager;
private readonly Dictionary<Type, HashSet<Engine>> typeToReaders = new Dictionary<Type, HashSet<Engine>>(); private readonly Dictionary<Type, HashSet<Engine>> _typeToReaders = new Dictionary<Type, HashSet<Engine>>();
private readonly HashSet<Engine> senders = new HashSet<Engine>(); private readonly HashSet<Engine> _senders = new HashSet<Engine>();
private readonly HashSet<Type> componentTypesToPreload = new HashSet<Type>(); private readonly HashSet<Type> _componentTypesToPreload = new HashSet<Type>();
private readonly HashSet<Type> messageTypes = new HashSet<Type>(); private readonly HashSet<Type> _messageTypes = new HashSet<Type>();
private readonly Dictionary<Type, int> typeToIndex = new Dictionary<Type, int>(); private readonly Dictionary<Type, int> _typeToIndex = new Dictionary<Type, int>();
public WorldBuilder(int entityCapacity = 32768) public WorldBuilder(int entityCapacity = 32768)
{ {
this.entityCapacity = entityCapacity; _entityCapacity = entityCapacity;
drawLayerManager = new DrawLayerManager(typeToIndex); _drawLayerManager = new DrawLayerManager();
timeManager = new TimeManager(); _timeManager = new TimeManager();
trackingManager = new TrackingManager(); _trackingManager = new TrackingManager();
componentManager = new ComponentManager(drawLayerManager, typeToIndex); _componentManager = new ComponentManager(_drawLayerManager, _typeToIndex);
messageManager = new MessageManager(timeManager); _messageManager = new MessageManager(_timeManager);
entityManager = new EntityManager(componentManager, entityCapacity); _entityManager = new EntityManager(_componentManager, entityCapacity);
renderManager = new RenderManager(entityManager, drawLayerManager); _renderManager = new RenderManager(_entityManager, _drawLayerManager);
startingExistingComponentStore = new ComponentStore(typeToIndex); _startingExistingComponentStore = new ComponentStore(_typeToIndex);
startingUpToDateComponentStore = new ComponentStore(typeToIndex); _startingUpToDateComponentStore = new ComponentStore(_typeToIndex);
} }
/// <summary> /// <summary>
@ -62,56 +62,56 @@ namespace Encompass
/// </summary> /// </summary>
public Entity CreateEntity() public Entity CreateEntity()
{ {
return entityManager.CreateEntity(); return _entityManager.CreateEntity();
} }
/// <summary> /// <summary>
/// Specifies that the given Message should be sent immediately on the first World Update. /// Specifies that the given Message should be sent immediately on the first World Update.
/// </summary> /// </summary>
public void SendMessage<TMessage>(TMessage message) where TMessage : struct, IMessage public void SendMessage<TMessage>(in TMessage message) where TMessage : struct, IMessage
{ {
messageManager.AddMessage(message); _messageManager.AddMessage(message);
} }
/// <summary> /// <summary>
/// Specifies that the given Message should be sent after the specified number of seconds after the first World Update. /// Specifies that the given Message should be sent after the specified number of seconds after the first World Update.
/// </summary> /// </summary>
public void SendMessage<TMessage>(TMessage message, double time) where TMessage : struct, IMessage public void SendMessage<TMessage>(in TMessage message, double time) where TMessage : struct, IMessage
{ {
messageManager.AddMessage<TMessage>(message, time); _messageManager.AddMessage<TMessage>(message, time);
} }
/// <summary> /// <summary>
/// Sets Component data for the specified Component Type on the specified Entity. /// Sets Component data for the specified Component Type on the specified Entity.
/// </summary> /// </summary>
public void SetComponent<TComponent>(Entity entity, TComponent component) where TComponent : struct, IComponent public void SetComponent<TComponent>(Entity entity, in TComponent component) where TComponent : struct
{ {
RegisterComponentType<TComponent>(); RegisterComponentType<TComponent>();
startingExistingComponentStore.Set(entity.ID, component); _startingExistingComponentStore.Set(entity.ID, component);
startingUpToDateComponentStore.Set(entity.ID, component); _startingUpToDateComponentStore.Set(entity.ID, component);
if (component is IDrawableComponent drawableComponent) if (component is IDrawableComponent drawableComponent)
{ {
componentManager.RegisterDrawableComponent(entity.ID, component, drawableComponent.Layer); _componentManager.RegisterDrawableComponent<TComponent>(entity.ID, drawableComponent.Layer);
drawLayerManager.RegisterOrderedDrawable<TComponent>(); _drawLayerManager.RegisterOrderedDrawable<TComponent>();
} }
} }
internal void RegisterComponentType<TComponent>() where TComponent : struct, IComponent internal void RegisterComponentType<TComponent>() where TComponent : struct
{ {
if (!typeToIndex.ContainsKey(typeof(TComponent))) if (!_typeToIndex.ContainsKey(typeof(TComponent)))
{ {
typeToIndex.Add(typeof(TComponent), typeToIndex.Count); _typeToIndex.Add(typeof(TComponent), _typeToIndex.Count);
componentTypesToPreload.Add(typeof(TComponent)); _componentTypesToPreload.Add(typeof(TComponent));
componentManager.RegisterComponentType<TComponent>(); _componentManager.RegisterComponentType<TComponent>();
startingExistingComponentStore.RegisterComponentType<TComponent>(); _startingExistingComponentStore.RegisterComponentType<TComponent>();
startingUpToDateComponentStore.RegisterComponentType<TComponent>(); _startingUpToDateComponentStore.RegisterComponentType<TComponent>();
} }
} }
internal void RegisterMessageTypes(IEnumerable<Type> types) internal void RegisterMessageTypes(IEnumerable<Type> types)
{ {
messageTypes.UnionWith(types); _messageTypes.UnionWith(types);
} }
/// <summary> /// <summary>
@ -120,21 +120,21 @@ namespace Encompass
/// <param name="engine">An instance of an Engine.</param> /// <param name="engine">An instance of an Engine.</param>
public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine public Engine AddEngine<TEngine>(TEngine engine) where TEngine : Engine
{ {
engine.AssignEntityManager(entityManager); engine.AssignEntityManager(_entityManager);
engine.AssignComponentManager(componentManager); engine.AssignComponentManager(_componentManager);
engine.AssignMessageManager(messageManager); engine.AssignMessageManager(_messageManager);
engine.AssignTimeManager(timeManager); engine.AssignTimeManager(_timeManager);
engine.AssignTrackingManager(trackingManager); engine.AssignTrackingManager(_trackingManager);
engines.Add(engine); _engines.Add(engine);
engineGraph.AddNode(engine); _engineGraph.AddNode(engine);
var messageReceiveTypes = engine.receiveTypes; var messageReceiveTypes = engine.ReceiveTypes;
var messageSendTypes = engine.sendTypes; var messageSendTypes = engine.SendTypes;
RegisterMessageTypes(engine.receiveTypes.Union(engine.sendTypes)); RegisterMessageTypes(engine.ReceiveTypes.Union(engine.SendTypes));
foreach (var writeImmediateType in engine.writeImmediateTypes.Intersect(engine.readImmediateTypes)) foreach (var writeImmediateType in engine.WriteImmediateTypes.Intersect(engine.ReadImmediateTypes))
{ {
throw new EngineSelfCycleException("Engine {0} both writes and reads immediate Component {1}", engine.GetType().Name, writeImmediateType.Name); throw new EngineSelfCycleException("Engine {0} both writes and reads immediate Component {1}", engine.GetType().Name, writeImmediateType.Name);
} }
@ -144,28 +144,28 @@ namespace Encompass
throw new EngineSelfCycleException("Engine {0} both receives and sends Message {1}", engine.GetType().Name, messageType.Name); throw new EngineSelfCycleException("Engine {0} both receives and sends Message {1}", engine.GetType().Name, messageType.Name);
} }
if (messageSendTypes.Count > 0 || engine.writeImmediateTypes.Count > 0) if (messageSendTypes.Count > 0 || engine.WriteImmediateTypes.Count > 0)
{ {
senders.Add(engine); _senders.Add(engine);
} }
foreach (var componentType in engine.queryWithTypes.Union(engine.queryWithoutTypes)) foreach (var componentType in engine.QueryWithTypes.Union(engine.QueryWithoutTypes))
{ {
trackingManager.RegisterComponentTypeToEngine(componentType, engine); _trackingManager.RegisterComponentTypeToEngine(componentType, engine);
if (engine.readImmediateTypes.Contains(componentType)) if (engine.ReadImmediateTypes.Contains(componentType))
{ {
trackingManager.RegisterImmediateComponentTypeToEngine(componentType, engine); _trackingManager.RegisterImmediateComponentTypeToEngine(componentType, engine);
} }
} }
foreach (var receiveType in engine.receiveTypes.Union(engine.readImmediateTypes)) foreach (var receiveType in engine.ReceiveTypes.Union(engine.ReadImmediateTypes))
{ {
if (!typeToReaders.ContainsKey(receiveType)) if (!_typeToReaders.ContainsKey(receiveType))
{ {
typeToReaders.Add(receiveType, new HashSet<Engine>()); _typeToReaders.Add(receiveType, new HashSet<Engine>());
} }
typeToReaders[receiveType].Add(engine); _typeToReaders[receiveType].Add(engine);
} }
return engine; return engine;
@ -177,7 +177,7 @@ namespace Encompass
/// <param name="layer">The draw layer to register.</param> /// <param name="layer">The draw layer to register.</param>
public void RegisterDrawLayer(int layer) public void RegisterDrawLayer(int layer)
{ {
drawLayerManager.RegisterDrawLayer(layer); _drawLayerManager.RegisterDrawLayer(layer);
} }
/// <summary> /// <summary>
@ -186,9 +186,9 @@ namespace Encompass
public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IComponent, IDrawableComponent public OrderedRenderer<TComponent> AddOrderedRenderer<TComponent>(OrderedRenderer<TComponent> renderer) where TComponent : struct, IComponent, IDrawableComponent
{ {
RegisterComponentType<TComponent>(); RegisterComponentType<TComponent>();
renderer.AssignEntityManager(entityManager); renderer.AssignEntityManager(_entityManager);
renderer.AssignComponentManager(componentManager); renderer.AssignComponentManager(_componentManager);
renderManager.RegisterOrderedRenderer<TComponent>(renderer.InternalRender); _renderManager.RegisterOrderedRenderer<TComponent>(renderer.InternalRender);
return renderer; return renderer;
} }
@ -200,29 +200,29 @@ namespace Encompass
/// <param name="layer">The layer at which the GeneralRenderer should render. Higher numbers draw over lower numbers.</param> /// <param name="layer">The layer at which the GeneralRenderer should render. Higher numbers draw over lower numbers.</param>
public TRenderer AddGeneralRenderer<TRenderer>(TRenderer renderer, int layer) where TRenderer : GeneralRenderer public TRenderer AddGeneralRenderer<TRenderer>(TRenderer renderer, int layer) where TRenderer : GeneralRenderer
{ {
renderer.AssignEntityManager(entityManager); renderer.AssignEntityManager(_entityManager);
renderer.AssignComponentManager(componentManager); renderer.AssignComponentManager(_componentManager);
renderManager.RegisterGeneralRendererWithLayer(renderer, layer); _renderManager.RegisterGeneralRendererWithLayer(renderer, layer);
return renderer; return renderer;
} }
private void BuildEngineGraph() private void BuildEngineGraph()
{ {
foreach (var senderEngine in senders) foreach (var senderEngine in _senders)
{ {
foreach (var messageType in senderEngine.sendTypes.Union(senderEngine.writeImmediateTypes)) foreach (var messageType in senderEngine.SendTypes.Union(senderEngine.WriteImmediateTypes))
{ {
if (typeToReaders.ContainsKey(messageType)) if (_typeToReaders.ContainsKey(messageType))
{ {
foreach (var readerEngine in typeToReaders[messageType]) foreach (var readerEngine in _typeToReaders[messageType])
{ {
if (senderEngine != readerEngine) if (senderEngine != readerEngine)
{ {
if (!engineGraph.Exists(senderEngine, readerEngine)) if (!_engineGraph.Exists(senderEngine, readerEngine))
{ {
engineGraph.AddEdge(senderEngine, readerEngine); _engineGraph.AddEdge(senderEngine, readerEngine);
} }
} }
} }
@ -240,9 +240,9 @@ namespace Encompass
{ {
BuildEngineGraph(); BuildEngineGraph();
if (engineGraph.Cyclic()) if (_engineGraph.Cyclic())
{ {
var cycles = engineGraph.SimpleCycles(); var cycles = _engineGraph.SimpleCycles();
var errorString = "Cycle(s) found in Engines: "; var errorString = "Cycle(s) found in Engines: ";
foreach (var cycle in cycles) foreach (var cycle in cycles)
{ {
@ -262,25 +262,25 @@ namespace Encompass
var writePriorities = new Dictionary<Type, HashSet<int>>(); var writePriorities = new Dictionary<Type, HashSet<int>>();
var writeMessageToEngines = new Dictionary<Type, List<Engine>>(); var writeMessageToEngines = new Dictionary<Type, List<Engine>>();
foreach (var engine in engines) foreach (var engine in _engines)
{ {
if (engine.GetType().GetCustomAttribute<IgnoresTimeDilation>() != null) if (engine.GetType().GetCustomAttribute<IgnoresTimeDilation>() != null)
{ {
engine.usesTimeDilation = false; engine._usesTimeDilation = false;
} }
var defaultWritePriorityAttribute = engine.GetType().GetCustomAttribute<DefaultWritePriority>(false); var defaultWritePriorityAttribute = engine.GetType().GetCustomAttribute<DefaultWritePriority>(false);
foreach (var writeType in engine.writeTypes) foreach (var writeType in engine.WriteTypes)
{ {
int? priority = null; int? priority = null;
if (engine.writePriorities.ContainsKey(writeType)) if (engine.WritePriorities.ContainsKey(writeType))
{ {
priority = engine.writePriorities[writeType]; priority = engine.WritePriorities[writeType];
} }
else if (defaultWritePriorityAttribute != null) else if (defaultWritePriorityAttribute != null)
{ {
priority = defaultWritePriorityAttribute.writePriority; priority = defaultWritePriorityAttribute.WritePriority;
} }
if (priority.HasValue) if (priority.HasValue)
@ -354,25 +354,11 @@ namespace Encompass
throw new EngineWriteConflictException(errorString); throw new EngineWriteConflictException(errorString);
} }
// doing reflection to grab all component types, because not all writes need to be declared PreloadJIT(_messageTypes);
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var componentType in assembly.GetTypes())
{
if (typeof(IComponent).IsAssignableFrom(componentType) && componentType.IsValueType && !componentType.IsEnum && !componentType.IsPrimitive)
{
var method = typeof(WorldBuilder).GetMethod("RegisterComponentType", BindingFlags.NonPublic | BindingFlags.Instance);
var generic = method.MakeGenericMethod(componentType);
generic.Invoke(this, null);
}
}
}
PreloadJIT(componentTypesToPreload, messageTypes);
var engineOrder = new List<Engine>(); var engineOrder = new List<Engine>();
foreach (var engine in engineGraph.TopologicalSort()) foreach (var engine in _engineGraph.TopologicalSort())
{ {
engineOrder.Add(engine); engineOrder.Add(engine);
engine.BuildEntityQuery(); engine.BuildEntityQuery();
@ -380,18 +366,18 @@ namespace Encompass
var world = new World( var world = new World(
engineOrder, engineOrder,
entityManager, _entityManager,
componentManager, _componentManager,
trackingManager, _trackingManager,
messageManager, _messageManager,
timeManager, _timeManager,
renderManager _renderManager
); );
componentManager.SetExistingComponentStore(startingExistingComponentStore); _componentManager.SetExistingComponentStore(_startingExistingComponentStore);
componentManager.SetUpToDateComponentStore(startingUpToDateComponentStore); _componentManager.SetUpToDateComponentStore(_startingUpToDateComponentStore);
trackingManager.InitializeTracking(entityManager.EntityIDs); _trackingManager.InitializeTracking(_entityManager.EntityIDs);
return world; return world;
} }
@ -402,19 +388,44 @@ namespace Encompass
/// It does so by grabbing all component and message types known to the WorldBuilder and /// It does so by grabbing all component and message types known to the WorldBuilder and
/// executing every possible generic method that could be executed with those types. /// executing every possible generic method that could be executed with those types.
/// </summary> /// </summary>
private void PreloadJIT(IEnumerable<Type> componentTypes, IEnumerable<Type> messageTypes) private void PreloadJIT(IEnumerable<Type> messageTypes)
{ {
var dummyTimeManager = new TimeManager(); var dummyTimeManager = new TimeManager();
var dummyMessageManager = new MessageManager(dummyTimeManager); var dummyMessageManager = new MessageManager(dummyTimeManager);
var dummyDrawLayerManager = new DrawLayerManager(typeToIndex); var dummyDrawLayerManager = new DrawLayerManager();
var dummyTrackingManager = new TrackingManager(); var dummyTrackingManager = new TrackingManager();
var dummyComponentManager = new ComponentManager(dummyDrawLayerManager, typeToIndex); var dummyComponentManager = new ComponentManager(dummyDrawLayerManager, _typeToIndex);
var dummyEntityManager = new EntityManager(dummyComponentManager, entityCapacity); var dummyEntityManager = new EntityManager(dummyComponentManager, _entityCapacity);
var dummyRenderManager = new RenderManager(dummyEntityManager, dummyDrawLayerManager); var dummyRenderManager = new RenderManager(dummyEntityManager, dummyDrawLayerManager);
// doing reflection to grab all component types, because not all writes need to be declared
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var componentType in assembly.GetTypes())
{
if (typeof(IComponent).IsAssignableFrom(componentType) && componentType.IsValueType && !componentType.IsEnum && !componentType.IsPrimitive)
{
var method = typeof(WorldBuilder).GetMethod("RegisterComponentType", BindingFlags.NonPublic | BindingFlags.Instance);
var generic = method.MakeGenericMethod(componentType);
generic.Invoke(this, null);
var dummyRegisterMethod = typeof(ComponentManager).GetMethod("RegisterComponentType", BindingFlags.Public | BindingFlags.Instance);
var dummyGeneric = dummyRegisterMethod.MakeGenericMethod(componentType);
dummyGeneric.Invoke(dummyComponentManager, null);
}
if (componentType.GetInterface("IDrawableComponent") != null)
{
var drawLayerManagerRegisterMethod = typeof(DrawLayerManager).GetMethod("RegisterOrderedDrawable");
var drawLayerManagerRegisterGenericMethod = drawLayerManagerRegisterMethod.MakeGenericMethod(componentType);
drawLayerManagerRegisterGenericMethod.Invoke(dummyDrawLayerManager, null);
}
}
}
var prepEngineOrder = new List<Engine>(); var prepEngineOrder = new List<Engine>();
var uberEngine = new UberEngine(componentTypes, messageTypes); var uberEngine = new UberEngine(_componentTypesToPreload, messageTypes);
uberEngine.AssignEntityManager(dummyEntityManager); uberEngine.AssignEntityManager(dummyEntityManager);
uberEngine.AssignComponentManager(dummyComponentManager); uberEngine.AssignComponentManager(dummyComponentManager);
@ -422,24 +433,10 @@ namespace Encompass
uberEngine.AssignTimeManager(dummyTimeManager); uberEngine.AssignTimeManager(dummyTimeManager);
uberEngine.AssignTrackingManager(dummyTrackingManager); uberEngine.AssignTrackingManager(dummyTrackingManager);
var uberRenderer = new UberRenderer(componentTypes); var uberRenderer = new UberRenderer(_componentTypesToPreload);
uberRenderer.AssignComponentManager(dummyComponentManager); uberRenderer.AssignComponentManager(dummyComponentManager);
uberRenderer.AssignEntityManager(dummyEntityManager); uberRenderer.AssignEntityManager(dummyEntityManager);
foreach (var type in componentTypes)
{
var componentManagerRegisterMethod = typeof(ComponentManager).GetMethod("RegisterComponentType");
var componentManagerRegisterGenericMethod = componentManagerRegisterMethod.MakeGenericMethod(type);
componentManagerRegisterGenericMethod.Invoke(dummyComponentManager, null);
if (type.GetInterface("IDrawableComponent") != null)
{
var drawLayerManagerRegisterMethod = typeof(DrawLayerManager).GetMethod("RegisterOrderedDrawable");
var drawLayerManagerRegisterGenericMethod = drawLayerManagerRegisterMethod.MakeGenericMethod(type);
drawLayerManagerRegisterGenericMethod.Invoke(dummyDrawLayerManager, null);
}
}
prepEngineOrder.Add(uberEngine); prepEngineOrder.Add(uberEngine);
var dummyWorld = new World( var dummyWorld = new World(

View File

@ -1,6 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace>Encompass</RootNamespace> <RootNamespace>Encompass</RootNamespace>
<PackageId>EncompassECS.Framework</PackageId> <PackageId>EncompassECS.Framework</PackageId>
<Version>0.20.0</Version> <Version>0.20.0</Version>
@ -26,5 +28,6 @@
<PackageReference Include="MoonTools.Core.Graph" Version="1.0.0" /> <PackageReference Include="MoonTools.Core.Graph" Version="1.0.0" />
<PackageReference Include="MoonTools.FastCollections" Version="1.0.0" /> <PackageReference Include="MoonTools.FastCollections" Version="1.0.0" />
<PackageReference Include="System.Collections.Immutable" Version="1.7.0" /> <PackageReference Include="System.Collections.Immutable" Version="1.7.0" />
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -2,14 +2,14 @@ using NUnit.Framework;
using FluentAssertions; using FluentAssertions;
using Encompass; using Encompass;
using System.Runtime.CompilerServices;
namespace Tests namespace Tests
{ {
public class ComponentTests public class ComponentTests
{ {
struct MockComponent : Encompass.IComponent struct MockComponent : IComponent
{ {
public string myString;
public int myInt; public int myInt;
} }
@ -26,7 +26,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var entityMessage in ReadMessages<EntityMessage>()) foreach (ref readonly var entityMessage in ReadMessages<EntityMessage>())
{ {
gottenMockComponent = GetComponent<MockComponent>(entityMessage.entity); gottenMockComponent = GetComponent<MockComponent>(entityMessage.entity);
} }
@ -45,25 +45,27 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var addComponentTestMessage in ReadMessages<AddComponentTestMessage>()) foreach (ref readonly var addComponentTestMessage in ReadMessages<AddComponentTestMessage>())
{ {
Assert.IsTrue(HasComponent<MockComponent>(addComponentTestMessage.entity)); Assert.IsTrue(HasComponent<MockComponent>(addComponentTestMessage.entity));
Assert.That(GetComponent<MockComponent>(addComponentTestMessage.entity), Is.EqualTo(addComponentTestMessage.mockComponent)); ref readonly var gottenComponent = ref GetComponent<MockComponent>(addComponentTestMessage.entity);
gottenComponent.Should().BeEquivalentTo(addComponentTestMessage.mockComponent);
} }
} }
} }
[Test] [Test]
public void AddComponent() public unsafe void AddComponent()
{ {
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new AddComponentTestEngine()); worldBuilder.AddEngine(new AddComponentTestEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
const string MyString = "hello";
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 3; mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
@ -85,16 +87,15 @@ namespace Tests
worldBuilder.AddEngine(new ReadMockComponentEngine()); worldBuilder.AddEngine(new ReadMockComponentEngine());
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, new MockComponent { myInt = 20, myString = "what" }); worldBuilder.SetComponent(entity, new MockComponent { myInt = 20 });
worldBuilder.SetComponent(entity, new MockComponent { myInt = 50, myString = "hi" }); worldBuilder.SetComponent(entity, new MockComponent { myInt = 50 });
worldBuilder.SetComponent(entity, new MockComponent { myInt = 40, myString = "wassup" }); worldBuilder.SetComponent(entity, new MockComponent { myInt = 40 });
var world = worldBuilder.Build(); var world = worldBuilder.Build();
world.Update(0.01); world.Update(0.01);
Assert.That(gottenMockComponent.myInt, Is.EqualTo(40)); Assert.That(gottenMockComponent.myInt, Is.EqualTo(40));
Assert.That(gottenMockComponent.myString, Is.EqualTo("wassup"));
} }
[Reads(typeof(MockComponent))] [Reads(typeof(MockComponent))]
@ -103,8 +104,9 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var (mockComponent, entity) in ReadComponentsIncludingEntity<MockComponent>()) foreach (ref readonly var entity in ReadEntities<MockComponent>())
{ {
ref readonly var mockComponent = ref GetComponent<MockComponent>(entity);
SetComponent(entity, new MockComponent { myInt = mockComponent.myInt + 1 }); SetComponent(entity, new MockComponent { myInt = mockComponent.myInt + 1 });
} }
} }
@ -149,8 +151,9 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var (mockComponent, entity) in ReadComponentsIncludingEntity<MockComponent>()) foreach (ref readonly var entity in ReadEntities<MockComponent>())
{ {
ref readonly var mockComponent = ref GetComponent<MockComponent>(entity);
SetComponent(entity, mockComponent); SetComponent(entity, mockComponent);
RemoveComponent<MockComponent>(entity); RemoveComponent<MockComponent>(entity);
} }
@ -191,7 +194,6 @@ namespace Tests
{ {
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 10; mockComponent.myInt = 10;
mockComponent.myString = "four";
AddMockComponentMessage addMockComponentMessage; AddMockComponentMessage addMockComponentMessage;
addMockComponentMessage.entity = entity; addMockComponentMessage.entity = entity;
@ -207,7 +209,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var message in ReadMessages<AddMockComponentMessage>()) foreach (ref readonly var message in ReadMessages<AddMockComponentMessage>())
{ {
SetComponent(message.entity, message.mockComponent); SetComponent(message.entity, message.mockComponent);
} }
@ -255,9 +257,8 @@ namespace Tests
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 3; mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent<MockComponent>(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
EntityMessage entityMessage; EntityMessage entityMessage;
entityMessage.entity = entity; entityMessage.entity = entity;
@ -281,7 +282,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var hasComponentTestEngine in ReadMessages<HasComponentTestMessage>()) foreach (ref readonly var hasComponentTestEngine in ReadMessages<HasComponentTestMessage>())
{ {
Assert.IsTrue(HasComponent<MockComponent>(hasComponentTestEngine.entity)); Assert.IsTrue(HasComponent<MockComponent>(hasComponentTestEngine.entity));
} }
@ -298,7 +299,6 @@ namespace Tests
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 3; mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
@ -319,7 +319,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var hasComponentTestEngine in ReadMessages<HasComponentTestMessage>()) foreach (ref readonly var hasComponentTestEngine in ReadMessages<HasComponentTestMessage>())
{ {
hasComponentRuntimeTypeResult = HasComponent(hasComponentTestEngine.entity, typeof(MockComponent)); hasComponentRuntimeTypeResult = HasComponent(hasComponentTestEngine.entity, typeof(MockComponent));
} }
@ -336,7 +336,6 @@ namespace Tests
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 3; mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
@ -382,7 +381,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var removeComponentMessage in ReadMessages<RemoveComponentTestMessage>()) foreach (ref readonly var removeComponentMessage in ReadMessages<RemoveComponentTestMessage>())
{ {
RemoveComponent<MockComponent>(removeComponentMessage.entity); RemoveComponent<MockComponent>(removeComponentMessage.entity);
} }
@ -427,7 +426,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var checkHasMockComponentMessage in ReadMessages<CheckHasMockComponentMessage>()) foreach (ref readonly var checkHasMockComponentMessage in ReadMessages<CheckHasMockComponentMessage>())
{ {
hasComponentResult = HasComponent<MockComponent>(checkHasMockComponentMessage.entity); hasComponentResult = HasComponent<MockComponent>(checkHasMockComponentMessage.entity);
} }
@ -446,7 +445,6 @@ namespace Tests
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 3; mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
@ -477,7 +475,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var checkHasMockComponentMessage in ReadMessages<CheckHasMockComponentMessage>()) foreach (ref readonly var checkHasMockComponentMessage in ReadMessages<CheckHasMockComponentMessage>())
{ {
Assert.IsTrue(HasComponent<MockComponent>(checkHasMockComponentMessage.entity)); Assert.IsTrue(HasComponent<MockComponent>(checkHasMockComponentMessage.entity));
} }

View File

@ -13,26 +13,25 @@ namespace Tests
struct MockComponent : IComponent struct MockComponent : IComponent
{ {
public int myInt; public int myInt;
public string myString;
} }
public class EngineTest public class EngineTest
{ {
static List<MockComponent> resultComponents; static MockComponent[] resultComponents;
static MockComponent resultComponent; static MockComponent resultComponent;
static List<MockMessage> resultMessages = new List<MockMessage>(); static MockMessage[] resultMessages;
[Reads(typeof(MockComponent))] [Reads(typeof(MockComponent))]
public class ReadComponentsTestEngine : Engine public class ReadComponentsTestEngine : Engine
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
resultComponents = ReadComponents<MockComponent>().ToList(); resultComponents = ReadComponents<MockComponent>().ToArray();
} }
} }
static List<(MockComponent, Entity)> resultComponentsIncludingEntity; static List<(MockComponent, Entity)> resultComponentsIncludingEntity = new List<(MockComponent, Entity)>();
static (MockComponent, Entity) resultComponentIncludingEntity; static (MockComponent, Entity) resultComponentIncludingEntity;
[Reads(typeof(MockComponent))] [Reads(typeof(MockComponent))]
@ -40,7 +39,11 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
resultComponentsIncludingEntity = ReadComponentsIncludingEntity<MockComponent>().ToList(); foreach (ref readonly var entity in ReadEntities<MockComponent>())
{
ref readonly var mockComponent = ref GetComponent<MockComponent>(entity);
resultComponentsIncludingEntity.Add((mockComponent, entity));
}
} }
} }
@ -58,7 +61,9 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
resultComponentIncludingEntity = ReadComponentIncludingEntity<MockComponent>(); ref readonly var entity = ref ReadEntity<MockComponent>();
ref readonly var mockComponent = ref GetComponent<MockComponent>(entity);
resultComponentIncludingEntity = (mockComponent, entity);
} }
} }
@ -72,12 +77,10 @@ namespace Tests
var entityB = worldBuilder.CreateEntity(); var entityB = worldBuilder.CreateEntity();
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 0; mockComponent.myInt = 2;
mockComponent.myString = "hello";
MockComponent mockComponentB; MockComponent mockComponentB;
mockComponentB.myInt = 1; mockComponentB.myInt = 1;
mockComponentB.myString = "howdy";
worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
worldBuilder.SetComponent(entityB, mockComponentB); worldBuilder.SetComponent(entityB, mockComponentB);
@ -93,6 +96,8 @@ namespace Tests
[Test] [Test]
public void ReadComponentsIncludingEntity() public void ReadComponentsIncludingEntity()
{ {
resultComponentsIncludingEntity.Clear();
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new ReadComponentsIncludingEntityEngine()); worldBuilder.AddEngine(new ReadComponentsIncludingEntityEngine());
@ -100,12 +105,10 @@ namespace Tests
var entityB = worldBuilder.CreateEntity(); var entityB = worldBuilder.CreateEntity();
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 0; mockComponent.myInt = 2;
mockComponent.myString = "hello";
MockComponent mockComponentB; MockComponent mockComponentB;
mockComponentB.myInt = 1; mockComponentB.myInt = 1;
mockComponentB.myString = "howdy";
worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
worldBuilder.SetComponent(entityB, mockComponentB); worldBuilder.SetComponent(entityB, mockComponentB);
@ -135,8 +138,7 @@ namespace Tests
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 0; mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
@ -157,12 +159,10 @@ namespace Tests
var entityB = worldBuilder.CreateEntity(); var entityB = worldBuilder.CreateEntity();
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 0; mockComponent.myInt = 2;
mockComponent.myString = "hello";
MockComponent mockComponentB; MockComponent mockComponentB;
mockComponentB.myInt = 1; mockComponentB.myInt = 1;
mockComponentB.myString = "howdy";
worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
worldBuilder.SetComponent(entityB, mockComponentB); worldBuilder.SetComponent(entityB, mockComponentB);
@ -183,8 +183,7 @@ namespace Tests
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 0; mockComponent.myInt = 2;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
@ -201,11 +200,8 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
var (component, entity) = ReadComponentIncludingEntity<MockComponent>(); ref readonly var entity = ref ReadEntity<MockComponent>();
SetComponent(entity, new MockComponent { myInt = 420 });
component.myInt = 420;
component.myString = "blaze it";
SetComponent(entity, component);
} }
} }
@ -222,8 +218,7 @@ namespace Tests
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 0; mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
@ -233,7 +228,6 @@ namespace Tests
world.Update(0.01); world.Update(0.01);
Assert.AreEqual(420, resultComponent.myInt); Assert.AreEqual(420, resultComponent.myInt);
Assert.AreEqual("blaze it", resultComponent.myString);
} }
[Reads(typeof(MockComponent))] [Reads(typeof(MockComponent))]
@ -241,13 +235,8 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
var (component, entity) = ReadComponentIncludingEntity<MockComponent>(); ref readonly var entity = ref ReadEntity<MockComponent>();
SetComponent(entity, new MockComponent { myInt = 420 });
component.myInt = 420;
component.myString = "blaze it";
SetComponent(entity, component);
component = ReadComponent<MockComponent>();
} }
} }
@ -260,8 +249,7 @@ namespace Tests
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 0; mockComponent.myInt = 3;
mockComponent.myString = "hello";
worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
@ -293,7 +281,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
resultMessages = this.ReadMessages<MockMessage>().ToList(); resultMessages = ReadMessages<MockMessage>().ToArray();
} }
} }
@ -322,14 +310,14 @@ namespace Tests
} }
} }
static IEnumerable<MockMessage> emptyReadMessagesResult; static MockMessage[] emptyReadMessagesResult;
[Receives(typeof(MockMessage))] [Receives(typeof(MockMessage))]
class ReadMessagesWhenNoneExistEngine : Engine class ReadMessagesWhenNoneExistEngine : Engine
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
emptyReadMessagesResult = ReadMessages<MockMessage>(); emptyReadMessagesResult = ReadMessages<MockMessage>().ToArray();
} }
} }
@ -578,10 +566,10 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
var components = ReadComponentsIncludingEntity<MockComponent>(); var entities = ReadEntities<MockComponent>();
pairA = components.First(); pairA = (GetComponent<MockComponent>(entities[0]), entities[0]);
pairB = components.Last(); pairB = (GetComponent<MockComponent>(entities[1]), entities[1]);
} }
} }
@ -594,11 +582,9 @@ namespace Tests
MockComponent componentA; MockComponent componentA;
componentA.myInt = 20; componentA.myInt = 20;
componentA.myString = "hello";
MockComponent componentB; MockComponent componentB;
componentB.myInt = 20; componentB.myInt = 20;
componentB.myString = "hello";
var entity = worldBuilder.CreateEntity(); var entity = worldBuilder.CreateEntity();
worldBuilder.SetComponent(entity, componentA); worldBuilder.SetComponent(entity, componentA);
@ -613,14 +599,12 @@ namespace Tests
Assert.That(EngineTest.pairA.Item1, Is.EqualTo(EngineTest.pairB.Item1)); Assert.That(EngineTest.pairA.Item1, Is.EqualTo(EngineTest.pairB.Item1));
} }
static IEnumerable<(MockComponent, Entity)> emptyComponentReadResult;
[Reads(typeof(MockComponent))] [Reads(typeof(MockComponent))]
class ReadEmptyMockComponentsEngine : Engine class ReadEmptyMockComponentsEngine : Engine
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
emptyComponentReadResult = ReadComponentsIncludingEntity<MockComponent>(); ReadEntities<MockComponent>().ToArray().Should().BeEmpty();
} }
} }
@ -632,8 +616,6 @@ namespace Tests
var world = worldBuilder.Build(); var world = worldBuilder.Build();
world.Update(0.01f); world.Update(0.01f);
Assert.That(emptyComponentReadResult, Is.Empty);
} }
struct DestroyerComponent : IComponent { } struct DestroyerComponent : IComponent { }
@ -643,21 +625,27 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var (component, entity) in ReadComponentsIncludingEntity<DestroyerComponent>()) foreach (ref readonly var entity in ReadEntities<DestroyerComponent>())
{ {
Destroy(entity); Destroy(entity);
} }
} }
} }
static List<(MockComponent, Entity)> results; static List<(MockComponent, Entity)> results = new List<(MockComponent, Entity)>();
[Reads(typeof(MockComponent))] [Reads(typeof(MockComponent))]
class ReaderEngine : Engine class ReaderEngine : Engine
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
results = ReadComponentsIncludingEntity<MockComponent>().ToList(); results.Clear();
foreach (ref readonly var entity in ReadEntities<MockComponent>())
{
ref readonly var mockComponent = ref GetComponent<MockComponent>(entity);
results.Add((mockComponent, entity));
}
} }
} }
@ -675,7 +663,6 @@ namespace Tests
DestroyerComponent destroyerComponent; DestroyerComponent destroyerComponent;
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 2; mockComponent.myInt = 2;
mockComponent.myString = "blah";
worldBuilder.SetComponent(entity, destroyerComponent); worldBuilder.SetComponent(entity, destroyerComponent);
worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entity, mockComponent);
@ -700,7 +687,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var message in ReadMessages<DestroyComponentMessage>()) foreach (ref readonly var message in ReadMessages<DestroyComponentMessage>())
{ {
Destroy(message.entity); Destroy(message.entity);
} }
@ -710,6 +697,8 @@ namespace Tests
[Test] [Test]
public void DestroyEntityWithoutID() public void DestroyEntityWithoutID()
{ {
results.Clear();
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new AddComponentEngine()); worldBuilder.AddEngine(new AddComponentEngine());
worldBuilder.AddEngine(new DestroyEntityEngine()); worldBuilder.AddEngine(new DestroyEntityEngine());
@ -733,7 +722,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var (componentPair, entity) in ReadComponentsIncludingEntity<DestroyerComponent>()) foreach (ref readonly var entity in ReadEntities<DestroyerComponent>())
{ {
RemoveComponent<MockComponent>(entity); RemoveComponent<MockComponent>(entity);
Destroy(entity); Destroy(entity);
@ -765,7 +754,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var (mockComponent, entity) in ReadComponentsIncludingEntity<MockComponent>()) foreach (ref readonly var entity in ReadEntities<MockComponent>())
{ {
RemoveComponent<MockComponent>(entity); RemoveComponent<MockComponent>(entity);
SetComponent(entity, new MockComponent()); SetComponent(entity, new MockComponent());
@ -780,7 +769,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
var (_, entity) = ReadComponentIncludingEntity<MockComponent>(); ref readonly var entity = ref ReadEntity<MockComponent>();
} }
} }
@ -805,7 +794,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var (component, entity) in ReadComponentsIncludingEntity<MockComponent>()) foreach (ref readonly var entity in ReadEntities<MockComponent>())
{ {
RemoveComponent<MockComponent>(entity); RemoveComponent<MockComponent>(entity);
SendMessage(new MockMessage { }, 1); SendMessage(new MockMessage { }, 1);
@ -816,7 +805,7 @@ namespace Tests
[Test] [Test]
public void EngineSendMessageDelayed() public void EngineSendMessageDelayed()
{ {
resultMessages.Clear(); Array.Clear(resultMessages, 0, resultMessages.Length);
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new ActivateTimeDilationEngine()); worldBuilder.AddEngine(new ActivateTimeDilationEngine());
@ -852,7 +841,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var (component, entity) in ReadComponentsIncludingEntity<MockComponent>()) foreach (ref readonly var entity in ReadEntities<MockComponent>())
{ {
RemoveComponent<MockComponent>(entity); RemoveComponent<MockComponent>(entity);
SendMessageIgnoringTimeDilation(new MockMessage { }, 1); SendMessageIgnoringTimeDilation(new MockMessage { }, 1);
@ -863,7 +852,7 @@ namespace Tests
[Test] [Test]
public void EngineSendMessageDelayedIgnoringTimeDilation() public void EngineSendMessageDelayedIgnoringTimeDilation()
{ {
resultMessages.Clear(); Array.Clear(resultMessages, 0, resultMessages.Length);
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new ActivateTimeDilationEngine()); worldBuilder.AddEngine(new ActivateTimeDilationEngine());
@ -896,7 +885,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var message in ReadMessages<MockMessage>()) foreach (ref readonly var message in ReadMessages<MockMessage>())
{ {
var entity = CreateEntity(); var entity = CreateEntity();
SetComponent(entity, new MockComponent { }); SetComponent(entity, new MockComponent { });
@ -977,12 +966,12 @@ namespace Tests
struct MockComponentB : IComponent struct MockComponentB : IComponent
{ {
private int value;
public MockComponentB(int value) public MockComponentB(int value)
{ {
this.value = value; this.value = value;
} }
int value;
} }
static MockComponentB getComponentResult; static MockComponentB getComponentResult;
@ -1134,7 +1123,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var (_, entity) in ReadComponentsIncludingEntity<MockComponent>()) foreach (ref readonly var entity in ReadEntities<MockComponent>())
{ {
RemoveComponent<MockComponent>(entity); RemoveComponent<MockComponent>(entity);
} }
@ -1329,8 +1318,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
var (component, entity) = ReadComponentIncludingEntity<MockComponent>(); ref readonly var entity = ref ReadEntity<MockComponent>();
AddComponent(entity, new MockComponent()); AddComponent(entity, new MockComponent());
} }
} }
@ -1364,8 +1352,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
var (component, entity) = ReadComponentIncludingEntity<MockComponentB>(); ref readonly var component = ref ReadComponent<MockComponentB>();
getComponentResult = component; getComponentResult = component;
} }
} }
@ -1811,7 +1798,7 @@ namespace Tests
} }
public override void Update(double dt) public override void Update(double dt)
{ {
_components.AddRange(ReadComponents<MockComponentB>()); _components.AddRange(ReadComponents<MockComponentB>().ToArray());
} }
} }
@ -1847,12 +1834,11 @@ namespace Tests
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var (component, entity) in ReadComponentsIncludingEntity<MockComponent>()) foreach (ref readonly var entity in ReadEntities<MockComponent>())
{ {
if (HasComponent<MockComponentB>(entity)) if (HasComponent<MockComponentB>(entity))
{ {
_components.Add(GetComponent<MockComponentB>(entity)); _components.Add(GetComponent<MockComponentB>(entity));
} }
} }
} }
@ -1879,12 +1865,12 @@ namespace Tests
struct MockTimerComponent : IComponent struct MockTimerComponent : IComponent
{ {
public double Timer { get; }
public MockTimerComponent(double time) public MockTimerComponent(double time)
{ {
Timer = time; Timer = time;
} }
public double Timer { get; set; }
} }
[Reads(typeof(MockTimerComponent))] [Reads(typeof(MockTimerComponent))]
@ -1893,19 +1879,18 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var (component, entity) in ReadComponentsIncludingEntity<MockTimerComponent>()) foreach (ref readonly var entity in ReadEntities<MockTimerComponent>())
{ {
var updatedComponent = component; ref readonly var component = ref GetComponent<MockTimerComponent>(entity);
updatedComponent.Timer -= dt;
if (updatedComponent.Timer <= 0) if (component.Timer - dt <= 0)
{ {
RemoveComponent<MockTimerComponent>(entity); RemoveComponent<MockTimerComponent>(entity);
} }
else else
{ {
SetComponent<MockTimerComponent>(entity, updatedComponent); SetComponent<MockTimerComponent>(entity, new MockTimerComponent(component.Timer - dt));
} }
} }
} }

View File

@ -15,7 +15,8 @@ namespace Tests
{ {
public override void Render() public override void Render()
{ {
result = ReadComponentIncludingEntity<AComponent>(); ref readonly var entity = ref ReadEntity<AComponent>();
result = (GetComponent<AComponent>(entity), entity);
} }
} }

View File

@ -20,13 +20,13 @@ namespace Tests
class TestRenderer : OrderedRenderer<TestDrawComponent> class TestRenderer : OrderedRenderer<TestDrawComponent>
{ {
public override void Render(Entity entity, TestDrawComponent testDrawComponent) { } public override void Render(Entity entity, in TestDrawComponent testDrawComponent) { }
} }
static bool called = false; static bool called = false;
class DeactivatedRenderer : TestRenderer class DeactivatedRenderer : TestRenderer
{ {
public override void Render(Entity entity, TestDrawComponent testDrawComponent) public override void Render(Entity entity, in TestDrawComponent testDrawComponent)
{ {
called = true; called = true;
} }
@ -37,7 +37,7 @@ namespace Tests
class CalledRenderer : OrderedRenderer<TestDrawComponent> class CalledRenderer : OrderedRenderer<TestDrawComponent>
{ {
public override void Render(Entity entity, TestDrawComponent testDrawComponent) public override void Render(Entity entity, in TestDrawComponent testDrawComponent)
{ {
resultComponent = (testDrawComponent, entity); resultComponent = (testDrawComponent, entity);
calledOnDraw = true; calledOnDraw = true;

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using Encompass.Exceptions; using Encompass.Exceptions;
using System.Linq; using System.Linq;
using FluentAssertions; using FluentAssertions;
using System;
namespace Tests namespace Tests
{ {
@ -114,7 +115,7 @@ namespace Tests
public class MultipleEngineWriteConflict public class MultipleEngineWriteConflict
{ {
struct AComponent : IComponent { } struct AComponent { }
[Writes(typeof(AComponent))] [Writes(typeof(AComponent))]
class AEngine : Engine class AEngine : Engine
@ -158,7 +159,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var setMessage in ReadMessages<SetMessage>()) foreach (ref readonly var setMessage in ReadMessages<SetMessage>())
{ {
SetComponent(setMessage.entity, new AComponent { myInt = 0 }); SetComponent(setMessage.entity, new AComponent { myInt = 0 });
} }
@ -172,7 +173,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var setMessage in ReadMessages<SetMessage>()) foreach (ref readonly var setMessage in ReadMessages<SetMessage>())
{ {
SetComponent(setMessage.entity, new AComponent { myInt = 1 }); SetComponent(setMessage.entity, new AComponent { myInt = 1 });
} }
@ -228,7 +229,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var setMessage in ReadMessages<SetMessage>()) foreach (ref readonly var setMessage in ReadMessages<SetMessage>())
{ {
SetComponent(setMessage.entity, new AComponent { myInt = 5 }); SetComponent(setMessage.entity, new AComponent { myInt = 5 });
} }
@ -243,7 +244,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var setMessage in ReadMessages<SetMessage>()) foreach (ref readonly var setMessage in ReadMessages<SetMessage>())
{ {
SetComponent(setMessage.entity, new AComponent { myInt = 1 }); SetComponent(setMessage.entity, new AComponent { myInt = 1 });
} }
@ -257,7 +258,7 @@ namespace Tests
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
foreach (var setMessage in ReadMessages<SetMessage>()) foreach (ref readonly var setMessage in ReadMessages<SetMessage>())
{ {
SetComponent(setMessage.entity, new AComponent { myInt = 3 }); SetComponent(setMessage.entity, new AComponent { myInt = 3 });
} }
@ -318,28 +319,6 @@ namespace Tests
} }
} }
public class IllegalReadType
{
struct ANonMessage { }
[Reads(typeof(ANonMessage))]
class MyEngine : Engine
{
public override void Update(double dt)
{
}
}
[Test]
public void ThrowsError()
{
var worldBuilder = new WorldBuilder();
Assert.Throws<IllegalReadTypeException>(() => worldBuilder.AddEngine(new MyEngine()), "ANonMessage must be a Message or Component");
}
}
public class IllegalWriteType public class IllegalWriteType
{ {
struct ANonMessage { } struct ANonMessage { }
@ -501,20 +480,22 @@ namespace Tests
Assert.That(order.IndexOf(engineC), Is.LessThan(order.IndexOf(engineD))); Assert.That(order.IndexOf(engineC), Is.LessThan(order.IndexOf(engineD)));
} }
static List<AMessage> resultMessages = new List<AMessage>(); static AMessage[] resultMessages;
[Receives(typeof(AMessage))] [Receives(typeof(AMessage))]
class ReadMessageEngine : Engine class ReadMessageEngine : Engine
{ {
public override void Update(double dt) public override void Update(double dt)
{ {
resultMessages = ReadMessages<AMessage>().ToList(); resultMessages = ReadMessages<AMessage>().ToArray();
} }
} }
[Test] [Test]
public void SendMessageDelayed() public void SendMessageDelayed()
{ {
resultMessages = Array.Empty<AMessage>();
var worldBuilder = new WorldBuilder(); var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine(new ReadMessageEngine()); worldBuilder.AddEngine(new ReadMessageEngine());

View File

@ -21,7 +21,7 @@ namespace Tests
class TestEntityRenderer : OrderedRenderer<TestDrawComponent> class TestEntityRenderer : OrderedRenderer<TestDrawComponent>
{ {
public override void Render(Entity entity, TestDrawComponent testDrawComponent) public override void Render(Entity entity, in TestDrawComponent testDrawComponent)
{ {
drawOrder.Add(entity); drawOrder.Add(entity);
} }

View File

@ -5,6 +5,7 @@
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
<RootNamespace>Tests</RootNamespace> <RootNamespace>Tests</RootNamespace>
<AssemblyName>EncompassECS.Framework.Tests</AssemblyName> <AssemblyName>EncompassECS.Framework.Tests</AssemblyName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.7.0" /> <PackageReference Include="FluentAssertions" Version="5.7.0" />