fixed ecs components issue

This commit is contained in:
Peter Li 2025-05-17 01:26:02 -07:00
parent 5c1f349673
commit 63fe7961b1
6 changed files with 33 additions and 6 deletions

View File

@ -112,16 +112,21 @@ var gEcsRegistry: *EcsRegistry = undefined;
// SparseMultiSet (AOS version of sparseSet Really specialized, only used for core engine systems) // SparseMultiSet (AOS version of sparseSet Really specialized, only used for core engine systems)
pub fn createEntity() !Entity { pub fn createEntity() !Entity {
return .{ .handle = try gEcsRegistry.baseSet.createObject(.{}) }; const rv = Entity{ .handle = try gEcsRegistry.baseSet.createObject(.{}) };
core.engine_log("entity created: {d}", .{rv.handle.index});
return rv;
} }
pub fn destroyEntity(e: Entity) void { pub fn destroyEntity(e: Entity) void {
core.engine_log("entity destroyed: {d}", .{e.handle.index});
if (gEcsRegistry.baseSet.get(e.handle)) |entityEntry| { if (gEcsRegistry.baseSet.get(e.handle)) |entityEntry| {
for (entityEntry.containers.items) |ref| { for (entityEntry.containers.items) |ref| {
ref.vtable.destroyObject(ref.ptr, e.handle); ref.vtable.destroyObject(ref.ptr, e.handle);
} }
entityEntry.containers.deinit(gEcsRegistry.allocator); entityEntry.containers.deinit(gEcsRegistry.allocator);
gEcsRegistry.baseSet.destroyObject(e.handle); gEcsRegistry.baseSet.destroyObject(e.handle);
} else {
core.engine_log("UNABLE TO DESTROY ENTITY {d}", .{e.handle.index});
} }
} }
@ -317,6 +322,7 @@ pub const Entity = struct {
} }
pub fn addComponent(self: @This(), comptime Component: type) ?*Component { pub fn addComponent(self: @This(), comptime Component: type) ?*Component {
core.engine_log("adding component: {d} {s}", .{ self.handle.index, @typeName(Component) });
const rv = Component.BaseContainer.createWithHandleECS(self.handle); const rv = Component.BaseContainer.createWithHandleECS(self.handle);
const list = &gEcsRegistry.baseSet.get(self.handle).?.containers; const list = &gEcsRegistry.baseSet.get(self.handle).?.containers;

View File

@ -128,6 +128,11 @@ pub const Scene = struct {
_ = SceneObjectContainer.createWithHandleECS(handle); _ = SceneObjectContainer.createWithHandleECS(handle);
} }
pub fn deinitECS(self: *@This(), handle: core.ObjectHandle) void {
_ = self;
_ = SceneObjectContainer.destroyObject(handle);
}
pub fn printHandleIndex(self: @This()) void { pub fn printHandleIndex(self: @This()) void {
core.engine_log("handle.index = 0x{x} generation = {d} alive={any}", .{ self.handle.index, self.handle.generation, self.handle.alive }); core.engine_log("handle.index = 0x{x} generation = {d} alive={any}", .{ self.handle.index, self.handle.generation, self.handle.alive });
} }

View File

@ -55,9 +55,8 @@ pub const MeshPool = struct {
var iter = self.invalidations.iterator(); var iter = self.invalidations.iterator();
while (iter.next()) |n| { while (iter.next()) |n| {
const invalidate = n.value_ptr; const invalidate = n.value_ptr;
_ = invalidate; self.indexSpans.removeSpan(invalidate.index);
// self.indexSpans.removeSpan(invalidate.index); self.vertexSpans.removeSpan(invalidate.vertex);
// self.vertexSpans.removeSpan(invalidate.vertex);
} }
self.invalidations.clearRetainingCapacity(); self.invalidations.clearRetainingCapacity();

View File

@ -18,6 +18,7 @@ pub const TBMap = struct {
pub fn create(allocator: std.mem.Allocator) !*@This() { pub fn create(allocator: std.mem.Allocator) !*@This() {
const self = try allocator.create(@This()); const self = try allocator.create(@This());
core.engine_logs("creating map root");
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.root = try core.createEntity(), .root = try core.createEntity(),
@ -131,8 +132,10 @@ pub const TBMap = struct {
// physics.gPhysicsRuntime.shapes.get(name.handle()).?.shape.release(); // physics.gPhysicsRuntime.shapes.get(name.handle()).?.shape.release();
} }
self.colliderSpecs.deinit(self.allocator); self.colliderSpecs.deinit(self.allocator);
self.allocator.destroy(self);
self.root.destroy(); self.root.destroy();
self.allocator.destroy(self);
} }
}; };

