Compare commits

..

5 commits

Author SHA1 Message Date
Ukendio
f9b589547b Add test cases for preregistered tags
Some checks failed
analysis / Run Luau Analyze (push) Has been cancelled
unit-testing / Run Luau Tests (push) Has been cancelled
2025-04-22 04:23:16 +02:00
Ukendio
9c915e131b Allow focus to capture a single Test 2025-04-22 04:22:55 +02:00
Ukendio
ca19d15d04 Add is_tag function 2025-04-22 04:18:43 +02:00
Ukendio
cc3367c302 Add component to hook calls' arguments 2025-04-22 04:15:43 +02:00
Ukendio
c0aeb4e151 Fix type issues 2025-04-22 04:13:25 +02:00
4 changed files with 54 additions and 21 deletions

View file

@ -45,9 +45,9 @@ type ecs_id_record_t = {
flags: number,
size: number,
hooks: {
on_add: ((entity: i53, data: any?) -> ())?,
on_change: ((entity: i53, data: any) -> ())?,
on_remove: ((entity: i53) -> ())?,
on_add: ((entity: i53, id: i53, data: any?) -> ())?,
on_change: ((entity: i53, id: i53, data: any) -> ())?,
on_remove: ((entity: i53, id: i53) -> ())?,
},
}
@ -144,7 +144,7 @@ end
local function ECS_META(id: i53, ty: i53, value: any?)
local bundle = ecs_metadata[id]
if bundle then
if bundle == nil then
bundle = {}
ecs_metadata[id] = bundle
end
@ -500,6 +500,14 @@ local function world_has_one_inline(world: ecs_world_t, entity: i53, id: i53): b
return records[id] ~= nil
end
local function ecs_is_tag(world: ecs_world_t, entity: i53): boolean
local idr = world.component_index[entity]
if idr then
return bit32.band(idr.flags, ECS_ID_IS_TAG) ~= 0
end
return not world_has_one_inline(world, entity, EcsComponent)
end
local function world_has(world: ecs_world_t, entity: i53,
a: i53, b: i53?, c: i53?, d: i53?, e: i53?): boolean
@ -849,7 +857,7 @@ local function world_add(
local on_add = idr.hooks.on_add
if on_add then
on_add(entity)
on_add(entity, id)
end
end
@ -874,7 +882,7 @@ local function world_set(world: ecs_world_t, entity: i53, id: i53, data: unknown
-- and just set the data directly.
local on_change = idr_hooks.on_change
if on_change then
on_change(entity, data)
on_change(entity, id, data)
end
return
@ -897,7 +905,7 @@ local function world_set(world: ecs_world_t, entity: i53, id: i53, data: unknown
local on_add = idr_hooks.on_add
if on_add then
on_add(entity, data)
on_add(entity, id, data)
end
end
@ -929,7 +937,7 @@ local function world_remove(world: ecs_world_t, entity: i53, id: i53)
local idr = world.component_index[id]
local on_remove = idr.hooks.on_remove
if on_remove then
on_remove(entity)
on_remove(entity, id)
end
local to = archetype_traverse_remove(world, id, record.archetype)
@ -981,7 +989,7 @@ local function archetype_delete(world: ecs_world_t, archetype: ecs_archetype_t,
local idr = component_index[id]
local on_remove = idr.hooks.on_remove
if on_remove then
on_remove(delete)
on_remove(delete, id)
end
end
@ -1041,7 +1049,7 @@ local function world_clear(world: ecs_world_t, entity: i53)
continue
end
if not ids then
ids = {}
ids = {} :: { [i53]: boolean }
end
ids[id] = true
removal_queued = true
@ -1052,7 +1060,7 @@ local function world_clear(world: ecs_world_t, entity: i53)
end
if not queue then
queue = {}
queue = {} :: { i53 }
end
local n = #entities
@ -1242,7 +1250,7 @@ local function world_delete(world: ecs_world_t, entity: i53)
break
else
if not ids then
ids = {}
ids = {} :: { [i53]: boolean }
end
ids[id] = true
removal_queued = true
@ -1253,7 +1261,7 @@ local function world_delete(world: ecs_world_t, entity: i53)
continue
end
if not children then
children = {}
children = {} :: { i53 }
end
local n = #entities
table.move(entities, 1, n, count + 1, children)
@ -2523,6 +2531,7 @@ return {
component = (ECS_COMPONENT :: any) :: <T>() -> Entity<T>,
tag = (ECS_TAG :: any) :: <T>() -> Entity<T>,
meta = (ECS_META :: any) :: <T>(id: Entity, id: Id<T>, value: T) -> Entity<T>,
is_tag = (ecs_is_tag :: any) :: <T>(World, Id<T>) -> boolean,
OnAdd = EcsOnAdd :: Entity<(entity: Entity) -> ()>,
OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>,

View file

@ -82,6 +82,25 @@ TEST("addons/observers", function()
world:set(e, A, true)
CHECK(count == 3)
end
do CASE "Call on pairs"
local A = world:component()
local callcount = 0
world:added(A, function(entity)
callcount += 1
end)
world:added(A, function(entity)
callcount += 1
end)
local e = world:entity()
local e1 = world:entity()
world:add(e1, jecs.pair(A, e))
world:add(e, jecs.pair(A, e1))
CHECK(callcount == 4)
end
end)
return FINISH()

View file

@ -22,6 +22,7 @@ type Entity<T=nil> = jecs.Entity<T>
type Id<T=unknown> = jecs.Id<T>
local entity_visualiser = require("@tools/entity_visualiser")
local lifetime_tracker_add = require("@tools/lifetime_tracker")
local dwi = entity_visualiser.stringify
TEST("world:add()", function()
@ -246,6 +247,7 @@ TEST("world:component()", function()
end)
TEST("world:contains()", function()
local tag = jecs.tag()
local world = jecs.world()
local id = world:entity()
CHECK(world:contains(id))
@ -255,6 +257,9 @@ TEST("world:contains()", function()
world:delete(id)
CHECK(not world:contains(id))
end
CHECK(world:contains(tag))
jecs.ECS_META_RESET()
end)
TEST("world:delete()", function()
@ -628,6 +633,7 @@ end)
TEST("world:entity()", function()
local N = 2^8
do CASE "unique IDs"
local world = jecs.world()
local set = {}
@ -1427,8 +1433,6 @@ TEST("#adding a recycled target", function()
end)
TEST("#repro2", function()
local world = jecs.world()
local Lifetime = world:component() :: Id<number>

View file

@ -226,6 +226,8 @@ local function CHECK<T>(value: T, stack: number?): T?
return value
end
local test_focused = false
local function TEST(name: string, fn: () -> ())
test = {
@ -238,6 +240,10 @@ local function TEST(name: string, fn: () -> ())
local t = test
if check_for_focused and not test_focused then
test.focus = true
test_focused = true
end
table.insert(tests, t)
end
@ -245,15 +251,10 @@ local function FOCUS()
assert(test, "no active test")
check_for_focused = true
if test.case then
test.case.focus = true
else
test.focus = true
end
test_focused = false
end
local function FINISH(): number
local success = true
local total_cases = 0
local passed_cases = 0
local passed_focus_cases = 0