This commit is contained in:
Ukendio 2024-07-15 20:31:51 +02:00
parent 459e670ce9
commit 02548f2ac0

View file

@ -649,8 +649,8 @@ do
return nil return nil
end end
local records = archetype.records local tr = archetype.records
local columns = archetype.records local columns = archetype.columns
local row = record.row local row = record.row
local va = fetch(a, tr, columns, row) local va = fetch(a, tr, columns, row)
@ -855,88 +855,80 @@ do
end end
end end
local cache
function world_query(world: World, ...: number): Query function world_query(world: World, ...: number): Query
-- breaking? -- breaking?
if (...) == nil then if (...) == nil then
error("Missing components") error("Missing components")
end end
indices = {}
indices = {}
length = 0
components = { ... }
if cache then
compatibleArchetypes = cache
else
compatibleArchetypes = {} compatibleArchetypes = {}
local archetypes: { Archetype } = world.archetypes :: any length = 0
local firstArchetypeMap: ArchetypeMap components = { ... }
local componentIndex = world.componentIndex
for _, componentId in components do local archetypes: { Archetype } = world.archetypes :: any
local map: ArchetypeMap = componentIndex[componentId] :: any local firstArchetypeMap: ArchetypeMap
if not map then local componentIndex = world.componentIndex
return EmptyQuery
end
if (firstArchetypeMap :: any) == nil or firstArchetypeMap.size > map.size then for _, componentId in components do
firstArchetypeMap = map local map: ArchetypeMap = componentIndex[componentId] :: any
end if not map then
return EmptyQuery
end
if (firstArchetypeMap :: any) == nil or firstArchetypeMap.size > map.size then
firstArchetypeMap = map
end
end
for id in firstArchetypeMap.cache do
local compatibleArchetype = archetypes[id]
local archetypeRecords = compatibleArchetype.records
local records: { number } = {}
local skip = false
for i, componentId in components do
local index = archetypeRecords[componentId]
if not index then
skip = true
break
end
-- index should be index.offset
records[i] = index
end
if skip then
continue
end
length += 1
compatibleArchetypes[length] = compatibleArchetype
indices[length] = records
end
lastArchetype = 1
archetype = compatibleArchetypes[lastArchetype]
if not archetype then
return EmptyQuery
end end
for id in firstArchetypeMap.cache do queryOutput = {}
local compatibleArchetype = archetypes[id] queryLength = #components
local archetypeRecords = compatibleArchetype.records
local records: { number } = {} entities = archetype.entities
local skip = false i = #entities
for i, componentId in components do local it = {
local index = archetypeRecords[componentId] __iter = world_query_iter,
if not index then next = world_query_next,
skip = true without = world_query_without,
break replace = world_query_replace
end }
-- index should be index.offset
records[i] = index
end
if skip then return setmetatable(it, it) :: any
continue
end
length += 1
compatibleArchetypes[length] = compatibleArchetype
indices[length] = records
end
cache = compatibleArchetypes
end end
lastArchetype = 1
archetype = compatibleArchetypes[lastArchetype]
if not archetype then
return EmptyQuery
end
queryOutput = {}
queryLength = #components
entities = archetype.entities
i = #entities
local it = {
__iter = world_query_iter,
next = world_query_next,
without = world_query_without,
replace = world_query_replace
}
return setmetatable(it, it) :: any
end
end end
type WorldIterator = (() -> (i53, { [unknown]: unknown? })) & (() -> ()) & (() -> i53) type WorldIterator = (() -> (i53, { [unknown]: unknown? })) & (() -> ()) & (() -> i53)