mirror of
https://github.com/Ukendio/jecs.git
synced 2025-07-09 08:09:18 +00:00
Updated observers
This commit is contained in:
parent
b92cf9ab76
commit
6dfb428296
1 changed files with 25 additions and 17 deletions
|
@ -4,11 +4,11 @@ export type PatchedWorld = jecs.World & {
|
||||||
added: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
|
added: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
|
||||||
removed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id) -> ()) -> () -> (),
|
removed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id) -> ()) -> () -> (),
|
||||||
changed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
|
changed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
|
||||||
observer: (
|
observer: <T...>(
|
||||||
PatchedWorld,
|
PatchedWorld,
|
||||||
any,
|
jecs.Query<T...>,
|
||||||
(jecs.Entity) -> ()
|
(jecs.Entity) -> ()
|
||||||
) -> (),
|
) -> () -> (jecs.Entity),
|
||||||
monitor: (
|
monitor: (
|
||||||
PatchedWorld,
|
PatchedWorld,
|
||||||
any,
|
any,
|
||||||
|
@ -25,6 +25,8 @@ local function observers_new(world, query, callback)
|
||||||
end
|
end
|
||||||
|
|
||||||
local entity_index = world.entity_index :: any
|
local entity_index = world.entity_index :: any
|
||||||
|
local i = 0
|
||||||
|
local entities = {}
|
||||||
local function emplaced(entity, id, value)
|
local function emplaced(entity, id, value)
|
||||||
local r = jecs.entity_index_try_get_fast(
|
local r = jecs.entity_index_try_get_fast(
|
||||||
entity_index, entity :: any)
|
entity_index, entity :: any)
|
||||||
|
@ -36,7 +38,9 @@ local function observers_new(world, query, callback)
|
||||||
local archetype = r.archetype
|
local archetype = r.archetype
|
||||||
|
|
||||||
if jecs.query_match(query, archetype) then
|
if jecs.query_match(query, archetype) then
|
||||||
callback(entity)
|
i += 1
|
||||||
|
entities[i] = entity
|
||||||
|
callback(entity, id, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -44,6 +48,21 @@ local function observers_new(world, query, callback)
|
||||||
world:added(term, emplaced)
|
world:added(term, emplaced)
|
||||||
world:changed(term, emplaced)
|
world:changed(term, emplaced)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return setmetatable({}, {
|
||||||
|
__iter = function()
|
||||||
|
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
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
local function join(world, component)
|
local function join(world, component)
|
||||||
|
@ -115,20 +134,9 @@ local function monitors_new(world, query, callback)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function removed(entity: jecs.Entity, component: jecs.Id)
|
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
|
|
||||||
end
|
|
||||||
|
|
||||||
local archetype = r.archetype
|
|
||||||
|
|
||||||
if jecs.query_match(query, archetype) then
|
|
||||||
local EcsOnRemove = jecs.OnRemove :: jecs.Id
|
local EcsOnRemove = jecs.OnRemove :: jecs.Id
|
||||||
callback(entity, EcsOnRemove)
|
callback(entity, EcsOnRemove)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
for _, term in terms do
|
for _, term in terms do
|
||||||
world:added(term, emplaced)
|
world:added(term, emplaced)
|
||||||
|
|
Loading…
Reference in a new issue