From ad9b3f6736ce880ebb4e9853594aac503d0709a9 Mon Sep 17 00:00:00 2001 From: copygirl Date: Wed, 28 Dec 2022 12:12:49 +0100 Subject: [PATCH] Add Entity.GetTargets which returns IEnumerable Old GetTarget was removed / made private. --- src/gaemstone.ECS/EntityRef.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/gaemstone.ECS/EntityRef.cs b/src/gaemstone.ECS/EntityRef.cs index ce688ae..0ae590c 100644 --- a/src/gaemstone.ECS/EntityRef.cs +++ b/src/gaemstone.ECS/EntityRef.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using gaemstone.Utility; @@ -60,7 +61,7 @@ public unsafe class EntityRef public EntityRef? Parent - => GetTarget(World.ChildOf); + => GetTargets(World.ChildOf).FirstOrDefault(); public IEnumerable GetChildren() { @@ -139,12 +140,14 @@ public unsafe class EntityRef return this; } - public EntityRef? GetTarget(Entity relation, int index = 0) + private EntityRef? GetTarget(Entity relation, int index) => CreateOrNull(World, new(ecs_get_target(World, this, relation, index))); - public EntityRef? GetTarget(string symbol, int index = 0) - => GetTarget(World.LookupBySymbolOrThrow(symbol), index); - public EntityRef? GetTarget(int index = 0) - => GetTarget(World.LookupByTypeOrThrow(typeof(T)), index); + public IEnumerable GetTargets(Entity relation) + { var index = 0; while (GetTarget(relation, index++) is EntityRef target) yield return target; } + public IEnumerable GetTargets(string symbol) + => GetTargets(World.LookupBySymbolOrThrow(symbol)); + public IEnumerable GetTargets() + => GetTargets(World.LookupByTypeOrThrow(typeof(T))); public bool Equals(EntityRef? other) => (other is not null) && (World == other.World) && (Entity == other.Entity); public override bool Equals(object? obj) => Equals(obj as EntityRef);