mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
Creator specific iterators for returns
This commit is contained in:
parent
a6b592727e
commit
a5302a14fb
1 changed files with 76 additions and 26 deletions
66
lib/init.lua
66
lib/init.lua
|
@ -322,8 +322,6 @@ function World.archetypesWith(world: World, componentId: i53)
|
|||
return compatibleArchetypes
|
||||
end
|
||||
|
||||
|
||||
|
||||
function World.query(world: World, ...: i53): () -> (number, ...any)
|
||||
local compatibleArchetypes = {}
|
||||
local components = { ... }
|
||||
|
@ -332,6 +330,61 @@ function World.query(world: World, ...: i53): () -> (number, ...any)
|
|||
local a, b, c, d, e = ...
|
||||
local firstArchetypeMap = world.componentIndex[components[1]]
|
||||
|
||||
if queryLength == 1 then
|
||||
local function single()
|
||||
local id = next(firstArchetypeMap)
|
||||
local archetype = archetypes[id :: number]
|
||||
local lastRow
|
||||
|
||||
return function(): any
|
||||
local row, entity = next(archetype.entities, lastRow)
|
||||
while row == nil do
|
||||
id = next(firstArchetypeMap, id)
|
||||
if id == nil then
|
||||
return
|
||||
end
|
||||
archetype = archetypes[id]
|
||||
row = next(archetype.entities, row)
|
||||
end
|
||||
lastRow = row
|
||||
|
||||
return entity, archetype.columns[archetype.records[a]]
|
||||
end
|
||||
end
|
||||
|
||||
return single()
|
||||
elseif queryLength == 2 then
|
||||
for id in firstArchetypeMap do
|
||||
local archetype = archetypes[id]
|
||||
if archetype.records[b] then
|
||||
table.insert(compatibleArchetypes, archetype)
|
||||
end
|
||||
end
|
||||
|
||||
local function double(): any
|
||||
local lastArchetype, archetype = next(compatibleArchetypes)
|
||||
local lastRow
|
||||
|
||||
return function(): any
|
||||
local row = next(archetype.entities, lastRow)
|
||||
while row == nil do
|
||||
lastArchetype, archetype = next(compatibleArchetypes, lastArchetype)
|
||||
if lastArchetype == nil then
|
||||
return
|
||||
end
|
||||
row = next(archetype.entities, row)
|
||||
end
|
||||
lastRow = row
|
||||
|
||||
local entity = archetype.entities[row::number]
|
||||
local columns = archetype.columns
|
||||
local archetypeRecords = archetype.records
|
||||
return entity, columns[archetypeRecords[a]], columns[archetypeRecords[b]]
|
||||
end
|
||||
end
|
||||
return double()
|
||||
end
|
||||
|
||||
for id in firstArchetypeMap do
|
||||
local archetype = archetypes[id]
|
||||
local archetypeRecords = archetype.records
|
||||
|
@ -346,6 +399,7 @@ function World.query(world: World, ...: i53): () -> (number, ...any)
|
|||
table.insert(compatibleArchetypes, archetype)
|
||||
end
|
||||
end
|
||||
|
||||
local lastArchetype, archetype = next(compatibleArchetypes)
|
||||
|
||||
local lastRow
|
||||
|
@ -365,11 +419,7 @@ function World.query(world: World, ...: i53): () -> (number, ...any)
|
|||
local entityId = archetype.entities[row :: number]
|
||||
local archetypeRecords = archetype.records
|
||||
|
||||
if queryLength == 1 then
|
||||
return entityId, columns[archetypeRecords[a]]
|
||||
elseif queryLength == 2 then
|
||||
return entityId, columns[archetypeRecords[a]], columns[archetypeRecords[b]]
|
||||
elseif queryLength == 3 then
|
||||
if queryLength == 3 then
|
||||
return entityId,
|
||||
columns[archetypeRecords[a]],
|
||||
columns[archetypeRecords[b]],
|
||||
|
@ -398,9 +448,9 @@ function World.query(world: World, ...: i53): () -> (number, ...any)
|
|||
end
|
||||
|
||||
return function()
|
||||
-- consider this to be the iterator that gets invoked each iteration step
|
||||
return queryNext()
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
return {
|
||||
|
|
Loading…
Reference in a new issue