Add Entity.add and .remove functions

main
copygirl 8 months ago
parent 09b018ecec
commit 4001570462
  1. 28
      src/entity.zig

@ -78,16 +78,16 @@ pub fn Entity(comptime ctx: anytype) type {
/// existing entity `.id` is specified but its `.name` doesn't match,
/// an error is returned.
///
/// `add` is a tuple that specifies `Id`s added to the entity.
/// `ids` is a tuple that specifies `Id`s added to the entity.
/// For example: `.{ Position, Velocity, .{ ChildOf, parent } }`.
///
/// When adding components, they are just default-initialized.
/// To set their values you'll need to use `set(...)` on the
/// `Entity` returned from this function.
pub fn init(world: *World(ctx), config: Config, add: anytype) !Self {
pub fn init(world: *World(ctx), config: Config, ids: anytype) !Self {
const meta = @typeInfo(@TypeOf(add));
if (meta != .Struct or (meta.Struct.is_tuple == false and meta.Struct.fields.len > 0))
@compileError("Expected tuple or empty struct, got '" ++ @typeName(@TypeOf(add)) ++ "'");
@compileError("Expected tuple or empty struct, got '" ++ @typeName(@TypeOf(ids)) ++ "'");
if (meta.Struct.fields.len > c.FLECS_ID_DESC_MAX)
@compileError("Adding more than FLECS_ID_DESC_MAX ids");
@ -153,10 +153,10 @@ pub fn Entity(comptime ctx: anytype) type {
.use_low_id = config.use_low_id,
});
inline for (add, 0..) |a, i|
inline for (ids, 0..) |a, i|
desc.add[i] = util.anyToId(ctx, a);
for (desc.add[0..add.len]) |i|
for (desc.add[0..ids.len]) |i|
if (c.ecs_id_is_pair(i) and c.ECS_PAIR_FIRST(i) == c.EcsChildOf)
if (config.parent != null or config.path != null)
return error.FoundChildOf;
@ -261,10 +261,20 @@ pub fn Entity(comptime ctx: anytype) type {
/// Returns whether this `Entity` has the specified value.
///
/// `value` must be convertible to an id. See also: `util.anyToId(...)`.
pub fn has(self: Self, value: anytype) bool {
const id = util.anyToId(ctx, value);
return c.ecs_has_id(self.world.raw, self.raw, id);
/// `id` must be convertible to an id. See also: `util.anyToId(...)`.
pub fn has(self: Self, id: anytype) bool {
const ecs_id = util.anyToId(ctx, id);
return c.ecs_has_id(self.world.raw, self.raw, ecs_id);
}
pub fn add(self: Self, id: anytype) void {
const ecs_id = util.anyToId(ctx, id);
c.ecs_add_id(self.world.raw, self.raw, ecs_id);
}
pub fn remove(self: Self, id: anytype) void {
const ecs_id = util.anyToId(ctx, id);
c.ecs_remove_id(self.world.raw, self.raw, ecs_id);
}
pub fn get(self: Self, comptime T: type) ?*const T {

Loading…
Cancel
Save