jecs/addons/ob.luau

320 lines
7.3 KiB
Text
Raw Normal View History

2025-07-27 12:39:43 +00:00
--!strict
local jecs = require("@jecs")
type World = jecs.World
type Query<T...> = jecs.Query<T...>
type Id<T=any> = jecs.Id<T>
export type Iter<T...> = (Observer<T...>) -> () -> (jecs.Entity, T...)
export type Observer<T...> = {
disconnect: (Observer<T...>) -> (),
}
2025-11-18 20:02:39 +00:00
export type Monitor = {
disconnect: () -> (),
added: ((jecs.Entity) -> ()) -> (),
removed: ((jecs.Entity) -> ()) -> ()
}
2025-07-27 12:39:43 +00:00
local function observers_new<T...>(
query: Query<T...>,
callback: (jecs.Entity) -> ()
2025-07-27 12:39:43 +00:00
): Observer<T...>
2025-11-18 20:02:39 +00:00
local cachedquery = query:cached()
2025-09-15 21:15:11 +00:00
2025-11-18 20:02:39 +00:00
local world = (cachedquery :: Query<any> & { world: World }).world
2025-07-27 12:39:43 +00:00
callback = callback
2025-11-18 20:02:39 +00:00
local archetypes = cachedquery.archetypes_map
local terms = query.filter_with :: { jecs.Id<any, any> }
2025-07-27 12:39:43 +00:00
2025-11-18 20:02:39 +00:00
local entity_index = world.entity_index
2025-09-15 21:15:11 +00:00
local function emplaced<a>(
entity: jecs.Entity,
2025-11-18 20:02:39 +00:00
id: jecs.Id<a>
2025-07-27 12:39:43 +00:00
)
local r = entity_index.sparse_array[jecs.ECS_ID(entity)]
local archetype = r.archetype
if archetypes[archetype.id] then
callback(entity)
2025-07-27 12:39:43 +00:00
end
end
local cleanup = {}
2025-07-27 12:39:43 +00:00
for _, term in terms do
if jecs.IS_PAIR(term) then
local rel = jecs.ECS_PAIR_FIRST(term)
local tgt = jecs.ECS_PAIR_SECOND(term)
local wc = tgt == jecs.w
local onadded = world:added(rel, function(entity, id)
if not wc and id ~= term then
return
end
local r = jecs.record(world, entity)
if archetypes[r.archetype.id] then
callback(entity)
end
end)
table.insert(cleanup, onadded)
else
local onadded = world:added(term, emplaced)
local onchanged = world:changed(term, emplaced)
table.insert(cleanup, onadded)
table.insert(cleanup, onchanged)
end
2025-07-27 12:39:43 +00:00
end
local without = query.filter_without
if without then
for _, term in without do
if jecs.IS_PAIR(term) then
local rel = jecs.ECS_PAIR_FIRST(term)
2025-09-16 08:35:52 +00:00
local tgt = jecs.ECS_PAIR_SECOND(term)
local wc = tgt == jecs.w
2025-09-16 09:02:52 +00:00
local onremoved = world:removed(rel, function(entity, id)
if not wc and id ~= term then
2025-09-16 09:02:52 +00:00
return
end
2025-09-16 08:35:52 +00:00
local r = jecs.record(world, entity)
local archetype = r.archetype
if archetype then
local dst = jecs.archetype_traverse_remove(world, id, archetype)
if archetypes[dst.id] then
callback(entity)
end
end
end)
table.insert(cleanup, onremoved)
else
local onremoved = world:removed(term, function(entity, id)
local r = jecs.record(world, entity)
local archetype = r.archetype
if archetype then
local dst = jecs.archetype_traverse_remove(world, id, archetype)
if archetypes[dst.id] then
callback(entity)
end
end
2025-09-16 08:35:52 +00:00
end)
table.insert(cleanup, onremoved)
end
end
end
2025-07-27 12:39:43 +00:00
local function disconnect()
for _, disconnect in cleanup do
disconnect()
end
end
2025-07-27 12:39:43 +00:00
local observer = {
disconnect = disconnect,
}
2025-07-27 12:39:43 +00:00
return observer
2025-07-27 12:39:43 +00:00
end
2025-11-18 20:02:39 +00:00
local function monitors_new(query: Query<...any>): Monitor
local cachedquery = query:cached()
2025-07-27 12:39:43 +00:00
2025-11-18 20:02:39 +00:00
local world = (cachedquery :: Query<...any> & { world: World }).world :: jecs.World
2025-07-27 12:39:43 +00:00
2025-11-18 20:02:39 +00:00
local archetypes = cachedquery.archetypes_map
local terms = cachedquery.filter_with :: { jecs.Id<any, any> }
2025-07-27 12:39:43 +00:00
local entity_index = world.entity_index :: any
local callback_added: ((jecs.Entity) -> ())?
local callback_removed: ((jecs.Entity) -> ())?
2025-07-27 12:39:43 +00:00
2025-11-18 20:02:39 +00:00
local function emplaced<T, a>(
entity: jecs.Entity<T>,
id: jecs.Id<a>
2025-07-27 12:39:43 +00:00
)
if callback_added == nil then
return
end
2025-07-27 12:39:43 +00:00
local r = jecs.entity_index_try_get_fast(
entity_index, entity :: any) :: jecs.Record
local archetype = r.archetype
if archetypes[archetype.id] then
callback_added(entity)
2025-07-27 12:39:43 +00:00
end
end
2025-11-18 20:02:39 +00:00
local function removed(entity: jecs.Entity, component)
if callback_removed == nil then
return
end
2025-08-25 01:01:05 +00:00
local r = jecs.record(world, entity)
if not archetypes[r.archetype.id] then
return
end
callback_removed(entity)
2025-07-27 12:39:43 +00:00
end
local cleanup = {}
2025-07-27 12:39:43 +00:00
for _, term in terms do
if jecs.IS_PAIR(term) then
local rel = jecs.ECS_PAIR_FIRST(term)
local tgt = jecs.ECS_PAIR_SECOND(term)
local wc = tgt == jecs.w
local onadded = world:added(rel, function(entity, id)
if callback_added == nil then
return
end
if not wc and id ~= term then
return
end
local r = jecs.entity_index_try_get_fast(
entity_index, entity :: any) :: jecs.Record
local archetype = r.archetype
if archetypes[archetype.id] then
callback_added(entity)
end
end)
local onremoved = world:removed(rel, function(entity, id)
if callback_removed == nil then
return
end
if not wc and id ~= term then
return
end
local r = jecs.record(world, entity)
if not archetypes[r.archetype.id] then
return
end
callback_removed(entity)
end)
table.insert(cleanup, onadded)
table.insert(cleanup, onremoved)
else
local onadded = world:added(term, emplaced)
local onremoved = world:removed(term, removed)
table.insert(cleanup, onadded)
table.insert(cleanup, onremoved)
end
2025-11-18 20:02:39 +00:00
2025-07-27 12:39:43 +00:00
end
local without = query.filter_without
if without then
for _, term in without do
if jecs.IS_PAIR(term) then
2025-09-16 08:35:52 +00:00
local rel = jecs.ECS_PAIR_FIRST(term)
local tgt = jecs.ECS_PAIR_SECOND(term)
local wc = tgt == jecs.w
local onadded = world:added(rel, function(entity, id)
if callback_removed == nil then
return
end
2025-09-16 09:02:52 +00:00
if not wc and id ~= term then
return
end
2025-09-16 08:35:52 +00:00
local r = jecs.record(world, entity)
local archetype = r.archetype
if archetype then
local dst = jecs.archetype_traverse_remove(world, id, archetype)
if archetypes[dst.id] then
callback_removed(entity)
end
end
end)
local onremoved = world:removed(rel, function(entity, id)
if callback_added == nil then
return
end
2025-09-16 09:02:52 +00:00
if not wc and id ~= term then
return
end
2025-09-16 08:35:52 +00:00
local r = jecs.record(world, entity)
local archetype = r.archetype
if archetype then
local dst = jecs.archetype_traverse_remove(world, id, archetype)
if archetypes[dst.id] then
callback_added(entity)
end
end
end)
table.insert(cleanup, onadded)
table.insert(cleanup, onremoved)
else
local onadded = world:added(term, function(entity, id)
if callback_removed == nil then
return
end
local r = jecs.record(world, entity)
local archetype = r.archetype
if archetype then
local dst = jecs.archetype_traverse_remove(world, id, archetype)
if archetypes[dst.id] then
callback_removed(entity)
end
end
end)
local onremoved = world:removed(term, function(entity, id)
if callback_added == nil then
return
end
local r = jecs.record(world, entity)
local archetype = r.archetype
if archetype then
local dst = jecs.archetype_traverse_remove(world, id, archetype)
if archetypes[dst.id] then
callback_added(entity)
end
end
end)
table.insert(cleanup, onadded)
table.insert(cleanup, onremoved)
end
end
end
2025-07-27 12:39:43 +00:00
local function disconnect()
for _, disconnect in cleanup do
disconnect()
end
2025-07-27 12:39:43 +00:00
end
local function monitor_added(callback)
callback_added = callback
end
local function monitor_removed(callback)
callback_removed = callback
2025-07-27 12:39:43 +00:00
end
local monitor = {
2025-07-27 12:39:43 +00:00
disconnect = disconnect,
added = monitor_added,
removed = monitor_removed
2025-11-18 20:02:39 +00:00
} :: Monitor
2025-07-27 12:39:43 +00:00
return monitor
2025-07-27 12:39:43 +00:00
end
return {
monitor = monitors_new,
observer = observers_new
}