MoonTools.ECS/src/EntityComponentReader.cs

84 lines
2.3 KiB
C#
Raw Normal View History

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-04-08 05:52:03 +00:00
internal EntityStorage EntityStorage;
internal ComponentDepot ComponentDepot;
internal RelationDepot RelationDepot;
protected FilterBuilder FilterBuilder => new FilterBuilder(ComponentDepot);
2022-03-05 02:01:44 +00:00
2022-04-08 05:52:03 +00:00
internal void RegisterEntityStorage(EntityStorage entityStorage)
{
EntityStorage = entityStorage;
}
2022-03-05 02:01:44 +00:00
2022-04-08 05:52:03 +00:00
internal void RegisterComponentDepot(ComponentDepot componentDepot)
{
ComponentDepot = componentDepot;
}
2022-04-06 19:53:50 +00:00
2022-04-08 05:52:03 +00:00
internal void RegisterRelationDepot(RelationDepot relationDepot)
{
RelationDepot = relationDepot;
}
2022-03-05 02:01:44 +00:00
2022-04-08 05:52:03 +00:00
protected ReadOnlySpan<TComponent> ReadComponents<TComponent>() where TComponent : struct
{
return ComponentDepot.ReadComponents<TComponent>();
}
2022-03-05 02:01:44 +00:00
2022-04-08 05:52:03 +00:00
protected bool Has<TComponent>(in Entity entity) where TComponent : struct
{
return ComponentDepot.Has<TComponent>(entity.ID);
}
2022-03-05 02:01:44 +00:00
2022-04-08 05:52:03 +00:00
protected bool Some<TComponent>() where TComponent : struct
{
return ComponentDepot.Some<TComponent>();
}
2022-03-21 23:21:42 +00:00
2022-04-08 05:52:03 +00:00
protected ref readonly TComponent Get<TComponent>(in Entity entity) where TComponent : struct
{
return ref ComponentDepot.Get<TComponent>(entity.ID);
}
2022-03-21 23:21:42 +00:00
2022-04-08 05:52:03 +00:00
protected ref readonly TComponent GetSingleton<TComponent>() where TComponent : struct
{
return ref ComponentDepot.Get<TComponent>();
}
2022-04-08 05:52:03 +00:00
protected Entity GetSingletonEntity<TComponent>() where TComponent : struct
{
return ComponentDepot.GetSingletonEntity<TComponent>();
}
2022-04-06 19:53:50 +00:00
2022-04-08 05:52:03 +00:00
protected bool Exists(in Entity entity)
{
return EntityStorage.Exists(entity);
}
2022-04-06 19:53:50 +00:00
2022-04-19 19:35:21 +00:00
protected IEnumerable<(Entity, Entity, TRelationKind)> Relations<TRelationKind>() where TRelationKind : struct
2022-04-08 05:52:03 +00:00
{
return RelationDepot.Relations<TRelationKind>();
}
2022-04-07 03:08:28 +00:00
2022-04-19 19:35:21 +00:00
protected bool Related<TRelationKind>(in Entity a, in Entity b) where TRelationKind : struct
2022-04-08 05:52:03 +00:00
{
return RelationDepot.Related<TRelationKind>(a.ID, b.ID);
}
2022-04-06 19:53:50 +00:00
2022-04-19 19:35:21 +00:00
protected IEnumerable<(Entity, TRelationKind)> RelatedToA<TRelationKind>(in Entity entity) where TRelationKind : struct
2022-04-08 05:52:03 +00:00
{
return RelationDepot.RelatedToA<TRelationKind>(entity.ID);
}
2022-04-19 19:35:21 +00:00
protected IEnumerable<(Entity, TRelationKind)> RelatedToB<TRelationKind>(in Entity entity) where TRelationKind : struct
2022-04-08 05:52:03 +00:00
{
return RelationDepot.RelatedToB<TRelationKind>(entity.ID);
}
2022-04-06 19:53:50 +00:00
}
2022-03-05 02:01:44 +00:00
}