diff --git a/encompass-cs/Collections/ComponentDeltaStore.cs b/encompass-cs/Collections/ComponentDeltaStore.cs index 48d44c4..75a3b75 100644 --- a/encompass-cs/Collections/ComponentDeltaStore.cs +++ b/encompass-cs/Collections/ComponentDeltaStore.cs @@ -74,7 +74,7 @@ namespace Encompass } } - public Span AllEntities() where TComponent : struct + public ReadOnlySpan AllEntities() where TComponent : struct { return _store.AllEntities(); } diff --git a/encompass-cs/Collections/ComponentStore.cs b/encompass-cs/Collections/ComponentStore.cs index bfbc8af..99b85d3 100644 --- a/encompass-cs/Collections/ComponentStore.cs +++ b/encompass-cs/Collections/ComponentStore.cs @@ -113,16 +113,26 @@ namespace Encompass return Lookup().Count > 0; } - public Span All() where TComponent : struct + public ReadOnlySpan All() where TComponent : struct { return Lookup().AllComponents(); } - public Span AllEntities() where TComponent : struct + public IEnumerable AllAsEnumerable() where TComponent : struct + { + return Lookup().AllComponentsAsEnumerable(); + } + + public ReadOnlySpan AllEntities() where TComponent : struct { return Lookup().AllEntities(); } + public IEnumerable AllEntitiesAsEnumerable() where TComponent : struct + { + return Lookup().AllEntitiesAsEnumerable(); + } + public void Clear() where TComponent : struct { Lookup().Clear(); diff --git a/encompass-cs/Collections/MessageStore.cs b/encompass-cs/Collections/MessageStore.cs index 3f57158..196dc7f 100644 --- a/encompass-cs/Collections/MessageStore.cs +++ b/encompass-cs/Collections/MessageStore.cs @@ -41,7 +41,7 @@ namespace Encompass return ref Lookup().First(); } - public Span All() where TMessage : struct, IMessage + public ReadOnlySpan All() where TMessage : struct, IMessage { return Lookup().All(); } diff --git a/encompass-cs/Collections/TypedComponentStore.cs b/encompass-cs/Collections/TypedComponentStore.cs index 232fa2b..5b837c6 100644 --- a/encompass-cs/Collections/TypedComponentStore.cs +++ b/encompass-cs/Collections/TypedComponentStore.cs @@ -127,14 +127,24 @@ namespace Encompass _priorities.Clear(); } - public Span AllEntities() + public ReadOnlySpan AllEntities() { - return new Span(_storageIndexToEntities, 0, _nextID); + return new ReadOnlySpan(_storageIndexToEntities, 0, _nextID); } - public Span AllComponents() + public IEnumerable AllEntitiesAsEnumerable() { - return new Span(_components, 0, _nextID); + return new ArraySegment(_storageIndexToEntities, 0, _nextID); + } + + public ReadOnlySpan AllComponents() + { + return new ReadOnlySpan(_components, 0, _nextID); + } + + public IEnumerable AllComponentsAsEnumerable() + { + return new ArraySegment(_components, 0, _nextID); } } } diff --git a/encompass-cs/Collections/TypedMessageStore.cs b/encompass-cs/Collections/TypedMessageStore.cs index c8a263e..a9f222b 100644 --- a/encompass-cs/Collections/TypedMessageStore.cs +++ b/encompass-cs/Collections/TypedMessageStore.cs @@ -92,9 +92,9 @@ namespace Encompass return _nextIndex != 0; } - public Span All() + public ReadOnlySpan All() { - return new Span(_store, 0, _nextIndex); + return new ReadOnlySpan(_store, 0, _nextIndex); } public IEnumerable WithEntity(int entityID) diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index 49fe78d..da54e8e 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -98,7 +98,7 @@ namespace Encompass // existing or immediate reads - internal Span ReadExistingAndImmediateComponentsByType() where TComponent : struct + internal ReadOnlySpan ReadExistingAndImmediateComponentsByType() where TComponent : struct { return _upToDateComponentStore.All(); } @@ -108,7 +108,7 @@ namespace Encompass return ref _upToDateComponentStore.Singular(); } - internal Span GetExistingAndImmediateEntities() where TComponent : struct + internal ReadOnlySpan GetExistingAndImmediateEntities() where TComponent : struct { return _upToDateComponentStore.AllEntities(); } @@ -125,7 +125,7 @@ namespace Encompass // existing reads - internal Span GetExistingComponents() where TComponent : struct + internal ReadOnlySpan GetExistingComponents() where TComponent : struct { return _existingComponentStore.All(); } @@ -135,11 +135,16 @@ namespace Encompass return ref _existingComponentStore.Singular(); } - internal Span GetExistingEntities() where TComponent : struct + internal ReadOnlySpan GetExistingEntities() where TComponent : struct { return _existingComponentStore.AllEntities(); } + internal IEnumerable GetExistingEntitiesAsEnumerable() where TComponent : struct + { + return _existingComponentStore.AllEntitiesAsEnumerable(); + } + internal ref readonly Entity ExistingSingularEntity() where TComponent : struct { return ref _existingComponentStore.SingularEntity(); @@ -152,7 +157,7 @@ namespace Encompass // immediate reads - internal Span ReadImmediateComponentsByType() where TComponent : struct + internal ReadOnlySpan ReadImmediateComponentsByType() where TComponent : struct { return _immediateComponentStore.All(); } @@ -162,7 +167,7 @@ namespace Encompass return ref _immediateComponentStore.Singular(); } - internal Span GetImmediateEntities() where TComponent : struct + internal ReadOnlySpan GetImmediateEntities() where TComponent : struct { return _immediateComponentStore.AllEntities(); } @@ -226,11 +231,16 @@ namespace Encompass return _immediateComponentStore.Has(type, entityID); } - internal Span GetComponentsByType() where TComponent : struct + internal ReadOnlySpan GetComponentsByType() where TComponent : struct { return _existingComponentStore.All(); } + internal IEnumerable GetComponentsByTypeEnumerable() where TComponent : struct + { + return _existingComponentStore.AllAsEnumerable(); + } + internal ref readonly TComponent GetComponentByEntityAndType(int entityID) where TComponent : struct { return ref _existingComponentStore.Get(entityID); diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index 548d7f6..46abe4d 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -223,7 +223,7 @@ namespace Encompass /// /// Returns all Entities containing the specified Component type. /// - protected Span ReadEntities() where TComponent : struct, IComponent + protected ReadOnlySpan ReadEntities() where TComponent : struct, IComponent { var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent)); @@ -273,7 +273,7 @@ namespace Encompass /// /// Returns all of the Components with the specified Component Type. /// - protected Span ReadComponents() where TComponent : struct, IComponent + protected ReadOnlySpan ReadComponents() where TComponent : struct, IComponent { var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent)); @@ -550,7 +550,7 @@ namespace Encompass /// /// Thrown when the Engine does not declare that it Receives the specified Message Type. /// - protected Span ReadMessages() where TMessage : struct, IMessage + protected ReadOnlySpan ReadMessages() where TMessage : struct, IMessage { CheckMessageRead(); return _messageManager.GetMessagesByType(); diff --git a/encompass-cs/MessageManager.cs b/encompass-cs/MessageManager.cs index 234f46f..73f4c20 100644 --- a/encompass-cs/MessageManager.cs +++ b/encompass-cs/MessageManager.cs @@ -38,7 +38,7 @@ namespace Encompass _messageStore.ProcessDelayedMessages(dt * _timeManager.TimeDilationFactor, dt); } - internal Span GetMessagesByType() where TMessage : struct, IMessage + internal ReadOnlySpan GetMessagesByType() where TMessage : struct, IMessage { return _messageStore.All(); } diff --git a/encompass-cs/Renderer.cs b/encompass-cs/Renderer.cs index 85b5489..7891489 100644 --- a/encompass-cs/Renderer.cs +++ b/encompass-cs/Renderer.cs @@ -18,21 +18,31 @@ namespace Encompass _componentManager = componentManager; } - protected Span ReadEntities() where TComponent : struct, IComponent + protected ReadOnlySpan ReadEntities() where TComponent : struct, IComponent { return _componentManager.GetExistingEntities(); } + protected IEnumerable ReadEntitiesAsEnumerable() where TComponent : struct, IComponent + { + return _componentManager.GetExistingEntitiesAsEnumerable(); + } + protected ref readonly Entity ReadEntity() where TComponent : struct, IComponent { return ref _componentManager.ExistingSingularEntity(); } - protected Span ReadComponents() where TComponent : struct, IComponent + protected ReadOnlySpan ReadComponents() where TComponent : struct, IComponent { return _componentManager.GetComponentsByType(); } + protected IEnumerable ReadComponentsAsEnumerable() where TComponent : struct, IComponent + { + return _componentManager.GetComponentsByTypeEnumerable(); + } + protected ref readonly TComponent ReadComponent() where TComponent : struct, IComponent { return ref _componentManager.ExistingSingular(); diff --git a/encompass-cs/WorldBuilder.cs b/encompass-cs/WorldBuilder.cs index 7da9ced..2fe307e 100644 --- a/encompass-cs/WorldBuilder.cs +++ b/encompass-cs/WorldBuilder.cs @@ -82,7 +82,7 @@ namespace Encompass /// /// Sets Component data for the specified Component Type on the specified Entity. /// - public void SetComponent(Entity entity, in TComponent component) where TComponent : struct + public void SetComponent(Entity entity, in TComponent component) where TComponent : struct, IComponent { RegisterComponentType(); _startingExistingComponentStore.Set(entity.ID, component); @@ -95,7 +95,7 @@ namespace Encompass } } - internal void RegisterComponentType() where TComponent : struct + internal void RegisterComponentType() where TComponent : struct, IComponent { if (!_typeToIndex.ContainsKey(typeof(TComponent))) {