jecs/addons/observers.luau

162 lines
4.2 KiB
Text
Raw Normal View History

2025-03-30 19:31:18 +00:00
local jecs = require("@jecs")
2025-04-10 17:52:07 +00:00
type Observer<T...> = {
callback: (jecs.Entity) -> (),
query: jecs.Query<T...>,
}
export type PatchedWorld = jecs.World & {
added: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id, value: any) -> ()) -> (),
removed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id) -> ()) -> (),
changed: (PatchedWorld, jecs.Id, (e: jecs.Entity, id: jecs.Id) -> ()) -> (),
observer: (PatchedWorld, Observer<any>) -> (),
monitor: (PatchedWorld, Observer<any>) -> (),
}
2025-03-30 19:31:18 +00:00
2025-03-30 20:14:22 +00:00
local function observers_new(world, description)
2025-03-30 19:31:18 +00:00
local query = description.query
local callback = description.callback
2025-04-10 17:52:07 +00:00
local terms = query.filter_with :: { jecs.Id }
2025-03-30 19:31:18 +00:00
if not terms then
local ids = query.ids
query.filter_with = ids
terms = ids
end
2025-04-10 17:52:07 +00:00
local entity_index = world.entity_index :: any
local function emplaced(entity: jecs.Entity)
2025-03-30 19:31:18 +00:00
local r = jecs.entity_index_try_get_fast(
2025-04-10 17:52:07 +00:00
entity_index, entity :: any)
2025-03-30 19:31:18 +00:00
2025-04-04 22:41:38 +00:00
if not r then
return
end
2025-03-30 19:31:18 +00:00
local archetype = r.archetype
if jecs.query_match(query, archetype) then
callback(entity)
end
end
for _, term in terms do
2025-03-30 20:14:22 +00:00
world:added(term, emplaced)
world:changed(term, emplaced)
2025-03-30 19:31:18 +00:00
end
end
2025-04-10 17:52:07 +00:00
local function monitors_new(world, description)
local query = description.query
local callback = description.callback
local terms = query.filter_with :: { jecs.Id }
if not terms then
local ids = query.ids
query.filter_with = ids
terms = ids
end
2025-03-30 19:31:18 +00:00
2025-04-10 17:52:07 +00:00
local entity_index = world.entity_index :: any
local function emplaced(entity: jecs.Entity)
2025-03-30 19:31:18 +00:00
local r = jecs.entity_index_try_get_fast(
2025-04-10 17:52:07 +00:00
entity_index, entity :: any)
2025-03-30 19:31:18 +00:00
2025-04-04 22:41:38 +00:00
if not r then
return
end
2025-03-30 19:31:18 +00:00
local archetype = r.archetype
2025-04-10 17:52:07 +00:00
if jecs.query_match(query, archetype) then
callback(entity, jecs.OnAdd)
2025-03-30 19:31:18 +00:00
end
end
2025-04-10 17:52:07 +00:00
local function removed(entity: jecs.Entity, component: jecs.Id)
local r = jecs.entity_index_try_get_fast(
entity_index, entity :: any)
if not r then
return
2025-03-30 19:31:18 +00:00
end
2025-04-10 17:52:07 +00:00
local archetype = r.archetype
if jecs.query_match(query, archetype) then
callback(entity, jecs.OnRemove)
end
2025-03-30 19:31:18 +00:00
end
for _, term in terms do
world:added(term, emplaced)
2025-04-10 17:52:07 +00:00
world:removed(term, removed)
2025-03-30 19:31:18 +00:00
end
end
2025-04-10 17:52:07 +00:00
local function observers_add(world: jecs.World & { [string]: any }): PatchedWorld
2025-03-30 19:31:18 +00:00
local signals = {
added = {},
emplaced = {},
removed = {}
}
2025-04-10 17:52:07 +00:00
2025-03-30 19:31:18 +00:00
world.added = function(_, component, fn)
local listeners = signals.added[component]
2025-04-22 02:52:21 +00:00
local component_index = world.component_index :: jecs.ComponentIndex
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
2025-03-30 19:31:18 +00:00
if not listeners then
listeners = {}
signals.added[component] = listeners
2025-04-22 02:49:52 +00:00
local function on_add(entity: number, id: number, value: any)
for _, listener in listeners :: any do
listener(entity, id, value)
2025-03-30 19:31:18 +00:00
end
end
2025-04-22 02:49:52 +00:00
world:set(component, jecs.OnAdd, on_add) end
2025-03-30 19:31:18 +00:00
table.insert(listeners, fn)
end
2025-03-30 20:14:22 +00:00
world.changed = function(_, component, fn)
2025-03-30 19:31:18 +00:00
local listeners = signals.emplaced[component]
2025-04-22 02:52:21 +00:00
local component_index = world.component_index :: jecs.ComponentIndex
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
2025-03-30 19:31:18 +00:00
if not listeners then
listeners = {}
signals.emplaced[component] = listeners
2025-04-22 02:49:52 +00:00
local function on_change(entity: number, id: number, value: any)
for _, listener in listeners :: any do
listener(entity, id, value)
2025-03-30 19:31:18 +00:00
end
end
2025-04-22 02:49:52 +00:00
world:set(component, jecs.OnChange, on_change)
2025-03-30 19:31:18 +00:00
end
table.insert(listeners, fn)
end
world.removed = function(_, component, fn)
local listeners = signals.removed[component]
2025-04-22 02:52:21 +00:00
local component_index = world.component_index :: jecs.ComponentIndex
assert(component_index[component] == nil, "You cannot use hooks on components you intend to use this signal with")
2025-03-30 19:31:18 +00:00
if not listeners then
listeners = {}
signals.removed[component] = listeners
2025-04-22 02:49:52 +00:00
local function on_remove(entity: number, id: number, value: any)
for _, listener in listeners :: any do
listener(entity, id, value)
2025-03-30 19:31:18 +00:00
end
end
2025-04-22 02:49:52 +00:00
world:set(component, jecs.OnRemove, on_remove)
2025-03-30 19:31:18 +00:00
end
table.insert(listeners, fn)
end
world.signals = signals
2025-03-30 20:14:22 +00:00
world.observer = observers_new
2025-04-10 17:52:07 +00:00
world.monitor = monitors_new
2025-04-22 02:49:52 +00:00
return world :: PatchedWorld
2025-03-30 19:31:18 +00:00
end
2025-04-10 19:10:42 +00:00
return observers_add