From 4e101a16436e7252c07607ea7d14454a53c73622 Mon Sep 17 00:00:00 2001 From: copygirl Date: Sun, 19 Nov 2023 19:02:27 +0100 Subject: [PATCH] Add Entity.getTargets --- src/entity.zig | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/entity.zig b/src/entity.zig index f0e4502..e194814 100644 --- a/src/entity.zig +++ b/src/entity.zig @@ -259,6 +259,26 @@ pub fn Entity(comptime ctx: anytype) type { return if (result != 0) fromRaw(self.world, result) else null; } + /// Returns an iterator that yields each of the targets of the + /// specified `relation` that this `Entity` has, if any. + pub fn getTargets(self: Self, relation: anytype) TargetIterator { + const rel = util.anyToEntity(ctx, relation); + return .{ .world = self.world, .entity = self.raw, .relation = rel.raw }; + } + + pub const TargetIterator = struct { + world: World(ctx), + entity: c.ecs_entity_t, + relation: c.ecs_entity_t, + index: c_int = 0, + + pub fn next(self: *TargetIterator) ?Self { + var result = c.ecs_get_target(self.world.raw, self.entity, self.relation, self.index); + self.index += 1; + return if (result != 0) Self.fromRaw(self.world, result) else null; + } + }; + /// Returns whether this `Entity` has the specified value. /// /// `id` must be convertible to an id. See also: `util.anyToId(...)`.