From a2ae7aae8357c8c2684d85fd58b0c5a0563ebab9 Mon Sep 17 00:00:00 2001 From: sanine-a Date: Tue, 9 May 2023 11:31:17 -0500 Subject: refactor: split ecs systems into multiple files --- honey/ecs/node.lua | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 honey/ecs/node.lua (limited to 'honey/ecs/node.lua') diff --git a/honey/ecs/node.lua b/honey/ecs/node.lua new file mode 100644 index 0000000..39f1898 --- /dev/null +++ b/honey/ecs/node.lua @@ -0,0 +1,47 @@ +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 + end + + -- helper function + local function recursiveTransform(node) + if node._visited then + return node._matrix + end + + if not node.parent then + node._matrix = node.matrix + else + local parentTransform = self.db:getComponent(node.parent, "node") + local parentMatrix = recursiveTransform(parentTransform) + node._matrix = parentMatrix * node.matrix + end + node._visited = true + return node._matrix + end + + -- compute nodes + for id, node in pairs(nodes) do + recursiveTransform(node) + end + end, + } +end + + + +return module -- cgit v1.2.1