diff --git a/newMatter.lua b/newMatter.lua index 150bbb8..4a50791 100644 --- a/newMatter.lua +++ b/newMatter.lua @@ -434,11 +434,6 @@ local function transitionArchetype( from: Archetype, sourceRow: i24 ) - -- local columns = sourceArchetype.columns - -- local sourceEntities = sourceArchetype.entities - -- local destinationEntities = destinationArchetype.entities - -- local destinationColumns = destinationArchetype.columns - local columns = from.columns local sourceEntities = from.entities local destinationEntities = to.entities @@ -550,6 +545,7 @@ function World.new() local self = setmetatable({ entityIndex = {}, componentIndex = {}, + componentIdToComponent = {}, archetypes = {}, archetypeIndex = {}, nextId = 0, @@ -623,7 +619,12 @@ local function archetypeTraverseAdd(world: World, componentId: i53, archetype: A end local function componentAdd(world: World, entityId: i53, componentInstance) - local componentId = #getmetatable(componentInstance) + local component = getmetatable(componentInstance) + local componentId = #component + + -- TODO: + -- This never gets cleaned up + world.componentIdToComponent[componentId] = component local record = world:ensureRecord(entityId) local sourceArchetype = record.archetype @@ -792,7 +793,29 @@ function World.entity(world: World) end function World:__iter() - return error("NOT IMPLEMENTED YET") + local previous = nil + return function() + local entityId, data = next(self.entityIndex, previous) + previous = entityId + + if entityId == nil then + return nil + end + + local archetype = data.archetype + if not archetype then + return entityId, {} + end + + local columns = archetype.columns + local components = {} + for i, map in columns do + local componentId = archetype.types[i] + components[self.componentIdToComponent[componentId]] = map[data.row] + end + + return entityId, components + end end function World._trackChanged(world: World, metatable, id, old, new) @@ -921,7 +944,7 @@ local emptyQueryResult = setmetatable({ }) local function queryResult(compatibleArchetypes, components: { number }, queryLength, ...): any - local a: any, b: any, c: any, d: any, e: any = ... + local a: any, b: any, c: any, d: any, e: any, f: any, g: any, h: any = ... local lastArchetype, archetype = next(compatibleArchetypes) if not lastArchetype then return emptyQueryResult @@ -967,6 +990,31 @@ local function queryResult(compatibleArchetypes, components: { number }, queryLe columns[archetypeRecords[c]][row], columns[archetypeRecords[d]][row], columns[archetypeRecords[e]][row] + elseif queryLength == 6 then + return entityId, + columns[archetypeRecords[a]][row], + columns[archetypeRecords[b]][row], + columns[archetypeRecords[c]][row], + columns[archetypeRecords[d]][row], + columns[archetypeRecords[e]][row], + columns[archetypeRecords[f]][row] + elseif queryLength == 7 then + return columns[archetypeRecords[a]][row], + columns[archetypeRecords[b]][row], + columns[archetypeRecords[c]][row], + columns[archetypeRecords[d]][row], + columns[archetypeRecords[e]][row], + columns[archetypeRecords[f]][row], + columns[archetypeRecords[g]][row] + elseif queryLength == 8 then + return columns[archetypeRecords[a]][row], + columns[archetypeRecords[b]][row], + columns[archetypeRecords[c]][row], + columns[archetypeRecords[d]][row], + columns[archetypeRecords[e]][row], + columns[archetypeRecords[f]][row], + columns[archetypeRecords[g]][row], + columns[archetypeRecords[h]][row] end for i, componentId in components do @@ -1229,7 +1277,7 @@ function World.query(world: World, ...: Component): any local components = { ... } local archetypes = world.archetypes local queryLength = select("#", ...) - local a: any, b: any, c: any, d: any, e: any = ... + local a: any, b: any, c: any, d: any, e: any, f: any, g: any, h: any = ... if queryLength == 0 then return emptyQueryResult @@ -1319,6 +1367,36 @@ function World.query(world: World, ...: Component): any e = #e components = { a, b, c, d, e } + elseif queryLength == 6 then + a = #a + b = #b + c = #c + d = #d + e = #e + f = #f + + components = { a, b, c, d, e, f } + elseif queryLength == 7 then + a = #a + b = #b + c = #c + d = #d + e = #e + f = #f + g = #g + + components = { a, b, c, d, e, f, g } + elseif queryLength == 8 then + a = #a + b = #b + c = #c + d = #d + e = #e + f = #f + g = #g + h = #h + + components = { a, b, c, d, e, f, g, h } else for i, component in components do components[i] = (#component) :: any @@ -1354,7 +1432,7 @@ function World.query(world: World, ...: Component): any end end - return queryResult(compatibleArchetypes, components :: any, queryLength, a, b, c, d, e) + return queryResult(compatibleArchetypes, components :: any, queryLength, a, b, c, d, e, f, g, h) end local function cleanupQueryChanged(hookState)