2025-07-27 12:39:43 +00:00
|
|
|
--!strict
|
|
|
|
local jecs = require("@jecs")
|
|
|
|
|
|
|
|
type World = jecs.World
|
|
|
|
type Query<T...> = jecs.Query<T...>
|
2025-08-31 04:00:56 +00:00
|
|
|
type QueryInner<T...> = Query<T...> & jecs.QueryInner
|
2025-07-27 12:39:43 +00:00
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
type Id<T = any> = jecs.Id<T>
|
2025-07-27 12:39:43 +00:00
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
type Entity<T = any> = jecs.Entity<T>
|
2025-07-27 12:39:43 +00:00
|
|
|
|
|
|
|
export type Iter<T...> = (Observer<T...>) -> () -> (jecs.Entity, T...)
|
|
|
|
|
|
|
|
export type Observer<T...> = typeof(setmetatable(
|
|
|
|
{} :: {
|
|
|
|
iter: Iter<T...>,
|
|
|
|
entities: { Entity<nil> },
|
2025-08-31 04:00:56 +00:00
|
|
|
disconnect: (Observer<T...>) -> (),
|
2025-07-27 12:39:43 +00:00
|
|
|
},
|
|
|
|
{} :: {
|
|
|
|
__iter: Iter<T...>,
|
|
|
|
}
|
|
|
|
))
|
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
local function get_matching_archetypes(world: jecs.World, query: QueryInner<...any>)
|
|
|
|
local archetypes = {}
|
2025-07-27 12:39:43 +00:00
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
for _, archetype in query.compatible_archetypes do
|
|
|
|
archetypes[archetype.id] = true
|
|
|
|
end
|
2025-07-27 12:39:43 +00:00
|
|
|
|
|
|
|
local terms = query.ids
|
|
|
|
local first = terms[1]
|
|
|
|
|
|
|
|
local observers_on_create = world.observable[jecs.ArchetypeCreate][first]
|
|
|
|
local observer_on_create = observers_on_create[#observers_on_create]
|
|
|
|
observer_on_create.callback = function(archetype)
|
|
|
|
archetypes[archetype.id] = true
|
|
|
|
end
|
2025-08-31 04:00:56 +00:00
|
|
|
|
2025-07-27 12:39:43 +00:00
|
|
|
local observers_on_delete = world.observable[jecs.ArchetypeDelete][first]
|
|
|
|
local observer_on_delete = observers_on_delete[#observers_on_delete]
|
2025-08-31 04:00:56 +00:00
|
|
|
|
2025-07-27 12:39:43 +00:00
|
|
|
observer_on_delete.callback = function(archetype)
|
|
|
|
archetypes[archetype.id] = nil
|
|
|
|
end
|
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
local function disconnect()
|
|
|
|
table.remove(observers_on_create, table.find(observers_on_create, observer_on_create))
|
|
|
|
table.remove(observers_on_delete, table.find(observers_on_delete, observer_on_delete))
|
|
|
|
end
|
|
|
|
|
|
|
|
return archetypes, disconnect
|
|
|
|
end
|
|
|
|
|
|
|
|
local function observers_new<T...>(query: Query<T...>, callback: ((Entity<nil>, Id<any>, value: any?) -> ())?): Observer<T...>
|
|
|
|
query:cached()
|
|
|
|
|
|
|
|
local world = (query :: QueryInner<T...>).world
|
|
|
|
callback = callback
|
|
|
|
|
|
|
|
local archetypes, disconnect_query = get_matching_archetypes(world, query :: QueryInner<T...>)
|
|
|
|
|
|
|
|
local terms = query.ids
|
|
|
|
local query_with = query.filter_with or terms
|
|
|
|
|
2025-07-27 12:39:43 +00:00
|
|
|
local entity_index = world.entity_index :: any
|
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
local iter_indexes = {} :: { [Entity]: number }
|
|
|
|
local iter_queue = {} :: { Entity }
|
|
|
|
|
|
|
|
local function remove_queued(entity: jecs.Entity, index: number)
|
|
|
|
if index ~= nil then
|
|
|
|
local last = table.remove(iter_queue)
|
|
|
|
if last and last ~= entity then
|
|
|
|
iter_queue[index] = last
|
|
|
|
iter_indexes[last] = index
|
|
|
|
end
|
|
|
|
iter_indexes[entity] = nil
|
|
|
|
end
|
|
|
|
end
|
2025-07-27 12:39:43 +00:00
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
local function emplaced<T>(entity: jecs.Entity, id: jecs.Id<T>, value: T?)
|
|
|
|
local r = entity_index.sparse_array[jecs.ECS_ID(entity)]
|
|
|
|
local index = iter_indexes[entity]
|
|
|
|
if r == nil then
|
|
|
|
remove_queued(entity, index)
|
|
|
|
end
|
2025-07-27 12:39:43 +00:00
|
|
|
local archetype = r.archetype
|
|
|
|
|
|
|
|
if archetypes[archetype.id] then
|
2025-08-31 04:00:56 +00:00
|
|
|
if index == nil then
|
|
|
|
table.insert(iter_queue, entity)
|
|
|
|
iter_indexes[entity] = #iter_queue
|
|
|
|
end
|
2025-07-27 12:39:43 +00:00
|
|
|
if callback ~= nil then
|
2025-08-31 04:00:56 +00:00
|
|
|
callback(entity :: Entity, id, value)
|
2025-07-27 12:39:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
local function removed(entity: jecs.Entity)
|
|
|
|
local index = iter_indexes[entity]
|
|
|
|
if index ~= nil then
|
|
|
|
remove_queued(entity, index)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local hooked = {} :: { () -> () }
|
|
|
|
for _, term in query_with do
|
2025-07-27 15:32:18 +00:00
|
|
|
if jecs.IS_PAIR(term) then
|
|
|
|
term = jecs.ECS_PAIR_FIRST(term)
|
|
|
|
end
|
2025-08-31 04:00:56 +00:00
|
|
|
table.insert(hooked, world:added(term, emplaced))
|
|
|
|
table.insert(hooked, world:changed(term, emplaced))
|
|
|
|
table.insert(hooked, world:removed(term, removed))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function disconnect()
|
|
|
|
disconnect_query()
|
|
|
|
for _, unhook in hooked do
|
|
|
|
unhook()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function iter()
|
|
|
|
local row = #iter_queue
|
|
|
|
return function()
|
|
|
|
if row == 0 then
|
|
|
|
table.clear(iter_queue)
|
|
|
|
table.clear(iter_indexes)
|
|
|
|
end
|
|
|
|
local entity = iter_queue[row]
|
|
|
|
row -= 1
|
|
|
|
return entity
|
|
|
|
end
|
|
|
|
end
|
2025-07-27 12:39:43 +00:00
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
local observer = {
|
|
|
|
disconnect = disconnect,
|
|
|
|
entities = iter_queue,
|
|
|
|
__iter = iter,
|
|
|
|
iter = iter,
|
|
|
|
}
|
2025-07-27 12:39:43 +00:00
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
setmetatable(observer, observer)
|
|
|
|
|
|
|
|
return (observer :: any) :: Observer<T...>
|
|
|
|
end
|
|
|
|
|
|
|
|
local function monitors_new<T...>(query: Query<T...>, callback: ((Entity<nil>, Id<any>, value: any?) -> ())?): Observer<T...>
|
2025-07-27 12:39:43 +00:00
|
|
|
query:cached()
|
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
local world = (query :: QueryInner<T...>).world
|
|
|
|
|
|
|
|
local archetypes, disconnect_query = get_matching_archetypes(world, query :: QueryInner<T...>)
|
2025-07-27 12:39:43 +00:00
|
|
|
|
|
|
|
local terms = query.ids
|
2025-08-31 04:00:56 +00:00
|
|
|
local query_with = query.filter_with or terms
|
2025-07-27 12:39:43 +00:00
|
|
|
|
|
|
|
local entity_index = world.entity_index :: any
|
|
|
|
local i = 0
|
|
|
|
local entities = {}
|
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
local function emplaced<T, a>(entity: jecs.Entity<T>, id: jecs.Id<a>, value: a?)
|
|
|
|
local r = jecs.entity_index_try_get_fast(entity_index, entity :: any) :: jecs.Record
|
|
|
|
if not r or not r.archetype then
|
|
|
|
if callback then
|
|
|
|
callback(entity :: Entity, jecs.OnRemove)
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
2025-07-27 12:39:43 +00:00
|
|
|
|
|
|
|
local archetype = r.archetype
|
|
|
|
|
|
|
|
if archetypes[archetype.id] then
|
|
|
|
i += 1
|
|
|
|
entities[i] = entity
|
|
|
|
if callback ~= nil then
|
2025-08-31 04:00:56 +00:00
|
|
|
callback(entity :: Entity, jecs.OnAdd)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if callback ~= nil then
|
|
|
|
callback(entity :: Entity, jecs.OnRemove)
|
2025-07-27 12:39:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function removed(entity: jecs.Entity, component: jecs.Id)
|
2025-08-25 01:01:05 +00:00
|
|
|
local r = jecs.record(world, entity)
|
2025-08-31 04:00:56 +00:00
|
|
|
if not r or not archetypes[r.archetype.id] then
|
2025-08-25 01:01:05 +00:00
|
|
|
return
|
|
|
|
end
|
2025-07-27 12:39:43 +00:00
|
|
|
local EcsOnRemove = jecs.OnRemove :: jecs.Id
|
|
|
|
if callback ~= nil then
|
|
|
|
callback(entity, EcsOnRemove)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
local hooked = {} :: { () -> () }
|
|
|
|
for _, term in query_with do
|
2025-07-27 15:32:18 +00:00
|
|
|
if jecs.IS_PAIR(term) then
|
|
|
|
term = jecs.ECS_PAIR_FIRST(term)
|
|
|
|
end
|
2025-08-31 04:00:56 +00:00
|
|
|
table.insert(hooked, world:added(term, emplaced))
|
|
|
|
table.insert(hooked, world:removed(term, removed))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function disconnect()
|
|
|
|
disconnect_query()
|
|
|
|
for _, unhook in hooked do
|
|
|
|
unhook()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function iter()
|
|
|
|
local row = i
|
|
|
|
return function()
|
|
|
|
if row == 0 then
|
|
|
|
i = 0
|
|
|
|
table.clear(entities)
|
|
|
|
end
|
|
|
|
local entity = entities[row]
|
|
|
|
row -= 1
|
|
|
|
return entity
|
|
|
|
end
|
|
|
|
end
|
2025-07-27 12:39:43 +00:00
|
|
|
|
|
|
|
local observer = {
|
2025-08-31 04:00:56 +00:00
|
|
|
disconnect = disconnect,
|
|
|
|
entities = entities,
|
|
|
|
__iter = iter,
|
|
|
|
iter = iter,
|
|
|
|
}
|
2025-07-27 12:39:43 +00:00
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
setmetatable(observer, observer)
|
2025-07-27 12:39:43 +00:00
|
|
|
|
2025-08-31 04:00:56 +00:00
|
|
|
return (observer :: any) :: Observer<T...>
|
2025-07-27 12:39:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
monitor = monitors_new,
|
2025-08-31 04:00:56 +00:00
|
|
|
observer = observers_new,
|
2025-07-27 12:39:43 +00:00
|
|
|
}
|