mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-25 01:20:04 +00:00
49 lines
955 B
Text
49 lines
955 B
Text
|
local events = {}
|
||
|
|
||
|
local function trackers_invoke(event, component, entity, ...)
|
||
|
local trackers = events[event][component]
|
||
|
if not trackers then
|
||
|
return
|
||
|
end
|
||
|
|
||
|
for _, tracker in trackers do
|
||
|
tracker(entity, data)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
local function trackers_init(event, component, fn)
|
||
|
local ob = events[event]
|
||
|
|
||
|
return {
|
||
|
connect = function(component, fn)
|
||
|
local trackers = ob[component]
|
||
|
if not trackers then
|
||
|
trackers = {}
|
||
|
ob[component] = trackers
|
||
|
end
|
||
|
|
||
|
table.insert(trackers, fn)
|
||
|
end,
|
||
|
invoke = function(component, ...)
|
||
|
trackers_invoke(event, component, ...)
|
||
|
end
|
||
|
}
|
||
|
return function(component, fn)
|
||
|
local trackers = ob[component]
|
||
|
if not trackers then
|
||
|
trackers = {}
|
||
|
ob[component] = trackers
|
||
|
end
|
||
|
|
||
|
table.insert(trackers, fn)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
local trackers = {
|
||
|
emplace = trackers_init("emplace"),
|
||
|
add = trackers_init("added"),
|
||
|
remove = trackers_init("removed")
|
||
|
}
|
||
|
|
||
|
return trackers
|