diff --git a/encompass-cs/Attributes/QueryWith.cs b/encompass-cs/Attributes/QueryWith.cs index 5574c5b..ac8797d 100644 --- a/encompass-cs/Attributes/QueryWith.cs +++ b/encompass-cs/Attributes/QueryWith.cs @@ -14,13 +14,6 @@ namespace Encompass { foreach (var queryWithType in queryWithTypes) { - var isComponent = queryWithType.GetInterfaces().Contains(typeof(IComponent)); - - if (!isComponent) - { - throw new IllegalReadTypeException("{0} must be a Component", queryWithType.Name); - } - QueryWithTypes.Add(queryWithType); } } diff --git a/encompass-cs/Attributes/QueryWithout.cs b/encompass-cs/Attributes/QueryWithout.cs index 3588285..3332b45 100644 --- a/encompass-cs/Attributes/QueryWithout.cs +++ b/encompass-cs/Attributes/QueryWithout.cs @@ -14,14 +14,7 @@ namespace Encompass { foreach (var type in queryWithoutTypes) { - var isComponent = type.GetInterfaces().Contains(typeof(IComponent)); - - if (!isComponent) - { - throw new IllegalReadTypeException("{0} must be a Component", type.Name); - } - - this.QueryWithoutTypes.Add(type); + QueryWithoutTypes.Add(type); } } } diff --git a/encompass-cs/Attributes/Reads.cs b/encompass-cs/Attributes/Reads.cs index b958a26..e508775 100644 --- a/encompass-cs/Attributes/Reads.cs +++ b/encompass-cs/Attributes/Reads.cs @@ -14,14 +14,7 @@ namespace Encompass { foreach (var readType in readTypes) { - var isComponent = readType.GetInterfaces().Contains(typeof(IComponent)); - - if (!isComponent) - { - throw new IllegalReadTypeException("{0} must be a Component", readType.Name); - } - - this.ReadTypes.Add(readType); + ReadTypes.Add(readType); } } } diff --git a/encompass-cs/Attributes/ReadsImmediate.cs b/encompass-cs/Attributes/ReadsImmediate.cs index 5d4b0ff..d399ae4 100644 --- a/encompass-cs/Attributes/ReadsImmediate.cs +++ b/encompass-cs/Attributes/ReadsImmediate.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using Encompass.Exceptions; namespace Encompass { @@ -14,14 +12,7 @@ namespace Encompass { foreach (var readImmediateType in readImmediateTypes) { - var isComponent = readImmediateType.GetInterfaces().Contains(typeof(IComponent)); - - if (!isComponent) - { - throw new IllegalReadTypeException("{0} must be a Component", readImmediateType.Name); - } - - this.ReadImmediateTypes.Add(readImmediateType); + ReadImmediateTypes.Add(readImmediateType); } } } diff --git a/encompass-cs/Attributes/Writes.cs b/encompass-cs/Attributes/Writes.cs index 8eec04a..0cc1250 100644 --- a/encompass-cs/Attributes/Writes.cs +++ b/encompass-cs/Attributes/Writes.cs @@ -15,24 +15,12 @@ namespace Encompass { foreach (var writeType in writeTypes) { - var isComponent = writeType.GetInterfaces().Contains(typeof(IComponent)); - if (!isComponent) - { - throw new IllegalWriteTypeException("{0} must be a Component", writeType.Name); - } - - this.WriteTypes.Add(writeType); + WriteTypes.Add(writeType); } } public Writes(Type writeType, int priority) { - var isComponent = writeType.GetInterfaces().Contains(typeof(IComponent)); - if (!isComponent) - { - throw new IllegalWriteTypeException("{0} must be a Component", writeType.Name); - } - WriteTypes.Add(writeType); Priorities.Add(writeType, priority); } diff --git a/encompass-cs/Attributes/WritesImmediate.cs b/encompass-cs/Attributes/WritesImmediate.cs index 0b5db7c..1011b55 100644 --- a/encompass-cs/Attributes/WritesImmediate.cs +++ b/encompass-cs/Attributes/WritesImmediate.cs @@ -1,7 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using Encompass.Exceptions; namespace Encompass { @@ -14,13 +12,7 @@ namespace Encompass { foreach (var writeImmediateType in writeImmediateTypes) { - var isComponent = writeImmediateType.GetInterfaces().Contains(typeof(IComponent)); - if (!isComponent) - { - throw new IllegalWriteImmediateTypeException("{0} must be a Component", writeImmediateType.Name); - } - - this.WriteImmediateTypes.Add(writeImmediateType); + WriteImmediateTypes.Add(writeImmediateType); } } } diff --git a/encompass-cs/Collections/ComponentBitSet.cs b/encompass-cs/Collections/ComponentBitSet.cs index 8c90cf5..2867686 100644 --- a/encompass-cs/Collections/ComponentBitSet.cs +++ b/encompass-cs/Collections/ComponentBitSet.cs @@ -24,13 +24,13 @@ namespace Encompass _entities.Add(entityID, BitSet512.Zero); } - public void Set(int entityID) where TComponent : struct, IComponent + public void Set(int entityID) where TComponent : struct { if (!_entities.ContainsKey(entityID)) { AddEntity(entityID); } _entities[entityID] = _entities[entityID].Set(_typeToIndex[typeof(TComponent)]); } - public void RemoveComponent(int entityID) where TComponent : struct, IComponent + public void RemoveComponent(int entityID) where TComponent : struct { if (_entities.ContainsKey(entityID)) { diff --git a/encompass-cs/Collections/ComponentStore.cs b/encompass-cs/Collections/ComponentStore.cs index 8f34731..e7d6491 100644 --- a/encompass-cs/Collections/ComponentStore.cs +++ b/encompass-cs/Collections/ComponentStore.cs @@ -8,27 +8,32 @@ namespace Encompass { private Dictionary _stores = new Dictionary(512); public ComponentBitSet ComponentBitSet { get; private set; } + private Dictionary _typeToIndex; public ComponentStore(Dictionary typeToIndex) { + _typeToIndex = typeToIndex; ComponentBitSet = new ComponentBitSet(typeToIndex); } - public virtual void RegisterComponentType() where TComponent : struct, IComponent + public virtual void RegisterComponentType() where TComponent : unmanaged { if (!_stores.ContainsKey(typeof(TComponent))) { var store = new TypedComponentStore(); _stores.Add(typeof(TComponent), store); } + + if (!_typeToIndex.ContainsKey(typeof(TComponent))) { _typeToIndex.Add(typeof(TComponent), _typeToIndex.Count); } } - private TypedComponentStore Lookup() where TComponent : struct, IComponent + private TypedComponentStore Lookup() where TComponent : unmanaged { + RegisterComponentType(); return _stores[typeof(TComponent)] as TypedComponentStore; } - public bool Has(int entityID) where TComponent : struct, IComponent + public bool Has(int entityID) where TComponent : unmanaged { return Lookup().Has(entityID); } @@ -43,18 +48,18 @@ namespace Encompass return ComponentBitSet.EntityBitArray(entityID); } - public ref readonly TComponent Get(int entityID) where TComponent : struct, IComponent + public ref readonly TComponent Get(int entityID) where TComponent : unmanaged { return ref Lookup().Get(entityID); } - public virtual void Set(int entityID, TComponent component) where TComponent : struct, IComponent + public virtual void Set(int entityID, TComponent component) where TComponent : unmanaged { Lookup().Set(entityID, component); ComponentBitSet.Set(entityID); } - public virtual bool Set(int entityID, TComponent component, int priority) where TComponent : struct, IComponent + public virtual bool Set(int entityID, TComponent component, int priority) where TComponent : unmanaged { if (Lookup().Set(entityID, component, priority)) { @@ -64,7 +69,7 @@ namespace Encompass return false; } - public virtual bool Remove(int entityID, int priority) where TComponent : struct, IComponent + public virtual bool Remove(int entityID, int priority) where TComponent : unmanaged { if (Lookup().Remove(entityID, priority)) { @@ -74,7 +79,7 @@ namespace Encompass return false; } - public void ForceRemove(int entityID) where TComponent : struct, IComponent + public void ForceRemove(int entityID) where TComponent : unmanaged { Lookup().ForceRemove(entityID); ComponentBitSet.RemoveComponent(entityID); @@ -89,17 +94,17 @@ namespace Encompass ComponentBitSet.RemoveEntity(entityID); } - public bool Any() where TComponent : struct, IComponent + public bool Any() where TComponent : unmanaged { return Lookup().Count > 0; } - public IEnumerable<(TComponent, int)> All() where TComponent : struct, IComponent + public IEnumerable<(TComponent, int)> All() where TComponent : unmanaged { return Lookup().All(); } - public void Clear() where TComponent : struct, IComponent + public void Clear() where TComponent : unmanaged { Lookup().Clear(); } diff --git a/encompass-cs/Collections/Replayer.cs b/encompass-cs/Collections/Replayer.cs index b5cd3f7..0bb9414 100644 --- a/encompass-cs/Collections/Replayer.cs +++ b/encompass-cs/Collections/Replayer.cs @@ -2,7 +2,7 @@ namespace Encompass { - internal abstract class Replayer + internal abstract class Replayer { public abstract void Replay(ComponentStore store); public abstract void MarkRemoval(int entityID); @@ -10,7 +10,7 @@ namespace Encompass public abstract void Clear(); } - internal class Replayer : Replayer where TComponent : struct, IComponent + internal class Replayer : Replayer where TComponent : unmanaged { private readonly ComponentDeltaStore _deltaStore; private readonly HashSet _removals = new HashSet(); diff --git a/encompass-cs/Collections/TypedComponentStore.cs b/encompass-cs/Collections/TypedComponentStore.cs index e1caa24..0fd06fa 100644 --- a/encompass-cs/Collections/TypedComponentStore.cs +++ b/encompass-cs/Collections/TypedComponentStore.cs @@ -14,7 +14,7 @@ namespace Encompass public abstract void ClearPriorities(); } - internal class TypedComponentStore : TypedComponentStore where TComponent : struct, IComponent + internal class TypedComponentStore : TypedComponentStore where TComponent : unmanaged { private readonly Dictionary _indices = new Dictionary(512); private readonly Dictionary _priorities = new Dictionary(512); diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index 928c9a0..da1a782 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -29,7 +29,7 @@ namespace Encompass TypeToIndex = typeToIndex; } - public void RegisterComponentType() where TComponent : struct, IComponent + public void RegisterComponentType() where TComponent : unmanaged { _existingComponentStore.RegisterComponentType(); _immediateComponentStore.RegisterComponentType(); @@ -47,7 +47,7 @@ namespace Encompass _upToDateComponentStore.SwapWith(componentStore); } - internal void RegisterDrawableComponent(int entityID, int layer) where TComponent : struct, IComponent + internal void RegisterDrawableComponent(int entityID, int layer) where TComponent : unmanaged { _drawLayerManager.RegisterComponentWithLayer(entityID, layer); } @@ -61,7 +61,7 @@ namespace Encompass _replayStore.ClearAll(); } - internal bool AddImmediateComponent(int entityID, TComponent component, int priority) where TComponent : struct, IComponent + internal bool AddImmediateComponent(int entityID, TComponent component, int priority) where TComponent : unmanaged { if (_immediateComponentStore.Set(entityID, component, priority)) { @@ -73,14 +73,14 @@ namespace Encompass return false; } - internal void AddImmediateComponent(int entityID, TComponent component) where TComponent : struct, IComponent + internal void AddImmediateComponent(int entityID, TComponent component) where TComponent : unmanaged { _immediateComponentStore.Set(entityID, component); _replayStore.Set(entityID, component); _upToDateComponentStore.Set(entityID, component); } - internal bool UpdateComponent(int entityID, TComponent component, int priority) where TComponent : struct, IComponent + internal bool UpdateComponent(int entityID, TComponent component, int priority) where TComponent : unmanaged { var result = _upToDateComponentStore.Set(entityID, component, priority); if (result) @@ -90,7 +90,7 @@ namespace Encompass return result; } - internal void AddComponent(int entityID, TComponent component) where TComponent : struct, IComponent + internal void AddComponent(int entityID, TComponent component) where TComponent : unmanaged { _upToDateComponentStore.Set(entityID, component); _replayStore.Set(entityID, component); @@ -98,12 +98,12 @@ namespace Encompass // existing or immediate reads - internal IEnumerable<(TComponent, int)> ReadExistingAndImmediateComponentsByType() where TComponent : struct, IComponent + internal IEnumerable<(TComponent, int)> ReadExistingAndImmediateComponentsByType() where TComponent : unmanaged { return _upToDateComponentStore.All(); } - internal (TComponent, int) ReadFirstExistingOrImmediateComponentByType() where TComponent : struct, IComponent + internal (TComponent, int) ReadFirstExistingOrImmediateComponentByType() where TComponent : unmanaged { if (!SomeExistingOrImmediateComponent()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); } var enumerator = ReadExistingAndImmediateComponentsByType().GetEnumerator(); @@ -111,14 +111,14 @@ namespace Encompass return enumerator.Current; } - internal bool SomeExistingOrImmediateComponent() where TComponent : struct, IComponent + internal bool SomeExistingOrImmediateComponent() where TComponent : unmanaged { return _upToDateComponentStore.Any(); } // existing reads - internal (TComponent, int) ReadFirstExistingComponentByType() where TComponent : struct, IComponent + internal (TComponent, int) ReadFirstExistingComponentByType() where TComponent : unmanaged { if (!SomeExistingComponent()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); } var enumerator = GetComponentsIncludingEntity().GetEnumerator(); @@ -126,19 +126,19 @@ namespace Encompass return enumerator.Current; } - internal bool SomeExistingComponent() where TComponent : struct, IComponent + internal bool SomeExistingComponent() where TComponent : unmanaged { return _existingComponentStore.Any(); } // immediate reads - internal IEnumerable<(TComponent, int)> ReadImmediateComponentsByType() where TComponent : struct, IComponent + internal IEnumerable<(TComponent, int)> ReadImmediateComponentsByType() where TComponent : unmanaged { return _immediateComponentStore.All(); } - internal (TComponent, int) ReadFirstImmediateComponentByType() where TComponent : struct, IComponent + internal (TComponent, int) ReadFirstImmediateComponentByType() where TComponent : unmanaged { if (!SomeImmediateComponent()) { throw new Exceptions.NoComponentOfTypeException($"No Component with type {typeof(TComponent)} exists"); } var enumerator = ReadImmediateComponentsByType().GetEnumerator(); @@ -146,31 +146,31 @@ namespace Encompass return enumerator.Current; } - internal bool SomeImmediateComponent() where TComponent : struct, IComponent + internal bool SomeImmediateComponent() where TComponent : unmanaged { return _immediateComponentStore.Any(); } // component getters - internal ref readonly TComponent ReadImmediateOrExistingComponentByEntityAndType(int entityID) where TComponent : struct, IComponent + internal ref readonly TComponent ReadImmediateOrExistingComponentByEntityAndType(int entityID) where TComponent : unmanaged { return ref _upToDateComponentStore.Get(entityID); } - internal ref readonly TComponent ReadExistingComponentByEntityAndType(int entityID) where TComponent : struct, IComponent + internal ref readonly TComponent ReadExistingComponentByEntityAndType(int entityID) where TComponent : unmanaged { return ref _existingComponentStore.Get(entityID); } - internal ref readonly TComponent ReadImmediateComponentByEntityAndType(int entityID) where TComponent : struct, IComponent + internal ref readonly TComponent ReadImmediateComponentByEntityAndType(int entityID) where TComponent : unmanaged { return ref _immediateComponentStore.Get(entityID); } // has checkers - internal bool HasExistingOrImmediateComponent(int entityID) where TComponent : struct, IComponent + internal bool HasExistingOrImmediateComponent(int entityID) where TComponent : unmanaged { return _upToDateComponentStore.Has(entityID); } @@ -180,7 +180,7 @@ namespace Encompass return _upToDateComponentStore.Has(type, entityID); } - internal bool HasExistingComponent(int entityID) where TComponent : struct, IComponent + internal bool HasExistingComponent(int entityID) where TComponent : unmanaged { return _existingComponentStore.Has(entityID); } @@ -190,7 +190,7 @@ namespace Encompass return _existingComponentStore.Has(type, entityID); } - internal bool HasImmediateComponent(int entityID) where TComponent : struct, IComponent + internal bool HasImmediateComponent(int entityID) where TComponent : unmanaged { return _immediateComponentStore.Has(entityID); } @@ -200,12 +200,12 @@ namespace Encompass return _immediateComponentStore.Has(type, entityID); } - internal IEnumerable<(TComponent, int)> GetComponentsIncludingEntity() where TComponent : struct, IComponent + internal IEnumerable<(TComponent, int)> GetComponentsIncludingEntity() where TComponent : unmanaged { return _existingComponentStore.All(); } - internal IEnumerable GetComponentsByType() where TComponent : struct, IComponent + internal IEnumerable GetComponentsByType() where TComponent : unmanaged { foreach (var pair in _existingComponentStore.All()) { @@ -213,12 +213,12 @@ namespace Encompass } } - internal ref readonly TComponent GetComponentByEntityAndType(int entityID) where TComponent : struct, IComponent + internal ref readonly TComponent GetComponentByEntityAndType(int entityID) where TComponent : unmanaged { return ref _existingComponentStore.Get(entityID); } - internal bool EntityHasComponentOfType(int entityID) where TComponent : struct, IComponent + internal bool EntityHasComponentOfType(int entityID) where TComponent : unmanaged { return _existingComponentStore.Has(entityID); } @@ -242,7 +242,7 @@ namespace Encompass _entitiesMarkedForRemoval.Clear(); } - public bool RemoveImmediate(int entityID, int priority) where TComponent : struct, IComponent + public bool RemoveImmediate(int entityID, int priority) where TComponent : unmanaged { if (_immediateComponentStore.Remove(entityID, priority)) { @@ -254,7 +254,7 @@ namespace Encompass return false; } - public void Remove(int entityID, int priority) where TComponent : struct, IComponent + public void Remove(int entityID, int priority) where TComponent : unmanaged { if (_upToDateComponentStore.Remove(entityID, priority)) { diff --git a/encompass-cs/DrawLayerManager.cs b/encompass-cs/DrawLayerManager.cs index 3c14f21..71eaec0 100644 --- a/encompass-cs/DrawLayerManager.cs +++ b/encompass-cs/DrawLayerManager.cs @@ -30,7 +30,7 @@ namespace Encompass } } - public void RegisterOrderedDrawable() where TComponent : struct, IComponent + public void RegisterOrderedDrawable() where TComponent : struct { if (!_typeToEntityToLayer.ContainsKey(typeof(TComponent))) { @@ -53,7 +53,7 @@ namespace Encompass set.Add(renderer); } - public void RegisterComponentWithLayer(int entityID, int layer) where TComponent : struct, IComponent + public void RegisterComponentWithLayer(int entityID, int layer) where TComponent : struct { if (!_layerIndexToTypeToID.ContainsKey(layer)) { @@ -68,7 +68,7 @@ namespace Encompass _typeToEntityToLayer[typeof(TComponent)].Add(entityID, layer); } - public void UnRegisterComponentWithLayer(int entityID) where TComponent : struct, IComponent + public void UnRegisterComponentWithLayer(int entityID) where TComponent : struct { if (!_typeToEntityToLayer.ContainsKey(typeof(TComponent))) { return; } diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index fab7457..bc29221 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -213,7 +213,7 @@ namespace Encompass /// /// Returns an Entity containing the specified Component type. /// - protected Entity ReadEntity() where TComponent : struct, IComponent + protected Entity ReadEntity() where TComponent : unmanaged { return _entityManager.GetEntity(ReadComponentHelper().Item2); } @@ -221,7 +221,7 @@ namespace Encompass /// /// Returns all Entities containing the specified Component type. /// - protected IEnumerable ReadEntities() where TComponent : struct, IComponent + protected IEnumerable ReadEntities() where TComponent : unmanaged { foreach (var pair in ReadComponentsHelper()) { @@ -231,12 +231,12 @@ namespace Encompass // these next two are for the ComponentMessageEmitter only - internal IEnumerable ReadComponentsFromWorld() where TComponent : struct, IComponent + internal IEnumerable ReadComponentsFromWorld() where TComponent : unmanaged { return _componentManager.GetComponentsByType(); } - private IEnumerable<(TComponent, int)> ReadComponentsHelper() where TComponent : struct, IComponent + private IEnumerable<(TComponent, int)> ReadComponentsHelper() where TComponent : unmanaged { var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent)); @@ -261,7 +261,7 @@ namespace Encompass /// /// Returns all of the Components with the specified Component Type. /// - protected IEnumerable ReadComponents() where TComponent : struct, IComponent + protected IEnumerable ReadComponents() where TComponent : unmanaged { foreach (var pair in ReadComponentsHelper()) { @@ -272,7 +272,7 @@ namespace Encompass /// /// Returns all of the components of the specified type including an Entity reference for each Component. /// - protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity() where TComponent : struct, IComponent + protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity() where TComponent : unmanaged { foreach (var (component, id) in ReadComponentsHelper()) { @@ -280,7 +280,7 @@ namespace Encompass } } - private (TComponent, int) ReadComponentHelper() where TComponent : struct, IComponent + private (TComponent, int) ReadComponentHelper() where TComponent : unmanaged { var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent)); @@ -305,7 +305,7 @@ namespace Encompass /// /// Returns a Component with the specified Component Type. If multiples exist, an arbitrary Component is returned. /// - protected TComponent ReadComponent() where TComponent : struct, IComponent + protected TComponent ReadComponent() where TComponent : unmanaged { return ReadComponentHelper().Item1; } @@ -313,7 +313,7 @@ namespace Encompass /// /// Returns a component of the specified type including its Entity reference. If multiples exist, an arbitrary Component is returned. /// - protected (TComponent, Entity) ReadComponentIncludingEntity() where TComponent : struct, IComponent + protected (TComponent, Entity) ReadComponentIncludingEntity() where TComponent : unmanaged { var (component, id) = ReadComponentHelper(); return (component, _entityManager.GetEntity(id)); @@ -322,7 +322,7 @@ namespace Encompass /// /// Returns true if any Component with the specified Component Type exists. /// - protected bool SomeComponent() where TComponent : struct, IComponent + protected bool SomeComponent() where TComponent : unmanaged { var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent)); @@ -344,7 +344,7 @@ namespace Encompass } } - private ref readonly TComponent GetComponentHelper(int entityID) where TComponent : struct, IComponent + private ref readonly TComponent GetComponentHelper(int entityID) where TComponent : unmanaged { var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent)); @@ -375,7 +375,7 @@ namespace Encompass /// /// Thrown when the Engine does not declare that it reads the given Component Type. /// - protected ref readonly TComponent GetComponent(Entity entity) where TComponent : struct, IComponent + protected ref readonly TComponent GetComponent(Entity entity) where TComponent : unmanaged { return ref GetComponentHelper(entity.ID); } @@ -386,7 +386,7 @@ namespace Encompass /// /// Thrown when the Engine does not declare that is Reads the given Component Type. /// - protected bool HasComponent(Entity entity) where TComponent : struct, IComponent + protected bool HasComponent(Entity entity) where TComponent : unmanaged { var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent)); @@ -444,7 +444,7 @@ namespace Encompass /// /// Thrown when the Engine does not declare that it Writes the given Component Type. /// - protected void SetComponent(Entity entity, TComponent component) where TComponent : struct, IComponent + protected void SetComponent(Entity entity, TComponent component) where TComponent : unmanaged { var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority; @@ -484,7 +484,7 @@ namespace Encompass /// /// Thrown when the Engine does not declare that it Writes the given Component Type. /// - protected void AddComponent(Entity entity, TComponent component) where TComponent : struct, IComponent + protected void AddComponent(Entity entity, TComponent component) where TComponent : unmanaged { if (!EntityCreatedThisFrame(entity.ID)) { @@ -592,7 +592,7 @@ namespace Encompass /// Destroys an arbitrary Entity containing a Component of the specified Type. /// Entity destruction takes place after all the Engines have been processed by World Update. /// - protected void DestroyWith() where TComponent : struct, IComponent + protected void DestroyWith() where TComponent : unmanaged { Destroy(ReadEntity()); } @@ -601,7 +601,7 @@ namespace Encompass /// Destroys all Entities containing a Component of the specified Type. /// Entity destruction takes place after all the Engines have been processed by World Update. /// - protected void DestroyAllWith() where TComponent : struct, IComponent + protected void DestroyAllWith() where TComponent : unmanaged { foreach (var entity in ReadEntities()) { @@ -614,7 +614,7 @@ namespace Encompass /// Note that the Engine must Read the Component type that is being removed. /// If a Component with the specified type does not exist on the Entity, returns false and does not mutate the Entity. /// - protected void RemoveComponent(Entity entity) where TComponent : struct, IComponent + protected void RemoveComponent(Entity entity) where TComponent : unmanaged { var priority = WritePriorities.ContainsKey(typeof(TComponent)) ? WritePriorities[typeof(TComponent)] : DefaultWritePriority; @@ -764,24 +764,28 @@ namespace Encompass var withMask = BitSet512.Zero; foreach (var type in QueryWithTypes) { + if (!_componentManager.TypeToIndex.ContainsKey(type)) { _componentManager.TypeToIndex.Add(type, _componentManager.TypeToIndex.Count); } withMask = withMask.Set(_componentManager.TypeToIndex[type]); } var withoutMask = BitSet512.Zero; foreach (var type in QueryWithoutTypes) { + if (!_componentManager.TypeToIndex.ContainsKey(type)) { _componentManager.TypeToIndex.Add(type, _componentManager.TypeToIndex.Count); } withoutMask = withoutMask.Set(_componentManager.TypeToIndex[type]); } var immediateMask = BitSet512.Zero; foreach (var type in ReadImmediateTypes) { + if (!_componentManager.TypeToIndex.ContainsKey(type)) { _componentManager.TypeToIndex.Add(type, _componentManager.TypeToIndex.Count); } immediateMask = immediateMask.Set(_componentManager.TypeToIndex[type]); } var existingMask = BitSet512.Zero; foreach (var type in ReadTypes) { + if (!_componentManager.TypeToIndex.ContainsKey(type)) { _componentManager.TypeToIndex.Add(type, _componentManager.TypeToIndex.Count); } existingMask = existingMask.Set(_componentManager.TypeToIndex[type]); } diff --git a/encompass-cs/Exceptions/IllegalReadTypeException.cs b/encompass-cs/Exceptions/IllegalReadTypeException.cs deleted file mode 100644 index 3bc2512..0000000 --- a/encompass-cs/Exceptions/IllegalReadTypeException.cs +++ /dev/null @@ -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)) { } - } -} diff --git a/encompass-cs/IComponent.cs b/encompass-cs/IComponent.cs deleted file mode 100644 index 4fcce29..0000000 --- a/encompass-cs/IComponent.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Encompass -{ - /// - /// Structs that implement IComponent are considered to be Components. - /// - public interface IComponent { } -} diff --git a/encompass-cs/RenderManager.cs b/encompass-cs/RenderManager.cs index 3f603b4..0fa030b 100644 --- a/encompass-cs/RenderManager.cs +++ b/encompass-cs/RenderManager.cs @@ -16,7 +16,7 @@ namespace Encompass _drawLayerManager = drawLayerManager; } - public void RegisterOrderedRenderer(Action renderAction) where TComponent : struct, IComponent + public void RegisterOrderedRenderer(Action renderAction) where TComponent : unmanaged { _drawComponentTypeToOrderedRenderer.Add(typeof(TComponent), renderAction); _drawLayerManager.RegisterOrderedDrawable(); diff --git a/encompass-cs/Renderer.cs b/encompass-cs/Renderer.cs index f7f3b88..07939f2 100644 --- a/encompass-cs/Renderer.cs +++ b/encompass-cs/Renderer.cs @@ -17,7 +17,7 @@ namespace Encompass _componentManager = componentManager; } - protected IEnumerable ReadEntities() where TComponent : struct, IComponent + protected IEnumerable ReadEntities() where TComponent : unmanaged { foreach (var pair in ReadComponentsIncludingEntity()) { @@ -25,17 +25,17 @@ namespace Encompass } } - protected Entity ReadEntity() where TComponent : struct, IComponent + protected Entity ReadEntity() where TComponent : unmanaged { return ReadComponentIncludingEntity().Item2; } - protected IEnumerable ReadComponents() where TComponent : struct, IComponent + protected IEnumerable ReadComponents() where TComponent : unmanaged { return _componentManager.GetComponentsByType(); } - protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity() where TComponent : struct, IComponent + protected IEnumerable<(TComponent, Entity)> ReadComponentsIncludingEntity() where TComponent : unmanaged { foreach (var (component, id) in _componentManager.GetComponentsIncludingEntity()) { @@ -43,31 +43,31 @@ namespace Encompass } } - protected TComponent ReadComponent() where TComponent : struct, IComponent + protected TComponent ReadComponent() where TComponent : unmanaged { var enumerator = ReadComponents().GetEnumerator(); enumerator.MoveNext(); return enumerator.Current; } - protected (TComponent, Entity) ReadComponentIncludingEntity() where TComponent : struct, IComponent + protected (TComponent, Entity) ReadComponentIncludingEntity() where TComponent : unmanaged { var enumerator = ReadComponentsIncludingEntity().GetEnumerator(); enumerator.MoveNext(); return enumerator.Current; } - protected ref readonly TComponent GetComponent(Entity entity) where TComponent : struct, IComponent + protected ref readonly TComponent GetComponent(Entity entity) where TComponent : unmanaged { return ref _componentManager.GetComponentByEntityAndType(entity.ID); } - protected bool HasComponent(Entity entity) where TComponent : struct, IComponent + protected bool HasComponent(Entity entity) where TComponent : unmanaged { return _componentManager.EntityHasComponentOfType(entity.ID); } - protected bool SomeComponent() where TComponent : struct, IComponent + protected bool SomeComponent() where TComponent : unmanaged { return _componentManager.SomeExistingComponent(); } diff --git a/encompass-cs/Renderers/OrderedRenderer.cs b/encompass-cs/Renderers/OrderedRenderer.cs index 162322a..a8458ae 100644 --- a/encompass-cs/Renderers/OrderedRenderer.cs +++ b/encompass-cs/Renderers/OrderedRenderer.cs @@ -5,7 +5,7 @@ namespace Encompass /// /// OrdereredRenderer provides a structure for the common pattern of wishing to draw a specific DrawComponent at a specific layer. /// - public abstract class OrderedRenderer : Renderer where TComponent : struct, IComponent, IDrawableComponent + public abstract class OrderedRenderer : Renderer where TComponent : unmanaged, IDrawableComponent { public abstract void Render(Entity entity, in TComponent drawComponent); diff --git a/encompass-cs/WorldBuilder.cs b/encompass-cs/WorldBuilder.cs index ad8a5ba..461c159 100644 --- a/encompass-cs/WorldBuilder.cs +++ b/encompass-cs/WorldBuilder.cs @@ -84,7 +84,7 @@ namespace Encompass /// /// Sets Component data for the specified Component Type on the specified Entity. /// - public void SetComponent(Entity entity, TComponent component) where TComponent : struct, IComponent + public void SetComponent(Entity entity, TComponent component) where TComponent : unmanaged { RegisterComponentType(); _startingExistingComponentStore.Set(entity.ID, component); @@ -97,7 +97,7 @@ namespace Encompass } } - internal void RegisterComponentType() where TComponent : struct, IComponent + internal void RegisterComponentType() where TComponent : unmanaged { if (!_typeToIndex.ContainsKey(typeof(TComponent))) { @@ -183,7 +183,7 @@ namespace Encompass /// /// Adds the specified OrderedRenderer to the World. /// - public OrderedRenderer AddOrderedRenderer(OrderedRenderer renderer) where TComponent : struct, IComponent, IDrawableComponent + public OrderedRenderer AddOrderedRenderer(OrderedRenderer renderer) where TComponent : unmanaged, IDrawableComponent { RegisterComponentType(); renderer.AssignEntityManager(_entityManager); @@ -354,20 +354,6 @@ namespace Encompass throw new EngineWriteConflictException(errorString); } - // 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); - } - } - } - PreloadJIT(_componentTypesToPreload, _messageTypes); var engineOrder = new List(); diff --git a/test/ComponentTest.cs b/test/ComponentTest.cs index 1b94e51..684a07f 100644 --- a/test/ComponentTest.cs +++ b/test/ComponentTest.cs @@ -2,14 +2,14 @@ using NUnit.Framework; using FluentAssertions; using Encompass; +using System.Runtime.CompilerServices; namespace Tests { public class ComponentTests { - struct MockComponent : Encompass.IComponent + struct MockComponent { - public string myString; public int myInt; } @@ -54,16 +54,17 @@ namespace Tests } [Test] - public void AddComponent() + public unsafe void AddComponent() { var worldBuilder = new WorldBuilder(); worldBuilder.AddEngine(new AddComponentTestEngine()); var entity = worldBuilder.CreateEntity(); + const string MyString = "hello"; + MockComponent mockComponent; mockComponent.myInt = 3; - mockComponent.myString = "hello"; worldBuilder.SetComponent(entity, mockComponent); @@ -85,16 +86,15 @@ namespace Tests worldBuilder.AddEngine(new ReadMockComponentEngine()); var entity = worldBuilder.CreateEntity(); - worldBuilder.SetComponent(entity, new MockComponent { myInt = 20, myString = "what" }); - worldBuilder.SetComponent(entity, new MockComponent { myInt = 50, myString = "hi" }); - worldBuilder.SetComponent(entity, new MockComponent { myInt = 40, myString = "wassup" }); + worldBuilder.SetComponent(entity, new MockComponent { myInt = 20 }); + worldBuilder.SetComponent(entity, new MockComponent { myInt = 50 }); + worldBuilder.SetComponent(entity, new MockComponent { myInt = 40 }); var world = worldBuilder.Build(); world.Update(0.01); Assert.That(gottenMockComponent.myInt, Is.EqualTo(40)); - Assert.That(gottenMockComponent.myString, Is.EqualTo("wassup")); } [Reads(typeof(MockComponent))] @@ -191,7 +191,6 @@ namespace Tests { MockComponent mockComponent; mockComponent.myInt = 10; - mockComponent.myString = "four"; AddMockComponentMessage addMockComponentMessage; addMockComponentMessage.entity = entity; @@ -255,9 +254,8 @@ namespace Tests MockComponent mockComponent; mockComponent.myInt = 3; - mockComponent.myString = "hello"; - worldBuilder.SetComponent(entity, mockComponent); + worldBuilder.SetComponent(entity, mockComponent); EntityMessage entityMessage; entityMessage.entity = entity; @@ -298,7 +296,6 @@ namespace Tests MockComponent mockComponent; mockComponent.myInt = 3; - mockComponent.myString = "hello"; worldBuilder.SetComponent(entity, mockComponent); @@ -336,7 +333,6 @@ namespace Tests MockComponent mockComponent; mockComponent.myInt = 3; - mockComponent.myString = "hello"; worldBuilder.SetComponent(entity, mockComponent); @@ -446,7 +442,6 @@ namespace Tests MockComponent mockComponent; mockComponent.myInt = 3; - mockComponent.myString = "hello"; worldBuilder.SetComponent(entity, mockComponent); diff --git a/test/EngineTest.cs b/test/EngineTest.cs index 77ceeb1..208f455 100644 --- a/test/EngineTest.cs +++ b/test/EngineTest.cs @@ -10,10 +10,9 @@ using Encompass.Exceptions; namespace Tests { - struct MockComponent : IComponent + struct MockComponent { public int myInt; - public string myString; } public class EngineTest @@ -72,12 +71,10 @@ namespace Tests var entityB = worldBuilder.CreateEntity(); MockComponent mockComponent; - mockComponent.myInt = 0; - mockComponent.myString = "hello"; + mockComponent.myInt = 2; MockComponent mockComponentB; mockComponentB.myInt = 1; - mockComponentB.myString = "howdy"; worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entityB, mockComponentB); @@ -100,12 +97,10 @@ namespace Tests var entityB = worldBuilder.CreateEntity(); MockComponent mockComponent; - mockComponent.myInt = 0; - mockComponent.myString = "hello"; + mockComponent.myInt = 2; MockComponent mockComponentB; mockComponentB.myInt = 1; - mockComponentB.myString = "howdy"; worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entityB, mockComponentB); @@ -135,8 +130,7 @@ namespace Tests var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; - mockComponent.myInt = 0; - mockComponent.myString = "hello"; + mockComponent.myInt = 3; worldBuilder.SetComponent(entity, mockComponent); @@ -157,12 +151,10 @@ namespace Tests var entityB = worldBuilder.CreateEntity(); MockComponent mockComponent; - mockComponent.myInt = 0; - mockComponent.myString = "hello"; + mockComponent.myInt = 2; MockComponent mockComponentB; mockComponentB.myInt = 1; - mockComponentB.myString = "howdy"; worldBuilder.SetComponent(entity, mockComponent); worldBuilder.SetComponent(entityB, mockComponentB); @@ -183,8 +175,7 @@ namespace Tests var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; - mockComponent.myInt = 0; - mockComponent.myString = "hello"; + mockComponent.myInt = 2; worldBuilder.SetComponent(entity, mockComponent); @@ -204,7 +195,6 @@ namespace Tests var (component, entity) = ReadComponentIncludingEntity(); component.myInt = 420; - component.myString = "blaze it"; SetComponent(entity, component); } } @@ -222,8 +212,7 @@ namespace Tests var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; - mockComponent.myInt = 0; - mockComponent.myString = "hello"; + mockComponent.myInt = 3; worldBuilder.SetComponent(entity, mockComponent); @@ -233,7 +222,6 @@ namespace Tests world.Update(0.01); Assert.AreEqual(420, resultComponent.myInt); - Assert.AreEqual("blaze it", resultComponent.myString); } [Reads(typeof(MockComponent))] @@ -244,7 +232,6 @@ namespace Tests var (component, entity) = ReadComponentIncludingEntity(); component.myInt = 420; - component.myString = "blaze it"; SetComponent(entity, component); component = ReadComponent(); @@ -260,8 +247,7 @@ namespace Tests var entity = worldBuilder.CreateEntity(); MockComponent mockComponent; - mockComponent.myInt = 0; - mockComponent.myString = "hello"; + mockComponent.myInt = 3; worldBuilder.SetComponent(entity, mockComponent); @@ -594,11 +580,9 @@ namespace Tests MockComponent componentA; componentA.myInt = 20; - componentA.myString = "hello"; MockComponent componentB; componentB.myInt = 20; - componentB.myString = "hello"; var entity = worldBuilder.CreateEntity(); worldBuilder.SetComponent(entity, componentA); @@ -636,7 +620,7 @@ namespace Tests Assert.That(emptyComponentReadResult, Is.Empty); } - struct DestroyerComponent : IComponent { } + struct DestroyerComponent { } [Reads(typeof(DestroyerComponent))] class DestroyerEngine : Engine @@ -675,7 +659,6 @@ namespace Tests DestroyerComponent destroyerComponent; MockComponent mockComponent; mockComponent.myInt = 2; - mockComponent.myString = "blah"; worldBuilder.SetComponent(entity, destroyerComponent); worldBuilder.SetComponent(entity, mockComponent); @@ -975,14 +958,14 @@ namespace Tests entity.Should().BeEquivalentTo(readEntity); } - struct MockComponentB : IComponent + struct MockComponentB { + private int value; + public MockComponentB(int value) { this.value = value; } - - int value; } static MockComponentB getComponentResult; @@ -1420,9 +1403,9 @@ namespace Tests public class QueryTests { - struct MockComponentB : IComponent { } - struct MockComponentC : IComponent { } - struct MockComponentD : IComponent { } + struct MockComponentB { } + struct MockComponentC { } + struct MockComponentD { } [Reads(typeof(MockComponent), typeof(MockComponentB))] [Writes(typeof(MockComponentB))] @@ -1877,14 +1860,14 @@ namespace Tests _components.Should().NotBeEmpty(); } - struct MockTimerComponent : IComponent + struct MockTimerComponent { + public double Timer { get; set; } + public MockTimerComponent(double time) { Timer = time; } - - public double Timer { get; set; } } [Reads(typeof(MockTimerComponent))] diff --git a/test/GeneralRendererTest.cs b/test/GeneralRendererTest.cs index bc1aedc..9627d34 100644 --- a/test/GeneralRendererTest.cs +++ b/test/GeneralRendererTest.cs @@ -5,7 +5,7 @@ namespace Tests { public static class GeneralRendererTest { - struct AComponent : IComponent { } + struct AComponent { } public class SingletonRead { diff --git a/test/OrderedRendererTest.cs b/test/OrderedRendererTest.cs index 92cb3b8..2ee2af7 100644 --- a/test/OrderedRendererTest.cs +++ b/test/OrderedRendererTest.cs @@ -9,11 +9,11 @@ namespace Tests { public class OrderedRendererTest { - struct AComponent : IComponent { } - struct BComponent : IComponent { } - struct CComponent : IComponent { } + struct AComponent { } + struct BComponent { } + struct CComponent { } - struct TestDrawComponent : IComponent, IDrawableComponent + struct TestDrawComponent : IDrawableComponent { public int Layer { get; set; } } diff --git a/test/SpawnerTest.cs b/test/SpawnerTest.cs index c0c5f0c..e31ed37 100644 --- a/test/SpawnerTest.cs +++ b/test/SpawnerTest.cs @@ -5,7 +5,7 @@ namespace Tests { public class SpawnerTest { - struct TestComponent : IComponent { } + struct TestComponent { } struct SpawnMessageA : IMessage { } static Entity resultEntity; diff --git a/test/WorldBuilderTest.cs b/test/WorldBuilderTest.cs index 2fd62fd..9a705f2 100644 --- a/test/WorldBuilderTest.cs +++ b/test/WorldBuilderTest.cs @@ -114,7 +114,7 @@ namespace Tests public class MultipleEngineWriteConflict { - struct AComponent : IComponent { } + struct AComponent { } [Writes(typeof(AComponent))] class AEngine : Engine @@ -146,7 +146,7 @@ namespace Tests public Entity entity; } - struct AComponent : IComponent + struct AComponent { public int myInt; } @@ -215,7 +215,7 @@ namespace Tests public Entity entity; } - struct AComponent : IComponent + struct AComponent { public int myInt; } @@ -318,28 +318,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(() => worldBuilder.AddEngine(new MyEngine()), "ANonMessage must be a Message or Component"); - } - } - public class IllegalWriteType { struct ANonMessage { } @@ -428,8 +406,8 @@ namespace Tests { static List order = new List(); - struct AComponent : IComponent { } - struct BComponent : IComponent { } + struct AComponent { } + struct BComponent { } struct AMessage : IMessage { } struct BMessage : IMessage { } diff --git a/test/WorldTest.cs b/test/WorldTest.cs index 6ce089b..f985dc1 100644 --- a/test/WorldTest.cs +++ b/test/WorldTest.cs @@ -11,8 +11,8 @@ namespace Tests { public class WorldTest { - struct TestComponent : IComponent { } - struct TestDrawComponent : IComponent, IDrawableComponent + struct TestComponent { } + struct TestDrawComponent : IDrawableComponent { public int Layer { get; set; } } diff --git a/test/test.csproj b/test/test.csproj index 8f6a401..9479dc3 100644 --- a/test/test.csproj +++ b/test/test.csproj @@ -5,6 +5,7 @@ false Tests EncompassECS.Framework.Tests + true