diff options
author | sanine <sanine.not@pm.me> | 2023-03-24 20:45:41 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-03-24 20:45:41 -0500 |
commit | 314e9cd4c96f30f47b6c02eefbadf063e1a58025 (patch) | |
tree | 8dcb33b32cdcf1a903d6a0ae750fd23458d86729 | |
parent | 627ca789065bfcbce53a51887be4e8f8fb20d26b (diff) |
fix ecs removing
-rw-r--r-- | honey/ecs.lua | 11 | ||||
-rw-r--r-- | main.lua | 48 |
2 files changed, 38 insertions, 21 deletions
diff --git a/honey/ecs.lua b/honey/ecs.lua index b7806f9..3632f34 100644 --- a/honey/ecs.lua +++ b/honey/ecs.lua @@ -152,8 +152,8 @@ end function Level.addEntity(self, entity) - table.insert(self.entities, entity) local id = self.nextId + self.entities[id] = entity self.nextId = id + 1 for _, system in ipairs(self.systems) do @@ -181,6 +181,7 @@ end function Level.removeEntity(self, id) local entity = self.entities[id] + if not entity then error("bad id: "..tostring(id)) end for _, system in ipairs(self.systems) do removeEntityFromSystem(system, id, entity) end @@ -205,12 +206,16 @@ function Level.update(self, dt, paused) if system.prepareEntity then for id in pairs(system.entities) do local entity = self.entities[id] - system:prepareEntity(entity) + if entity then + system:prepareEntity(entity) + end end end for id in pairs(system.entities) do local entity = self.entities[id] - system:update(entity, dt) + if entity then + system:update(entity, dt) + end end if system.postUpdate then system:postUpdate() @@ -62,9 +62,9 @@ level:addSystem{ self.time = 0 self.world=world self.space=space - ode.WorldSetGravity(self.world, 0, -10, 0) + ode.WorldSetGravity(self.world, 0, -100, 0) ode.WorldSetCFM(self.world, 1e-5) - self.contactgroup = ode.JointGroupCreate(32) + self.contactgroup = ode.JointGroupCreate(200) self.__gc = honey.util.gc_canary(function() ode.WorldDestroy(self.world) ode.SpaceDestroy(self.space) @@ -167,22 +167,32 @@ local groundPlane = { level:addEntity(groundPlane) -local ball = { - transform=Mat4():identity(), - parent=false, - mesh=icosa, - shader=shader, - collisionShape=ode.CreateSphere(space, 1.0), - physicsBody=ode.BodyCreate(world), -} -level:addEntity(ball) - -local mass = ode.MassCreate() -ode.MassSetSphere(mass, 1, 0.5) -ode.BodySetMass(ball.physicsBody, mass) -ode.GeomSetBody(ball.collisionShape, ball.physicsBody) -ode.BodySetPosition(ball.physicsBody, -5, 3, 0) -ode.BodySetLinearVel(ball.physicsBody, 3, 0, 0) +local balls = {} +local function createNewBall() + local ball = { + transform=Mat4():identity(), + parent=false, + mesh=icosa, + shader=shader, + collisionShape=ode.CreateSphere(space, 1.0), + physicsBody=ode.BodyCreate(world), + } + local id = level:addEntity(ball) + table.insert(balls, id) + if #balls > 200 then + level:removeEntity(table.remove(balls, 1)) + end + + local mass = ode.MassCreate() + ode.MassSetSphere(mass, 1, 0.5) + ode.BodySetMass(ball.physicsBody, mass) + local function tiny() return (6 * math.random()) - 3 end + local x, y, z = tiny(), tiny(), tiny() + ode.GeomSetBody(ball.collisionShape, ball.physicsBody) + ode.BodySetPosition(ball.physicsBody, x, y, z) + ode.BodySetLinearVel(ball.physicsBody, x, y, z) +end +createNewBall() ode.CreatePlane(space, 0, 1, 0, -2) @@ -193,6 +203,8 @@ window:setKeyCallback(function(_, key, scancode, action) window:setShouldClose(true) elseif key == glfw.KEY_SPACE then paused = not paused + elseif key == glfw.KEY_B or key == glfw.KEY_V then + createNewBall() end end end) |