1
0
Fork 0
mirror of https://github.com/Reuh/ubiquitousse.git synced 2025-10-27 09:09:30 +00:00
ubiquitousse/ecs/callback.can

35 lines
875 B
Text

--- Callback system
-- Allow to call callbacks defined in other systems in entity methods.
-- Example:
-- entity.callback:onMove(...) -- call onAdd(system, entitySystemTable, ...) on every system the entity belong to (which has this callback)
-- Can be also used for onAdd, etc.
--- Recursively get a list of systems with a certain method.
let recGetSystemsWithMethod = (method, systems, l={})
for _, s in ipairs(systems) do
if s[method] then
table.insert(l, s)
end
recGetSystemsWithMethod(method, s.systems, l)
end
return l
end
return {
name = "callback",
filter = true,
methods = {
__index = :(c, k)
let s = recGetSystemsWithMethod(k, {@world})
@methods[k] = :(c, ...)
let e = c.entity
for _, sys in ipairs(s) do
if sys._previous[e] then
sys[k](sys, e[sys.name], ...)
end
end
end
return @_methods_mt[k]
end
}
}