Compare commits

..

1 commit

Author SHA1 Message Date
PepeElToro41
fb8f8bddc1
Merge f912866fcb into 5de842d144 2025-08-23 20:30:15 +03:00
3 changed files with 24 additions and 80 deletions

View file

@ -160,10 +160,6 @@ local function monitors_new<T...>(
end end
local function removed(entity: jecs.Entity, component: jecs.Id) local function removed(entity: jecs.Entity, component: jecs.Id)
local r = jecs.record(world, entity)
if not archetypes[r.archetype.id] then
return
end
local EcsOnRemove = jecs.OnRemove :: jecs.Id local EcsOnRemove = jecs.OnRemove :: jecs.Id
if callback ~= nil then if callback ~= nil then
callback(entity, EcsOnRemove) callback(entity, EcsOnRemove)

View file

@ -1088,6 +1088,18 @@ local function archetype_traverse_add(
return to return to
end end
local function world_component(world: world): i53
local id = (world.max_component_id :: number) + 1
if id > HI_COMPONENT_ID then
-- IDs are partitioned into ranges because component IDs are not nominal,
-- so it needs to error when IDs intersect into the entity range.
error("Too many components, consider using world:entity() instead to create components.")
end
world.max_component_id = id
return id
end
local function archetype_fast_delete_last(columns: { Column }, column_count: number) local function archetype_fast_delete_last(columns: { Column }, column_count: number)
for i, column in columns do for i, column in columns do
if column ~= NULL_ARRAY then if column ~= NULL_ARRAY then
@ -1325,9 +1337,6 @@ local function query_iter_init(query: QueryInner): () -> (number, ...any)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1350,9 +1359,6 @@ local function query_iter_init(query: QueryInner): () -> (number, ...any)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1376,9 +1382,6 @@ local function query_iter_init(query: QueryInner): () -> (number, ...any)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1403,9 +1406,6 @@ local function query_iter_init(query: QueryInner): () -> (number, ...any)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1431,9 +1431,6 @@ local function query_iter_init(query: QueryInner): () -> (number, ...any)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1460,9 +1457,6 @@ local function query_iter_init(query: QueryInner): () -> (number, ...any)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1490,9 +1484,6 @@ local function query_iter_init(query: QueryInner): () -> (number, ...any)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1521,9 +1512,6 @@ local function query_iter_init(query: QueryInner): () -> (number, ...any)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1555,9 +1543,6 @@ local function query_iter_init(query: QueryInner): () -> (number, ...any)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1725,9 +1710,6 @@ local function query_cached(query: QueryInner)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1750,9 +1732,6 @@ local function query_cached(query: QueryInner)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1776,9 +1755,6 @@ local function query_cached(query: QueryInner)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1803,9 +1779,6 @@ local function query_cached(query: QueryInner)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1890,10 +1863,6 @@ local function query_cached(query: QueryInner)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
b = columns_map[B] b = columns_map[B]
@ -1921,9 +1890,6 @@ local function query_cached(query: QueryInner)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -1955,9 +1921,6 @@ local function query_cached(query: QueryInner)
entities = archetype.entities entities = archetype.entities
i = #entities i = #entities
if i == 0 then
continue
end
entity = entities[i] entity = entities[i]
columns_map = archetype.columns_map columns_map = archetype.columns_map
a = columns_map[A] a = columns_map[A]
@ -2197,10 +2160,6 @@ local function world_new()
removed = {} :: Signal removed = {} :: Signal
} }
-- We need to cache the moment the world is registered, that way
-- `world:component` will not pollute the global registration of components.
local max_component_id = ecs_max_component_id
local world = { local world = {
archetype_edges = archetype_edges, archetype_edges = archetype_edges,
@ -3178,19 +3137,6 @@ local function world_new()
world.archetype_index = new_archetype_map world.archetype_index = new_archetype_map
end end
local function world_component(world: world): i53
max_component_id += 1
if max_component_id > HI_COMPONENT_ID then
-- IDs are partitioned into ranges because component IDs are not nominal,
-- so it needs to error when IDs intersect into the entity range.
error("Too many components, consider using world:entity() instead to create components.")
end
world.max_component_id = max_component_id
inner_world_add(world, max_component_id, EcsComponent)
return max_component_id
end
world.entity = inner_world_entity world.entity = inner_world_entity
world.query = world_query :: any world.query = world_query :: any
world.remove = inner_world_remove world.remove = inner_world_remove
@ -3210,12 +3156,14 @@ local function world_new()
world.children = world_children world.children = world_children
world.range = world_range world.range = world_range
for i = 1, EcsRest do for i = 1, HI_COMPONENT_ID do
entity_index_new_id(entity_index) local e = entity_index_new_id(entity_index)
inner_world_add(world, e, EcsComponent)
end end
for i = 1, max_component_id do for i = HI_COMPONENT_ID + 1, EcsRest do
inner_world_add(world, i, EcsComponent) -- Initialize built-in components
entity_index_new_id(entity_index)
end end
inner_world_add(world, EcsName, EcsComponent) inner_world_add(world, EcsName, EcsComponent)

View file

@ -550,9 +550,9 @@ TEST("world:add()", function()
end) end)
TEST("world:children()", function() TEST("world:children()", function()
local world = jecs.world()
local C = jecs.component() local C = jecs.component()
local T = jecs.tag() local T = jecs.tag()
local world = jecs.world()
local e1 = world:entity() local e1 = world:entity()
world:set(e1, C, true) world:set(e1, C, true)
@ -2510,7 +2510,7 @@ TEST("#repro2", function()
local entity = world:entity() local entity = world:entity()
world:set(entity, pair(Lifetime, Particle), 1) world:set(entity, pair(Lifetime, Particle), 1)
world:set(entity, pair(Lifetime, Beam), 2) world:set(entity, pair(Lifetime, Beam), 2)
world:set(entity, pair(world:component(), world:component()), 6) -- noise world:set(entity, pair(4 :: any, 5 :: any), 6) -- noise
CHECK(world:get(entity, pair(Lifetime, Particle)) == 1) CHECK(world:get(entity, pair(Lifetime, Particle)) == 1)
CHECK(world:get(entity, pair(Lifetime, Beam)) == 2) CHECK(world:get(entity, pair(Lifetime, Beam)) == 2)