|
|
|
@ -130,7 +130,7 @@ pub fn Entity(comptime ctx: anytype) type { |
|
|
|
|
.name => |n| { |
|
|
|
|
const found = c.ecs_lookup_child(world.raw, parent_, n.ptr); |
|
|
|
|
if (found == 0) { |
|
|
|
|
var desc = std.mem.zeroInit(c.ecs_entity_desc_t, .{ .sep = "".ptr, .name = n.ptr }); |
|
|
|
|
var desc = c.ecs_entity_desc_t{ .sep = "".ptr, .name = n.ptr }; |
|
|
|
|
desc.add[0] = c.ecs_pair(c.EcsChildOf, parent_); |
|
|
|
|
scope = c.ecs_entity_init(world.raw, &desc); |
|
|
|
|
if (scope == 0) return errors.getLastErrorOrUnknown(); |
|
|
|
@ -145,13 +145,13 @@ pub fn Entity(comptime ctx: anytype) type { |
|
|
|
|
const previous = if (scope) |s| world.setScope(s) else null; |
|
|
|
|
defer _ = if (scope != null) world.setScope(previous); |
|
|
|
|
|
|
|
|
|
var desc = std.mem.zeroInit(c.ecs_entity_desc_t, .{ |
|
|
|
|
var desc = c.ecs_entity_desc_t{ |
|
|
|
|
.sep = "".ptr, // Disable tokenization. |
|
|
|
|
.id = if (id) |i| i else 0, |
|
|
|
|
.name = if (name_) |n| n.ptr else null, |
|
|
|
|
.symbol = if (config.symbol) |s| s.ptr else null, |
|
|
|
|
.use_low_id = config.use_low_id, |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
inline for (ids, 0..) |a, i| |
|
|
|
|
desc.add[i] = Context.anyToId(a); |
|
|
|
@ -227,7 +227,7 @@ pub fn Entity(comptime ctx: anytype) type { |
|
|
|
|
/// the entity is alive and its name doesn't change. |
|
|
|
|
pub fn name(self: Self) ?[:0]const u8 { |
|
|
|
|
const result = c.ecs_get_name(self.world.raw, self.raw); |
|
|
|
|
return std.mem.sliceTo(result, 0); |
|
|
|
|
return if (result != 0) std.mem.sliceTo(result, 0) else null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Sets the name of this `Entity` to the specified value, if any. |
|
|
|
@ -243,7 +243,7 @@ pub fn Entity(comptime ctx: anytype) type { |
|
|
|
|
/// the entity is alive and its symbol doesn't change. |
|
|
|
|
pub fn symbol(self: Self) ?[:0]const u8 { |
|
|
|
|
const result = c.ecs_get_symbol(self.world.raw, self.raw); |
|
|
|
|
return std.mem.sliceTo(result, 0); |
|
|
|
|
return if (result != 0) std.mem.sliceTo(result, 0) else null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Sets the symbol of this `Entity` to the specified value, if any. |
|
|
|
@ -340,12 +340,70 @@ pub fn Entity(comptime ctx: anytype) type { |
|
|
|
|
const id = Context.anyToId(T); |
|
|
|
|
_ = c.ecs_set_id(self.world.raw, self.raw, id, @sizeOf(@TypeOf(value)), &value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// The `fmt` parameter can be one of: |
|
|
|
|
/// - `d`, `x` or `X`: Output entity ID as decimal or hexadecimal. |
|
|
|
|
/// - `s`: Output entity name, if any, otherwise falls back to `d`. |
|
|
|
|
/// - `any`: Output full entity path with `Path.FormatOptions.default`. |
|
|
|
|
/// - Other `Path.FormatOptions` are also supported (such as `flecs_c`). |
|
|
|
|
pub fn format( |
|
|
|
|
self: Self, |
|
|
|
|
comptime fmt: []const u8, |
|
|
|
|
options: std.fmt.FormatOptions, |
|
|
|
|
writer: anytype, |
|
|
|
|
) !void { |
|
|
|
|
if (fmt.len == 1 and (fmt[0] == 'd' or fmt[0] == 'x' or fmt[0] == 'X')) { |
|
|
|
|
try std.fmt.formatIntValue(self.entityId(), fmt, options, writer); |
|
|
|
|
} else if (fmt.len == 1 and fmt[0] == 's') { |
|
|
|
|
if (self.name()) |n| |
|
|
|
|
try writer.writeAll(n) |
|
|
|
|
else |
|
|
|
|
try self.format("d", options, writer); |
|
|
|
|
} else { |
|
|
|
|
const opt = Path.fmtToFormatOptions(fmt); |
|
|
|
|
if (self.parent()) |p| { |
|
|
|
|
try p.format(fmt, .{}, writer); |
|
|
|
|
try writer.writeAll(opt.sep); |
|
|
|
|
} else if (opt.root_sep) |sep| |
|
|
|
|
try writer.writeAll(sep); |
|
|
|
|
try self.format("s", .{}, writer); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const expect = @import("./test/expect.zig"); |
|
|
|
|
const flecszigble = @import("./main.zig"); |
|
|
|
|
|
|
|
|
|
test "Entity with name and symbol" { |
|
|
|
|
flecszigble.init(std.testing.allocator); |
|
|
|
|
var world = try flecszigble.World(void).initMinimal(); |
|
|
|
|
defer world.deinit(); |
|
|
|
|
|
|
|
|
|
const entity = try world.entity(.{ .name = "name", .symbol = "symbol" }, .{}); |
|
|
|
|
|
|
|
|
|
try expect.equalStrings("name", entity.name()); |
|
|
|
|
try expect.equalStrings("symbol", entity.symbol()); |
|
|
|
|
|
|
|
|
|
try expect.equal(entity, world.lookupChild(null, "name")); |
|
|
|
|
try expect.equal(entity, world.lookupSymbol("symbol")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
test "Entity get and set name" { |
|
|
|
|
flecszigble.init(std.testing.allocator); |
|
|
|
|
var world = try flecszigble.World(void).initMinimal(); |
|
|
|
|
defer world.deinit(); |
|
|
|
|
|
|
|
|
|
const entity = try world.entity(.{}, .{}); |
|
|
|
|
try expect.equal(null, entity.name()); |
|
|
|
|
|
|
|
|
|
entity.setName("hello"); |
|
|
|
|
try expect.equalStrings("hello", entity.name()); |
|
|
|
|
|
|
|
|
|
entity.setName(null); |
|
|
|
|
try expect.equal(null, entity.name()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
test "Entity get and set" { |
|
|
|
|
flecszigble.init(std.testing.allocator); |
|
|
|
|
var world = try flecszigble.World(void).initMinimal(); |
|
|
|
@ -418,3 +476,34 @@ test "Entity set and get with pair type" { |
|
|
|
|
try expect.equal(.{ .value = 9001 }, entity.get(.{ Rank, Position })); |
|
|
|
|
try expect.equal(.{ .x = 60, .y = 80 }, entity.get(.{ Copy, Position })); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
test "Entity format" { |
|
|
|
|
flecszigble.init(std.testing.allocator); |
|
|
|
|
var world = try flecszigble.World(void).initMinimal(); |
|
|
|
|
defer world.deinit(); |
|
|
|
|
|
|
|
|
|
const grandparent = try world.entity(.{ .name = "grandparent" }, .{}); |
|
|
|
|
const parent = try world.entity(.{ .name = "parent", .parent = grandparent }, .{}); |
|
|
|
|
const child = try world.entity(.{ .name = "child", .parent = parent }, .{}); |
|
|
|
|
|
|
|
|
|
try expect.fmt("410", "{d}", .{grandparent}); |
|
|
|
|
try expect.fmt("411", "{d}", .{parent}); |
|
|
|
|
try expect.fmt("412", "{d}", .{child}); |
|
|
|
|
|
|
|
|
|
try expect.fmt("grandparent", "{s}", .{grandparent}); |
|
|
|
|
try expect.fmt("parent", "{s}", .{parent}); |
|
|
|
|
try expect.fmt("child", "{s}", .{child}); |
|
|
|
|
|
|
|
|
|
try expect.fmt("grandparent", "{}", .{grandparent}); |
|
|
|
|
try expect.fmt("grandparent.parent", "{}", .{parent}); |
|
|
|
|
try expect.fmt("grandparent.parent.child", "{}", .{child}); |
|
|
|
|
|
|
|
|
|
try expect.fmt("/grandparent", "{unix}", .{grandparent}); |
|
|
|
|
try expect.fmt("/grandparent/parent", "{unix}", .{parent}); |
|
|
|
|
try expect.fmt("/grandparent/parent/child", "{unix}", .{child}); |
|
|
|
|
|
|
|
|
|
parent.setName(null); |
|
|
|
|
try expect.fmt("411", "{s}", .{parent}); |
|
|
|
|
try expect.fmt("/grandparent/411", "{unix}", .{parent}); |
|
|
|
|
try expect.fmt("/grandparent/411/child", "{unix}", .{child}); |
|
|
|
|
} |
|
|
|
|