View File

@ -199,7 +199,7 @@ pub fn SparseMultiSetAdvanced(comptime T: type, comptime SparseSize: u32) type {
return self.createObjectInternal(initValue, handle.index, handle.generation); return self.createObjectInternal(initValue, handle.index, handle.generation);
} }
pub fn destroyObject(self: *@This(), handle: SetHandle) false { pub fn destroyObject(self: *@This(), handle: SetHandle) bool {
// to destroy an object // to destroy an object
// get handle and get the dense position, swap and remove. // get handle and get the dense position, swap and remove.
// Then insert the tombstone value into the sparse handle // Then insert the tombstone value into the sparse handle
@ -225,6 +225,8 @@ pub fn SparseMultiSetAdvanced(comptime T: type, comptime SparseSize: u32) type {
if (self.containerListener) |l| { if (self.containerListener) |l| {
l.onHandleRemoved(l.ptr, self.containerID, handle); l.onHandleRemoved(l.ptr, self.containerID, handle);
} }
return true;
} }
var prng = std.Random.DefaultPrng.init(0x1234); var prng = std.Random.DefaultPrng.init(0x1234);
@ -389,6 +391,11 @@ pub fn SparseSetAdvanced(comptime T: type, comptime SparseSize: u32) type {
// to destroy an object // to destroy an object
// get handle and get the dense position, swap and remove. // get handle and get the dense position, swap and remove.
// Then insert the tombstone value into the sparse handle // Then insert the tombstone value into the sparse handle
if (@hasDecl(T, "deinitECS")) {
self.get(handle).?.deinitECS(handle);
}
const denseIndex = self.sparseToDense(handle) orelse return; const denseIndex = self.sparseToDense(handle) orelse return;
const tailDenseIndex = self.dense.items.len - 1; const tailDenseIndex = self.dense.items.len - 1;
const sparseIndexToSwap = self.dense.items[tailDenseIndex].sparseIndex; const sparseIndexToSwap = self.dense.items[tailDenseIndex].sparseIndex;
@ -608,6 +615,11 @@ pub fn SparseMap(comptime T: type) type {
pub fn destroyObject(self: *@This(), handle: SetHandle) void { pub fn destroyObject(self: *@This(), handle: SetHandle) void {
std.debug.assert(self.map.contains(handle)); std.debug.assert(self.map.contains(handle));
if (@hasDecl(T, "deinitECS")) {
self.map.get(handle).?.deinitECS(handle);
}
const alloc = self.allocator(); const alloc = self.allocator();
const index = self.listEntriesByHandle.get(handle).?; const index = self.listEntriesByHandle.get(handle).?;

View File

@ -366,6 +366,8 @@ pub fn tick(self: *@This(), dt: f64) void {
_ = ig.dockSpaceOverViewport(ig.getMainViewport(), .{ .passthru_central_node = true }, null); _ = ig.dockSpaceOverViewport(ig.getMainViewport(), .{ .passthru_central_node = true }, null);
self.loadMap2() catch unreachable;
self.fileWatch -= dt; self.fileWatch -= dt;
if (self.fileWatch < 0) { if (self.fileWatch < 0) {
self.fileWatch = 3.0; self.fileWatch = 3.0;