mirror of
https://github.com/Ukendio/jecs.git
synced 2025-09-23 08:39:16 +00:00
Compare commits
9 commits
Author | SHA1 | Date | |
---|---|---|---|
|
9514fb758a | ||
|
e2ab3be3e5 | ||
|
35cb0bca4e | ||
|
7e1f43aff5 | ||
|
690e9ec4d7 | ||
|
8ed8c2a0e0 | ||
|
23bf021f01 | ||
|
4ed2fb7a40 | ||
|
e16e4a04e4 |
7 changed files with 521 additions and 178 deletions
|
@ -12,6 +12,10 @@ export type Iter<T...> = (Observer<T...>) -> () -> (jecs.Entity, T...)
|
||||||
|
|
||||||
export type Observer<T...> = {
|
export type Observer<T...> = {
|
||||||
disconnect: (Observer<T...>) -> (),
|
disconnect: (Observer<T...>) -> (),
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Monitor<T...> = {
|
||||||
|
disconnect: (Observer<T...>) -> (),
|
||||||
added: ((jecs.Entity) -> ()) -> (),
|
added: ((jecs.Entity) -> ()) -> (),
|
||||||
removed: ((jecs.Entity) -> ()) -> ()
|
removed: ((jecs.Entity) -> ()) -> ()
|
||||||
}
|
}
|
||||||
|
@ -22,11 +26,12 @@ local function observers_new<T...>(
|
||||||
): Observer<T...>
|
): Observer<T...>
|
||||||
query:cached()
|
query:cached()
|
||||||
|
|
||||||
|
|
||||||
local world = (query :: Query<T...> & { world: World }).world
|
local world = (query :: Query<T...> & { world: World }).world
|
||||||
callback = callback
|
callback = callback
|
||||||
|
|
||||||
local archetypes = {}
|
local archetypes = {}
|
||||||
local terms = query.ids
|
local terms = query.filter_with :: { jecs.Id }
|
||||||
local first = terms[1]
|
local first = terms[1]
|
||||||
|
|
||||||
local observers_on_create = world.observable[jecs.ArchetypeCreate][first]
|
local observers_on_create = world.observable[jecs.ArchetypeCreate][first]
|
||||||
|
@ -42,6 +47,10 @@ local function observers_new<T...>(
|
||||||
|
|
||||||
local entity_index = world.entity_index :: any
|
local entity_index = world.entity_index :: any
|
||||||
|
|
||||||
|
for _, archetype in query:archetypes() do
|
||||||
|
archetypes[archetype.id] = true
|
||||||
|
end
|
||||||
|
|
||||||
local function emplaced<T, a>(
|
local function emplaced<T, a>(
|
||||||
entity: jecs.Entity<T>,
|
entity: jecs.Entity<T>,
|
||||||
id: jecs.Id<a>,
|
id: jecs.Id<a>,
|
||||||
|
@ -72,8 +81,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_FIRST(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 id ~= term 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
|
||||||
|
@ -84,9 +110,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(
|
||||||
|
@ -113,7 +141,7 @@ local function observers_new<T...>(
|
||||||
return (observer :: any) :: Observer<T...>
|
return (observer :: any) :: Observer<T...>
|
||||||
end
|
end
|
||||||
|
|
||||||
local function monitors_new<T...>(query: Query<T...>): Observer<T...>
|
local function monitors_new<T...>(query: Query<T...>): Monitor<T...>
|
||||||
query:cached()
|
query:cached()
|
||||||
|
|
||||||
local world = (query :: Query<T...> & { world: World }).world
|
local world = (query :: Query<T...> & { world: World }).world
|
||||||
|
@ -132,6 +160,10 @@ local function monitors_new<T...>(query: Query<T...>): Observer<T...>
|
||||||
observer_on_delete.callback = function(archetype)
|
observer_on_delete.callback = function(archetype)
|
||||||
archetypes[archetype.id] = nil
|
archetypes[archetype.id] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
for _, archetype in query:archetypes() do
|
||||||
|
archetypes[archetype.id] = true
|
||||||
|
end
|
||||||
local entity_index = world.entity_index :: any
|
local entity_index = world.entity_index :: any
|
||||||
|
|
||||||
local callback_added: ((jecs.Entity) -> ())?
|
local callback_added: ((jecs.Entity) -> ())?
|
||||||
|
@ -183,9 +215,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
|
||||||
|
@ -203,6 +283,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(
|
||||||
|
@ -236,7 +317,7 @@ local function monitors_new<T...>(query: Query<T...>): Observer<T...>
|
||||||
removed = monitor_removed
|
removed = monitor_removed
|
||||||
}
|
}
|
||||||
|
|
||||||
return (observer :: any) :: Observer<T...>
|
return (observer :: any) :: Monitor<T...>
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
15
jecs.d.ts
vendored
15
jecs.d.ts
vendored
|
@ -236,6 +236,12 @@ export class World {
|
||||||
*/
|
*/
|
||||||
contains(entity: Entity): boolean;
|
contains(entity: Entity): boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if an entity exists in the world.
|
||||||
|
* @param entity The entity to verify.
|
||||||
|
*/
|
||||||
|
contains(entity: number): entity is Entity;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if an entity with the given ID is currently alive, ignoring its generation.
|
* Checks if an entity with the given ID is currently alive, ignoring its generation.
|
||||||
* @param entity The entity to verify.
|
* @param entity The entity to verify.
|
||||||
|
@ -344,12 +350,19 @@ export type ComponentRecord = {
|
||||||
export function component_record(world: World, id: Id): ComponentRecord;
|
export function component_record(world: World, id: Id): ComponentRecord;
|
||||||
|
|
||||||
type TagToUndefined<T> = T extends TagDiscriminator ? undefined : T
|
type TagToUndefined<T> = T extends TagDiscriminator ? undefined : T
|
||||||
|
type TrimOptional<T extends unknown[]> = T extends [...infer L, infer R]
|
||||||
|
? unknown extends R
|
||||||
|
? L | T | TrimOptional<L>
|
||||||
|
: R extends undefined
|
||||||
|
? L | T | TrimOptional<L>
|
||||||
|
: T
|
||||||
|
: T
|
||||||
|
|
||||||
export function bulk_insert<const C extends Id[]>(
|
export function bulk_insert<const C extends Id[]>(
|
||||||
world: World,
|
world: World,
|
||||||
entity: Entity,
|
entity: Entity,
|
||||||
ids: C,
|
ids: C,
|
||||||
values: { [K in keyof C]: TagToUndefined<InferComponent<C[K]>> },
|
values: TrimOptional<{ [K in keyof C]: TagToUndefined<InferComponent<C[K]>> }>,
|
||||||
): void;
|
): void;
|
||||||
export function bulk_remove(world: World, entity: Entity, ids: Id[]): void;
|
export function bulk_remove(world: World, entity: Entity, ids: Id[]): void;
|
||||||
|
|
||||||
|
|
|
@ -1607,6 +1607,9 @@ local function query_cached(query: QueryInner)
|
||||||
local lastArchetype = 1
|
local lastArchetype = 1
|
||||||
|
|
||||||
local A, B, C, D, E, F, G, H, I = unpack(ids :: { Id })
|
local A, B, C, D, E, F, G, H, I = unpack(ids :: { Id })
|
||||||
|
if not A then
|
||||||
|
A = query.filter_with[1]
|
||||||
|
end
|
||||||
local a: Column, b: Column, c: Column, d: Column
|
local a: Column, b: Column, c: Column, d: Column
|
||||||
local e: Column, f: Column, g: Column, h: Column
|
local e: Column, f: Column, g: Column, h: Column
|
||||||
|
|
||||||
|
@ -3244,8 +3247,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"))
|
||||||
|
|
|
@ -5,20 +5,108 @@ local CASE, TEST, FINISH, CHECK = test.CASE, test.TEST, test.FINISH, test.CHECK
|
||||||
local FOCUS = test.FOCUS
|
local FOCUS = test.FOCUS
|
||||||
local ob = require("@addons/ob")
|
local ob = require("@addons/ob")
|
||||||
|
|
||||||
TEST("addons/ob", function()
|
TEST("addons/ob::observer", function()
|
||||||
local world = jecs.world()
|
local world = jecs.world()
|
||||||
do CASE "Should support query:without()"
|
do CASE "should match against archetypes that were already created"
|
||||||
|
local A = world:component()
|
||||||
|
|
||||||
|
local e1 = world:entity()
|
||||||
|
world:add(e1, A)
|
||||||
|
|
||||||
|
local c = 1
|
||||||
|
ob.observer(world:query(A), function()
|
||||||
|
c+=1
|
||||||
|
end)
|
||||||
|
|
||||||
|
world:remove(e1, A)
|
||||||
|
world:add(e1, A)
|
||||||
|
CHECK(c==2)
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "Should enter observer 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
|
||||||
|
|
||||||
|
ob.observer(world:query(A):without(jecs.pair(B, C)), function()
|
||||||
|
c += 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
local child = world:entity()
|
||||||
|
world:add(child, A)
|
||||||
|
CHECK(c==2)
|
||||||
|
world:add(child, jecs.pair(B, D))
|
||||||
|
CHECK(c==2)
|
||||||
|
world:add(child, jecs.pair(B, C))
|
||||||
|
CHECK(c==2)
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "Should enter observer 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()
|
||||||
|
|
||||||
|
local e = world:entity()
|
||||||
|
world:add(e, A)
|
||||||
|
world:add(e, jecs.pair(B, D))
|
||||||
|
|
||||||
|
local c = 1
|
||||||
|
ob.observer(world:query(A):without(jecs.pair(B, D)), function()
|
||||||
|
c += 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)
|
||||||
|
world:remove(child, jecs.pair(B, C))
|
||||||
|
CHECK(c==2)
|
||||||
|
world:remove(child, jecs.pair(B, D))
|
||||||
|
CHECK(c==3)
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "Should enter observer 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
|
||||||
|
ob.observer(world:query(A):without(jecs.pair(B, jecs.w)), function() c+= 1 end)
|
||||||
|
|
||||||
|
local child = world:entity()
|
||||||
|
world:add(child, A)
|
||||||
|
CHECK(c==2)
|
||||||
|
world:add(child, jecs.pair(B, D))
|
||||||
|
CHECK(c==2)
|
||||||
|
world:add(child, jecs.pair(B, C))
|
||||||
|
CHECK(c==2)
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "Should enter observer at query:without(t1) when removing t1"
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
local B = world:component()
|
local B = world:component()
|
||||||
|
|
||||||
local c = 1
|
local c = 1
|
||||||
local monitor = ob.monitor(world:query(A):without(B))
|
ob.observer(world:query(A):without(B), function() c+= 1 end)
|
||||||
monitor.added(function()
|
|
||||||
c += 1
|
|
||||||
end)
|
|
||||||
monitor.removed(function()
|
|
||||||
c += 1
|
|
||||||
end)
|
|
||||||
|
|
||||||
local child = world:entity()
|
local child = world:entity()
|
||||||
world:add(child, B)
|
world:add(child, B)
|
||||||
|
@ -28,28 +116,22 @@ TEST("addons/ob", function()
|
||||||
world:remove(child, B)
|
world:remove(child, B)
|
||||||
CHECK(c==2)
|
CHECK(c==2)
|
||||||
world:remove(child, A)
|
world:remove(child, A)
|
||||||
CHECK(c==3)
|
CHECK(c==2)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "monitors should accept pairs"
|
do CASE "observers should accept pairs"
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
local B = world:component()
|
local B = world:component()
|
||||||
|
|
||||||
local c = 1
|
local c = 1
|
||||||
local monitor = ob.monitor(world:query(jecs.pair(A, B)))
|
ob.observer(world:query(jecs.pair(A, B)), function() c+= 1 end)
|
||||||
monitor.added(function()
|
|
||||||
c += 1
|
|
||||||
end)
|
|
||||||
monitor.removed(function()
|
|
||||||
c += 1
|
|
||||||
end)
|
|
||||||
|
|
||||||
local child = world:entity()
|
local child = world:entity()
|
||||||
world:add(child, jecs.pair(A, B))
|
world:add(child, jecs.pair(A, B))
|
||||||
CHECK(c == 2)
|
CHECK(c == 2)
|
||||||
|
|
||||||
world:remove(child, jecs.pair(A, B))
|
world:remove(child, jecs.pair(A, B))
|
||||||
CHECK(c == 3)
|
CHECK(c == 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "Ensure ordering between signals and observers"
|
do CASE "Ensure ordering between signals and observers"
|
||||||
|
@ -99,6 +181,191 @@ TEST("addons/ob", function()
|
||||||
CHECK(count == 4)
|
CHECK(count == 4)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
do CASE "Call off pairs"
|
||||||
|
local A = world:component()
|
||||||
|
|
||||||
|
local callcount = 1
|
||||||
|
world:added(A, function(entity)
|
||||||
|
callcount += 1
|
||||||
|
end)
|
||||||
|
world:added(A, function(entity)
|
||||||
|
callcount += 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
local e = world:entity()
|
||||||
|
local e2 = world:entity()
|
||||||
|
|
||||||
|
world:add(e2, jecs.pair(A, e))
|
||||||
|
world:add(e, jecs.pair(A, e2))
|
||||||
|
CHECK(callcount == 1 + 2 * 2)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
TEST("addons/ob::monitor", function()
|
||||||
|
local world = jecs.world()
|
||||||
|
do CASE "should match against archetypes that were already created"
|
||||||
|
local A = world:component()
|
||||||
|
|
||||||
|
local e1 = world:entity()
|
||||||
|
world:add(e1, A)
|
||||||
|
|
||||||
|
local monitor = ob.monitor(world:query(A))
|
||||||
|
local c = 1
|
||||||
|
monitor.added(function()
|
||||||
|
c += 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
world:remove(e1, A)
|
||||||
|
world:add(e1, A)
|
||||||
|
CHECK(c==2)
|
||||||
|
end
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
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 B = world:component()
|
||||||
|
|
||||||
|
local c = 1
|
||||||
|
local monitor = ob.monitor(world:query(A):without(B))
|
||||||
|
monitor.added(function()
|
||||||
|
c += 1
|
||||||
|
end)
|
||||||
|
monitor.removed(function()
|
||||||
|
c += 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
local child = world:entity()
|
||||||
|
world:add(child, B)
|
||||||
|
CHECK(c==1)
|
||||||
|
world:add(child, A)
|
||||||
|
CHECK(c==1)
|
||||||
|
world:remove(child, B)
|
||||||
|
CHECK(c==2)
|
||||||
|
world:remove(child, A)
|
||||||
|
CHECK(c==3)
|
||||||
|
end
|
||||||
|
|
||||||
|
do CASE "monitors should accept pairs"
|
||||||
|
local A = world:component()
|
||||||
|
local B = world:component()
|
||||||
|
|
||||||
|
local c = 1
|
||||||
|
local monitor = ob.monitor(world:query(jecs.pair(A, B)))
|
||||||
|
monitor.added(function()
|
||||||
|
c += 1
|
||||||
|
end)
|
||||||
|
monitor.removed(function()
|
||||||
|
c += 1
|
||||||
|
end)
|
||||||
|
|
||||||
|
local child = world:entity()
|
||||||
|
world:add(child, jecs.pair(A, B))
|
||||||
|
CHECK(c == 2)
|
||||||
|
|
||||||
|
world:remove(child, jecs.pair(A, B))
|
||||||
|
CHECK(c == 3)
|
||||||
|
end
|
||||||
|
|
||||||
do CASE "Don't report changed components in monitor"
|
do CASE "Don't report changed components in monitor"
|
||||||
local A = world:component()
|
local A = world:component()
|
||||||
local count = 1
|
local count = 1
|
||||||
|
@ -120,25 +387,6 @@ TEST("addons/ob", function()
|
||||||
world:set(e, A, false)
|
world:set(e, A, false)
|
||||||
CHECK(count == 4)
|
CHECK(count == 4)
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE "Call off pairs"
|
|
||||||
local A = world:component()
|
|
||||||
|
|
||||||
local callcount = 1
|
|
||||||
world:added(A, function(entity)
|
|
||||||
callcount += 1
|
|
||||||
end)
|
|
||||||
world:added(A, function(entity)
|
|
||||||
callcount += 1
|
|
||||||
end)
|
|
||||||
|
|
||||||
local e = world:entity()
|
|
||||||
local e2 = world:entity()
|
|
||||||
|
|
||||||
world:add(e2, jecs.pair(A, e))
|
|
||||||
world:add(e, jecs.pair(A, e2))
|
|
||||||
CHECK(callcount == 1 + 2 * 2)
|
|
||||||
end
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
return FINISH()
|
return FINISH()
|
||||||
|
|
|
@ -1365,7 +1365,6 @@ TEST("world:added", function()
|
||||||
end
|
end
|
||||||
|
|
||||||
do CASE ""
|
do CASE ""
|
||||||
local world = jecs.world()
|
|
||||||
local IsNearby = world:component()
|
local IsNearby = world:component()
|
||||||
world:set(IsNearby, jecs.Name, "IsNearby")
|
world:set(IsNearby, jecs.Name, "IsNearby")
|
||||||
local person1, person2 = world:entity(), world:entity()
|
local person1, person2 = world:entity(), world:entity()
|
||||||
|
|
Loading…
Reference in a new issue