jecs/addons/observers.luau

158 lines
3.1 KiB
Text
Raw Permalink Normal View History

2025-03-30 19:31:18 +00:00
local jecs = require("@jecs")
local testkit = require("@testkit")
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
local terms = query.filter_with
if not terms then
local ids = query.ids
query.filter_with = ids
terms = ids
end
local entity_index = world.entity_index
local function emplaced(entity)
local r = jecs.entity_index_try_get_fast(
entity_index, entity)
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
local function world_track(world, ...)
local entity_index = world.entity_index
local terms = { ... }
local q_shim = { filter_with = terms }
local n = 0
local dense_array = {}
local sparse_array = {}
local function emplaced(entity)
local r = jecs.entity_index_try_get_fast(
entity_index, entity)
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-04 22:41:38 +00:00
if jecs.query_match(q_shim :: any, archetype) then
2025-03-30 19:31:18 +00:00
n += 1
dense_array[n] = entity
sparse_array[entity] = n
end
end
local function removed(entity)
local i = sparse_array[entity]
if i ~= n then
dense_array[i] = dense_array[n]
end
dense_array[n] = nil
end
for _, term in terms do
world:added(term, emplaced)
2025-03-30 20:14:22 +00:00
world:changed(term, emplaced)
2025-03-30 19:31:18 +00:00
end
local function iter()
local i = n
return function()
local row = i
if row == 0 then
return nil
end
i -= 1
2025-04-04 22:41:38 +00:00
return dense_array[row] :: any
2025-03-30 19:31:18 +00:00
end
end
local it = {
2025-03-30 20:14:22 +00:00
__iter = iter,
without = function(self, ...)
q_shim.filter_without = { ... }
return self
end
2025-03-30 19:31:18 +00:00
}
return setmetatable(it, it)
end
2025-03-30 20:14:22 +00:00
local function observers_add(world)
2025-03-30 19:31:18 +00:00
local signals = {
added = {},
emplaced = {},
removed = {}
}
world.added = function(_, component, fn)
local listeners = signals.added[component]
if not listeners then
listeners = {}
signals.added[component] = listeners
local idr = jecs.id_record_ensure(world, component)
idr.hooks.on_add = function(entity)
for _, listener in listeners do
listener(entity)
end
end
end
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]
if not listeners then
listeners = {}
signals.emplaced[component] = listeners
local idr = jecs.id_record_ensure(world, component)
2025-03-30 20:14:22 +00:00
idr.hooks.on_change = function(entity, value)
2025-03-30 19:31:18 +00:00
for _, listener in listeners do
listener(entity, value)
end
end
end
table.insert(listeners, fn)
end
world.removed = function(_, component, fn)
local listeners = signals.removed[component]
if not listeners then
listeners = {}
signals.removed[component] = listeners
local idr = jecs.id_record_ensure(world, component)
idr.hooks.on_remove = function(entity)
for _, listener in listeners do
listener(entity)
end
end
end
table.insert(listeners, fn)
end
world.signals = signals
world.track = world_track
2025-03-30 20:14:22 +00:00
world.observer = observers_new
return world
2025-03-30 19:31:18 +00:00
end
2025-03-30 20:14:22 +00:00
return observers_add