Expose some enumerable iterators + some tweaks (#15)
continuous-integration/drone/push Build is passing Details

pull/16/head
cosmonaut 2020-11-16 07:21:51 +00:00
parent 327a458f17
commit 40e6d75638
10 changed files with 65 additions and 25 deletions

View File

@ -74,7 +74,7 @@ namespace Encompass
} }
} }
public Span<Entity> AllEntities<TComponent>() where TComponent : struct public ReadOnlySpan<Entity> AllEntities<TComponent>() where TComponent : struct
{ {
return _store.AllEntities<TComponent>(); return _store.AllEntities<TComponent>();
} }

View File

@ -113,16 +113,26 @@ namespace Encompass
return Lookup<TComponent>().Count > 0; return Lookup<TComponent>().Count > 0;
} }
public Span<TComponent> All<TComponent>() where TComponent : struct public ReadOnlySpan<TComponent> All<TComponent>() where TComponent : struct
{ {
return Lookup<TComponent>().AllComponents(); return Lookup<TComponent>().AllComponents();
} }
public Span<Entity> AllEntities<TComponent>() where TComponent : struct public IEnumerable<TComponent> AllAsEnumerable<TComponent>() where TComponent : struct
{
return Lookup<TComponent>().AllComponentsAsEnumerable();
}
public ReadOnlySpan<Entity> AllEntities<TComponent>() where TComponent : struct
{ {
return Lookup<TComponent>().AllEntities(); return Lookup<TComponent>().AllEntities();
} }
public IEnumerable<Entity> AllEntitiesAsEnumerable<TComponent>() where TComponent : struct
{
return Lookup<TComponent>().AllEntitiesAsEnumerable();
}
public void Clear<TComponent>() where TComponent : struct public void Clear<TComponent>() where TComponent : struct
{ {
Lookup<TComponent>().Clear(); Lookup<TComponent>().Clear();

View File

@ -41,7 +41,7 @@ namespace Encompass
return ref Lookup<TMessage>().First(); return ref Lookup<TMessage>().First();
} }
public Span<TMessage> All<TMessage>() where TMessage : struct, IMessage public ReadOnlySpan<TMessage> All<TMessage>() where TMessage : struct, IMessage
{ {
return Lookup<TMessage>().All(); return Lookup<TMessage>().All();
} }

View File

@ -127,14 +127,24 @@ namespace Encompass
_priorities.Clear(); _priorities.Clear();
} }
public Span<Entity> AllEntities() public ReadOnlySpan<Entity> AllEntities()
{ {
return new Span<Entity>(_storageIndexToEntities, 0, _nextID); return new ReadOnlySpan<Entity>(_storageIndexToEntities, 0, _nextID);
} }
public Span<TComponent> AllComponents() public IEnumerable<Entity> AllEntitiesAsEnumerable()
{ {
return new Span<TComponent>(_components, 0, _nextID); return new ArraySegment<Entity>(_storageIndexToEntities, 0, _nextID);
}
public ReadOnlySpan<TComponent> AllComponents()
{
return new ReadOnlySpan<TComponent>(_components, 0, _nextID);
}
public IEnumerable<TComponent> AllComponentsAsEnumerable()
{
return new ArraySegment<TComponent>(_components, 0, _nextID);
} }
} }
} }

View File

@ -92,9 +92,9 @@ namespace Encompass
return _nextIndex != 0; return _nextIndex != 0;
} }
public Span<TMessage> All() public ReadOnlySpan<TMessage> All()
{ {
return new Span<TMessage>(_store, 0, _nextIndex); return new ReadOnlySpan<TMessage>(_store, 0, _nextIndex);
} }
public IEnumerable<TMessage> WithEntity(int entityID) public IEnumerable<TMessage> WithEntity(int entityID)

View File

