using System; using System.Collections.Generic; namespace MoonTools.ECS { public abstract class EntityComponentReader { internal readonly World World; internal EntityStorage EntityStorage => World.EntityStorage; internal ComponentDepot ComponentDepot => World.ComponentDepot; internal RelationDepot RelationDepot => World.RelationDepot; protected FilterBuilder FilterBuilder => new FilterBuilder(FilterStorage, ComponentTypeIndices); internal FilterStorage FilterStorage => World.FilterStorage; internal TypeIndices ComponentTypeIndices => World.ComponentTypeIndices; internal TypeIndices RelationTypeIndices => World.RelationTypeIndices; public EntityComponentReader(World world) { World = world; } protected ReadOnlySpan ReadComponents() where TComponent : unmanaged { return ComponentDepot.ReadComponents(); } protected bool Has(in Entity entity) where TComponent : unmanaged { var storageIndex = ComponentTypeIndices.GetIndex(); return EntityStorage.HasComponent(entity.ID, storageIndex); } protected bool Some() where TComponent : unmanaged { return ComponentDepot.Some(); } protected ref readonly TComponent Get(in Entity entity) where TComponent : unmanaged { return ref ComponentDepot.Get(entity.ID); } protected ref readonly TComponent GetSingleton() where TComponent : unmanaged { return ref ComponentDepot.GetFirst(); } protected Entity GetSingletonEntity() where TComponent : unmanaged { return ComponentDepot.GetSingletonEntity(); } protected bool Exists(in Entity entity) { return EntityStorage.Exists(entity); } protected IEnumerable<(Entity, Entity, TRelationKind)> Relations() where TRelationKind : unmanaged { return RelationDepot.Relations(); } protected bool Related(in Entity a, in Entity b) where TRelationKind : unmanaged { return RelationDepot.Related(a.ID, b.ID); } // relations go A->B, so given A, will give all outgoing B relations. protected IEnumerable<(Entity, TRelationKind)> OutRelations(in Entity entity) where TRelationKind : unmanaged { return RelationDepot.OutRelations(entity.ID); } protected (Entity, TRelationKind) OutRelationSingleton(in Entity entity) where TRelationKind : unmanaged { return RelationDepot.OutRelationSingleton(entity.ID); } protected bool HasOutRelation(in Entity entity) where TRelationKind : unmanaged { return RelationDepot.HasOutRelation(entity.ID); } protected int OutRelationCount(in Entity entity) where TRelationKind : unmanaged { return RelationDepot.OutRelationCount(entity.ID); } // Relations go A->B, so given B, will give all incoming A relations. protected IEnumerable<(Entity, TRelationKind)> InRelations(in Entity entity) where TRelationKind : unmanaged { return RelationDepot.InRelations(entity.ID); } protected (Entity, TRelationKind) InRelationSingleton(in Entity entity) where TRelationKind : unmanaged { return RelationDepot.InRelationSingleton(entity.ID); } protected bool HasInRelation(in Entity entity) where TRelationKind : unmanaged { return RelationDepot.HasInRelation(entity.ID); } protected int InRelationCount(in Entity entity) where TRelationKind : unmanaged { return RelationDepot.InRelationCount(entity.ID); } } }