summaryrefslogtreecommitdiff
path: root/honey/ecs/node.lua
blob: 4953af5d7e95c84a00180671fe9b656423ba214d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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,
	}
end



return module