@ -98,7 +98,7 @@ namespace Encompass
// existing or immediate reads // existing or immediate reads
internal Span<TComponent> ReadExistingAndImmediateComponentsByType<TComponent>() where TComponent : struct internal ReadOnlySpan<TComponent> ReadExistingAndImmediateComponentsByType<TComponent>() where TComponent : struct
{ {
return _upToDateComponentStore.All<TComponent>(); return _upToDateComponentStore.All<TComponent>();
} }
@ -108,7 +108,7 @@ namespace Encompass
return ref _upToDateComponentStore.Singular<TComponent>(); return ref _upToDateComponentStore.Singular<TComponent>();
} }
internal Span<Entity> GetExistingAndImmediateEntities<TComponent>() where TComponent : struct internal ReadOnlySpan<Entity> GetExistingAndImmediateEntities<TComponent>() where TComponent : struct
{ {
return _upToDateComponentStore.AllEntities<TComponent>(); return _upToDateComponentStore.AllEntities<TComponent>();
} }
@ -125,7 +125,7 @@ namespace Encompass
// existing reads // existing reads
internal Span<TComponent> GetExistingComponents<TComponent>() where TComponent : struct internal ReadOnlySpan<TComponent> GetExistingComponents<TComponent>() where TComponent : struct
{ {
return _existingComponentStore.All<TComponent>(); return _existingComponentStore.All<TComponent>();
} }
@ -135,11 +135,16 @@ namespace Encompass
return ref _existingComponentStore.Singular<TComponent>(); return ref _existingComponentStore.Singular<TComponent>();
} }
internal Span<Entity> GetExistingEntities<TComponent>() where TComponent : struct internal ReadOnlySpan<Entity> GetExistingEntities<TComponent>() where TComponent : struct
{ {
return _existingComponentStore.AllEntities<TComponent>(); return _existingComponentStore.AllEntities<TComponent>();
} }
internal IEnumerable<Entity> GetExistingEntitiesAsEnumerable<TComponent>() where TComponent : struct
{
return _existingComponentStore.AllEntitiesAsEnumerable<TComponent>();
}
internal ref readonly Entity ExistingSingularEntity<TComponent>() where TComponent : struct internal ref readonly Entity ExistingSingularEntity<TComponent>() where TComponent : struct
{ {
return ref _existingComponentStore.SingularEntity<TComponent>(); return ref _existingComponentStore.SingularEntity<TComponent>();
@ -152,7 +157,7 @@ namespace Encompass
// immediate reads // immediate reads
internal Span<TComponent> ReadImmediateComponentsByType<TComponent>() where TComponent : struct internal ReadOnlySpan<TComponent> ReadImmediateComponentsByType<TComponent>() where TComponent : struct
{ {
return _immediateComponentStore.All<TComponent>(); return _immediateComponentStore.All<TComponent>();
} }
@ -162,7 +167,7 @@ namespace Encompass
return ref _immediateComponentStore.Singular<TComponent>(); return ref _immediateComponentStore.Singular<TComponent>();
} }
internal Span<Entity> GetImmediateEntities<TComponent>() where TComponent : struct internal ReadOnlySpan<Entity> GetImmediateEntities<TComponent>() where TComponent : struct
{ {
return _immediateComponentStore.AllEntities<TComponent>(); return _immediateComponentStore.AllEntities<TComponent>();
} }
@ -226,11 +231,16 @@ namespace Encompass
return _immediateComponentStore.Has(type, entityID); return _immediateComponentStore.Has(type, entityID);
} }
internal Span<TComponent> GetComponentsByType<TComponent>() where TComponent : struct internal ReadOnlySpan<TComponent> GetComponentsByType<TComponent>() where TComponent : struct
{ {
return _existingComponentStore.All<TComponent>(); return _existingComponentStore.All<TComponent>();
} }
internal IEnumerable<TComponent> GetComponentsByTypeEnumerable<TComponent>() where TComponent : struct
{
return _existingComponentStore.AllAsEnumerable<TComponent>();
}
internal ref readonly TComponent GetComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct internal ref readonly TComponent GetComponentByEntityAndType<TComponent>(int entityID) where TComponent : struct
{ {
return ref _existingComponentStore.Get<TComponent>(entityID); return ref _existingComponentStore.Get<TComponent>(entityID);

View File

@ -223,7 +223,7 @@ namespace Encompass
/// <summary> /// <summary>
/// Returns all Entities containing the specified Component type. /// Returns all Entities containing the specified Component type.
/// </summary> /// </summary>
protected Span<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent protected ReadOnlySpan<Entity> ReadEntities<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));
@ -273,7 +273,7 @@ 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 Span<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent protected ReadOnlySpan<TComponent> ReadComponents<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));
@ -550,7 +550,7 @@ 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 Span<TMessage> ReadMessages<TMessage>() where TMessage : struct, IMessage protected ReadOnlySpan<TMessage> ReadMessages<TMessage>() where TMessage : struct, IMessage
{ {
CheckMessageRead<TMessage>(); CheckMessageRead<TMessage>();
return _messageManager.GetMessagesByType<TMessage>(); return _messageManager.GetMessagesByType<TMessage>();

View File

@ -38,7 +38,7 @@ namespace Encompass
_messageStore.ProcessDelayedMessages(dt * _timeManager.TimeDilationFactor, dt); _messageStore.ProcessDelayedMessages(dt * _timeManager.TimeDilationFactor, dt);
} }
internal Span<TMessage> GetMessagesByType<TMessage>() where TMessage : struct, IMessage internal ReadOnlySpan<TMessage> GetMessagesByType<TMessage>() where TMessage : struct, IMessage
{ {
return _messageStore.All<TMessage>(); return _messageStore.All<TMessage>();
} }

