mirror of
https://github.com/Ukendio/jecs.git
synced 2025-09-23 08:39:16 +00:00
Compare commits
No commits in common. "main" and "v0.9.0" have entirely different histories.
7 changed files with 174 additions and 517 deletions
|
@ -12,10 +12,6 @@ export type Iter<T...> = (Observer<T...>) -> () -> (jecs.Entity, T...)
|
|||
|
||||
export type Observer<T...> = {
|
||||
disconnect: (Observer<T...>) -> (),
|
||||
}
|
||||
|
||||
export type Monitor<T...> = {
|
||||
disconnect: (Observer<T...>) -> (),
|
||||
added: ((jecs.Entity) -> ()) -> (),
|
||||
removed: ((jecs.Entity) -> ()) -> ()
|
||||
}
|
||||
|
@ -26,12 +22,11 @@ local function observers_new<T...>(
|
|||
): Observer<T...>
|
||||
query:cached()
|
||||
|
||||
|
||||
local world = (query :: Query<T...> & { world: World }).world
|
||||
callback = callback
|
||||
|
||||
local archetypes = {}
|
||||
local terms = query.filter_with :: { jecs.Id }
|
||||
local terms = query.ids
|
||||
local first = terms[1]
|
||||
|
||||
local observers_on_create = world.observable[jecs.ArchetypeCreate][first]
|
||||
|
@ -47,10 +42,6 @@ local function observers_new<T...>(
|
|||
|
||||
local entity_index = world.entity_index :: any
|
||||
|
||||
for _, archetype in query:archetypes() do
|
||||
archetypes[archetype.id] = true
|
||||
end
|
||||
|
||||
local function emplaced<T, a>(
|
||||
entity: jecs.Entity<T>,
|
||||
id: jecs.Id<a>,
|
||||
|
@ -81,25 +72,8 @@ local function observers_new<T...>(
|
|||
if without then
|
||||
for _, term in without 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 onremoved = world:removed(rel, function(entity, id)
|
||||
if not wc and id ~= term then
|
||||
return
|
||||
term = jecs.ECS_PAIR_FIRST(term)
|
||||
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 r = jecs.record(world, entity)
|
||||
local archetype = r.archetype
|
||||
|
@ -110,11 +84,9 @@ local function observers_new<T...>(
|
|||
end
|
||||
end
|
||||
end)
|
||||
|
||||
table.insert(cleanup, onremoved)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function disconnect()
|
||||
table.remove(observers_on_create, table.find(
|
||||
|
@ -141,7 +113,7 @@ local function observers_new<T...>(
|
|||
return (observer :: any) :: Observer<T...>
|
||||
end
|
||||
|
||||
local function monitors_new<T...>(query: Query<T...>): Monitor<T...>
|
||||
local function monitors_new<T...>(query: Query<T...>): Observer<T...>
|
||||
query:cached()
|
||||
|
||||
local world = (query :: Query<T...> & { world: World }).world
|
||||
|
@ -160,10 +132,6 @@ local function monitors_new<T...>(query: Query<T...>): Monitor<T...>
|
|||
observer_on_delete.callback = function(archetype)
|
||||
archetypes[archetype.id] = nil
|
||||
end
|
||||
|
||||
for _, archetype in query:archetypes() do
|
||||
archetypes[archetype.id] = true
|
||||
end
|
||||
local entity_index = world.entity_index :: any
|
||||
|
||||
local callback_added: ((jecs.Entity) -> ())?
|
||||
|
@ -215,57 +183,9 @@ local function monitors_new<T...>(query: Query<T...>): Monitor<T...>
|
|||
if without then
|
||||
for _, term in without 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_removed == nil then
|
||||
return
|
||||
term = jecs.ECS_PAIR_FIRST(term)
|
||||
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_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 onadded = world:added(term, removed)
|
||||
local onremoved = world:removed(term, function(entity, id)
|
||||
if callback_added == nil then
|
||||
return
|
||||
|
@ -283,7 +203,6 @@ local function monitors_new<T...>(query: Query<T...>): Monitor<T...>
|
|||
table.insert(cleanup, onremoved)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function disconnect()
|
||||
table.remove(observers_on_create, table.find(
|
||||
|
@ -317,7 +236,7 @@ local function monitors_new<T...>(query: Query<T...>): Monitor<T...>
|
|||
removed = monitor_removed
|
||||
}
|
||||
|
||||
return (observer :: any) :: Monitor<T...>
|
||||
return (observer :: any) :: Observer<T...>
|
||||
end
|
||||
|
||||
return {
|
||||
|
|
15
jecs.d.ts
vendored
15
jecs.d.ts
vendored
|
@ -236,12 +236,6 @@ export class World {
|
|||
*/
|
||||
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.
|
||||
* @param entity The entity to verify.
|
||||
|
@ -350,19 +344,12 @@ export type ComponentRecord = {
|
|||
export function component_record(world: World, id: Id): ComponentRecord;
|
||||
|
||||
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[]>(
|
||||
world: World,
|
||||
entity: Entity,
|
||||
ids: C,
|
||||
values: TrimOptional<{ [K in keyof C]: TagToUndefined<InferComponent<C[K]>> }>,
|
||||
values: { [K in keyof C]: TagToUndefined<InferComponent<C[K]>> },
|
||||
): void;
|
||||
export function bulk_remove(world: World, entity: Entity, ids: Id[]): void;
|
||||
|
||||
|
|
|
@ -1607,9 +1607,6 @@ local function query_cached(query: QueryInner)
|
|||
local lastArchetype = 1
|
||||
|
||||
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 e: Column, f: Column, g: Column, h: Column
|
||||
|
||||
|
@ -3247,7 +3244,8 @@ local function world_new()
|
|||
return world
|
||||
end
|
||||
|
||||
-- type function ecs_id_t(ty)
|
||||
-- type function ecs_id_t(entity)
|
||||
-- local ty = entity:components()[2]
|
||||
-- local __T = ty:readproperty(types.singleton("__T"))
|
||||
-- if not __T then
|
||||
-- return ty:readproperty(types.singleton("__jecs_pair_value"))
|
||||
|
|
|
@ -5,109 +5,21 @@ local CASE, TEST, FINISH, CHECK = test.CASE, test.TEST, test.FINISH, test.CHECK
|
|||
local FOCUS = test.FOCUS
|
||||
local ob = require("@addons/ob")
|
||||
|
||||
TEST("addons/ob::observer", function()
|
||||
TEST("addons/ob", 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 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)"
|
||||
do CASE "Should support query:without()"
|
||||
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()
|
||||
local monitor = ob.monitor(world:query(A):without(B))
|
||||
monitor.added(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()
|
||||
monitor.removed(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 B = world:component()
|
||||
|
||||
local c = 1
|
||||
ob.observer(world:query(A):without(B), function() c+= 1 end)
|
||||
|
||||
local child = world:entity()
|
||||
world:add(child, B)
|
||||
CHECK(c==1)
|
||||
|
@ -116,22 +28,28 @@ TEST("addons/ob::observer", function()
|
|||
world:remove(child, B)
|
||||
CHECK(c==2)
|
||||
world:remove(child, A)
|
||||
CHECK(c==2)
|
||||
CHECK(c==3)
|
||||
end
|
||||
|
||||
do CASE "observers should accept pairs"
|
||||
do CASE "monitors should accept pairs"
|
||||
local A = world:component()
|
||||
local B = world:component()
|
||||
|
||||
local c = 1
|
||||
ob.observer(world:query(jecs.pair(A, B)), function() c+= 1 end)
|
||||
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 == 2)
|
||||
CHECK(c == 3)
|
||||
end
|
||||
|
||||
do CASE "Ensure ordering between signals and observers"
|
||||
|
@ -181,191 +99,6 @@ TEST("addons/ob::observer", function()
|
|||
CHECK(count == 4)
|
||||
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"
|
||||
local A = world:component()
|
||||
local count = 1
|
||||
|
@ -387,6 +120,25 @@ TEST("addons/ob::monitor", function()
|
|||
world:set(e, A, false)
|
||||
CHECK(count == 4)
|
||||
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)
|
||||
|
||||
return FINISH()
|
||||
|
|
|
@ -1365,6 +1365,7 @@ TEST("world:added", function()
|
|||
end
|
||||
|
||||
do CASE ""
|
||||
local world = jecs.world()
|
||||
local IsNearby = world:component()
|
||||
world:set(IsNearby, jecs.Name, "IsNearby")
|
||||
local person1, person2 = world:entity(), world:entity()
|
||||
|
|
Loading…
Reference in a new issue