Rename lookup to lookupByType, add lookupAlive

main
copygirl 1 year ago
parent bb5f20e810
commit 38baf0c54c
  1. 16
      src/world.zig
  2. 4
      test/world.zig

@ -38,12 +38,20 @@ pub fn World(comptime ctx: anytype) type {
return c.ecs_progress(self.raw, delta_time);
}
pub fn lookup(self: *Self, comptime T: type) ?Entity(ctx) {
const id = Lookup(ctx, T).id;
const result = Entity(ctx).fromRaw(self, id);
return if (result.isAlive()) result else null;
/// Returns an `Entity` for the specified `ecs_entity_t` value, or an
/// error if the entity is invalid or not alive in this `World`.
pub fn lookupAlive(self: *Self, id: c.ecs_entity_t) !Entity(ctx) {
return Entity(ctx).fromRaw(self, id).ensureAlive();
}
/// Returns the component `Entity` registered for the specified
/// type `T`, or an error if an association has not been made.
pub fn lookupByType(self: *Self, comptime T: type) !Entity(ctx) {
return lookupAlive(self, Lookup(ctx, T).id);
}
// TODO: Replace these functions with an entity builder interface.
pub fn entity(
self: *Self,
comptime add: []const type,

@ -57,8 +57,8 @@ test "World_progress_w_0" {
try expectEql(ctx.param, null);
try expectEql(ctx.e[0], e1.raw);
try expectEql(ctx.c[0][0], world.lookup(Position).?.raw);
try expectEql(ctx.c[0][1], world.lookup(Velocity).?.raw);
try expectEql(ctx.c[0][0], (try world.lookupByType(Position)).raw);
try expectEql(ctx.c[0][1], (try world.lookupByType(Velocity)).raw);
try expectEql(ctx.s[0][0], 0);
try expectEql(ctx.s[0][1], 0);

Loading…
Cancel
Save