View File

@ -18,21 +18,31 @@ namespace Encompass
_componentManager = componentManager; _componentManager = componentManager;
} }
protected Span<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent protected ReadOnlySpan<Entity> ReadEntities<TComponent>() where TComponent : struct, IComponent
{ {
return _componentManager.GetExistingEntities<TComponent>(); return _componentManager.GetExistingEntities<TComponent>();
} }
protected IEnumerable<Entity> ReadEntitiesAsEnumerable<TComponent>() where TComponent : struct, IComponent
{
return _componentManager.GetExistingEntitiesAsEnumerable<TComponent>();
}
protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct, IComponent protected ref readonly Entity ReadEntity<TComponent>() where TComponent : struct, IComponent
{ {
return ref _componentManager.ExistingSingularEntity<TComponent>(); return ref _componentManager.ExistingSingularEntity<TComponent>();
} }
protected Span<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent protected ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent
{ {
return _componentManager.GetComponentsByType<TComponent>(); return _componentManager.GetComponentsByType<TComponent>();
} }
protected IEnumerable<TComponent> ReadComponentsAsEnumerable<TComponent>() where TComponent : struct, IComponent
{
return _componentManager.GetComponentsByTypeEnumerable<TComponent>();
}
protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent protected ref readonly TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent
{ {
return ref _componentManager.ExistingSingular<TComponent>(); return ref _componentManager.ExistingSingular<TComponent>();

View File

@ -82,7 +82,7 @@ namespace Encompass
/// <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, in TComponent component) where TComponent : struct public void SetComponent<TComponent>(Entity entity, in TComponent component) where TComponent : struct, IComponent
{ {
RegisterComponentType<TComponent>(); RegisterComponentType<TComponent>();
_startingExistingComponentStore.Set(entity.ID, component); _startingExistingComponentStore.Set(entity.ID, component);
@ -95,7 +95,7 @@ namespace Encompass
} }
} }
internal void RegisterComponentType<TComponent>() where TComponent : struct internal void RegisterComponentType<TComponent>() where TComponent : struct, IComponent
{ {
if (!_typeToIndex.ContainsKey(typeof(TComponent))) if (!_typeToIndex.ContainsKey(typeof(TComponent)))
{ {