|
|
|
@ -1,6 +1,7 @@ |
|
|
|
|
const std = @import("std"); |
|
|
|
|
const Allocator = std.mem.Allocator; |
|
|
|
|
|
|
|
|
|
const meta = @import("./meta.zig"); |
|
|
|
|
const flecs = @import("./main.zig"); |
|
|
|
|
const Path = flecs.Path; |
|
|
|
|
const c = flecs.c; |
|
|
|
@ -84,10 +85,10 @@ pub fn Entity(comptime ctx: anytype) type { |
|
|
|
|
/// To set their values you'll need to use `set(...)` on the |
|
|
|
|
/// `Entity` returned from this function. |
|
|
|
|
pub fn init(world: *World, config: Config, ids: anytype) !Self { |
|
|
|
|
const meta = @typeInfo(@TypeOf(ids)); |
|
|
|
|
if (meta != .Struct or (meta.Struct.is_tuple == false and meta.Struct.fields.len > 0)) |
|
|
|
|
const info = @typeInfo(@TypeOf(ids)); |
|
|
|
|
if (info != .Struct or (info.Struct.is_tuple == false and info.Struct.fields.len > 0)) |
|
|
|
|
@compileError("Expected tuple or empty struct, got '" ++ @typeName(@TypeOf(ids)) ++ "'"); |
|
|
|
|
if (meta.Struct.fields.len > c.FLECS_ID_DESC_MAX) |
|
|
|
|
if (info.Struct.fields.len > c.FLECS_ID_DESC_MAX) |
|
|
|
|
@compileError("Adding more than FLECS_ID_DESC_MAX ids"); |
|
|
|
|
|
|
|
|
|
var id = if (config.id) |i| i.raw else null; |
|
|
|
@ -303,7 +304,7 @@ pub fn Entity(comptime ctx: anytype) type { |
|
|
|
|
|
|
|
|
|
/// Gets a value copy of a component. |
|
|
|
|
/// If the component does not exist, returns null. |
|
|
|
|
pub fn get(self: Self, comptime T: type) ?T { |
|
|
|
|
pub fn get(self: Self, comptime T: anytype) ?meta.AnyToType(T) { |
|
|
|
|
return if (getRef(self, T)) |p| p.* else null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -313,8 +314,8 @@ pub fn Entity(comptime ctx: anytype) type { |
|
|
|
|
/// The returned pointer may become invalid after calling other Flecs |
|
|
|
|
/// functions, notably when an `Entity` moves to another table caused |
|
|
|
|
/// by adding or removing other components. |
|
|
|
|
pub fn getRef(self: Self, comptime T: type) ?*const T { |
|
|
|
|
const id = Context.lookup(T).*; |
|
|
|
|
pub fn getRef(self: Self, comptime T: anytype) ?*const meta.AnyToType(T) { |
|
|
|
|
const id = Context.anyToId(T); |
|
|
|
|
const ptr = c.ecs_get_id(self.world.raw, self.raw, id); |
|
|
|
|
return @alignCast(@ptrCast(ptr)); |
|
|
|
|
} |
|
|
|
@ -329,15 +330,15 @@ pub fn Entity(comptime ctx: anytype) type { |
|
|
|
|
/// If `getMut` is called when the world is in deferred / readonly |
|
|
|
|
/// mode, and the component does not yet exist, it will return a |
|
|
|
|
/// pointer to a temp storage. |
|
|
|
|
pub fn getMut(self: Self, comptime T: type) *T { |
|
|
|
|
const id = Context.lookup(T).*; |
|
|
|
|
pub fn getMut(self: Self, comptime T: anytype) *meta.AnyToType(T) { |
|
|
|
|
const id = Context.anyToId(T); |
|
|
|
|
const ptr = c.ecs_get_mut_id(self.world.raw, self.raw, id); |
|
|
|
|
return @alignCast(@ptrCast(ptr)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn set(self: Self, comptime T: type, value: T) void { |
|
|
|
|
const id = Context.lookup(T).*; |
|
|
|
|
_ = c.ecs_set_id(self.world.raw, self.raw, id, @sizeOf(T), &value); |
|
|
|
|
pub fn set(self: Self, comptime T: anytype, value: meta.AnyToType(T)) void { |
|
|
|
|
const id = Context.anyToId(T); |
|
|
|
|
_ = c.ecs_set_id(self.world.raw, self.raw, id, @sizeOf(@TypeOf(value)), &value); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|