diff options
author | sanine <sanine.not@pm.me> | 2023-04-15 01:56:14 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-04-15 01:56:14 -0500 |
commit | e603997055259039cefcdceaece5604e3856e36d (patch) | |
tree | f09e2c8984b0e61438ac9f407fe9f8ab0b91fd9c /honey | |
parent | f41a96c6669354ae49b6b5e42830d010571f8a36 (diff) |
fix circular reference bug for physics objects
Diffstat (limited to 'honey')
-rw-r--r-- | honey/ecs-systems.lua | 24 | ||||
-rw-r--r-- | honey/ecs.lua | 56 |
2 files changed, 20 insertions, 60 deletions
diff --git a/honey/ecs-systems.lua b/honey/ecs-systems.lua index 54bac89..47c0f6d 100644 --- a/honey/ecs-systems.lua +++ b/honey/ecs-systems.lua @@ -157,9 +157,11 @@ end --===== physics =====-- + physics = function(params) local interval = params.interval or 0.016 local groupSize = params.groupSize or 20 + local refs = {} return { db=params.db, space=params.space, @@ -169,14 +171,21 @@ physics = function(params) priority=0, update=function(self, dt) + for i, ref in ipairs(refs) do + print(i, ref.tbl, ref.physics) + end local query = self.db:queryComponent("physics") + for id, physics in pairs(query) do if not physics._body then - physics._body = ode.BodyCreate(self.world) + print("add physics body for "..id) + local body = ode.BodyCreate(self.world) physics._gc = honey.util.gc_canary(function() print("releasing physics body for " .. id) - ode.BodyDestroy(physics._body) + ode.BodyDestroy(body) + body = nil end) + local mass = ode.MassCreate() local class = physics.mass.class if not class then @@ -188,30 +197,31 @@ physics = function(params) physics.mass.radius ) end - ode.BodySetMass(physics._body, mass) + ode.BodySetMass(body, mass) local m = self.db:getComponent(id, "transform").matrix ode.BodySetPosition( - physics._body, + body, m[1][4], m[2][4], m[3][4] ) ode.BodySetRotation( - physics._body, + body, m[1][1], m[1][2], m[1][3], m[2][1], m[2][2], m[2][3], m[3][1], m[3][2], m[3][3] ) ode.BodySetLinearVel( - physics._body, + body, physics.velocity[1], physics.velocity[2], physics.velocity[3] ) ode.BodySetAngularVel( - physics._body, + body, physics.angularVelocity[1], physics.angularVelocity[2], physics.angularVelocity[3] ) + physics._body = body end end diff --git a/honey/ecs.lua b/honey/ecs.lua index d544a37..260c389 100644 --- a/honey/ecs.lua +++ b/honey/ecs.lua @@ -77,11 +77,13 @@ end -- load database from file function EntityDb.load(self, filename) + print(collectgarbage("count")) self.entities = {} self.components = {} + collectgarbage() + print(collectgarbage("count")) local env = { Entity = function(id, components) - print("add entity:", id) self:createEntity(id) self:addComponents(id, components) end, @@ -261,56 +263,4 @@ function SystemDb.removeSystem(self, id) end ---===== Database =====-- - -Database = {} -Database.__index = Database - -function Database.new(_) - local self = {} - self.entityDb = EntityDb() - self.systemDb = SystemDb(self.entityDb) - setmetatable(self, Database) - return self -end -setmetatable(Database, {__call=Database.new}) - - -function Database.isValidEntity(self, id) - return self.entityDb:checkIsValid(id) -end -function Database.createEntity(self, id) - return self.entityDb:createEntity(id) -end -function Database.addComponent(self, id, name, value) - return self.entityDb:addComponent(id, name, value) -end -function Database.queryComponent(self, name) - return self.entityDb:queryComponent(name) -end -function Database.queryEntity(self, id) - return self.entityDb:queryEntity(id) -end -function Database.getComponent(self, id, name) - return self.entityDb:getComponent(id, name) -end -function Database.removeComponent(self, id, name) - return self.entityDb:removeComponent(id, name) -end -function Database.deleteEntity(self, id) - return self.entityDb.deleteEntity(id) -end - - -function Database.addSystem(self, func, params) - return self.systemDb:addSystem(func, params) -end -function Database.update(self, dt) - return self.systemDb:update(dt) -end -function Database.removeSystem(self, id) - return self.systemDb:removeSystem(id) -end - - return module |