diff --git a/.luaurc b/.luaurc index 07221f7..d1ae244 100644 --- a/.luaurc +++ b/.luaurc @@ -1,8 +1,9 @@ { "aliases": { "jecs": "jecs", - "testkit": "test/testkit", - "mirror": "mirror" + "testkit": "tools/testkit", + "mirror": "mirror", + "tools": "tools", }, "languageMode": "strict" } diff --git a/jecs.luau b/jecs.luau index e53d366..69a4ad7 100644 --- a/jecs.luau +++ b/jecs.luau @@ -13,40 +13,50 @@ type Column = { any } type Map = { [K]: V } -type GraphEdge = { - from: Archetype, - to: Archetype?, +type ecs_graph_edge_t = { + from: ecs_archetype_t, + to: ecs_archetype_t?, id: number, - prev: GraphEdge?, - next: GraphEdge?, + prev: ecs_graph_edge_t?, + next: ecs_graph_edge_t?, } -type GraphEdges = Map +type ecs_graph_edges_t = Map -type GraphNode = { - add: GraphEdges, - remove: GraphEdges, - refs: GraphEdge, +type ecs_graph_node_t = { + add: ecs_graph_edges_t, + remove: ecs_graph_edges_t, + refs: ecs_graph_edge_t, } +type ecs_archetype_t = { + id: number, + types: Ty, + type: string, + entities: { number }, + columns: { Column }, + records: { [i53]: number }, + counts: { [i53]: number }, +} & ecs_graph_node_t + export type Archetype = { id: number, types: Ty, type: string, entities: { number }, columns: { Column }, - records: { number }, - counts: { number }, -} & GraphNode + records: { [Id]: number }, + counts: { [Id]: number }, +} -export type Record = { - archetype: Archetype, +type ecs_record_t = { + archetype: ecs_archetype_t, row: number, dense: i24, } -type IdRecord = { - columns: { number }, +type ecs_id_record_t = { + cache: { number }, counts: { number }, flags: number, size: number, @@ -57,22 +67,46 @@ type IdRecord = { }, } -type ComponentIndex = Map +type ecs_id_index_t = Map -type Archetypes = { [ArchetypeId]: Archetype } +type ecs_archetypes_map_t = { [string]: ecs_archetype_t } -type ArchetypeDiff = { - added: Ty, - removed: Ty, -} +type ecs_archetypes_t = { ecs_archetype_t } -type EntityIndex = { - dense_array: Map, - sparse_array: Map, +type ecs_entity_index_t = { + dense_array: Map, + sparse_array: Map, alive_count: number, max_id: number, } +type ecs_query_data_t = { + compatible_archetypes: { ecs_archetype_t }, + ids: { i53 }, + filter_with: { i53 }, + filter_without: { i53 }, + next: () -> (number, ...any), + world: ecs_world_t, +} + +type ecs_observer_t = { + callback: (archetype: ecs_archetype_t) -> (), + query: ecs_query_data_t, +} + +type ecs_observable_t = Map> + +type ecs_world_t = { + entity_index: ecs_entity_index_t, + component_index: ecs_id_index_t, + archetypes: ecs_archetypes_t, + archetype_index: ecs_archetypes_map_t, + max_archetype_id: number, + max_component_id: number, + ROOT_ARCHETYPE: ecs_archetype_t, + observable: Map>, +} + local HI_COMPONENT_ID = _G.__JECS_HI_COMPONENT_ID or 256 -- stylua: ignore start local EcsOnAdd = HI_COMPONENT_ID + 1 @@ -90,60 +124,39 @@ local EcsOnArchetypeCreate = HI_COMPONENT_ID + 12 local EcsOnArchetypeDelete = HI_COMPONENT_ID + 13 local EcsRest = HI_COMPONENT_ID + 14 -local ECS_PAIR_FLAG = 0x8 -local ECS_ID_FLAGS_MASK = 0x10 -local ECS_ENTITY_MASK = bit32.lshift(1, 24) -local ECS_GENERATION_MASK = bit32.lshift(1, 16) - local ECS_ID_DELETE = 0b0000_0001 local ECS_ID_IS_TAG = 0b0000_0010 local ECS_ID_HAS_ON_ADD = 0b0000_0100 local ECS_ID_HAS_ON_SET = 0b0000_1000 local ECS_ID_HAS_ON_REMOVE = 0b0001_0000 local ECS_ID_MASK = 0b0000_0000 --- stylua: ignore end -local NULL_ARRAY = table.freeze({}) :: Column -local function FLAGS_ADD(is_pair: boolean): number - local flags = 0x0 +local ECS_ENTITY_MASK = bit32.lshift(1, 24) +local ECS_GENERATION_MASK = bit32.lshift(1, 16) - if is_pair then - flags = bit32.bor(flags, ECS_PAIR_FLAG) -- HIGHEST bit in the ID. - end - if false then - flags = bit32.bor(flags, 0x4) -- Set the second flag to true - end - if false then - flags = bit32.bor(flags, 0x2) -- Set the third flag to true - end - if false then - flags = bit32.bor(flags, 0x1) -- LAST BIT in the ID. - end +local NULL_ARRAY = table.freeze({}) +local ECS_INTERNAL_ERROR = [[ + This is an internal error, please file a bug report via the following link: - return flags -end - -local function ECS_COMBINE(source: number, target: number): i53 - return (source * 268435456) + (target * ECS_ID_FLAGS_MASK) + https://github.com/Ukendio/jecs/issues/new?template=BUG-REPORT.md +]] + +local function ECS_COMBINE(id: number, generation: number): i53 + return id + (generation * ECS_ENTITY_MASK) end +local ECS_PAIR_OFFSET = 2^48 local function ECS_IS_PAIR(e: number): boolean - return if e > ECS_ENTITY_MASK then (e % ECS_ID_FLAGS_MASK) // ECS_PAIR_FLAG ~= 0 else false + return e > ECS_PAIR_OFFSET end --- HIGH 24 bits LOW 24 bits -local function ECS_GENERATION(e: i53): i24 - return if e > ECS_ENTITY_MASK then (e // ECS_ID_FLAGS_MASK) % ECS_GENERATION_MASK else 0 -end - -local function ECS_GENERATION_INC(e: i53) +local function ECS_GENERATION_INC(e: i53): i53 if e > ECS_ENTITY_MASK then - local flags = e // ECS_ID_FLAGS_MASK - local id = flags // ECS_ENTITY_MASK - local generation = flags % ECS_GENERATION_MASK + local id = e % ECS_ENTITY_MASK + local generation = e // ECS_ENTITY_MASK local next_gen = generation + 1 - if next_gen > ECS_GENERATION_MASK then + if next_gen >= ECS_GENERATION_MASK then return id end @@ -152,29 +165,38 @@ local function ECS_GENERATION_INC(e: i53) return ECS_COMBINE(e, 1) end --- FIRST gets the high ID -local function ECS_ENTITY_T_HI(e: i53): i24 - return if e > ECS_ENTITY_MASK then (e // ECS_ID_FLAGS_MASK) % ECS_ENTITY_MASK else e -end - --- SECOND local function ECS_ENTITY_T_LO(e: i53): i24 - return if e > ECS_ENTITY_MASK then (e // ECS_ID_FLAGS_MASK) // ECS_ENTITY_MASK else e + return e % ECS_ENTITY_MASK end -local function _STRIP_GENERATION(e: i53): i24 - return ECS_ENTITY_T_LO(e) +local function ECS_GENERATION(e: i53) + return e // ECS_ENTITY_MASK +end + +local function ECS_ENTITY_T_HI(e: i53): i24 + return e // ECS_ENTITY_MASK end local function ECS_PAIR(pred: i53, obj: i53): i53 - return ECS_COMBINE(ECS_ENTITY_T_LO(pred), ECS_ENTITY_T_LO(obj)) + FLAGS_ADD(--[[isPair]] true) :: i53 + pred %= ECS_ENTITY_MASK + obj %= ECS_ENTITY_MASK + + return obj + (pred * ECS_ENTITY_MASK) + ECS_PAIR_OFFSET end -local function entity_index_try_get_any(entity_index: EntityIndex, entity: number): Record? +local function ECS_PAIR_FIRST(e: i53): i24 + return (e - ECS_PAIR_OFFSET) // ECS_ENTITY_MASK +end + +local function ECS_PAIR_SECOND(e: i53): i24 + return (e - ECS_PAIR_OFFSET) % ECS_ENTITY_MASK +end + +local function entity_index_try_get_any( + entity_index: ecs_entity_index_t, + entity: number +): ecs_record_t? local r = entity_index.sparse_array[ECS_ENTITY_T_LO(entity)] - if not r then - return nil - end if not r or r.dense == 0 then return nil @@ -183,7 +205,7 @@ local function entity_index_try_get_any(entity_index: EntityIndex, entity: numbe return r end -local function entity_index_try_get(entity_index: EntityIndex, entity: number): Record? +local function entity_index_try_get(entity_index: ecs_entity_index_t, entity: number): ecs_record_t? local r = entity_index_try_get_any(entity_index, entity) if r then local r_dense = r.dense @@ -197,7 +219,7 @@ local function entity_index_try_get(entity_index: EntityIndex, entity: number): return r end -local function entity_index_try_get_fast(entity_index: EntityIndex, entity: number): Record? +local function entity_index_try_get_fast(entity_index: ecs_entity_index_t, entity: number): ecs_record_t? local r = entity_index.sparse_array[ECS_ENTITY_T_LO(entity)] if r then if entity_index.dense_array[r.dense] ~= entity then @@ -207,49 +229,74 @@ local function entity_index_try_get_fast(entity_index: EntityIndex, entity: numb return r end -local function entity_index_get_alive(index: EntityIndex, e: i24): i53 - local r = entity_index_try_get_any(index, e) - if r then - return index.dense_array[r.dense] - end - return 0 -end - -local function entity_index_is_alive(entity_index: EntityIndex, entity: number) +local function entity_index_is_alive(entity_index: ecs_entity_index_t, entity: i53) return entity_index_try_get(entity_index, entity) ~= nil end -local function entity_index_new_id(entity_index: EntityIndex): i53 +local function entity_index_get_alive(index: ecs_entity_index_t, entity: i53): i53? + local r = entity_index_try_get_any(index, entity) + if r then + return index.dense_array[r.dense] + end + return nil +end + +local function ecs_get_alive(world, entity) + if entity == 0 then + return 0 + end + + local eindex = world.entity_index + + if entity_index_is_alive(eindex, entity) then + return entity + end + + if entity > ECS_ENTITY_MASK then + return 0 + end + + local current = entity_index_get_alive(eindex, entity) + if not current or not entity_index_is_alive(eindex, current) then + return 0 + end + + return current +end + +local function entity_index_new_id(entity_index: ecs_entity_index_t): i53 local dense_array = entity_index.dense_array local alive_count = entity_index.alive_count - if alive_count ~= #dense_array then + local max_id = entity_index.max_id + if alive_count ~= max_id then alive_count += 1 entity_index.alive_count = alive_count local id = dense_array[alive_count] return id end - local id = entity_index.max_id + 1 + local id = max_id + 1 entity_index.max_id = id alive_count += 1 entity_index.alive_count = alive_count dense_array[alive_count] = id - entity_index.sparse_array[id] = { dense = alive_count } :: Record + entity_index.sparse_array[id] = { dense = alive_count } :: ecs_record_t return id end --- ECS_PAIR_FIRST, gets the relationship target / obj / HIGH bits -local function ecs_pair_first(world, e) - return entity_index_get_alive(world.entity_index, ECS_ENTITY_T_LO(e)) +local function ecs_pair_first(world: ecs_world_t, e: i53) + local pred = ECS_PAIR_FIRST(e) + return ecs_get_alive(world, pred) end --- ECS_PAIR_SECOND gets the relationship / pred / LOW bits -local function ecs_pair_second(world, e) - return entity_index_get_alive(world.entity_index, ECS_ENTITY_T_HI(e)) +local function ecs_pair_second(world: ecs_world_t, e: i53) + local obj = ECS_PAIR_SECOND(e) + return ecs_get_alive(world, obj) end -local function query_match(query, archetype: Archetype) +local function query_match(query: ecs_query_data_t, + archetype: ecs_archetype_t) local records = archetype.records local with = query.filter_with @@ -271,7 +318,8 @@ local function query_match(query, archetype: Archetype) return true end -local function find_observers(world: World, event, component): { Observer }? +local function find_observers(world: ecs_world_t, event: i53, + component: i53): { ecs_observer_t }? local cache = world.observable[event] if not cache then return nil @@ -279,7 +327,13 @@ local function find_observers(world: World, event, component): { Observer }? return cache[component] :: any end -local function archetype_move(entity_index: EntityIndex, to: Archetype, dst_row: i24, from: Archetype, src_row: i24) +local function archetype_move( + entity_index: ecs_entity_index_t, + to: ecs_archetype_t, + dst_row: i24, + from: ecs_archetype_t, + src_row: i24 +) local src_columns = from.columns local dst_columns = to.columns local dst_entities = to.entities @@ -333,21 +387,33 @@ local function archetype_move(entity_index: EntityIndex, to: Archetype, dst_row: record2.row = src_row end -local function archetype_append(entity: number, archetype: Archetype): number +local function archetype_append( + entity: i53, + archetype: ecs_archetype_t +): number local entities = archetype.entities local length = #entities + 1 entities[length] = entity return length end -local function new_entity(entity: i53, record: Record, archetype: Archetype): Record +local function new_entity( + entity: i53, + record: ecs_record_t, + archetype: ecs_archetype_t +): ecs_record_t local row = archetype_append(entity, archetype) record.archetype = archetype record.row = row return record end -local function entity_move(entity_index: EntityIndex, entity: i53, record: Record, to: Archetype) +local function entity_move( + entity_index: ecs_entity_index_t, + entity: i53, + record: ecs_record_t, + to: ecs_archetype_t +) local sourceRow = record.row local from = record.archetype local dst_row = archetype_append(entity, to) @@ -360,7 +426,8 @@ local function hash(arr: { number }): string return table.concat(arr, "_") end -local function fetch(id, records: { number }, columns: { Column }, row: number): any +local function fetch(id: i53, records: { number }, + columns: { Column }, row: number): any local tr = records[id] if not tr then @@ -370,7 +437,8 @@ local function fetch(id, records: { number }, columns: { Column }, row: number): return columns[tr][row] end -local function world_get(world: World, entity: i53, a: i53, b: i53?, c: i53?, d: i53?, e: i53?): ...any +local function world_get(world: ecs_world_t, entity: i53, + a: i53, b: i53?, c: i53?, d: i53?, e: i53?): ...any local record = entity_index_try_get_fast(world.entity_index, entity) if not record then return nil @@ -400,25 +468,7 @@ local function world_get(world: World, entity: i53, a: i53, b: i53?, c: i53?, d: end end -local function world_get_one_inline(world: World, entity: i53, id: i53): any - local record = entity_index_try_get_fast(world.entity_index, entity) - if not record then - return nil - end - - local archetype = record.archetype - if not archetype then - return nil - end - - local tr = archetype.records[id] - if not tr then - return nil - end - return archetype.columns[tr][record.row] -end - -local function world_has_one_inline(world: World, entity: number, id: i53): boolean +local function world_has_one_inline(world: ecs_world_t, entity: i53, id: i53): boolean local record = entity_index_try_get_fast(world.entity_index, entity) if not record then return false @@ -434,7 +484,7 @@ local function world_has_one_inline(world: World, entity: number, id: i53): bool return records[id] ~= nil end -local function world_has(world: World, entity: number, ...: i53): boolean +local function world_has(world: ecs_world_t, entity: i53, ...: i53): boolean local record = entity_index_try_get_fast(world.entity_index, entity) if not record then return false @@ -456,7 +506,7 @@ local function world_has(world: World, entity: number, ...: i53): boolean return true end -local function world_target(world: World, entity: i53, relation: i24, index: number?): i24? +local function world_target(world: ecs_world_t, entity: i53, relation: i24, index: number?): i24? local nth = index or 0 local record = entity_index_try_get_fast(world.entity_index, entity) if not record then @@ -468,13 +518,9 @@ local function world_target(world: World, entity: i53, relation: i24, index: num return nil end - local idr = world.component_index[ECS_PAIR(relation, EcsWildcard)] - if not idr then - return nil - end + local r = ECS_PAIR(relation, EcsWildcard) - local archetype_id = archetype.id - local count = idr.counts[archetype.id] + local count = archetype.counts[r] if not count then return nil end @@ -483,15 +529,13 @@ local function world_target(world: World, entity: i53, relation: i24, index: num nth = nth + count + 1 end - local tr = idr.columns[archetype_id] - - nth = archetype.types[nth + tr] - + nth = archetype.types[nth + archetype.records[r]] if not nth then return nil end - return ecs_pair_second(world, nth) + return entity_index_get_alive(world.entity_index, + ECS_PAIR_SECOND(nth)) end local function ECS_ID_IS_WILDCARD(e: i53): boolean @@ -500,16 +544,23 @@ local function ECS_ID_IS_WILDCARD(e: i53): boolean return first == EcsWildcard or second == EcsWildcard end -local function id_record_ensure(world: World, id: number): IdRecord +local function id_record_ensure(world: ecs_world_t, id: number): ecs_id_record_t local component_index = world.component_index - local idr: IdRecord = component_index[id] + local entity_index = world.entity_index + local idr: ecs_id_record_t = component_index[id] if not idr then local flags = ECS_ID_MASK local relation = id + local target = 0 local is_pair = ECS_IS_PAIR(id) if is_pair then - relation = ecs_pair_first(world, id) + relation = entity_index_get_alive(entity_index, ECS_PAIR_FIRST(id)) :: i53 + assert(relation and entity_index_is_alive( + entity_index, relation), ECS_INTERNAL_ERROR) + target = entity_index_get_alive(entity_index, ECS_PAIR_SECOND(id)) :: i53 + assert(target and entity_index_is_alive( + entity_index, target), ECS_INTERNAL_ERROR) end local cleanup_policy = world_target(world, relation, EcsOnDelete, 0) @@ -526,7 +577,7 @@ local function id_record_ensure(world: World, id: number): IdRecord local is_tag = not world_has_one_inline(world, relation, EcsComponent) if is_tag and is_pair then - is_tag = not world_has_one_inline(world, ecs_pair_second(world, id), EcsComponent) + is_tag = not world_has_one_inline(world, target, EcsComponent) end flags = bit32.bor( @@ -540,7 +591,7 @@ local function id_record_ensure(world: World, id: number): IdRecord idr = { size = 0, - columns = {}, + cache = {}, counts = {}, flags = flags, hooks = { @@ -557,15 +608,15 @@ local function id_record_ensure(world: World, id: number): IdRecord end local function archetype_append_to_records( - idr: IdRecord, - archetype: Archetype, - id: number, + idr: ecs_id_record_t, + archetype: ecs_archetype_t, + id: i53, index: number ) 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 @@ -581,7 +632,7 @@ local function archetype_append_to_records( end end -local function archetype_create(world: World, id_types: { i24 }, ty, prev: i53?): Archetype +local function archetype_create(world: ecs_world_t, id_types: { i24 }, ty, prev: i53?): ecs_archetype_t local archetype_id = (world.max_archetype_id :: number) + 1 world.max_archetype_id = archetype_id @@ -591,7 +642,7 @@ local function archetype_create(world: World, id_types: { i24 }, ty, prev: i53?) local records: { number } = {} local counts: {number} = {} - local archetype: Archetype = { + local archetype: ecs_archetype_t = { columns = columns, entities = {}, id = archetype_id, @@ -602,17 +653,16 @@ local function archetype_create(world: World, id_types: { i24 }, ty, prev: i53?) add = {}, remove = {}, - refs = {} :: GraphEdge, + refs = {} :: ecs_graph_edge_t, } - for i, componentId in id_types do - local idr = id_record_ensure(world, componentId) - archetype_append_to_records(idr, archetype, componentId, i) - - if ECS_IS_PAIR(componentId) then - local relation = ecs_pair_first(world, componentId) - local object = ecs_pair_second(world, componentId) + for i, component_id in id_types do + local idr = id_record_ensure(world, component_id) + archetype_append_to_records(idr, archetype, component_id, i) + if ECS_IS_PAIR(component_id) then + local relation = ECS_PAIR_FIRST(component_id) + local object = ECS_PAIR_SECOND(component_id) local r = ECS_PAIR(relation, EcsWildcard) local idr_r = id_record_ensure(world, r) archetype_append_to_records(idr_r, archetype, r, i) @@ -647,15 +697,15 @@ local function archetype_create(world: World, id_types: { i24 }, ty, prev: i53?) return archetype end -local function world_entity(world: World): i53 +local function world_entity(world: ecs_world_t): i53 return entity_index_new_id(world.entity_index) end -local function world_parent(world: World, entity: i53) +local function world_parent(world: ecs_world_t, entity: i53) return world_target(world, entity, EcsChildOf, 0) end -local function archetype_ensure(world: World, id_types): Archetype +local function archetype_ensure(world: ecs_world_t, id_types): ecs_archetype_t if #id_types < 1 then return world.ROOT_ARCHETYPE end @@ -681,7 +731,7 @@ local function find_insert(id_types: { i53 }, toAdd: i53): number return #id_types + 1 end -local function find_archetype_with(world: World, node: Archetype, id: i53): Archetype +local function find_archetype_with(world: ecs_world_t, node: ecs_archetype_t, id: i53): ecs_archetype_t local id_types = node.types -- Component IDs are added incrementally, so inserting and sorting -- them each time would be expensive. Instead this insertion sort can find the insertion @@ -699,7 +749,11 @@ local function find_archetype_with(world: World, node: Archetype, id: i53): Arch return archetype_ensure(world, dst) end -local function find_archetype_without(world: World, node: Archetype, id: i53): Archetype +local function find_archetype_without( + world: ecs_world_t, + node: ecs_archetype_t, + id: i53 +): ecs_archetype_t local id_types = node.types local at = table.find(id_types, id) if at == nil then @@ -712,23 +766,32 @@ local function find_archetype_without(world: World, node: Archetype, id: i53): A return archetype_ensure(world, dst) end -local function archetype_init_edge(archetype: Archetype, edge: GraphEdge, id: i53, to: Archetype) +local function archetype_init_edge( + archetype: ecs_archetype_t, + edge: ecs_graph_edge_t, + id: i53, + to: ecs_archetype_t +) edge.from = archetype edge.to = to edge.id = id end -local function archetype_ensure_edge(world, edges: GraphEdges, id): GraphEdge +local function archetype_ensure_edge( + world: ecs_world_t, + edges: ecs_graph_edges_t, + id: i53 +): ecs_graph_edge_t local edge = edges[id] if not edge then - edge = {} :: GraphEdge + edge = {} :: ecs_graph_edge_t edges[id] = edge end return edge end -local function init_edge_for_add(world, archetype: Archetype, edge: GraphEdge, id, to: Archetype) +local function init_edge_for_add(world, archetype: ecs_archetype_t, edge: ecs_graph_edge_t, id, to: ecs_archetype_t) archetype_init_edge(archetype, edge, id, to) archetype_ensure_edge(world, archetype.add, id) if archetype ~= to then @@ -745,7 +808,13 @@ local function init_edge_for_add(world, archetype: Archetype, edge: GraphEdge, i end end -local function init_edge_for_remove(world: World, archetype: Archetype, edge: GraphEdge, id: number, to: Archetype) +local function init_edge_for_remove( + world: ecs_world_t, + archetype: ecs_archetype_t, + edge: ecs_graph_edge_t, + id: number, + to: ecs_archetype_t +) archetype_init_edge(archetype, edge, id, to) archetype_ensure_edge(world, archetype.remove, id) if archetype ~= to then @@ -762,19 +831,33 @@ local function init_edge_for_remove(world: World, archetype: Archetype, edge: Gr end end -local function create_edge_for_add(world: World, node: Archetype, edge: GraphEdge, id: i53): Archetype +local function create_edge_for_add( + world: ecs_world_t, + node: ecs_archetype_t, + edge: ecs_graph_edge_t, + id: i53 +): ecs_archetype_t local to = find_archetype_with(world, node, id) init_edge_for_add(world, node, edge, id, to) return to end -local function create_edge_for_remove(world: World, node: Archetype, edge: GraphEdge, id: i53): Archetype +local function create_edge_for_remove( + world: ecs_world_t, + node: ecs_archetype_t, + edge: ecs_graph_edge_t, + id: i53 +): ecs_archetype_t local to = find_archetype_without(world, node, id) init_edge_for_remove(world, node, edge, id, to) return to end -local function archetype_traverse_add(world: World, id: i53, from: Archetype): Archetype +local function archetype_traverse_add( + world: ecs_world_t, + id: i53, + from: ecs_archetype_t +): ecs_archetype_t from = from or world.ROOT_ARCHETYPE local edge = archetype_ensure_edge(world, from.add, id) @@ -783,10 +866,14 @@ local function archetype_traverse_add(world: World, id: i53, from: Archetype): A to = create_edge_for_add(world, from, edge, id) end - return to :: Archetype + return to :: ecs_archetype_t end -local function archetype_traverse_remove(world: World, id: i53, from: Archetype): Archetype +local function archetype_traverse_remove( + world: ecs_world_t, + id: i53, + from: ecs_archetype_t +): ecs_archetype_t from = from or world.ROOT_ARCHETYPE local edge = archetype_ensure_edge(world, from.remove, id) @@ -796,10 +883,14 @@ local function archetype_traverse_remove(world: World, id: i53, from: Archetype) to = create_edge_for_remove(world, from, edge, id) end - return to :: Archetype + return to :: ecs_archetype_t end -local function world_add(world: World, entity: i53, id: i53): () +local function world_add( + world: ecs_world_t, + entity: i53, + id: i53 +): () local entity_index = world.entity_index local record = entity_index_try_get_fast(entity_index, entity) if not record then @@ -827,15 +918,15 @@ local function world_add(world: World, entity: i53, id: i53): () end end -local function world_set(world: World, entity: i53, id: i53, data: unknown): () +local function world_set(world: ecs_world_t, entity: i53, id: i53, data: unknown): () local entity_index = world.entity_index local record = entity_index_try_get_fast(entity_index, entity) if not record then return end - local from: Archetype = record.archetype - local to: Archetype = archetype_traverse_add(world, id, from) + local from: ecs_archetype_t = record.archetype + local to: ecs_archetype_t = archetype_traverse_add(world, id, from) local idr = world.component_index[id] local idr_hooks = idr.hooks @@ -863,16 +954,16 @@ local function world_set(world: World, entity: i53, id: i53, data: unknown): () end end - local on_add = idr_hooks.on_add - if on_add then - on_add(entity) - end - local tr = to.records[id] local column = to.columns[tr] column[record.row] = data + local on_add = idr_hooks.on_add + if on_add then + on_add(entity) + end + local on_set = idr_hooks.on_set if on_set then on_set(entity, data) @@ -891,7 +982,7 @@ local function world_component(world: World): i53 return id end -local function world_remove(world: World, entity: i53, id: i53) +local function world_remove(world: ecs_world_t, entity: i53, id: i53) local entity_index = world.entity_index local record = entity_index_try_get_fast(entity_index, entity) if not record then @@ -902,15 +993,16 @@ local function world_remove(world: World, entity: i53, id: i53) if not from then return end - local to = archetype_traverse_remove(world, id, from) - if from ~= to then + if from.records[id] then local idr = world.component_index[id] local on_remove = idr.hooks.on_remove if on_remove then on_remove(entity) end + local to = archetype_traverse_remove(world, id, record.archetype) + entity_move(entity_index, entity, record, to) end end @@ -932,7 +1024,7 @@ local function archetype_fast_delete(columns: { Column }, column_count: number, end end -local function archetype_delete(world: World, archetype: Archetype, row: number, destruct: boolean?) +local function archetype_delete(world: ecs_world_t, archetype: ecs_archetype_t, row: number) local entity_index = world.entity_index local component_index = world.component_index local columns = archetype.columns @@ -971,7 +1063,7 @@ local function archetype_delete(world: World, archetype: Archetype, row: number, end end -local function world_clear(world: World, entity: i53) +local function world_clear(world: ecs_world_t, entity: i53) --TODO: use sparse_get (stashed) local record = entity_index_try_get(world.entity_index, entity) if not record then @@ -981,6 +1073,22 @@ local function world_clear(world: World, entity: i53) local archetype = record.archetype local row = record.row + local idr = world.component_index[entity] + if idr then + local count = 0 + local queue = {} + for archetype_id in idr.cache do + local idr_archetype = world.archetypes[archetype_id] + local entities = idr_archetype.entities + local n = #entities + count += n + table.move(entities, 1, n, #queue + 1, queue) + end + for _, e in queue do + world_remove(world, e, entity) + end + end + if archetype then -- In the future should have a destruct mode for -- deleting archetypes themselves. Maybe requires recycling @@ -991,7 +1099,7 @@ local function world_clear(world: World, entity: i53) record.row = nil :: any end -local function archetype_disconnect_edge(edge: GraphEdge) +local function archetype_disconnect_edge(edge: ecs_graph_edge_t) local edge_next = edge.next local edge_prev = edge.prev if edge_next then @@ -1002,14 +1110,14 @@ local function archetype_disconnect_edge(edge: GraphEdge) end end -local function archetype_remove_edge(edges: Map, id: i53, edge: GraphEdge) +local function archetype_remove_edge(edges: ecs_graph_edges_t, id: i53, edge: ecs_graph_edge_t) archetype_disconnect_edge(edge) edges[id] = nil :: any end -local function archetype_clear_edges(archetype: Archetype) - local add: GraphEdges = archetype.add - local remove: GraphEdges = archetype.remove +local function archetype_clear_edges(archetype: ecs_archetype_t) + local add: ecs_graph_edges_t = archetype.add + local remove: ecs_graph_edges_t = archetype.remove local node_refs = archetype.refs for id, edge in add do archetype_disconnect_edge(edge) @@ -1022,7 +1130,7 @@ local function archetype_clear_edges(archetype: Archetype) local cur = node_refs.next while cur do - local edge = cur :: GraphEdge + local edge = cur :: ecs_graph_edge_t local next_edge = edge.next archetype_remove_edge(edge.from.add, edge.id, edge) cur = next_edge @@ -1030,7 +1138,7 @@ local function archetype_clear_edges(archetype: Archetype) cur = node_refs.prev while cur do - local edge: GraphEdge = cur + local edge: ecs_graph_edge_t = cur local next_edge = edge.prev archetype_remove_edge(edge.from.remove, edge.id, edge) cur = next_edge @@ -1040,7 +1148,7 @@ local function archetype_clear_edges(archetype: Archetype) node_refs.prev = nil end -local function archetype_destroy(world: World, archetype: Archetype) +local function archetype_destroy(world: ecs_world_t, archetype: ecs_archetype_t) if archetype == world.ROOT_ARCHETYPE then return end @@ -1066,7 +1174,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 @@ -1076,7 +1184,7 @@ local function archetype_destroy(world: World, archetype: Archetype) end end -local function world_cleanup(world: World) +local function world_cleanup(world: ecs_world_t) local archetypes = world.archetypes for _, archetype in archetypes do @@ -1085,7 +1193,7 @@ local function world_cleanup(world: World) end end - local new_archetypes = table.create(#archetypes) :: { Archetype } + local new_archetypes = table.create(#archetypes) :: { ecs_archetype_t } local new_archetype_map = {} for index, archetype in archetypes do @@ -1097,155 +1205,143 @@ local function world_cleanup(world: World) world.archetype_index = new_archetype_map end -local world_delete: (world: World, entity: i53, destruct: boolean?) -> () -do - function world_delete(world: World, entity: i53, destruct: boolean?) - local entity_index = world.entity_index - local record = entity_index_try_get(entity_index, entity) - if not record then - return - end - - local archetype = record.archetype - local row = record.row - - if archetype then - -- In the future should have a destruct mode for - -- deleting archetypes themselves. Maybe requires recycling - archetype_delete(world, archetype, row, destruct) - end - - local delete = entity - local component_index = world.component_index - local archetypes: Archetypes = world.archetypes - local tgt = ECS_PAIR(EcsWildcard, delete) - local idr_t = component_index[tgt] - local idr = component_index[delete] - - if idr then - local flags = idr.flags - if bit32.band(flags, ECS_ID_DELETE) ~= 0 then - for archetype_id in idr.columns do - local idr_archetype = archetypes[archetype_id] - - local entities = idr_archetype.entities - local n = #entities - for i = n, 1, -1 do - world_delete(world, entities[i]) - end - - archetype_destroy(world, idr_archetype) - end - else - for archetype_id in idr.columns do - local idr_archetype = archetypes[archetype_id] - local entities = idr_archetype.entities - local n = #entities - for i = n, 1, -1 do - world_remove(world, entities[i], delete) - end - - archetype_destroy(world, idr_archetype) - end - 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 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 - - 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 - end - end - end - - archetype_destroy(world, idr_t_archetype) - end - end - - local index_of_deleted_entity = record.dense - local index_of_last_alive_entity = entity_index.alive_count - entity_index.alive_count = index_of_last_alive_entity - 1 - - local last_alive_entity = dense_array[index_of_last_alive_entity] - local r_swap = entity_index_try_get_any(entity_index, last_alive_entity) :: Record - r_swap.dense = index_of_deleted_entity - record.archetype = nil :: any - record.row = nil :: any - record.dense = index_of_last_alive_entity - - dense_array[index_of_deleted_entity] = last_alive_entity - dense_array[index_of_last_alive_entity] = ECS_GENERATION_INC(entity) +local function world_delete(world: ecs_world_t, entity: i53) + local entity_index = world.entity_index + local record = entity_index_try_get(entity_index, entity) + if not record then + return end + + local archetype = record.archetype + local row = record.row + + if archetype then + -- In the future should have a destruct mode for + -- deleting archetypes themselves. Maybe requires recycling + archetype_delete(world, archetype, row) + end + + local delete = entity + local component_index = world.component_index + local archetypes = world.archetypes + local tgt = ECS_PAIR(EcsWildcard, delete) + local idr_t = component_index[tgt] + local idr = component_index[delete] + + if idr then + local flags = idr.flags + if bit32.band(flags, ECS_ID_DELETE) ~= 0 then + for archetype_id in idr.cache do + local idr_archetype = archetypes[archetype_id] + + local entities = idr_archetype.entities + local n = #entities + for i = n, 1, -1 do + world_delete(world, entities[i]) + end + + archetype_destroy(world, idr_archetype) + end + else + for archetype_id in idr.cache do + local idr_archetype = archetypes[archetype_id] + local entities = idr_archetype.entities + local n = #entities + for i = n, 1, -1 do + world_remove(world, entities[i], delete) + end + + archetype_destroy(world, idr_archetype) + end + end + end + + local dense_array = entity_index.dense_array + + if idr_t then + 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 + 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 = entity_index_get_alive( + entity_index, ECS_PAIR_SECOND(id)) + 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 + + 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 + + local index_of_deleted_entity = record.dense + local index_of_last_alive_entity = entity_index.alive_count + entity_index.alive_count = index_of_last_alive_entity - 1 + + local last_alive_entity = dense_array[index_of_last_alive_entity] + local r_swap = entity_index_try_get_any(entity_index, last_alive_entity) :: ecs_record_t + r_swap.dense = index_of_deleted_entity + record.archetype = nil :: any + record.row = nil :: any + record.dense = index_of_last_alive_entity + + dense_array[index_of_deleted_entity] = last_alive_entity + dense_array[index_of_last_alive_entity] = ECS_GENERATION_INC(entity) end -local function world_contains(world: World, entity): boolean +local function world_contains(world: ecs_world_t, entity): boolean return entity_index_is_alive(world.entity_index, entity) end local function NOOP() end -local function ARM(query, ...) - return query -end - -local EMPTY_LIST = {} -local EMPTY_QUERY = { - __iter = function() - return NOOP - end, - iter = function() - return NOOP - end, - with = ARM, - without = ARM, - archetypes = function() - return EMPTY_LIST - end, -} - -setmetatable(EMPTY_QUERY, EMPTY_QUERY) - -type QueryInner = { +export type QueryInner = { compatible_archetypes: { Archetype }, ids: { i53 }, filter_with: { i53 }, @@ -1254,7 +1350,9 @@ type QueryInner = { world: World, } -local function query_iter_init(query: QueryInner): () -> (number, ...any) + + +local function query_iter_init(query: ecs_query_data_t): () -> (number, ...any) local world_query_iter_next local compatible_archetypes = query.compatible_archetypes @@ -1599,7 +1697,7 @@ local function query_iter(query): () -> (number, ...any) return query_next end -local function query_without(query: QueryInner, ...: i53) +local function query_without(query: ecs_query_data_t, ...: i53) local without = { ... } query.filter_without = without local compatible_archetypes = query.compatible_archetypes @@ -1629,7 +1727,7 @@ local function query_without(query: QueryInner, ...: i53) return query :: any end -local function query_with(query: QueryInner, ...: i53) +local function query_with(query: ecs_query_data_t, ...: i53) local compatible_archetypes = query.compatible_archetypes local with = { ... } query.filter_with = with @@ -1667,7 +1765,7 @@ local function query_archetypes(query) return query.compatible_archetypes end -local function query_cached(query: QueryInner) +local function query_cached(query: ecs_query_data_t) local with = query.filter_with local ids = query.ids if with then @@ -1687,14 +1785,14 @@ local function query_cached(query: QueryInner) local columns: { Column } local entities: { number } local i: number - local archetype: Archetype + local archetype: ecs_archetype_t local records: { number } local archetypes = query.compatible_archetypes - local world = query.world :: { observable: Observable } + local world = query.world :: { observable: ecs_observable_t } -- Only need one observer for EcsArchetypeCreate and EcsArchetypeDelete respectively -- because the event will be emitted for all components of that Archetype. - local observable = world.observable :: Observable + local observable = world.observable :: ecs_observable_t local on_create_action = observable[EcsOnArchetypeCreate] if not on_create_action then on_create_action = {} @@ -1722,7 +1820,7 @@ local function query_cached(query: QueryInner) end local function on_delete_callback(archetype) - local i = table.find(archetypes, archetype) :: number + local i = table.find(archetypes, archetype) :: number local n = #archetypes archetypes[i] = archetypes[n] archetypes[n] = nil @@ -2088,7 +2186,7 @@ Query.with = query_with Query.archetypes = query_archetypes Query.cached = query_cached -local function world_query(world: World, ...) +local function world_query(world: ecs_world_t, ...) local compatible_archetypes = {} local length = 0 @@ -2096,7 +2194,7 @@ local function world_query(world: World, ...) local archetypes = world.archetypes - local idr: IdRecord? + local idr: ecs_id_record_t? local component_index = world.component_index local q = setmetatable({ @@ -2120,7 +2218,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 @@ -2148,15 +2246,15 @@ local function world_query(world: World, ...) return q end -local function world_each(world: World, id): () -> () +local function world_each(world: ecs_world_t, id: i53): () -> () local idr = world.component_index[id] if not idr then 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 @@ -2168,7 +2266,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 @@ -2182,10 +2280,36 @@ local function world_each(world: World, id): () -> () end end -local function world_children(world, parent) +local function world_children(world: ecs_world_t, parent: i53) return world_each(world, ECS_PAIR(EcsChildOf, parent)) end +export type Record = { + archetype: Archetype, + row: number, + dense: i24, +} +export type ComponentRecord = { + cache: { [Id]: number }, + counts: { [Id]: number }, + flags: number, + size: number, + hooks: { + on_add: ((entity: Entity) -> ())?, + on_set: ((entity: Entity, data: any) -> ())?, + on_remove: ((entity: Entity) -> ())?, + }, +} +export type ComponentIndex = Map +export type Archetypes = { [Id]: Archetype } + +export type EntityIndex = { + dense_array: Map, + sparse_array: Map, + alive_count: number, + max_id: number, +} + local World = {} World.__index = World @@ -2206,122 +2330,13 @@ World.cleanup = world_cleanup World.each = world_each World.children = world_children -if _G.__JECS_DEBUG then - local function dbg_info(n: number): any - return debug.info(n, "s") - end - local function throw(msg: string) - local s = 1 - local root = dbg_info(1) - repeat - s += 1 - until dbg_info(s) ~= root - if warn then - error(msg, s) - else - print(`[jecs] error: {msg}\n`) - end - end - - local function ASSERT(v: T, msg: string) - if v then - return - end - throw(msg) - end - - local function get_name(world, id) - return world_get_one_inline(world, id, EcsName) - end - - local function bname(world: World, id): string - local name: string - if ECS_IS_PAIR(id) then - local first = get_name(world, ecs_pair_first(world, id)) - local second = get_name(world, ecs_pair_second(world, id)) - name = `pair({first}, {second})` - else - return get_name(world, id) - end - if name then - return name - else - return `${id}` - end - end - - local function ID_IS_TAG(world: World, id) - if ECS_IS_PAIR(id) then - id = ecs_pair_first(world, id) - end - return not world_has_one_inline(world, id, EcsComponent) - end - - World.query = function(world: World, ...) - ASSERT((...), "Requires at least a single component") - return world_query(world, ...) - end - - World.set = function(world: World, entity: i53, id: i53, value: any): () - local is_tag = ID_IS_TAG(world, id) - if is_tag and value == nil then - local _1 = bname(world, entity) - local _2 = bname(world, id) - local why = "cannot set component value to nil" - throw(why) - return - elseif value ~= nil and is_tag then - local _1 = bname(world, entity) - local _2 = bname(world, id) - local why = `cannot set a component value because {_2} is a tag` - why ..= `\n[jecs] note: consider using "world:add({_1}, {_2})" instead` - throw(why) - return - end - - world_set(world, entity, id, value) - end - - World.add = function(world: World, entity: i53, id: i53, value: any) - if value ~= nil then - local _1 = bname(world, entity) - local _2 = bname(world, id) - throw("You provided a value when none was expected. " .. `Did you mean to use "world:add({_1}, {_2})"`) - end - - world_add(world, entity, id) - end - - World.get = function(world: World, entity: i53, ...) - local length = select("#", ...) - ASSERT(length < 5, "world:get does not support more than 4 components") - local _1 - for i = 1, length do - local id = select(i, ...) - local id_is_tag = not world_has(world, id, EcsComponent) - if id_is_tag then - local name = get_name(world, id) - if not _1 then - _1 = get_name(world, entity) - end - throw( - `cannot get (#{i}) component {name} value because it is a tag.` - .. `\n[jecs] note: If this was intentional, use "world:has({_1}, {name}) instead"` - ) - end - end - - return world_get(world, entity, ...) - end -end - -function World.new() - local entity_index: EntityIndex = { - dense_array = {} :: { [i24]: i53 }, - sparse_array = {} :: { [i53]: Record }, +local function world_new() + local entity_index = { + dense_array = {}, + sparse_array = {}, alive_count = 0, max_id = 0, - } + } :: ecs_entity_index_t local self = setmetatable({ archetype_index = {} :: { [string]: Archetype }, archetypes = {} :: Archetypes, @@ -2372,23 +2387,14 @@ function World.new() return self end -export type Entity = {__T: T} - -export type Id = - | Entity - | Pair, Entity> - | Pair> - | Pair, Entity> - -export type Pair = number & { - __P: P, - __O: O, -} - -type Item = (self: Query) -> (Entity, T...) - -type Iter = (query: Query) -> () -> (Entity, T...) +World.new = world_new +export type Entity = { __T: T } +export type Id = { __T: T } +export type Pair = Id

+type ecs_id_t = Id | Pair | Pair<"Tag", T> +export type Item = (self: Query) -> (Entity, T...) +export type Iter = (query: Query) -> () -> (Entity, T...) export type Query = typeof(setmetatable({}, { __iter = (nil :: any) :: Iter, @@ -2405,9 +2411,9 @@ export type Observer = { query: QueryInner, } -type Observable = { - [i53]: { - [i53]: { +export type Observable = { + [Id]: { + [Id]: { { Observer } } } @@ -2426,51 +2432,44 @@ export type World = { observable: any, --- Creates a new entity - entity: (self: World) -> Entity, + entity: (self: World, id: Entity?) -> Entity, --- Creates a new entity located in the first 256 ids. --- These should be used for static components for fast access. component: (self: World) -> Entity, --- Gets the target of an relationship. For example, when a user calls --- `world:target(id, ChildOf(parent), 0)`, you will obtain the parent entity. - target: (self: World, id: Entity, relation: Entity, index: number?) -> Entity?, + target: (self: World, id: Entity, relation: Id, index: number?) -> Entity?, --- Deletes an entity and all it's related components and relationships. - delete: (self: World, id: Entity) -> (), + delete: (self: World, id: Entity) -> (), --- Adds a component to the entity with no value - add: (self: World, id: Entity, component: Id) -> (), + add: (self: World, id: Entity, component: Id) -> (), --- Assigns a value to a component on the given entity - set: (self: World, id: Entity, component: Id, data: U) -> (), + set: (self: World, id: Entity, component: Id, data: T) -> (), cleanup: (self: World) -> (), -- Clears an entity from the world - clear: (self: World, id: Entity) -> (), + clear: (self: World, id: Entity) -> (), --- Removes a component from the given entity - remove: (self: World, id: Entity, component: Id) -> (), + remove: (self: World, id: Entity, component: Id) -> (), --- Retrieves the value of up to 4 components. These values may be nil. - get: ((self: World, id: Entity, Id) -> A?) - & ((self: World, id: Entity, Id, Id) -> (A?, B?)) - & ((self: World, id: Entity, Id, Id, Id) -> (A?, B?, C?)) - & (self: World, id: Entity, Id, Id, Id, Id) -> (A?, B?, C?, D?), + get: ((self: World, id: Entity, Id) -> A?) + & ((self: World, id: Entity, Id, Id) -> (A?, B?)) + & ((self: World, id: Entity, Id, Id, Id) -> (A?, B?, C?)) + & (self: World, id: Entity, Id, Id, Id, Id) -> (A?, B?, C?, D?), --- Returns whether the entity has the ID. - has: ((self: World, entity: Entity, ...Id) -> boolean) - & ((self: World, entity: Entity, Id, Id) -> boolean) - & ((self: World, entity: Entity, Id, Id, Id) -> boolean) - & ((self: World, entity: Entity, Id, Id, Id, Id) -> boolean) - & ((self: World, entity: Entity, Id, Id, Id, Id, Id) -> boolean) - & ((self: World, entity: Entity, Id, Id, Id, Id, Id, Id) -> boolean) - & ((self: World, entity: Entity, Id, Id, Id, Id, Id, Id, Id) -> boolean) - & ((self: World, entity: Entity, Id, Id, Id, Id, Id, Id, Id, ...unknown) -> boolean), + has: (self: World, entity: Entity, ...Id) -> boolean, --- Get parent (target of ChildOf relationship) for entity. If there is no ChildOf relationship pair, it will return nil. - parent: (self: World, entity: Entity) -> Entity, + parent:(self: World, entity: Entity) -> Entity, --- Checks if the world contains the given entity - contains: (self: World, entity: Entity) -> boolean, + contains:(self: World, entity: Entity) -> boolean, - each: (self: World, id: Id) -> () -> Entity, + each: (self: World, id: Id) -> () -> Entity, - children: (self: World, id: Id) -> () -> Entity, + children: (self: World, id: Id) -> () -> Entity, --- Searches the world for entities that match a given query query: ((World, Id) -> Query) @@ -2501,6 +2500,7 @@ export type World = { return { World = World :: { new: () -> World }, + world = World.new :: () -> World, OnAdd = EcsOnAdd :: Entity<(entity: Entity) -> ()>, OnRemove = EcsOnRemove :: Entity<(entity: Entity) -> ()>, @@ -2516,7 +2516,7 @@ return { Name = EcsName :: Entity, Rest = EcsRest :: Entity, - pair = ECS_PAIR :: (first: P, second: O) -> Pair, + pair = (ECS_PAIR :: any) :: (first: Id

, second: Id) -> Pair, -- Inwards facing API for testing ECS_ID = ECS_ENTITY_T_LO, @@ -2524,6 +2524,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, diff --git a/pesde-rbx.toml b/pesde-rbx.toml index f1b1745..62662ad 100644 --- a/pesde-rbx.toml +++ b/pesde-rbx.toml @@ -11,7 +11,7 @@ includes = [ license = "MIT" name = "marked/jecs" repository = "https://git.devmarked.win/marked/jecs-pesde" -version = "0.5.5" +version = "0.6.0-rc.1" [indices] default = "https://github.com/pesde-pkg/index" diff --git a/pesde.lock b/pesde.lock index a1e2363..30c36fb 100644 --- a/pesde.lock +++ b/pesde.lock @@ -2,5 +2,5 @@ # It is not intended for manual editing. format = 1 name = "marked/jecs" -version = "0.5.5" +version = "0.6.0-rc.1" target = "luau" diff --git a/pesde.toml b/pesde.toml index ccfcc7c..e533800 100644 --- a/pesde.toml +++ b/pesde.toml @@ -11,7 +11,7 @@ includes = [ license = "MIT" name = "marked/jecs" repository = "https://git.devmarked.win/marked/jecs-pesde" -version = "0.5.5" +version = "0.6.0-rc.1" [indices] default = "https://github.com/pesde-pkg/index"