mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
Compare commits
5 commits
6811b6de0e
...
f9b589547b
Author | SHA1 | Date | |
---|---|---|---|
|
f9b589547b | ||
|
9c915e131b | ||
|
ca19d15d04 | ||
|
cc3367c302 | ||
|
c0aeb4e151 |
4 changed files with 54 additions and 21 deletions
35
jecs.luau
35
jecs.luau
|
@ -45,9 +45,9 @@ type ecs_id_record_t = {
|
||||||
flags: number,
|
flags: number,
|
||||||
size: number,
|
size: number,
|
||||||
hooks: {
|
hooks: {
|
||||||
on_add: ((entity: i53, data: any?) -> ())?,
|
on_add: ((entity: i53, id: i53, data: any?) -> ())?,
|
||||||
on_change: ((entity: i53, data: any) -> ())?,
|
on_change: ((entity: i53, id: i53, data: any) -> ())?,
|
||||||
on_remove: ((entity: i53) -> ())?,
|
on_remove: ((entity: i53, id: i53) -> ())?,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,7 +144,7 @@ end
|
||||||
|
|
||||||
local function ECS_META(id: i53, ty: i53, value: any?)
|
local function ECS_META(id: i53, ty: i53, value: any?)
|
||||||
local bundle = ecs_metadata[id]
|
local bundle = ecs_metadata[id]
|
||||||
if bundle then
|
if bundle == nil then
|
||||||
bundle = {}
|
bundle = {}
|
||||||
ecs_metadata[id] = bundle
|
ecs_metadata[id] = bundle
|
||||||
end
|
end
|
||||||
|
@ -500,6 +500,14 @@ local function world_has_one_inline(world: ecs_world_t, entity: i53, id: i53): b
|
||||||
return records[id] ~= nil
|
return records[id] ~= nil
|
||||||
end
|
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,
|
local function world_has(world: ecs_world_t, entity: i53,
|
||||||
a: i53, b: i53?, c: i53?, d: i53?, e: i53?): boolean
|
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
|
local on_add = idr.hooks.on_add
|
||||||
|
|
||||||
if on_add then
|
if on_add then
|
||||||
on_add(entity)
|
on_add(entity, id)
|
||||||
end
|
end
|
||||||
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.
|
-- and just set the data directly.
|
||||||
local on_change = idr_hooks.on_change
|
local on_change = idr_hooks.on_change
|
||||||
if on_change then
|
if on_change then
|
||||||
on_change(entity, data)
|
on_change(entity, id, data)
|
||||||
end
|
end
|
||||||
|
|
||||||
return
|
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
|
local on_add = idr_hooks.on_add
|
||||||
if on_add then
|
if on_add then
|
||||||
on_add(entity, data)
|
on_add(entity, id, data)
|
||||||
end
|
end
|
||||||
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 idr = world.component_index[id]
|
||||||
local on_remove = idr.hooks.on_remove
|
local on_remove = idr.hooks.on_remove
|
||||||
if on_remove then
|
if on_remove then
|
||||||
on_remove(entity)
|
on_remove(entity, id)
|
||||||
end
|
end
|
||||||
|
|
||||||
local to = archetype_traverse_remove(world, id, record.archetype)
|
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 idr = component_index[id]
|
||||||
local on_remove = idr.hooks.on_remove
|
local on_remove = idr.hooks.on_remove
|
||||||
if on_remove then
|
if on_remove then
|
||||||
on_remove(delete)
|
on_remove(delete, id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1041,7 +1049,7 @@ local function world_clear(world: ecs_world_t, entity: i53)
|
||||||
continue
|
continue
|
||||||
end
|
end
|
||||||
if not ids then
|
if not ids then
|
||||||
ids = {}
|
ids = {} :: { [i53]: boolean }
|
||||||
end
|
end
|
||||||
ids[id] = true
|
ids[id] = true
|
||||||
removal_queued = true
|
removal_queued = true
|
||||||
|
@ -1052,7 +1060,7 @@ local function world_clear(world: ecs_world_t, entity: i53)
|
||||||
end
|
end
|
||||||
|
|
||||||
if not queue then
|
if not queue then
|
||||||
queue = {}
|
queue = {} :: { i53 }
|
||||||
end
|
end
|
||||||
|
|
||||||
local n = #entities
|
local n = #entities
|
||||||
|
@ -1242,7 +1250,7 @@ local function world_delete(world: ecs_world_t, entity: i53)
|
||||||
break
|
break
|
||||||
else
|
else
|
||||||
if not ids then
|
if not ids then
|
||||||
ids = {}
|
ids = {} :: { [i53]: boolean }
|
||||||
end
|
end
|
||||||
ids[id] = true
|
ids[id] = true
|
||||||
removal_queued = true
|
removal_queued = true
|
||||||
|
@ -1253,7 +1261,7 @@ local function world_delete(world: ecs_world_t, entity: i53)
|
||||||
continue
|
continue
|
||||||
end
|
end
|
||||||
if not children then
|
if not children then
|
||||||
children = {}
|
children = {} :: { i53 }
|
||||||
end
|
end
|
||||||
local n = #entities
|
local n = #entities
|
||||||
table.move(entities, 1, n, count + 1, children)
|
table.move(entities, 1, n, count + 1, children)
|
||||||
|
@ -2523,6 +2531,7 @@ return {
|
||||||
component = (ECS_COMPONENT :: any) :: <T>() -> Entity<T>,
|
component = (ECS_COMPONENT :: any) :: <T>() -> Entity<T>,
|
||||||
tag = (ECS_TAG :: 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>,
|
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) -> ()>,
|
OnAdd = EcsOnAdd :: Entity<(entity: Entity) -> ()>,
|
||||||
OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>,
|
OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>,
|
||||||
|
|
|
@ -82,6 +82,25 @@ TEST("addons/observers", function()
|
||||||
world:set(e, A, true)
|
world:set(e, A, true)
|
||||||
CHECK(count == 3)
|
CHECK(count == 3)
|
||||||
end
|
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)
|
end)
|
||||||
|
|
||||||
return FINISH()
|
return FINISH()
|
||||||
|
|
|
@ -22,6 +22,7 @@ type Entity<T=nil> = jecs.Entity<T>
|
||||||
type Id<T=unknown> = jecs.Id<T>
|
type Id<T=unknown> = jecs.Id<T>
|
||||||
|
|
||||||
local entity_visualiser = require("@tools/entity_visualiser")
|
local entity_visualiser = require("@tools/entity_visualiser")
|
||||||
|
local lifetime_tracker_add = require("@tools/lifetime_tracker")
|
||||||
local dwi = entity_visualiser.stringify
|
local dwi = entity_visualiser.stringify
|
||||||
|
|
||||||
TEST("world:add()", function()
|
TEST("world:add()", function()
|
||||||
|
@ -246,6 +247,7 @@ TEST("world:component()", function()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("world:contains()", function()
|
TEST("world:contains()", function()
|
||||||
|
local tag = jecs.tag()
|
||||||
local world = jecs.world()
|
local world = jecs.world()
|
||||||
local id = world:entity()
|
local id = world:entity()
|
||||||
CHECK(world:contains(id))
|
CHECK(world:contains(id))
|
||||||
|
@ -255,6 +257,9 @@ TEST("world:contains()", function()
|
||||||
world:delete(id)
|
world:delete(id)
|
||||||
CHECK(not world:contains(id))
|
CHECK(not world:contains(id))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
CHECK(world:contains(tag))
|
||||||
|
jecs.ECS_META_RESET()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
TEST("world:delete()", function()
|
TEST("world:delete()", function()
|
||||||
|
@ -628,6 +633,7 @@ end)
|
||||||
|
|
||||||
TEST("world:entity()", function()
|
TEST("world:entity()", function()
|
||||||
local N = 2^8
|
local N = 2^8
|
||||||
|
|
||||||
do CASE "unique IDs"
|
do CASE "unique IDs"
|
||||||
local world = jecs.world()
|
local world = jecs.world()
|
||||||
local set = {}
|
local set = {}
|
||||||
|
@ -1427,8 +1433,6 @@ TEST("#adding a recycled target", function()
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
TEST("#repro2", function()
|
TEST("#repro2", function()
|
||||||
local world = jecs.world()
|
local world = jecs.world()
|
||||||
local Lifetime = world:component() :: Id<number>
|
local Lifetime = world:component() :: Id<number>
|
||||||
|
|
|
@ -226,6 +226,8 @@ local function CHECK<T>(value: T, stack: number?): T?
|
||||||
return value
|
return value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local test_focused = false
|
||||||
|
|
||||||
local function TEST(name: string, fn: () -> ())
|
local function TEST(name: string, fn: () -> ())
|
||||||
|
|
||||||
test = {
|
test = {
|
||||||
|
@ -238,6 +240,10 @@ local function TEST(name: string, fn: () -> ())
|
||||||
|
|
||||||
local t = test
|
local t = test
|
||||||
|
|
||||||
|
if check_for_focused and not test_focused then
|
||||||
|
test.focus = true
|
||||||
|
test_focused = true
|
||||||
|
end
|
||||||
table.insert(tests, t)
|
table.insert(tests, t)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -245,15 +251,10 @@ local function FOCUS()
|
||||||
assert(test, "no active test")
|
assert(test, "no active test")
|
||||||
|
|
||||||
check_for_focused = true
|
check_for_focused = true
|
||||||
if test.case then
|
test_focused = false
|
||||||
test.case.focus = true
|
|
||||||
else
|
|
||||||
test.focus = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function FINISH(): number
|
local function FINISH(): number
|
||||||
local success = true
|
|
||||||
local total_cases = 0
|
local total_cases = 0
|
||||||
local passed_cases = 0
|
local passed_cases = 0
|
||||||
local passed_focus_cases = 0
|
local passed_focus_cases = 0
|
||||||
|
|
Loading…
Reference in a new issue