2022-04-08 05:52:03 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-03-05 02:01:44 +00:00
|
|
|
|
|
2022-04-08 05:52:03 +00:00
|
|
|
|
namespace MoonTools.ECS
|
2022-03-05 02:01:44 +00:00
|
|
|
|
{
|
2022-04-08 05:52:03 +00:00
|
|
|
|
public abstract class EntityComponentReader
|
2022-03-05 02:01:44 +00:00
|
|
|
|
{
|
2022-05-15 19:16:49 +00:00
|
|
|
|
internal readonly World World;
|
2022-05-03 04:51:11 +00:00
|
|
|
|
internal EntityStorage EntityStorage => World.EntityStorage;
|
|
|
|
|
internal ComponentDepot ComponentDepot => World.ComponentDepot;
|
|
|
|
|
internal RelationDepot RelationDepot => World.RelationDepot;
|
2023-01-10 00:41:00 +00:00
|
|
|
|
protected FilterBuilder FilterBuilder => new FilterBuilder(FilterStorage, ComponentTypeIndices);
|
|
|
|
|
internal FilterStorage FilterStorage => World.FilterStorage;
|
|
|
|
|
internal TypeIndices ComponentTypeIndices => World.ComponentTypeIndices;
|
|
|
|
|
internal TypeIndices RelationTypeIndices => World.RelationTypeIndices;
|
|
|
|
|
internal TemplateStorage TemplateStorage => World.TemplateStorage;
|
|
|
|
|
internal ComponentDepot TemplateComponentDepot => World.TemplateComponentDepot;
|
2022-03-05 02:01:44 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
public EntityComponentReader(World world)
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2022-05-03 04:51:11 +00:00
|
|
|
|
World = world;
|
2022-04-08 05:52:03 +00:00
|
|
|
|
}
|
2022-03-05 02:01:44 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
protected ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
|
|
|
|
return ComponentDepot.ReadComponents<TComponent>();
|
|
|
|
|
}
|
2022-03-05 02:01:44 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
protected bool Has<TComponent>(in Entity entity) where TComponent : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
var storageIndex = ComponentTypeIndices.GetIndex<TComponent>();
|
|
|
|
|
return EntityStorage.HasComponent(entity.ID, storageIndex);
|
2022-04-08 05:52:03 +00:00
|
|
|
|
}
|
2022-03-05 02:01:44 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
protected bool Some<TComponent>() where TComponent : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
|
|
|
|
return ComponentDepot.Some<TComponent>();
|
|
|
|
|
}
|
2022-03-21 23:21:42 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
protected ref readonly TComponent Get<TComponent>(in Entity entity) where TComponent : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
|
|
|
|
return ref ComponentDepot.Get<TComponent>(entity.ID);
|
|
|
|
|
}
|
2022-03-21 23:21:42 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
protected ref readonly TComponent GetSingleton<TComponent>() where TComponent : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2023-01-10 00:41:00 +00:00
|
|
|
|
return ref ComponentDepot.GetFirst<TComponent>();
|
2022-04-08 05:52:03 +00:00
|
|
|
|
}
|
2022-03-31 23:15:30 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
protected Entity GetSingletonEntity<TComponent>() where TComponent : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
|
|
|
|
return ComponentDepot.GetSingletonEntity<TComponent>();
|
|
|
|
|
}
|
2022-04-06 19:53:50 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
protected ReverseSpanEnumerator<(Entity, Entity)> Relations<TRelationKind>() where TRelationKind : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
|
|
|
|
return RelationDepot.Relations<TRelationKind>();
|
|
|
|
|
}
|
2022-04-07 03:08:28 +00:00
|
|
|
|
|
2022-05-03 04:51:11 +00:00
|
|
|
|
protected bool Related<TRelationKind>(in Entity a, in Entity b) where TRelationKind : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
|
|
|
|
return RelationDepot.Related<TRelationKind>(a.ID, b.ID);
|
|
|
|
|
}
|
2022-04-06 19:53:50 +00:00
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
protected TRelationKind GetRelationData<TRelationKind>(in Entity a, in Entity b) where TRelationKind : unmanaged
|
|
|
|
|
{
|
|
|
|
|
return RelationDepot.Get<TRelationKind>(a, b);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// relations go A->B, so given A, will give all entities in outgoing relations of this kind.
|
|
|
|
|
protected ReverseSpanEnumerator<Entity> OutRelations<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2022-08-09 21:41:31 +00:00
|
|
|
|
return RelationDepot.OutRelations<TRelationKind>(entity.ID);
|
2022-04-08 05:52:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
protected Entity OutRelationSingleton<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
2022-04-08 05:52:03 +00:00
|
|
|
|
{
|
2022-08-09 21:41:31 +00:00
|
|
|
|
return RelationDepot.OutRelationSingleton<TRelationKind>(entity.ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected bool HasOutRelation<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
|
|
|
|
{
|
|
|
|
|
return RelationDepot.HasOutRelation<TRelationKind>(entity.ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected int OutRelationCount<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
|
|
|
|
{
|
|
|
|
|
return RelationDepot.OutRelationCount<TRelationKind>(entity.ID);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
// Relations go A->B, so given B, will give all entities in incoming A relations of this kind.
|
|
|
|
|
protected ReverseSpanEnumerator<Entity> InRelations<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
2022-08-09 21:41:31 +00:00
|
|
|
|
{
|
|
|
|
|
return RelationDepot.InRelations<TRelationKind>(entity.ID);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 00:41:00 +00:00
|
|
|
|
protected Entity InRelationSingleton<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
2022-08-09 21:41:31 +00:00
|
|
|
|
{
|
|
|
|
|
return RelationDepot.InRelationSingleton<TRelationKind>(entity.ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected bool HasInRelation<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
|
|
|
|
{
|
|
|
|
|
return RelationDepot.HasInRelation<TRelationKind>(entity.ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected int InRelationCount<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
|
|
|
|
|
{
|
|
|
|
|
return RelationDepot.InRelationCount<TRelationKind>(entity.ID);
|
2022-04-08 05:52:03 +00:00
|
|
|
|
}
|
2022-04-06 19:53:50 +00:00
|
|
|
|
}
|
2022-03-05 02:01:44 +00:00
|
|
|
|
}
|