From 0b9d9530b9f4837d7fe1ad9c0af69dea03e10ca2 Mon Sep 17 00:00:00 2001 From: Ukendio Date: Mon, 12 Aug 2024 22:43:46 +0200 Subject: [PATCH] Fix types for iter --- src/init.luau | 93 +++++++++++++++++++++++++++------------------------ 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/src/init.luau b/src/init.luau index fa7b95e0..135c0a7d 100644 --- a/src/init.luau +++ b/src/init.luau @@ -177,55 +177,56 @@ local function entity_index_new_id(entityIndex: EntityIndex, index: i24): i53 return id end -local function archetype_move(entityIndex: EntityIndex, to: Archetype, - destinationRow: i24, from: Archetype, sourceRow: i24) +local function archetype_move(entity_index: EntityIndex, to: Archetype, + dst_row: i24, from: Archetype, src_row: i24) - local columns = from.columns - local sourceEntities = from.entities - local destinationEntities = to.entities - local destinationColumns = to.columns - local records = to.records + local src_columns = from.columns + local dst_columns = to.columns + local dst_entities = to.entities + local src_entities = from.entities + + local last = #src_entities local types = from.types - local last = #sourceEntities + local records = to.records - for i, column in columns do + for i, column in src_columns do -- 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 tr = records[types[i]] -- Sometimes target column may not exist, e.g. when you remove a component. if tr then - destinationColumns[tr.column][destinationRow] = column[sourceRow] + dst_columns[tr.column][dst_row] = column[src_row] end -- If the entity is the last row in the archetype then swapping it would be meaningless. - if sourceRow ~= last then + if src_row ~= last then -- Swap rempves columns to ensure there are no holes in the archetype. - column[sourceRow] = column[last] + column[src_row] = column[last] end column[last] = nil end - local sparse = entityIndex.sparse - local movedAway = #sourceEntities + local sparse = entity_index.sparse + local moved = #src_entities -- Move the entity from the source to the destination archetype. -- Because we have swapped columns we now have to update the records -- corresponding to the entities' rows that were swapped. - local e1 = sourceEntities[sourceRow] - local e2 = sourceEntities[movedAway] + local e1 = src_entities[src_row] + local e2 = src_entities[moved] - if sourceRow ~= movedAway then - sourceEntities[sourceRow] = e2 + if src_row ~= moved then + src_entities[src_row] = e2 end - sourceEntities[movedAway] = nil :: any - destinationEntities[destinationRow] = e1 + src_entities[moved] = nil :: any + dst_entities[dst_row] = e1 local record1 = sparse[e1] local record2 = sparse[e2] - record1.row = destinationRow - record2.row = sourceRow + record1.row = dst_row + record2.row = src_row end local function archetype_append(entity: number, archetype: Archetype): number @@ -259,14 +260,14 @@ local function id_record_ensure( componentIndex: ComponentIndex, componentId: number ): ArchetypeMap - local archetypesMap = componentIndex[componentId] + local idr = componentIndex[componentId] - if not archetypesMap then - archetypesMap = ({ size = 0, cache = {} } :: any) :: ArchetypeMap - componentIndex[componentId] = archetypesMap + if not idr then + idr = ({ size = 0, cache = {} } :: any) :: ArchetypeMap + componentIndex[componentId] = idr end - return archetypesMap + return idr end local function ECS_ID_IS_WILDCARD(e: i53): boolean @@ -725,13 +726,13 @@ end type CompatibleArchetype = { archetype: Archetype, indices: { number } } -local noop: Item = function() - return nil :: any +local function noop() end -local Arm = function(self, ...) - return self +local function Arm(query, ...) + return query end + local world_query do local empty_list = {} @@ -1275,7 +1276,7 @@ type Item = () -> (number, ...any) export type Entity = number & {__DO_NOT_USE_OR_YOU_WILL_BE_FIRED: T } -type Iter = () -> () -> (Entity, T...) +type Iter = (query: Query) -> () -> (Entity, T...) type Query = typeof(setmetatable({}, { __iter = (nil :: any) :: Iter @@ -1298,18 +1299,21 @@ export type World = { nextEntityId: number, ROOT_ARCHETYPE: Archetype, } & { - target: (world: World, entity: Entity, relation: Entity) -> Entity, - parent: (world: World, entity: Entity) -> Entity, - entity: (world: World) -> Entity, - clear: (world: World, entity: Entity) -> (), - delete: (world: World, entity: Entity) -> (), - component: (world: World) -> Entity, - get: (world: World, entity: Entity, id: Entity) -> T, - has: (world: World, entity: Entity, id: Entity) -> boolean, - add: (world: World, entity: Entity, id: Entity) -> (), - set: (world: World, entity: Entity, + target: (World, entity: Entity, relation: Entity) -> Entity, + parent: (World, entity: Entity) -> Entity, + entity: (World) -> Entity, + clear: (World, entity: Entity) -> (), + delete: (World, entity: Entity) -> (), + component: (World) -> Entity, + get: ((World, entity: Entity, id: Entity) -> T) + & ((World, id: Entity, Entity, Entity) -> (A, B)) + & ((World, id: Entity, Entity, Entity, Entity) -> (A, B, C)) + & (World, id: Entity, Entity, Entity, Entity, Entity) -> (A, B, C, D), + has: (World, entity: Entity, ...Entity) -> boolean, + add: (World, entity: Entity, id: Entity) -> (), + set: (World, entity: Entity, id: Entity, data: T) -> (), - remove: (world: World, entity: Entity, id: Entity) -> (), + remove: (World, entity: Entity, id: Entity) -> (), query: ((World, Entity) -> Query) & ((World, Entity, Entity) -> Query) @@ -1327,7 +1331,7 @@ export type World = { } return { - World = World , + World = World :: { new: () -> World }, OnAdd = EcsOnAdd :: Entity, OnRemove = EcsOnRemove :: Entity, @@ -1344,6 +1348,7 @@ return { ECS_ID = ECS_ENTITY_T_LO, ECS_GENERATION_INC = ECS_GENERATION_INC, ECS_GENERATION = ECS_GENERATION, + ECS_ID_IS_WILDCARD = ECS_ID_IS_WILDCARD, IS_PAIR = ECS_IS_PAIR, pair_first = ecs_pair_first,