diff options
author | sanine-a <sanine.not@pm.me> | 2023-05-12 14:10:48 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2023-05-12 14:10:48 -0500 |
commit | 0d96ebc90c9740e7e66a70aa11168b11f49d220b (patch) | |
tree | 758403f74f4fbcab61a228ef58e720850b3f70db /honey/ecs/node.lua | |
parent | 6036d1b5d7e0fc160637ce70595bac57ed1fcd00 (diff) |
run working program with refactored code
Diffstat (limited to 'honey/ecs/node.lua')
-rw-r--r-- | honey/ecs/node.lua | 77 |
1 files changed, 36 insertions, 41 deletions
diff --git a/honey/ecs/node.lua b/honey/ecs/node.lua index 4953af5..6a47a1f 100644 --- a/honey/ecs/node.lua +++ b/honey/ecs/node.lua @@ -1,52 +1,47 @@ +local ecs = require 'honey.ecs.ecs' + local module = {} setmetatable(module, {__index=_G}) setfenv(1, module) --===== transform cascading =====-- -system = function(params) - return { - db = params.db, - - priority = 2, - update = function(self, dt) - local nodes = self.db:queryComponent("node") - - -- prepare nodes - for id, node in pairs(nodes) do - node._visited = false - node._child = nil - end - - -- helper function - local function recursiveTransform(id, node) - if node._visited then - return node._matrix - end - - if not node.parent then - node._matrix = node.matrix - else - 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 - end - - -- compute nodes - for id, node in pairs(nodes) do - recursiveTransform(id, node) - end - end, - } +local function recursiveTransform(db, id, node) + if node._visited then + return node._matrix + end + + if not node.parent then + node._matrix = node.matrix + else + local parentNode = db:getComponent(node.parent, "node") + local parentMatrix = recursiveTransform(db, node.parent, parentNode) + node._matrix = parentMatrix * node.matrix + if node.name then + if not parentNode._child then parentNode._child = {} end + parentNode._child[node.name] = ecs.Accessor(db, id) + end + end + node._visited = true + return node._matrix end +system = {ecs.System("node", function(db, dt, params) + local nodes = db:queryComponent("node") + + -- prepare nodes + for id, node in pairs(nodes) do + node._visited = false + node._child = nil + end + + -- compute nodes + for id, node in pairs(nodes) do + recursiveTransform(db, id, node) + end +end)} + + return module |