summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--honey/ecs/ecs.lua25
-rw-r--r--honey/ecs/node.lua12
-rw-r--r--main.lua18
3 files changed, 43 insertions, 12 deletions
diff --git a/honey/ecs/ecs.lua b/honey/ecs/ecs.lua
index b0409e4..301ed36 100644
--- a/honey/ecs/ecs.lua
+++ b/honey/ecs/ecs.lua
@@ -271,4 +271,29 @@ function SystemDb.removeSystem(self, id)
end
+--===== Access helper =====--
+
+
+function Accessor(db, id)
+ local tbl = {
+ __db = db,
+ __id = id,
+ }
+
+ setmetatable(tbl, {
+ __index=function(self, key)
+ return self.__db:getComponent(self.__id, key)
+ end,
+ __newindex=function(self, key, value)
+ self.__db:addComponent(self.__id, key, value)
+ end,
+ __tostring=function(self)
+ return string.format("Accessor<%s>", self.__id)
+ end,
+ })
+
+ return tbl
+end
+
+
return module
diff --git a/honey/ecs/node.lua b/honey/ecs/node.lua
index 39f1898..645946c 100644
--- a/honey/ecs/node.lua
+++ b/honey/ecs/node.lua
@@ -18,7 +18,7 @@ system = function(params)
end
-- helper function
- local function recursiveTransform(node)
+ local function recursiveTransform(id, node)
if node._visited then
return node._matrix
end
@@ -26,9 +26,13 @@ system = function(params)
if not node.parent then
node._matrix = node.matrix
else
- local parentTransform = self.db:getComponent(node.parent, "node")
- local parentMatrix = recursiveTransform(parentTransform)
+ local parentNode = self.db:getComponent(node.parent, "node")
+ local parentMatrix = recursiveTransform(node.parent, parentNode)
node._matrix = parentMatrix * node.matrix
+ if node.name then
+ if not parentNode._child then parentNode._child = {} end
+ parentNode._child[node.name] = honey.ecs.Accessor(self.db, id)
+ end
end
node._visited = true
return node._matrix
@@ -36,7 +40,7 @@ system = function(params)
-- compute nodes
for id, node in pairs(nodes) do
- recursiveTransform(node)
+ recursiveTransform(id, node)
end
end,
}
diff --git a/main.lua b/main.lua
index efee1a2..4984355 100644
--- a/main.lua
+++ b/main.lua
@@ -160,11 +160,13 @@ function setupEntities()
mesh = { filename="assets/capsule.obj", index=1 },
shader = { vertex="vertex.glsl", fragment="fragment.glsl" },
},
+ script = { script = "capsuleMove" },
}
local pivotPivot = entities:createEntityWithComponents{
node = {
+ name = "p",
parent = capsule,
matrix = Mat4():identity():rotateX(-0.5*math.pi),
},
@@ -173,6 +175,7 @@ function setupEntities()
local capcamPivot = entities:createEntityWithComponents{
node = {
+ name = "pivot",
parent = pivotPivot,
matrix = Mat4():identity(),
},
@@ -181,7 +184,6 @@ function setupEntities()
yaw = 0,
},
onCursorPos = { script = "cameraCursorPos" },
- script = { script = "capsuleMove" },
}
package.loaded["cameraCursorPos"] = (function()
local prevx, prevy
@@ -191,7 +193,6 @@ function setupEntities()
end
local dx = data.xpos - prevx
local dy = data.ypos - prevy
- print(dx,dy)
prevx, prevy = data.xpos, data.ypos
local node = entities:getComponent(id, "node")
@@ -209,17 +210,18 @@ function setupEntities()
end)()
package.loaded["capsuleMove"] = function(entities, id, dt)
+ local self = ecs.Accessor(entities, id)
local pressed = function(key)
return glfw.GetKey(window.win, key) == glfw.PRESS
end
- local py = entities:getComponent(id, "pitchyaw")
- local yaw = math.rad(py.yaw)
+ local yaw = math.rad(
+ self.node._child.p
+ .node._child.pivot
+ .pitchyaw.yaw)
local forward = Vec3{-math.sin(yaw), 0, -math.cos(yaw)}
local left = Vec3{-math.cos(yaw), 0, math.sin(yaw)}
- print(forward, left)
-
local capsule = entities:getComponent(capsule, "physics")
local vel = Vec3{0,0,0}
@@ -239,9 +241,9 @@ function setupEntities()
vel = 10000 * vel:normalize()
- x, y, z = ode.BodyGetLinearVel(capsule._body)
+ x, y, z = ode.BodyGetLinearVel(self.physics._body)
if Vec3{x,y,z}:norm2() < 500 then
- ode.BodyAddForce(capsule._body, vel[1], vel[2], vel[3])
+ ode.BodyAddForce(self.physics._body, vel[1], vel[2], vel[3])
end
end