diff options
author | sanine-a <sanine.not@pm.me> | 2023-05-09 11:31:17 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2023-05-09 11:31:17 -0500 |
commit | a2ae7aae8357c8c2684d85fd58b0c5a0563ebab9 (patch) | |
tree | 5050161547408c80b521c9f4708bb9f7a9d0fa71 /honey/ecs/script.lua | |
parent | 2a14abecaee073aef1f1966bb397d6086b2e4785 (diff) |
refactor: split ecs systems into multiple files
Diffstat (limited to 'honey/ecs/script.lua')
-rw-r--r-- | honey/ecs/script.lua | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/honey/ecs/script.lua b/honey/ecs/script.lua new file mode 100644 index 0000000..9ae7d72 --- /dev/null +++ b/honey/ecs/script.lua @@ -0,0 +1,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 |