From 32381c9cf6e07e4f05223d9c562088a3dd6ab6cb Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 3 May 2024 12:10:12 -0400 Subject: [PATCH] Update newMatter.lua --- newMatter.lua | 108 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 98 insertions(+), 10 deletions(-) diff --git a/newMatter.lua b/newMatter.lua index 150bbb8..6ae1027 100644 --- a/newMatter.lua +++ b/newMatter.lua @@ -383,6 +383,16 @@ local function assertValidComponentInstance(value, position) end end +--!optimize 2 +--!native +--!strict +local component = require(script.Parent.component) +local topoRuntime = require(script.Parent.topoRuntime) +local Component = require(script.Parent.component) + +local assertValidComponentInstance = Component.assertValidComponentInstance +local assertValidComponent = Component.assertValidComponent + local ERROR_NO_ENTITY = "Entity doesn't exist, use world:contains to check if needed" local ERROR_DUPLICATE_ENTITY = "The world already contains an entity with ID %d. Use World:replace instead if this is intentional." @@ -434,11 +444,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 +555,7 @@ function World.new() local self = setmetatable({ entityIndex = {}, componentIndex = {}, + componentIdToComponent = {}, archetypes = {}, archetypeIndex = {}, nextId = 0, @@ -623,7 +629,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 +803,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 +954,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 +1000,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 +1287,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 +1377,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 +1442,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)