diff --git a/src/EntityComponentReader.cs b/src/EntityComponentReader.cs index e55f6dd..b752d43 100644 --- a/src/EntityComponentReader.cs +++ b/src/EntityComponentReader.cs @@ -78,6 +78,12 @@ namespace MoonTools.ECS return RelationDepot.OutRelationSingleton(entity.ID); } + // NOTE: this WILL crash if at least n + 1 relations do not exist! + protected Entity NthOutRelation(in Entity entity, int n) where TRelationKind : unmanaged + { + return RelationDepot.NthOutRelation(entity.ID, n); + } + protected bool HasOutRelation(in Entity entity) where TRelationKind : unmanaged { return RelationDepot.HasOutRelation(entity.ID); @@ -99,6 +105,12 @@ namespace MoonTools.ECS return RelationDepot.InRelationSingleton(entity.ID); } + // NOTE: this WILL crash if at least n + 1 relations do not exist! + protected Entity NthInRelation(in Entity entity, int n) where TRelationKind : unmanaged + { + return RelationDepot.NthInRelation(entity.ID, n); + } + protected bool HasInRelation(in Entity entity) where TRelationKind : unmanaged { return RelationDepot.HasInRelation(entity.ID); diff --git a/src/RelationDepot.cs b/src/RelationDepot.cs index 06bb2fe..860826d 100644 --- a/src/RelationDepot.cs +++ b/src/RelationDepot.cs @@ -76,6 +76,11 @@ namespace MoonTools.ECS return Lookup().OutFirst(entityID); } + public Entity NthOutRelation(int entityID, int n) where TRelationKind : unmanaged + { + return Lookup().OutNth(entityID, n); + } + public int OutRelationCount(int entityID) where TRelationKind : unmanaged { return Lookup().OutRelationCount(entityID); @@ -91,6 +96,11 @@ namespace MoonTools.ECS return Lookup().InRelations(entityID); } + public Entity NthInRelation(int entityID, int n) where TRelationKind : unmanaged + { + return Lookup().InNth(entityID, n); + } + public Entity InRelationSingleton(int entityID) where TRelationKind : unmanaged { return Lookup().InFirst(entityID); diff --git a/src/RelationStorage.cs b/src/RelationStorage.cs index eec556f..009ffe3 100644 --- a/src/RelationStorage.cs +++ b/src/RelationStorage.cs @@ -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)