summaryrefslogtreecommitdiff
path: root/honey/ecs/script.lua
blob: 9ae7d72e0444d0389ab7cc1b56f6660a8f0e8d00 (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
local module = {}
setmetatable(module, {__index=_G})
setfenv(1, module)


-- helper function for retrieving script functions 
getFunction = function(script)
	local f = require(script.script)
	if script.func then
		return f[script.func]
	else
		return f
	end
end


--===== dispatch messages to handlers =====--

dispatch = function(entities, msg, data)
	local query = entities:queryComponent(msg)
	for id, handler in pairs(query) do
		local f = getFunction(handler)
		f(entities, id, data)
	end
end

--===== script system =====--

system = function(params)
	return {
		db=params.db,
		update=function(self, dt)
			local entities = self.db:queryComponent("script")
			for id, script in pairs(entities) do
				local f = getFunction(script)
				f(self.db, id, dt)
			end
		end
	}
end



return module