summaryrefslogtreecommitdiff
path: root/honey/ecs
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2023-05-09 16:21:47 -0500
committersanine-a <sanine.not@pm.me>2023-05-09 16:21:47 -0500
commitb24a64fd45d71cfc18fea0e333906baceda031c8 (patch)
treed12a3353da5be1fd2faa7529e59fd5b31a0600b0 /honey/ecs
parent68ef488e8f3bafa0ed68eb8326ac26fd63416891 (diff)
fix bug when getting components no entity has
Diffstat (limited to 'honey/ecs')
-rw-r--r--honey/ecs/collision.lua3
-rw-r--r--honey/ecs/ecs.lua4
-rw-r--r--honey/ecs/physics.lua57
3 files changed, 44 insertions, 20 deletions
diff --git a/honey/ecs/collision.lua b/honey/ecs/collision.lua
index 722c256..5c0452b 100644
--- a/honey/ecs/collision.lua
+++ b/honey/ecs/collision.lua
@@ -26,6 +26,9 @@ local function createGeom(self, id, collision)
print(normal, d)
geom = ode.CreatePlane(self.space, normal[1], normal[2], normal[3], d)
end
+
+ ode.GeomSetData(geom, id)
+
collision._geom = geom
collision._gc = honey.util.gc_canary(function()
print("release geom for id"..id)
diff --git a/honey/ecs/ecs.lua b/honey/ecs/ecs.lua
index 301ed36..dda99cb 100644
--- a/honey/ecs/ecs.lua
+++ b/honey/ecs/ecs.lua
@@ -179,7 +179,9 @@ end
-- get a specific component from an entity
function EntityDb.getComponent(self, id, name)
self:checkIsValid(id)
- return self.components[name].data[id]
+ local components = self.components[name]
+ if not components then return nil end
+ return components.data[id]
end
diff --git a/honey/ecs/physics.lua b/honey/ecs/physics.lua
index e2c9b9a..fe08359 100644
--- a/honey/ecs/physics.lua
+++ b/honey/ecs/physics.lua
@@ -91,6 +91,43 @@ local function createPhysicsBody(db, world, id, component)
component._body = body
end
+
+local function handleCollision(db, self, other)
+ local handler = db:getComponent(self, "onCollision")
+ if handler then
+ handler(db, self, other)
+ end
+end
+
+
+local function collide(self, a, b, collision)
+ -- check for collision handlers
+ local idA = ode.GeomGetData(a)
+ local idB = ode.GeomGetData(b)
+ handleCollision(self.db, idA, idB)
+ --handleCollision(self.db, idB, idA)
+
+ -- set up the joint params
+ local contact = ode.CreateContact{ surface={
+ mode = ode.ContactBounce + ode.ContactSoftCFM,
+ mu = ode.Infinity,
+ bounce = 0.90,
+ bounce_vel = 0.1,
+ soft_cfm = 0.001,
+ }}
+ ode.ContactSetGeom(contact, collision)
+ -- create the joint
+ local joint = ode.JointCreateContact(
+ self.world,
+ self.contactGroup,
+ contact
+ )
+ -- attach the two bodies
+ local bodyA = ode.GeomGetBody(a)
+ local bodyB = ode.GeomGetBody(b)
+ ode.JointAttach(joint, bodyA, bodyB)
+end
+
--===== physics =====--
@@ -129,25 +166,7 @@ system = function(params)
-- check for actual collisions
local collisions = ode.Collide(a, b, 1)
if #collisions > 0 then
- -- set up the joint params
- local contact = ode.CreateContact{ surface={
- mode = ode.ContactBounce + ode.ContactSoftCFM,
- mu = ode.Infinity,
- bounce = 0.90,
- bounce_vel = 0.1,
- soft_cfm = 0.001,
- }}
- ode.ContactSetGeom(contact, collisions[1])
- -- create the joint
- local joint = ode.JointCreateContact(
- self.world,
- self.contactGroup,
- contact
- )
- -- attach the two bodies
- local bodyA = ode.GeomGetBody(a)
- local bodyB = ode.GeomGetBody(b)
- ode.JointAttach(joint, bodyA, bodyB)
+ collide(self, a, b, collisions[1])
end
end)
-- update the world