mirror of
https://github.com/Ukendio/jecs.git
synced 2025-09-23 16:39:17 +00:00
Compare commits
2 commits
4ed2fb7a40
...
8ed8c2a0e0
Author | SHA1 | Date | |
---|---|---|---|
|
8ed8c2a0e0 | ||
|
23bf021f01 |
3 changed files with 202 additions and 32 deletions
|
@ -77,8 +77,25 @@ local function observers_new<T...>(
|
||||||
if without then
|
if without then
|
||||||
for _, term in without do
|
for _, term in without do
|
||||||
if jecs.IS_PAIR(term) then
|
if jecs.IS_PAIR(term) then
|
||||||
term = jecs.ECS_PAIR_FIRST(term)
|
local rel = jecs.ECS_PAIR_SECOND(term)
|
||||||
|
local tgt = jecs.ECS_PAIR_SECOND(term)
|
||||||
|
local wc = tgt == jecs.w
|
||||||
|
local onremoved = world:removed(rel, function(entity, id)
|
||||||
|
if not wc and term ~= tgt then
|
||||||
|
return
|
||||||
end
|
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(entity)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
table.insert(cleanup, onremoved)
|
||||||
|
else
|
||||||
local onremoved = world:removed(term, function(entity, id)
|
local onremoved = world:removed(term, function(entity, id)
|
||||||
local r = jecs.record(world, entity)
|
local r = jecs.record(world, entity)
|
||||||
local archetype = r.archetype
|
local archetype = r.archetype
|
||||||
|
@ -89,9 +106,11 @@ local function observers_new<T...>(
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
table.insert(cleanup, onremoved)
|
table.insert(cleanup, onremoved)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function disconnect()
|
local function disconnect()
|
||||||
table.remove(observers_on_create, table.find(
|
table.remove(observers_on_create, table.find(
|
||||||
|
@ -192,9 +211,57 @@ local function monitors_new<T...>(query: Query<T...>): Observer<T...>
|
||||||
if without then
|
if without then
|
||||||
for _, term in without do
|
for _, term in without do
|
||||||
if jecs.IS_PAIR(term) then
|
if jecs.IS_PAIR(term) then
|
||||||
term = jecs.ECS_PAIR_FIRST(term)
|
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
|
end
|
||||||
local onadded = world:added(term, removed)
|
if not wc and id ~= term 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(rel, function(entity, id)
|
||||||
|
if callback_added == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if not wc and id ~= term 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)
|
||||||
|
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)
|
local onremoved = world:removed(term, function(entity, id)
|
||||||
if callback_added == nil then
|
if callback_added == nil then
|
||||||
return
|
return
|
||||||
|
@ -212,6 +279,7 @@ local function monitors_new<T...>(query: Query<T...>): Observer<T...>
|
||||||
table.insert(cleanup, onremoved)
|
table.insert(cleanup, onremoved)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function disconnect()
|
local function disconnect()
|
||||||
table.remove(observers_on_create, table.find(
|
table.remove(observers_on_create, table.find(
|
||||||
|
|
|
@ -3244,8 +3244,7 @@ local function world_new()
|
||||||
return world
|
return world
|
||||||
end
|
end
|
||||||
|
|
||||||
-- type function ecs_id_t(entity)
|
-- type function ecs_id_t(ty)
|
||||||
-- local ty = entity:components()[2]
|
|
||||||
-- local __T = ty:readproperty(types.singleton("__T"))
|
-- local __T = ty:readproperty(types.singleton("__T"))
|
||||||
-- if not __T then
|
-- if not __T then
|
||||||
-- return ty:readproperty(types.singleton("__jecs_pair_value"))
|
-- return ty:readproperty(types.singleton("__jecs_pair_value"))
|
||||||
|
|
|
@ -24,7 +24,110 @@ TEST("addons/ob", function()
|
||||||
CHECK(c==2)
|
CHECK(c==2)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "Should support query:without()"
|
do CASE "Should enter monitor at query:without(pair(R, t1)) when adding pair(R, t2)"
|
||||||
|
local A = world:component()
|
||||||
|
local B = world:component()
|
||||||
|
local C = world:component()
|
||||||
|
local D = world:component()
|
||||||
|
|
||||||
|
local e = world:entity()
|
||||||
|
world:add(e, A)
|
||||||
|
world:add(e, jecs.pair(B, D))
|
||||||
|
|
||||||
|
local c = 1
|
||||||
|
local r = 1
|
||||||
|
local monitor = ob.monitor(world:query(A):without(jecs.pair(B, C)))
|
||||||
|
monitor.added(function()
|
||||||
|
c += 1
|
||||||
|
end)
|
||||||
|
monitor.removed(function()
|
||||||
|
r += 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
local child = world:entity()
|
||||||
|
world:add(child, A)
|
||||||
|
CHECK(c==2)
|
||||||
|
world:add(child, jecs.pair(B, D))
|
||||||
|
CHECK(c==2)
|
||||||
|
CHECK(r==1)
|
||||||
|
world:add(child, jecs.pair(B, C))
|
||||||
|
CHECK(c==2)
|
||||||
|
CHECK(r==2)
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "Should enter monitor at query:without(pair(R, t1)) when removing pair(R, t1)"
|
||||||
|
local A = world:component()
|
||||||
|
local B = world:component()
|
||||||
|
local C = world:component()
|
||||||
|
local D = world:component()
|
||||||
|
|
||||||
|
print("B", B)
|
||||||
|
|
||||||
|
local e = world:entity()
|
||||||
|
world:add(e, A)
|
||||||
|
world:add(e, jecs.pair(B, D))
|
||||||
|
|
||||||
|
local c = 1
|
||||||
|
local r = 1
|
||||||
|
local monitor = ob.monitor(world:query(A):without(jecs.pair(B, D)))
|
||||||
|
monitor.added(function()
|
||||||
|
c += 1
|
||||||
|
end)
|
||||||
|
monitor.removed(function()
|
||||||
|
r += 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
local child = world:entity()
|
||||||
|
world:add(child, jecs.pair(B, C))
|
||||||
|
world:add(child, jecs.pair(B, D))
|
||||||
|
CHECK(c==1)
|
||||||
|
world:add(child, A)
|
||||||
|
CHECK(c==1)
|
||||||
|
world:remove(child, jecs.pair(B, C))
|
||||||
|
CHECK(c==1)
|
||||||
|
world:add(child, jecs.pair(B, C))
|
||||||
|
CHECK(c==1)
|
||||||
|
world:remove(child, jecs.pair(B, D))
|
||||||
|
CHECK(c==2)
|
||||||
|
world:add(child, jecs.pair(B, D))
|
||||||
|
CHECK(c==2)
|
||||||
|
CHECK(r==2)
|
||||||
|
world:remove(child, jecs.pair(B, C))
|
||||||
|
CHECK(c==2)
|
||||||
|
CHECK(r==2)
|
||||||
|
world:remove(child, jecs.pair(B, D))
|
||||||
|
CHECK(c==3)
|
||||||
|
CHECK(r==2)
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "Should enter monitor at query:without(pair(R, *)) when adding pair(R, t1)"
|
||||||
|
local A = world:component()
|
||||||
|
local B = world:component()
|
||||||
|
local C = world:component()
|
||||||
|
local D = world:component()
|
||||||
|
|
||||||
|
local c = 1
|
||||||
|
local r = 1
|
||||||
|
local monitor = ob.monitor(world:query(A):without(jecs.pair(B, jecs.w)))
|
||||||
|
monitor.added(function()
|
||||||
|
c += 1
|
||||||
|
end)
|
||||||
|
monitor.removed(function()
|
||||||
|
r += 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
local child = world:entity()
|
||||||
|
world:add(child, A)
|
||||||
|
CHECK(c==2)
|
||||||
|
world:add(child, jecs.pair(B, D))
|
||||||
|
CHECK(c==2)
|
||||||
|
CHECK(r==2)
|
||||||
|
world:add(child, jecs.pair(B, C))
|
||||||
|
CHECK(c==2)
|
||||||
|
CHECK(r==2)
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "Should enter monitor at query:without(t1) when removing t1"
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
local B = world:component()
|
local B = world:component()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue