get relation by index

pull/4/head
cosmonaut 2023-03-20 14:26:27 -07:00
parent 1a6d015fff
commit 3817a9e809
3 changed files with 34 additions and 2 deletions

View File

@ -78,6 +78,12 @@ namespace MoonTools.ECS
return RelationDepot.OutRelationSingleton<TRelationKind>(entity.ID);
}
// NOTE: this WILL crash if at least n + 1 relations do not exist!
protected Entity NthOutRelation<TRelationKind>(in Entity entity, int n) where TRelationKind : unmanaged
{
return RelationDepot.NthOutRelation<TRelationKind>(entity.ID, n);
}
protected bool HasOutRelation<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
{
return RelationDepot.HasOutRelation<TRelationKind>(entity.ID);
@ -99,6 +105,12 @@ namespace MoonTools.ECS
return RelationDepot.InRelationSingleton<TRelationKind>(entity.ID);
}
// NOTE: this WILL crash if at least n + 1 relations do not exist!
protected Entity NthInRelation<TRelationKind>(in Entity entity, int n) where TRelationKind : unmanaged
{
return RelationDepot.NthInRelation<TRelationKind>(entity.ID, n);
}
protected bool HasInRelation<TRelationKind>(in Entity entity) where TRelationKind : unmanaged
{
return RelationDepot.HasInRelation<TRelationKind>(entity.ID);

View File

@ -76,6 +76,11 @@ namespace MoonTools.ECS
return Lookup<TRelationKind>().OutFirst(entityID);
}
public Entity NthOutRelation<TRelationKind>(int entityID, int n) where TRelationKind : unmanaged
{
return Lookup<TRelationKind>().OutNth(entityID, n);
}
public int OutRelationCount<TRelationKind>(int entityID) where TRelationKind : unmanaged
{
return Lookup<TRelationKind>().OutRelationCount(entityID);
@ -91,6 +96,11 @@ namespace MoonTools.ECS
return Lookup<TRelationKind>().InRelations(entityID);
}
public Entity NthInRelation<TRelationKind>(int entityID, int n) where TRelationKind : unmanaged
{
return Lookup<TRelationKind>().InNth(entityID, n);
}
public Entity InRelationSingleton<TRelationKind>(int entityID) where TRelationKind : unmanaged
{
return Lookup<TRelationKind>().InFirst(entityID);

View File

@ -92,13 +92,18 @@ namespace MoonTools.ECS
public Entity OutFirst(int entityID)
{
return OutNth(entityID, 0);
}
public Entity OutNth(int entityID, int n)
{
#if DEBUG
if (!outRelations.ContainsKey(entityID) || outRelations[entityID].Count == 0)
{
throw new KeyNotFoundException("No out relations to this entity!");
}
#endif
return outRelations[entityID][0];
return outRelations[entityID][n];
}
public bool HasOutRelation(int entityID)
@ -125,6 +130,11 @@ namespace MoonTools.ECS
public Entity InFirst(int entityID)
{
return InNth(entityID, 0);
}
public Entity InNth(int entityID, int n)
{
#if DEBUG
if (!inRelations.ContainsKey(entityID) || inRelations[entityID].Count == 0)
{
@ -132,7 +142,7 @@ namespace MoonTools.ECS
}
#endif
return inRelations[entityID][0];
return inRelations[entityID][n];
}
public bool HasInRelation(int entityID)