Sync to released Jecs 0.5.5-nightly.20250312T202956Z (#19)

Reviewed-on: #19
This commit is contained in:
marked 2025-03-12 21:30:20 +01:00
parent 26ead3a62e
commit 005b7bcfbb
9 changed files with 651 additions and 56 deletions

View file

@ -46,7 +46,7 @@ export type Record = {
}
type IdRecord = {
columns: { number },
cache: { number },
counts: { number },
flags: number,
size: number,
@ -480,7 +480,7 @@ local function world_target(world: World, entity: i53, relation: i24, index: num
nth = nth + count + 1
end
local tr = idr.columns[archetype_id]
local tr = idr.cache[archetype_id]
nth = archetype.types[nth + tr]
@ -537,7 +537,7 @@ local function id_record_ensure(world: World, id: number): IdRecord
idr = {
size = 0,
columns = {},
cache = {},
counts = {},
flags = flags,
hooks = {
@ -562,7 +562,7 @@ local function archetype_append_to_records(
local archetype_id = archetype.id
local archetype_records = archetype.records
local archetype_counts = archetype.counts
local idr_columns = idr.columns
local idr_columns = idr.cache
local idr_counts = idr.counts
local tr = idr_columns[archetype_id]
if not tr then
@ -1063,7 +1063,7 @@ local function archetype_destroy(world: World, archetype: Archetype)
for id in records do
local idr = component_index[id]
idr.columns[archetype_id] = nil :: any
idr.cache[archetype_id] = nil :: any
idr.counts[archetype_id] = nil
idr.size -= 1
records[id] = nil :: any
@ -1122,7 +1122,7 @@ do
if idr then
local flags = idr.flags
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 entities = idr_archetype.entities
@ -1134,7 +1134,7 @@ do
archetype_destroy(world, idr_archetype)
end
else
for archetype_id in idr.columns do
for archetype_id in idr.cache do
local idr_archetype = archetypes[archetype_id]
local entities = idr_archetype.entities
local n = #entities
@ -1147,55 +1147,66 @@ do
end
end
local sparse_array = entity_index.sparse_array
local dense_array = entity_index.dense_array
if idr_t then
for archetype_id in idr_t.columns do
local children = {}
local children
local ids
local count = 0
local archetype_ids = idr_t.cache
for archetype_id in archetype_ids do
local idr_t_archetype = archetypes[archetype_id]
local idr_t_types = idr_t_archetype.types
for _, child in idr_t_archetype.entities do
table.insert(children, child)
end
local n = #children
local entities = idr_t_archetype.entities
local removal_queued = false
for _, id in idr_t_types do
if not ECS_IS_PAIR(id) then
continue
end
local object = ecs_pair_second(world, id)
if object == delete then
local id_record = component_index[id]
local flags = id_record.flags
local flags_delete_mask: number = bit32.band(flags, ECS_ID_DELETE)
if flags_delete_mask ~= 0 then
for i = n, 1, -1 do
world_delete(world, children[i])
end
break
else
local on_remove = id_record.hooks.on_remove
local to = archetype_traverse_remove(world, id, idr_t_archetype)
local empty = #to.types == 0
for i = n, 1, -1 do
local child = children[i]
if on_remove then
on_remove(child)
end
local r = sparse_array[ECS_ENTITY_T_LO(child)]
if not empty then
entity_move(entity_index, child, r, to)
end
end
if object ~= delete then
continue
end
local id_record = component_index[id]
local flags = id_record.flags
local flags_delete_mask: number = bit32.band(flags, ECS_ID_DELETE)
if flags_delete_mask ~= 0 then
for i = #entities, 1, -1 do
local child = entities[i]
world_delete(world, child)
end
break
else
if not ids then
ids = {}
end
ids[id] = true
removal_queued = true
end
end
archetype_destroy(world, idr_t_archetype)
if not removal_queued then
continue
end
if not children then
children = {}
end
local n = #entities
table.move(entities, 1, n, count + 1, children)
count += n
end
if ids then
for id in ids do
for _, child in children do
world_remove(world, child, id)
end
end
end
for archetype_id in archetype_ids do
archetype_destroy(world, archetypes[archetype_id])
end
end
@ -2096,7 +2107,7 @@ local function world_query(world: World, ...)
return q
end
for archetype_id in idr.columns do
for archetype_id in idr.cache do
local compatibleArchetype = archetypes[archetype_id]
if #compatibleArchetype.entities == 0 then
continue
@ -2130,9 +2141,9 @@ local function world_each(world: World, id): () -> ()
return NOOP
end
local idr_columns = idr.columns
local idr_cache = idr.cache
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]
if not archetype then
return NOOP
@ -2144,7 +2155,7 @@ local function world_each(world: World, id): () -> ()
return function(): any
local entity = entities[row]
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
return
end
@ -2477,6 +2488,7 @@ export type World = {
return {
World = World :: { new: () -> World },
world = World.new :: () -> World,
OnAdd = EcsOnAdd :: Entity<(entity: Entity) -> ()>,
OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>,
@ -2500,6 +2512,8 @@ return {
ECS_GENERATION = ECS_GENERATION,
ECS_ID_IS_WILDCARD = ECS_ID_IS_WILDCARD,
ECS_ID_DELETE = ECS_ID_DELETE,
IS_PAIR = ECS_IS_PAIR,
pair_first = ecs_pair_first,
pair_second = ecs_pair_second,