mirror of
https://github.com/Ukendio/jecs.git
synced 2025-08-04 19:29:18 +00:00
Compare commits
1 commit
62e1f05dfc
...
d03311733b
Author | SHA1 | Date | |
---|---|---|---|
|
d03311733b |
5 changed files with 217 additions and 257 deletions
|
@ -4,15 +4,15 @@ export type PatchedWorld = jecs.World & {
|
|||
added: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
|
||||
removed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id) -> ()) -> () -> (),
|
||||
changed: <T>(PatchedWorld, jecs.Id<T>, (e: jecs.Entity, id: jecs.Id, value: T) -> ()) -> () -> (),
|
||||
observer: <T...>(
|
||||
observer: (
|
||||
PatchedWorld,
|
||||
jecs.Query<T...>,
|
||||
(<a>(jecs.Entity, jecs.Id<a>, a) -> ())?
|
||||
) -> () -> (jecs.Entity),
|
||||
any,
|
||||
(jecs.Entity) -> ()
|
||||
) -> (),
|
||||
monitor: (
|
||||
PatchedWorld,
|
||||
any,
|
||||
(<a>(jecs.Entity, jecs.Id<a>) -> ())?
|
||||
(jecs.Entity, jecs.Id) -> ()
|
||||
) -> ()
|
||||
}
|
||||
|
||||
|
@ -25,8 +25,6 @@ local function observers_new(world, query, callback)
|
|||
end
|
||||
|
||||
local entity_index = world.entity_index :: any
|
||||
local i = 0
|
||||
local entities = {}
|
||||
local function emplaced(entity, id, value)
|
||||
local r = jecs.entity_index_try_get_fast(
|
||||
entity_index, entity :: any)
|
||||
|
@ -38,11 +36,7 @@ local function observers_new(world, query, callback)
|
|||
local archetype = r.archetype
|
||||
|
||||
if jecs.query_match(query, archetype) then
|
||||
i += 1
|
||||
entities[i] = entity
|
||||
if callback ~= nil then
|
||||
callback(entity, id, value)
|
||||
end
|
||||
callback(entity)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -50,19 +44,6 @@ local function observers_new(world, query, callback)
|
|||
world:added(term, emplaced)
|
||||
world:changed(term, emplaced)
|
||||
end
|
||||
|
||||
return function()
|
||||
local row = i
|
||||
return function()
|
||||
if row == 0 then
|
||||
i = 0
|
||||
table.clear(entities)
|
||||
end
|
||||
local entity = entities[row]
|
||||
row -= 1
|
||||
return entity
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function join(world, component)
|
||||
|
@ -118,9 +99,7 @@ local function monitors_new(world, query, callback)
|
|||
end
|
||||
|
||||
local entity_index = world.entity_index :: any
|
||||
local i = 0
|
||||
local entities = {}
|
||||
local function emplaced(entity, id, value)
|
||||
local function emplaced(entity: jecs.Entity)
|
||||
local r = jecs.entity_index_try_get_fast(
|
||||
entity_index, entity :: any)
|
||||
|
||||
|
@ -131,17 +110,22 @@ local function monitors_new(world, query, callback)
|
|||
local archetype = r.archetype
|
||||
|
||||
if jecs.query_match(query, archetype) then
|
||||
i += 1
|
||||
entities[i] = entity
|
||||
if callback ~= nil then
|
||||
callback(entity, id, value)
|
||||
end
|
||||
callback(entity, jecs.OnAdd)
|
||||
end
|
||||
end
|
||||
|
||||
local function removed(entity: jecs.Entity, component: jecs.Id)
|
||||
local EcsOnRemove = jecs.OnRemove :: jecs.Id
|
||||
if callback ~= nil then
|
||||
local r = jecs.entity_index_try_get_fast(
|
||||
entity_index, entity :: any)
|
||||
|
||||
if not r then
|
||||
return
|
||||
end
|
||||
|
||||
local archetype = r.archetype
|
||||
|
||||
if jecs.query_match(query, archetype) then
|
||||
local EcsOnRemove = jecs.OnRemove :: jecs.Id
|
||||
callback(entity, EcsOnRemove)
|
||||
end
|
||||
end
|
||||
|
@ -150,19 +134,6 @@ local function monitors_new(world, query, callback)
|
|||
world:added(term, emplaced)
|
||||
world:removed(term, removed)
|
||||
end
|
||||
|
||||
return function()
|
||||
local row = i
|
||||
return function()
|
||||
if row == 0 then
|
||||
i = 0
|
||||
table.clear(entities)
|
||||
end
|
||||
local entity = entities[row]
|
||||
row -= 1
|
||||
return entity
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function observers_add(world: jecs.World): PatchedWorld
|
||||
|
@ -198,7 +169,7 @@ local function observers_add(world: jecs.World): PatchedWorld
|
|||
|
||||
local idr = world.component_index[component]
|
||||
if idr then
|
||||
idr.on_add = on_add
|
||||
idr.hooks.on_add = on_add
|
||||
else
|
||||
world:set(component, jecs.OnAdd, on_add)
|
||||
end
|
||||
|
@ -232,7 +203,7 @@ local function observers_add(world: jecs.World): PatchedWorld
|
|||
end
|
||||
local idr = world.component_index[component]
|
||||
if idr then
|
||||
idr.on_change = on_change
|
||||
idr.hooks.on_change = on_change
|
||||
else
|
||||
world:set(component, jecs.OnChange, on_change)
|
||||
end
|
||||
|
@ -267,7 +238,7 @@ local function observers_add(world: jecs.World): PatchedWorld
|
|||
|
||||
local idr = world.component_index[component]
|
||||
if idr then
|
||||
idr.on_remove = on_remove
|
||||
idr.hooks.on_remove = on_remove
|
||||
else
|
||||
world:set(component, jecs.OnRemove, on_remove)
|
||||
end
|
||||
|
|
1
jecs.d.ts
vendored
1
jecs.d.ts
vendored
|
@ -310,7 +310,6 @@ export declare const OnDeleteTarget: Tag;
|
|||
export declare const Delete: Tag;
|
||||
export declare const Remove: Tag;
|
||||
export declare const Name: Entity<string>;
|
||||
export declare const Exclusive: Tag;
|
||||
export declare const Rest: Entity;
|
||||
|
||||
export type ComponentRecord = {
|
||||
|
|
396
jecs.luau
396
jecs.luau
|
@ -42,12 +42,14 @@ export type Iter<T...> = (query: Query<T...>) -> () -> (Entity, T...)
|
|||
export type Query<T...> = typeof(setmetatable(
|
||||
{} :: {
|
||||
iter: Iter<T...>,
|
||||
with: (<a>(Query<T...>, Id<a>) -> Query<T...>)
|
||||
with:
|
||||
(<a>(Query<T...>, Id<a>) -> Query<T...>)
|
||||
& (<a, b>(Query<T...>, Id<a>, Id<b>) -> Query<T...>)
|
||||
& (<a, b, c>(Query<T...>, Id<a>, Id<b>, Id<c>) -> Query<T...>)
|
||||
& (<a, b, c>(Query<T...>, Id<a>, Id<b>, Id<c>) -> Query<T...>)
|
||||
& (<a, b, c, d>(Query<T...>, Id<a>, Id<b>, Id<c>, Id) -> Query<T...>),
|
||||
without: (<a>(Query<T...>, Id<a>) -> Query<T...>)
|
||||
without:
|
||||
(<a>(Query<T...>, Id<a>) -> Query<T...>)
|
||||
& (<a, b>(Query<T...>, Id<a>, Id<b>) -> Query<T...>)
|
||||
& (<a, b, c>(Query<T...>, Id<a>, Id<b>, Id<c>) -> Query<T...>)
|
||||
& (<a, b, c>(Query<T...>, Id<a>, Id<b>, Id<c>) -> Query<T...>)
|
||||
|
@ -170,10 +172,11 @@ export type ComponentRecord = {
|
|||
counts: { [Id]: number },
|
||||
flags: number,
|
||||
size: number,
|
||||
|
||||
on_add: (<T>(entity: Entity, id: Entity<T>, value: T?) -> ())?,
|
||||
on_change: (<T>(entity: Entity, id: Entity<T>, value: T) -> ())?,
|
||||
on_remove: ((entity: Entity, id: Entity) -> ())?,
|
||||
hooks: {
|
||||
on_add: (<T>(entity: Entity, id: Entity<T>, value: T?) -> ())?,
|
||||
on_change: (<T>(entity: Entity, id: Entity<T>, value: T) -> ())?,
|
||||
on_remove: ((entity: Entity, id: Entity) -> ())?,
|
||||
},
|
||||
}
|
||||
export type ComponentIndex = Map<Id, ComponentRecord>
|
||||
export type Archetypes = { [Id]: Archetype }
|
||||
|
@ -193,10 +196,10 @@ local ECS_ENTITY_MASK = bit32.lshift(1, 24)
|
|||
local ECS_GENERATION_MASK = bit32.lshift(1, 16)
|
||||
local ECS_PAIR_OFFSET = 2^48
|
||||
|
||||
local ECS_ID_DELETE = 0b0001
|
||||
local ECS_ID_IS_TAG = 0b0010
|
||||
local ECS_ID_IS_EXCLUSIVE = 0b0100
|
||||
local ECS_ID_MASK = 0b0000
|
||||
local ECS_ID_DELETE = 0b0001
|
||||
local ECS_ID_IS_TAG = 0b0010
|
||||
local ECS_ID_IS_EXCLUSIVE = 0b0100
|
||||
local ECS_ID_MASK = 0b0000
|
||||
|
||||
local HI_COMPONENT_ID = 256
|
||||
local EcsOnAdd = HI_COMPONENT_ID + 1
|
||||
|
@ -745,10 +748,11 @@ local function id_record_ensure(world: World, id: Entity): ComponentRecord
|
|||
records = {},
|
||||
counts = {},
|
||||
flags = flags,
|
||||
|
||||
on_add = on_add,
|
||||
on_change = on_change,
|
||||
on_remove = on_remove,
|
||||
hooks = {
|
||||
on_add = on_add,
|
||||
on_change = on_change,
|
||||
on_remove = on_remove,
|
||||
},
|
||||
} :: ComponentRecord
|
||||
|
||||
component_index[id] = idr
|
||||
|
@ -1027,7 +1031,7 @@ local function archetype_delete(world: World, archetype: Archetype, row: number)
|
|||
|
||||
for _, id in id_types do
|
||||
local idr = component_index[id]
|
||||
local on_remove = idr.on_remove
|
||||
local on_remove = idr.hooks.on_remove
|
||||
if on_remove then
|
||||
on_remove(delete, id)
|
||||
end
|
||||
|
@ -1061,13 +1065,6 @@ local function archetype_destroy(world: World, archetype: Archetype)
|
|||
local columns_map = archetype.columns_map
|
||||
|
||||
for id in columns_map do
|
||||
local idr = component_index[id]
|
||||
idr.records[archetype_id] = nil :: any
|
||||
idr.counts[archetype_id] = nil
|
||||
idr.size -= 1
|
||||
if idr.size == 0 then
|
||||
component_index[id] = nil :: any
|
||||
end
|
||||
local observer_list = find_observers(world, EcsOnArchetypeDelete, id)
|
||||
if not observer_list then
|
||||
continue
|
||||
|
@ -1078,6 +1075,16 @@ local function archetype_destroy(world: World, archetype: Archetype)
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
for id in columns_map do
|
||||
local idr = component_index[id]
|
||||
idr.records[archetype_id] = nil :: any
|
||||
idr.counts[archetype_id] = nil
|
||||
idr.size -= 1
|
||||
if idr.size == 0 then
|
||||
component_index[id] = nil :: any
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function NOOP() end
|
||||
|
@ -2023,7 +2030,7 @@ local function ecs_bulk_insert(world: World, entity: Entity, ids: { Entity }, va
|
|||
local value = values[i]
|
||||
local cdr = component_index[id]
|
||||
|
||||
local on_add = cdr.on_add
|
||||
local on_add = cdr.hooks.on_add
|
||||
if value then
|
||||
columns_map[id][row] = value
|
||||
if on_add then
|
||||
|
@ -2068,11 +2075,11 @@ local function ecs_bulk_insert(world: World, entity: Entity, ids: { Entity }, va
|
|||
|
||||
local value = values[i] :: any
|
||||
|
||||
local on_add = idr.on_add
|
||||
local on_add = idr.hooks.on_add
|
||||
|
||||
if value ~= nil then
|
||||
columns_map[id][row] = value
|
||||
local on_change = idr.on_change
|
||||
local on_change = idr.hooks.on_change
|
||||
local hook = if set then on_change else on_add
|
||||
if hook then
|
||||
hook(entity, id, value :: any)
|
||||
|
@ -2107,7 +2114,7 @@ local function ecs_bulk_remove(world: World, entity: Entity, ids: { Entity })
|
|||
remove[id] = true
|
||||
local idr = component_index[id]
|
||||
|
||||
local on_remove = idr.on_remove
|
||||
local on_remove = idr.hooks.on_remove
|
||||
if on_remove then
|
||||
on_remove(entity, id)
|
||||
end
|
||||
|
@ -2181,76 +2188,6 @@ local function world_new()
|
|||
return r
|
||||
end
|
||||
|
||||
local function inner_archetype_move(
|
||||
entity: Entity,
|
||||
to: Archetype,
|
||||
dst_row: i24,
|
||||
from: Archetype,
|
||||
src_row: i24
|
||||
)
|
||||
local src_columns = from.columns
|
||||
local dst_entities = to.entities
|
||||
local src_entities = from.entities
|
||||
|
||||
local last = #src_entities
|
||||
local id_types = from.types
|
||||
local columns_map = to.columns_map
|
||||
|
||||
if src_row ~= last then
|
||||
for i, column in src_columns do
|
||||
if column == NULL_ARRAY then
|
||||
continue
|
||||
end
|
||||
local dst_column = columns_map[id_types[i]]
|
||||
|
||||
if dst_column then
|
||||
dst_column[dst_row] = column[src_row]
|
||||
end
|
||||
|
||||
column[src_row] = column[last]
|
||||
column[last] = nil
|
||||
end
|
||||
|
||||
local e2 = src_entities[last]
|
||||
src_entities[src_row] = e2
|
||||
|
||||
local record2 = eindex_sparse_array[ECS_ENTITY_T_LO(e2 :: number)]
|
||||
record2.row = src_row
|
||||
else
|
||||
for i, column in src_columns do
|
||||
if column == NULL_ARRAY then
|
||||
continue
|
||||
end
|
||||
-- Retrieves the new column index from the source archetype's record from each component
|
||||
-- We have to do this because the columns are tightly packed and indexes may not correspond to each other.
|
||||
local dst_column = columns_map[id_types[i]]
|
||||
|
||||
-- Sometimes target column may not exist, e.g. when you remove a component.
|
||||
if dst_column then
|
||||
dst_column[dst_row] = column[src_row]
|
||||
end
|
||||
|
||||
column[last] = nil
|
||||
end
|
||||
end
|
||||
src_entities[last] = nil :: any
|
||||
dst_entities[dst_row] = entity
|
||||
end
|
||||
|
||||
local function inner_entity_move(
|
||||
entity_index: EntityIndex,
|
||||
entity: Entity,
|
||||
record: Record,
|
||||
to: Archetype
|
||||
)
|
||||
local sourceRow = record.row
|
||||
local from = record.archetype
|
||||
local dst_row = archetype_append(entity, to)
|
||||
inner_archetype_move(entity, to, dst_row, from, sourceRow)
|
||||
record.archetype = to
|
||||
record.row = dst_row
|
||||
end
|
||||
|
||||
-- local function inner_entity_index_try_get(entity: number): Record?
|
||||
-- local r = inner_entity_index_try_get_any(entity)
|
||||
-- if r then
|
||||
|
@ -2303,7 +2240,7 @@ local function world_new()
|
|||
if idr and bit32.btest(idr.flags, ECS_ID_IS_EXCLUSIVE) then
|
||||
local cr = idr.records[src.id]
|
||||
if cr then
|
||||
local on_remove = idr.on_remove
|
||||
local on_remove = idr.hooks.on_remove
|
||||
local id_types = src.types
|
||||
if on_remove then
|
||||
on_remove(entity, id_types[cr])
|
||||
|
@ -2330,14 +2267,14 @@ local function world_new()
|
|||
return
|
||||
end
|
||||
if from then
|
||||
inner_entity_move(entity_index, entity, record, to)
|
||||
entity_move(entity_index, entity, record, to)
|
||||
else
|
||||
if #to.types > 0 then
|
||||
new_entity(entity, record, to)
|
||||
end
|
||||
end
|
||||
|
||||
local on_add = idr.on_add
|
||||
local on_add = idr.hooks.on_add
|
||||
|
||||
if on_add then
|
||||
on_add(entity, id)
|
||||
|
@ -2349,7 +2286,7 @@ local function world_new()
|
|||
return
|
||||
end
|
||||
if from then
|
||||
inner_entity_move(entity_index, entity, record, to)
|
||||
entity_move(entity_index, entity, record, to)
|
||||
else
|
||||
if #to.types > 0 then
|
||||
new_entity(entity, record, to)
|
||||
|
@ -2357,7 +2294,7 @@ local function world_new()
|
|||
end
|
||||
|
||||
local idr = component_index[id]
|
||||
local on_add = idr.on_add
|
||||
local on_add = idr.hooks.on_add
|
||||
|
||||
if on_add then
|
||||
on_add(entity, id)
|
||||
|
@ -2485,73 +2422,104 @@ local function world_new()
|
|||
end
|
||||
|
||||
local from: Archetype = record.archetype
|
||||
local src = from or ROOT_ARCHETYPE
|
||||
local column = src.columns_map[id]
|
||||
if column then
|
||||
local idr = component_index[id]
|
||||
column[record.row] = data
|
||||
|
||||
-- If the archetypes are the same it can avoid moving the entity
|
||||
-- and just set the data directly.
|
||||
local on_change = idr.on_change
|
||||
if on_change then
|
||||
on_change(entity, id, data)
|
||||
end
|
||||
else
|
||||
local to: Archetype
|
||||
if ECS_IS_PAIR(id::number) then
|
||||
local src = from or ROOT_ARCHETYPE
|
||||
local edge = archetype_edges[src.id]
|
||||
local to = edge[id]
|
||||
local idr: ComponentRecord
|
||||
if ECS_IS_PAIR(id::number) then
|
||||
local edge = archetype_edges[src.id]
|
||||
to = edge[id]
|
||||
if not to then
|
||||
local first = ECS_PAIR_FIRST(id::number)
|
||||
local wc = ECS_PAIR(first, EcsWildcard)
|
||||
idr = component_index[wc]
|
||||
if idr and bit32.btest(idr.flags, ECS_ID_IS_EXCLUSIVE) then
|
||||
local cr = idr.records[src.id]
|
||||
if cr then
|
||||
local on_remove = idr.on_remove
|
||||
local id_types = src.types
|
||||
if on_remove then
|
||||
on_remove(entity, id_types[cr])
|
||||
src = record.archetype
|
||||
id_types = src.types
|
||||
cr = idr.records[src.id]
|
||||
end
|
||||
local dst = table.clone(id_types)
|
||||
dst[cr] = id
|
||||
to = archetype_ensure(world, dst)
|
||||
else
|
||||
to = find_archetype_with(world, id, src)
|
||||
idr = component_index[id]
|
||||
if not to then
|
||||
local first = ECS_PAIR_FIRST(id::number)
|
||||
local wc = ECS_PAIR(first, EcsWildcard)
|
||||
idr = component_index[wc]
|
||||
if idr and bit32.btest(idr.flags, ECS_ID_IS_EXCLUSIVE) then
|
||||
local cr = idr.records[src.id]
|
||||
if cr then
|
||||
local on_remove = idr.hooks.on_remove
|
||||
local id_types = src.types
|
||||
if on_remove then
|
||||
on_remove(entity, id_types[cr])
|
||||
src = record.archetype
|
||||
id_types = src.types
|
||||
cr = idr.records[src.id]
|
||||
end
|
||||
local dst = table.clone(id_types)
|
||||
dst[cr] = id
|
||||
to = archetype_ensure(world, dst)
|
||||
else
|
||||
to = find_archetype_with(world, id, src)
|
||||
idr = component_index[id]
|
||||
end
|
||||
edge[id] = to
|
||||
else
|
||||
to = find_archetype_with(world, id, src)
|
||||
idr = component_index[id]
|
||||
end
|
||||
edge[id] = to
|
||||
else
|
||||
to = inner_archetype_traverse_add(id, from)
|
||||
idr = component_index[id]
|
||||
end
|
||||
local idr_hooks = idr.hooks
|
||||
if from == to then
|
||||
local column = to.columns_map[id]
|
||||
column[record.row] = data
|
||||
|
||||
-- If the archetypes are the same it can avoid moving the entity
|
||||
-- and just set the data directly.
|
||||
local on_change = idr_hooks.on_change
|
||||
if on_change then
|
||||
on_change(entity, id, data)
|
||||
end
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
if from then
|
||||
-- If there was a previous archetype, then the entity needs to move the archetype
|
||||
inner_entity_move(entity_index, entity, record, to)
|
||||
entity_move(entity_index, entity, record, to)
|
||||
else
|
||||
new_entity(entity, record, to)
|
||||
if #to.types > 0 then
|
||||
new_entity(entity, record, to)
|
||||
end
|
||||
end
|
||||
|
||||
column = to.columns_map[id]
|
||||
local column = to.columns_map[id]
|
||||
column[record.row] = data
|
||||
|
||||
local on_add = idr.on_add
|
||||
local on_add = idr.hooks.on_add
|
||||
|
||||
if on_add then
|
||||
on_add(entity, id, data)
|
||||
on_add(entity, id)
|
||||
end
|
||||
return
|
||||
end
|
||||
local to: Archetype = inner_archetype_traverse_add(id, from)
|
||||
local idr = component_index[id]
|
||||
local idr_hooks = idr.hooks
|
||||
|
||||
if from == to then
|
||||
local column = to.columns_map[id]
|
||||
column[record.row] = data
|
||||
|
||||
-- If the archetypes are the same it can avoid moving the entity
|
||||
-- and just set the data directly.
|
||||
local on_change = idr_hooks.on_change
|
||||
if on_change then
|
||||
on_change(entity, id, data)
|
||||
end
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
if from then
|
||||
-- If there was a previous archetype, then the entity needs to move the archetype
|
||||
entity_move(entity_index, entity, record, to)
|
||||
else
|
||||
new_entity(entity, record, to)
|
||||
end
|
||||
local column = to.columns_map[id]
|
||||
column[record.row] = data
|
||||
|
||||
local on_add = idr_hooks.on_add
|
||||
if on_add then
|
||||
on_add(entity, id, data)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -2629,14 +2597,14 @@ local function world_new()
|
|||
|
||||
if from.columns_map[id] then
|
||||
local idr = world.component_index[id]
|
||||
local on_remove = idr.on_remove
|
||||
local on_remove = idr.hooks.on_remove
|
||||
if on_remove then
|
||||
on_remove(entity, id)
|
||||
end
|
||||
|
||||
local to = archetype_traverse_remove(world, id, record.archetype)
|
||||
|
||||
inner_entity_move(entity_index, entity, record, to)
|
||||
entity_move(entity_index, entity, record, to)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -2663,13 +2631,16 @@ local function world_new()
|
|||
end
|
||||
|
||||
if idr_t then
|
||||
local queue: { i53 }
|
||||
local ids: Map<i53, boolean>
|
||||
|
||||
local count = 0
|
||||
local archetype_ids = idr_t.records
|
||||
for archetype_id in archetype_ids do
|
||||
local idr_t_archetype = archetypes[archetype_id]
|
||||
local idr_t_types = idr_t_archetype.types
|
||||
local entities = idr_t_archetype.entities
|
||||
|
||||
local node = idr_t_archetype
|
||||
local removal_queued = false
|
||||
|
||||
for _, id in idr_t_types do
|
||||
if not ECS_IS_PAIR(id::number) then
|
||||
|
@ -2680,48 +2651,57 @@ local function world_new()
|
|||
if object ~= entity then
|
||||
continue
|
||||
end
|
||||
node = archetype_traverse_remove(world, id, node)
|
||||
local on_remove = component_index[id].on_remove
|
||||
if on_remove then
|
||||
for _, entity in entities do
|
||||
on_remove(entity, id)
|
||||
end
|
||||
if not ids then
|
||||
ids = {} :: { [i53]: boolean }
|
||||
end
|
||||
ids[id] = true
|
||||
removal_queued = true
|
||||
end
|
||||
|
||||
for i = #entities, 1, -1 do
|
||||
local e = entities[i]
|
||||
local r = inner_entity_index_try_get_unsafe(e::number) :: Record
|
||||
inner_entity_move(entity_index, e, r, node)
|
||||
if not removal_queued then
|
||||
continue
|
||||
end
|
||||
|
||||
if not queue then
|
||||
queue = {} :: { i53 }
|
||||
end
|
||||
|
||||
local n = #entities
|
||||
table.move(entities, 1, n, count + 1, queue)
|
||||
count += n
|
||||
end
|
||||
|
||||
for id in ids do
|
||||
for _, child in queue do
|
||||
inner_world_remove(world, child, id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if idr_r then
|
||||
local count = 0
|
||||
local archetype_ids = idr_r.records
|
||||
local ids = {}
|
||||
local queue = {}
|
||||
local records = idr_r.records
|
||||
local counts = idr_r.counts
|
||||
for archetype_id in archetype_ids do
|
||||
local idr_r_archetype = archetypes[archetype_id]
|
||||
local node = idr_r_archetype
|
||||
local entities = idr_r_archetype.entities
|
||||
local tr = records[archetype_id]
|
||||
local tr_count = counts[archetype_id]
|
||||
local types = idr_r_archetype.types
|
||||
for i = tr, tr + tr_count - 1 do
|
||||
local id = types[i]
|
||||
node = archetype_traverse_remove(world, id, idr_r_archetype)
|
||||
local on_remove = component_index[id].on_remove
|
||||
if on_remove then
|
||||
for _, entity in entities do
|
||||
on_remove(entity, id)
|
||||
end
|
||||
end
|
||||
ids[types[i]] = true
|
||||
end
|
||||
for i = #entities, 1, -1 do
|
||||
local e = entities[i]
|
||||
local r = inner_entity_index_try_get_unsafe(e::number) :: Record
|
||||
inner_entity_move(entity_index, e, r, node)
|
||||
local n = #entities
|
||||
table.move(entities, 1, n, count + 1, queue)
|
||||
count += n
|
||||
end
|
||||
|
||||
for _, e in queue do
|
||||
for id in ids do
|
||||
inner_world_remove(world, e, id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -2767,7 +2747,7 @@ local function world_new()
|
|||
archetype_destroy(world, idr_archetype)
|
||||
end
|
||||
else
|
||||
local on_remove = idr.on_remove
|
||||
local on_remove = idr.hooks.on_remove
|
||||
if on_remove then
|
||||
for archetype_id in idr.records do
|
||||
local idr_archetype = archetypes[archetype_id]
|
||||
|
@ -2784,7 +2764,7 @@ local function world_new()
|
|||
-- this is hypothetically not that expensive of an operation anyways
|
||||
to = archetype_traverse_remove(world, entity, from)
|
||||
end
|
||||
inner_entity_move(entity_index, e, r, to)
|
||||
entity_move(entity_index, e, r, to)
|
||||
end
|
||||
|
||||
archetype_destroy(world, idr_archetype)
|
||||
|
@ -2805,15 +2785,19 @@ local function world_new()
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
if idr_t then
|
||||
local children: { i53 }
|
||||
local ids: Map<i53, boolean>
|
||||
|
||||
local count = 0
|
||||
local archetype_ids = idr_t.records
|
||||
for archetype_id in archetype_ids do
|
||||
local idr_t_archetype = archetypes[archetype_id]
|
||||
local node = idr_t_archetype
|
||||
local idr_t_types = idr_t_archetype.types
|
||||
local entities = idr_t_archetype.entities
|
||||
local removal_queued = false
|
||||
|
||||
local deleted = false
|
||||
for _, id in idr_t_types do
|
||||
if not ECS_IS_PAIR(id::number) then
|
||||
continue
|
||||
|
@ -2831,24 +2815,31 @@ local function world_new()
|
|||
local child = entities[i]
|
||||
inner_world_delete(world, child)
|
||||
end
|
||||
deleted = true
|
||||
break
|
||||
else
|
||||
node = archetype_traverse_remove(world, id, node)
|
||||
local on_remove = component_index[id].on_remove
|
||||
if on_remove then
|
||||
for _, entity in entities do
|
||||
on_remove(entity, id)
|
||||
end
|
||||
if not ids then
|
||||
ids = {} :: { [i53]: boolean }
|
||||
end
|
||||
ids[id] = true
|
||||
removal_queued = true
|
||||
end
|
||||
end
|
||||
|
||||
if not deleted then
|
||||
for i = #entities, 1, -1 do
|
||||
local e = entities[i]
|
||||
local r = inner_entity_index_try_get_unsafe(e::number) :: Record
|
||||
inner_entity_move(entity_index, e, r, node)
|
||||
if not removal_queued then
|
||||
continue
|
||||
end
|
||||
if not children then
|
||||
children = {} :: { i53 }
|
||||
end
|
||||
local n = #entities
|
||||
table.move(entities, 1, n, count + 1, children)
|
||||
count += n
|
||||
end
|
||||
|
||||
if ids then
|
||||
for _, child in children do
|
||||
for id in ids do
|
||||
inner_world_remove(world, child, id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -2872,29 +2863,28 @@ local function world_new()
|
|||
archetype_destroy(world, idr_r_archetype)
|
||||
end
|
||||
else
|
||||
local children = {}
|
||||
local count = 0
|
||||
local ids = {}
|
||||
local counts = idr_r.counts
|
||||
local records = idr_r.records
|
||||
for archetype_id in archetype_ids do
|
||||
local idr_r_archetype = archetypes[archetype_id]
|
||||
local node = idr_r_archetype
|
||||
local entities = idr_r_archetype.entities
|
||||
local tr = records[archetype_id]
|
||||
local tr_count = counts[archetype_id]
|
||||
local types = idr_r_archetype.types
|
||||
for i = tr, tr + tr_count - 1 do
|
||||
local id = types[i]
|
||||
node = archetype_traverse_remove(world, id, node)
|
||||
local on_remove = component_index[id].on_remove
|
||||
if on_remove then
|
||||
for _, entity in entities do
|
||||
on_remove(entity, id)
|
||||
end
|
||||
end
|
||||
ids[types[i]] = true
|
||||
end
|
||||
for i = #entities, 1, -1 do
|
||||
local e = entities[i]
|
||||
local r = inner_entity_index_try_get_unsafe(e::number) :: Record
|
||||
inner_entity_move(entity_index, e, r, node)
|
||||
|
||||
local n = #entities
|
||||
table.move(entities, 1, n, count + 1, children)
|
||||
count += n
|
||||
end
|
||||
for _, child in children do
|
||||
for id in ids do
|
||||
inner_world_remove(world, child, id)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@rbxts/jecs",
|
||||
"version": "0.8.1",
|
||||
"version": "0.7.3",
|
||||
"description": "Stupidly fast Entity Component System",
|
||||
"main": "jecs.luau",
|
||||
"repository": {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "ukendio/jecs"
|
||||
version = "0.8.1"
|
||||
version = "0.7.3"
|
||||
registry = "https://github.com/UpliftGames/wally-index"
|
||||
realm = "shared"
|
||||
license = "MIT"
|
||||
|
|
Loading…
Reference in a new issue