Alias for world consttructor

This commit is contained in:
Ukendio 2025-03-12 15:29:24 +01:00
parent 925864dd2b
commit 18019679d5
2 changed files with 109 additions and 12 deletions

View file

@ -46,7 +46,7 @@ export type Record = {
} }
type IdRecord = { type IdRecord = {
columns: { number }, cache: { number },
counts: { number }, counts: { number },
flags: number, flags: number,
size: number, size: number,
@ -480,7 +480,7 @@ local function world_target(world: World, entity: i53, relation: i24, index: num
nth = nth + count + 1 nth = nth + count + 1
end end
local tr = idr.columns[archetype_id] local tr = idr.cache[archetype_id]
nth = archetype.types[nth + tr] nth = archetype.types[nth + tr]
@ -537,7 +537,7 @@ local function id_record_ensure(world: World, id: number): IdRecord
idr = { idr = {
size = 0, size = 0,
columns = {}, cache = {},
counts = {}, counts = {},
flags = flags, flags = flags,
hooks = { hooks = {
@ -562,7 +562,7 @@ local function archetype_append_to_records(
local archetype_id = archetype.id local archetype_id = archetype.id
local archetype_records = archetype.records local archetype_records = archetype.records
local archetype_counts = archetype.counts local archetype_counts = archetype.counts
local idr_columns = idr.columns local idr_columns = idr.cache
local idr_counts = idr.counts local idr_counts = idr.counts
local tr = idr_columns[archetype_id] local tr = idr_columns[archetype_id]
if not tr then if not tr then
@ -1063,7 +1063,7 @@ local function archetype_destroy(world: World, archetype: Archetype)
for id in records do for id in records do
local idr = component_index[id] local idr = component_index[id]
idr.columns[archetype_id] = nil :: any idr.cache[archetype_id] = nil :: any
idr.counts[archetype_id] = nil idr.counts[archetype_id] = nil
idr.size -= 1 idr.size -= 1
records[id] = nil :: any records[id] = nil :: any
@ -1122,7 +1122,7 @@ do
if idr then if idr then
local flags = idr.flags local flags = idr.flags
if bit32.band(flags, ECS_ID_DELETE) ~= 0 then if bit32.band(flags, ECS_ID_DELETE) ~= 0 then
for archetype_id in idr.columns do for archetype_id in idr.cache do
local idr_archetype = archetypes[archetype_id] local idr_archetype = archetypes[archetype_id]
local entities = idr_archetype.entities local entities = idr_archetype.entities
@ -1134,7 +1134,7 @@ do
archetype_destroy(world, idr_archetype) archetype_destroy(world, idr_archetype)
end end
else else
for archetype_id in idr.columns do for archetype_id in idr.cache do
local idr_archetype = archetypes[archetype_id] local idr_archetype = archetypes[archetype_id]
local entities = idr_archetype.entities local entities = idr_archetype.entities
local n = #entities local n = #entities
@ -1153,7 +1153,7 @@ do
local children local children
local ids local ids
local count = 0 local count = 0
local archetype_ids = idr_t.columns local archetype_ids = idr_t.cache
for archetype_id in archetype_ids do for archetype_id in archetype_ids do
local idr_t_archetype = archetypes[archetype_id] local idr_t_archetype = archetypes[archetype_id]
local idr_t_types = idr_t_archetype.types local idr_t_types = idr_t_archetype.types
@ -2107,7 +2107,7 @@ local function world_query(world: World, ...)
return q return q
end end
for archetype_id in idr.columns do for archetype_id in idr.cache do
local compatibleArchetype = archetypes[archetype_id] local compatibleArchetype = archetypes[archetype_id]
if #compatibleArchetype.entities == 0 then if #compatibleArchetype.entities == 0 then
continue continue
@ -2141,9 +2141,9 @@ local function world_each(world: World, id): () -> ()
return NOOP return NOOP
end end
local idr_columns = idr.columns local idr_cache = idr.cache
local archetypes = world.archetypes local archetypes = world.archetypes
local archetype_id = next(idr_columns, nil) :: number local archetype_id = next(idr_cache, nil) :: number
local archetype = archetypes[archetype_id] local archetype = archetypes[archetype_id]
if not archetype then if not archetype then
return NOOP return NOOP
@ -2155,7 +2155,7 @@ local function world_each(world: World, id): () -> ()
return function(): any return function(): any
local entity = entities[row] local entity = entities[row]
while not entity do while not entity do
archetype_id = next(idr_columns, archetype_id) :: number archetype_id = next(idr_cache, archetype_id) :: number
if not archetype_id then if not archetype_id then
return return
end end
@ -2488,6 +2488,7 @@ export type World = {
return { return {
World = World :: { new: () -> World }, World = World :: { new: () -> World },
world = World.new :: () -> World,
OnAdd = EcsOnAdd :: Entity<(entity: Entity) -> ()>, OnAdd = EcsOnAdd :: Entity<(entity: Entity) -> ()>,
OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>, OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>,

View file

@ -26,6 +26,50 @@ local N = 2 ^ 8
type World = jecs.World type World = jecs.World
type Entity<T=nil> = jecs.Entity<T> type Entity<T=nil> = jecs.Entity<T>
local c = {
white_underline = function(s: any)
return `\27[1;4m{s}\27[0m`
end,
white = function(s: any)
return `\27[37;1m{s}\27[0m`
end,
green = function(s: any)
return `\27[32;1m{s}\27[0m`
end,
red = function(s: any)
return `\27[31;1m{s}\27[0m`
end,
yellow = function(s: any)
return `\27[33;1m{s}\27[0m`
end,
red_highlight = function(s: any)
return `\27[41;1;30m{s}\27[0m`
end,
green_highlight = function(s: any)
return `\27[42;1;30m{s}\27[0m`
end,
gray = function(s: any)
return `\27[30;1m{s}\27[0m`
end,
}
local function pe(e)
local gen = ECS_GENERATION(e)
return c.green(`e{ECS_ID(e)}`)..c.yellow(`v{gen}`)
end
local function pp(e)
local gen = ECS_GENERATION(e)
return c.green(`e{ECS_ID(e)}`)..c.yellow(`v{jecs.ECS_ENTITY_T_HI(e)}`)
end
local function debug_world_inspect(world: World) local function debug_world_inspect(world: World)
local function record(e): jecs.Record local function record(e): jecs.Record
return entity_index_try_get_any(world.entity_index, e) :: any return entity_index_try_get_any(world.entity_index, e) :: any
@ -67,10 +111,61 @@ local function debug_world_inspect(world: World)
} }
end end
local dwi = debug_world_inspect
local function name(world, e) local function name(world, e)
return world:get(e, jecs.Name) return world:get(e, jecs.Name)
end end
TEST("#repro", function()
local world = world_new()
local function getTargets(relation)
local tgts = {}
local pairwildcard = pair(relation, jecs.Wildcard)
for _, archetype in world:query(pairwildcard):archetypes() do
local tr = archetype.records[pairwildcard]
local count = archetype.counts[pairwildcard]
local types = archetype.types
for _, entity in archetype.entities do
for i = 0, count - 1 do
local tgt = jecs.pair_second(world, types[i + tr])
table.insert(tgts, tgt)
end
end
end
return tgts
end
local Attacks = world:component()
local Eats = world:component()
local function setAttacksAndEats(entity1, entity2)
world:add(entity1, pair(Attacks, entity2))
world:add(entity1, pair(Eats, entity2))
end
local e1 = world:entity()
local e2 = world:entity()
local e3 = world:entity()
print(e1, e2, e3)
setAttacksAndEats(e3, e1)
setAttacksAndEats(e3, e2)
setAttacksAndEats(e1, e2)
print("---------------- delete e2 ---------------")
local d = dwi(world)
for archetype_id in world.component_index[pair(jecs.Wildcard, e2)].cache do
local archetype = world.archetypes[archetype_id].type
testkit.print(archetype)
end
world:delete(e2)
print("-----------------------------")
testkit.print(d.tbl(e1).types)
-- testkit.print(d.tbl(e3).types)
-- testkit.print(getTargets(Attacks))
-- testkit.print(getTargets(Eats))
end)
TEST("archetype", function() TEST("archetype", function()
local archetype_traverse_add = jecs.archetype_traverse_add local archetype_traverse_add = jecs.archetype_traverse_add
local archetype_traverse_remove = jecs.archetype_traverse_remove local archetype_traverse_remove = jecs.archetype_traverse_remove
@ -1199,6 +1294,7 @@ TEST("world:delete", function()
world:delete(e) world:delete(e)
end) end)
local d = debug_world_inspect(world)
for i, friend in friends do for i, friend in friends do
CHECK(not world:has(friend, pair(FriendsWith, e))) CHECK(not world:has(friend, pair(FriendsWith, e)))
CHECK(world:has(friend, Health)) CHECK(world:has(friend, Health))