mirror of
https://github.com/Ukendio/jecs.git
synced 2025-09-23 16:39:17 +00:00
Compare commits
3 commits
fb8f8bddc1
...
3ee7dbc8cb
Author | SHA1 | Date | |
---|---|---|---|
|
3ee7dbc8cb | ||
|
29a66d92c2 | ||
|
f912866fcb |
3 changed files with 94 additions and 24 deletions
|
@ -160,6 +160,10 @@ 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)
|
||||||
|
|
102
jecs.luau
102
jecs.luau
|
@ -126,6 +126,8 @@ type world = {
|
||||||
|
|
||||||
max_component_id: number,
|
max_component_id: number,
|
||||||
max_archetype_id: number,
|
max_archetype_id: number,
|
||||||
|
start_component_id: number,
|
||||||
|
start_tag_id: number,
|
||||||
|
|
||||||
observable: Map<i53, Map<i53, { Observer }>>,
|
observable: Map<i53, Map<i53, { Observer }>>,
|
||||||
|
|
||||||
|
@ -163,6 +165,8 @@ export type World = {
|
||||||
|
|
||||||
max_component_id: number,
|
max_component_id: number,
|
||||||
max_archetype_id: number,
|
max_archetype_id: number,
|
||||||
|
start_component_id: number,
|
||||||
|
start_tag_id: number,
|
||||||
|
|
||||||
observable: Map<Id, Map<Id, { Observer }>>,
|
observable: Map<Id, Map<Id, { Observer }>>,
|
||||||
|
|
||||||
|
@ -760,6 +764,12 @@ local function ECS_ID_IS_WILDCARD(e: i53): boolean
|
||||||
return first == EcsWildcard or second == EcsWildcard
|
return first == EcsWildcard or second == EcsWildcard
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function get_max_ids_difference(world: World): (number, number)
|
||||||
|
local diff_components = world.start_component_id - ecs_max_component_id
|
||||||
|
local diff_tags = world.start_tag_id - ecs_max_tag_id
|
||||||
|
return diff_components, diff_tags
|
||||||
|
end
|
||||||
|
|
||||||
local function id_record_get(world: World, id: Entity): ComponentRecord?
|
local function id_record_get(world: World, id: Entity): ComponentRecord?
|
||||||
local component_index = world.component_index
|
local component_index = world.component_index
|
||||||
local idr: ComponentRecord = component_index[id]
|
local idr: ComponentRecord = component_index[id]
|
||||||
|
@ -1078,18 +1088,6 @@ 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
|
||||||
|
@ -1327,6 +1325,9 @@ 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]
|
||||||
|
@ -1349,6 +1350,9 @@ 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]
|
||||||
|
@ -1372,6 +1376,9 @@ 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]
|
||||||
|
@ -1396,6 +1403,9 @@ 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]
|
||||||
|
@ -1421,6 +1431,9 @@ 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]
|
||||||
|
@ -1447,6 +1460,9 @@ 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]
|
||||||
|
@ -1474,6 +1490,9 @@ 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]
|
||||||
|
@ -1502,6 +1521,9 @@ 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]
|
||||||
|
@ -1533,6 +1555,9 @@ 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]
|
||||||
|
@ -1700,6 +1725,9 @@ 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]
|
||||||
|
@ -1722,6 +1750,9 @@ 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]
|
||||||
|
@ -1745,6 +1776,9 @@ 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]
|
||||||
|
@ -1769,6 +1803,9 @@ 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]
|
||||||
|
@ -1853,6 +1890,10 @@ 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]
|
||||||
|
@ -1880,6 +1921,9 @@ 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]
|
||||||
|
@ -1911,6 +1955,9 @@ 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]
|
||||||
|
@ -2150,6 +2197,10 @@ 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,
|
||||||
|
|
||||||
|
@ -2162,6 +2213,9 @@ local function world_new()
|
||||||
max_archetype_id = 0,
|
max_archetype_id = 0,
|
||||||
max_component_id = ecs_max_component_id,
|
max_component_id = ecs_max_component_id,
|
||||||
|
|
||||||
|
start_component_id = ecs_max_component_id,
|
||||||
|
start_tag_id = ecs_max_tag_id,
|
||||||
|
|
||||||
observable = observable,
|
observable = observable,
|
||||||
signals = signals,
|
signals = signals,
|
||||||
} :: world
|
} :: world
|
||||||
|
@ -3124,6 +3178,19 @@ 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
|
||||||
|
@ -3143,14 +3210,12 @@ local function world_new()
|
||||||
world.children = world_children
|
world.children = world_children
|
||||||
world.range = world_range
|
world.range = world_range
|
||||||
|
|
||||||
for i = 1, HI_COMPONENT_ID do
|
for i = 1, EcsRest do
|
||||||
local e = entity_index_new_id(entity_index)
|
entity_index_new_id(entity_index)
|
||||||
inner_world_add(world, e, EcsComponent)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
for i = HI_COMPONENT_ID + 1, EcsRest do
|
for i = 1, max_component_id do
|
||||||
-- Initialize built-in components
|
inner_world_add(world, i, EcsComponent)
|
||||||
entity_index_new_id(entity_index)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
inner_world_add(world, EcsName, EcsComponent)
|
inner_world_add(world, EcsName, EcsComponent)
|
||||||
|
@ -3263,6 +3328,7 @@ return {
|
||||||
pair_first = ecs_pair_first :: <P, O>(world: World, pair: Pair<P, O>) -> Id<P>,
|
pair_first = ecs_pair_first :: <P, O>(world: World, pair: Pair<P, O>) -> Id<P>,
|
||||||
pair_second = ecs_pair_second :: <P, O>(world: World, pair: Pair<P, O>) -> Id<O>,
|
pair_second = ecs_pair_second :: <P, O>(world: World, pair: Pair<P, O>) -> Id<O>,
|
||||||
entity_index_get_alive = entity_index_get_alive,
|
entity_index_get_alive = entity_index_get_alive,
|
||||||
|
get_max_ids_difference = get_max_ids_difference,
|
||||||
|
|
||||||
archetype_append_to_records = archetype_append_to_records,
|
archetype_append_to_records = archetype_append_to_records,
|
||||||
id_record_ensure = id_record_ensure :: (World, Id) -> ComponentRecord,
|
id_record_ensure = id_record_ensure :: (World, Id) -> ComponentRecord,
|
||||||
|
|
|
@ -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(4 :: any, 5 :: any), 6) -- noise
|
world:set(entity, pair(world:component(), world:component()), 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)
|
||||||
|
|
Loading…
Reference in a new issue