mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-25 01:20:04 +00:00
Fix upvalues conflict in nested queries
This commit is contained in:
parent
6b465d1ed1
commit
3e639db371
1 changed files with 282 additions and 280 deletions
270
src/init.luau
270
src/init.luau
|
@ -714,17 +714,14 @@ export type Query = typeof({
|
|||
|
||||
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
|
||||
|
||||
local world_query: (World, ...i53) -> Query
|
||||
do
|
||||
|
||||
local noop: Item = function()
|
||||
local noop: Item = function()
|
||||
return nil :: any
|
||||
end
|
||||
end
|
||||
|
||||
local Arm = function(self: Query, ...)
|
||||
local Arm = function(self: Query, ...)
|
||||
return self
|
||||
end
|
||||
local EmptyQuery: Query = {
|
||||
end
|
||||
local EmptyQuery: Query = {
|
||||
__iter = function(): Item
|
||||
return noop
|
||||
end,
|
||||
|
@ -733,94 +730,75 @@ do
|
|||
replace = noop :: (Query, ...any) -> (),
|
||||
with = Arm,
|
||||
without = Arm,
|
||||
}
|
||||
}
|
||||
|
||||
setmetatable(EmptyQuery, EmptyQuery)
|
||||
setmetatable(EmptyQuery, EmptyQuery)
|
||||
|
||||
local lastArchetype: number
|
||||
local archetype: Archetype
|
||||
local queryOutput: { any }
|
||||
local entities: { number }
|
||||
local i: number
|
||||
local function world_query(world, ...)
|
||||
-- breaking
|
||||
if (...) == nil then
|
||||
error("Missing components")
|
||||
end
|
||||
|
||||
local compatible_archetypes: { Archetype }
|
||||
local ids: { number }
|
||||
local columns
|
||||
local compatible_archetypes = {}
|
||||
local length = 0
|
||||
|
||||
local A, B, C, D, E, F, G, H, I
|
||||
local components = { ... } :: any
|
||||
local A, B, C, D, E, F, G, H, I = ...
|
||||
local a, b, c, d, e, f, g, h
|
||||
|
||||
local init
|
||||
local drain
|
||||
local archetypes = world.archetypes
|
||||
|
||||
local function query_init(query)
|
||||
if init and drain then
|
||||
return
|
||||
local firstArchetypeMap: ArchetypeMap
|
||||
local componentIndex = world.componentIndex
|
||||
|
||||
for _, componentId in components do
|
||||
local map = componentIndex[componentId]
|
||||
if not map then
|
||||
return EmptyQuery
|
||||
end
|
||||
|
||||
init = true
|
||||
lastArchetype = 1
|
||||
archetype = compatible_archetypes[lastArchetype]
|
||||
|
||||
if not archetype then
|
||||
return
|
||||
end
|
||||
|
||||
queryOutput = {}
|
||||
|
||||
entities = archetype.entities
|
||||
i = #entities
|
||||
columns = archetype.columns
|
||||
|
||||
local records = archetype.records
|
||||
if not B then
|
||||
a = columns[records[A].column]
|
||||
elseif not C then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
elseif not D then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
elseif not E then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
d = columns[records[D].column]
|
||||
elseif not F then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
d = columns[records[D].column]
|
||||
e = columns[records[E].column]
|
||||
elseif not G then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
d = columns[records[D].column]
|
||||
e = columns[records[E].column]
|
||||
f = columns[records[F].column]
|
||||
elseif not H then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
d = columns[records[D].column]
|
||||
e = columns[records[E].column]
|
||||
f = columns[records[F].column]
|
||||
g = columns[records[G].column]
|
||||
elseif H then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[D].column]
|
||||
e = columns[records[E].column]
|
||||
f = columns[records[F].column]
|
||||
g = columns[records[G].column]
|
||||
h = columns[records[H].column]
|
||||
if firstArchetypeMap == nil or map.size < firstArchetypeMap.size then
|
||||
firstArchetypeMap = map
|
||||
end
|
||||
end
|
||||
|
||||
for id in firstArchetypeMap.cache do
|
||||
local compatibleArchetype = archetypes[id]
|
||||
local archetypeRecords = compatibleArchetype.records
|
||||
|
||||
local skip = false
|
||||
|
||||
for i, componentId in components do
|
||||
local index = archetypeRecords[componentId]
|
||||
if not index then
|
||||
skip = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if skip then
|
||||
continue
|
||||
end
|
||||
|
||||
length += 1
|
||||
compatible_archetypes[length] = compatibleArchetype
|
||||
end
|
||||
|
||||
|
||||
local init = false
|
||||
local drain = false
|
||||
local ids = components
|
||||
|
||||
local world_query_iter_next
|
||||
|
||||
local lastArchetype = 1
|
||||
local archetype
|
||||
local columns
|
||||
local entities
|
||||
local i
|
||||
local queryOutput
|
||||
|
||||
local function world_query_iter_create()
|
||||
if not B then
|
||||
function world_query_iter_next(): any
|
||||
|
@ -1007,6 +985,75 @@ do
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
local function query_init(query)
|
||||
if init and drain then
|
||||
return
|
||||
end
|
||||
|
||||
init = true
|
||||
lastArchetype = 1
|
||||
archetype = compatible_archetypes[lastArchetype]
|
||||
|
||||
if not archetype then
|
||||
return false
|
||||
end
|
||||
|
||||
queryOutput = {}
|
||||
|
||||
entities = archetype.entities
|
||||
i = #entities
|
||||
columns = archetype.columns
|
||||
|
||||
local records = archetype.records
|
||||
if not B then
|
||||
a = columns[records[A].column]
|
||||
elseif not C then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
elseif not D then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
elseif not E then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
d = columns[records[D].column]
|
||||
elseif not F then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
d = columns[records[D].column]
|
||||
e = columns[records[E].column]
|
||||
elseif not G then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
d = columns[records[D].column]
|
||||
e = columns[records[E].column]
|
||||
f = columns[records[F].column]
|
||||
elseif not H then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
d = columns[records[D].column]
|
||||
e = columns[records[E].column]
|
||||
f = columns[records[F].column]
|
||||
g = columns[records[G].column]
|
||||
elseif not I then
|
||||
a = columns[records[A].column]
|
||||
b = columns[records[B].column]
|
||||
c = columns[records[C].column]
|
||||
d = columns[records[D].column]
|
||||
e = columns[records[E].column]
|
||||
f = columns[records[F].column]
|
||||
g = columns[records[G].column]
|
||||
h = columns[records[H].column]
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function world_query_without(self, ...)
|
||||
local withoutComponents = { ... }
|
||||
for i = #compatible_archetypes, 1, -1 do
|
||||
|
@ -1119,9 +1166,11 @@ do
|
|||
|
||||
local function world_query_drain(query)
|
||||
drain = true
|
||||
query_init(query)
|
||||
if query_init(query) then
|
||||
return query
|
||||
end
|
||||
return EmptyQuery
|
||||
end
|
||||
|
||||
local function world_query_iter(query)
|
||||
query_init(query)
|
||||
|
@ -1135,6 +1184,10 @@ do
|
|||
return world_query_iter_next()
|
||||
end
|
||||
|
||||
if #compatible_archetypes == 0 then
|
||||
return EmptyQuery
|
||||
end
|
||||
|
||||
local it = {
|
||||
__iter = world_query_iter,
|
||||
drain = world_query_drain,
|
||||
|
@ -1147,56 +1200,6 @@ do
|
|||
|
||||
setmetatable(it, it)
|
||||
|
||||
function world_query(world: World, ...: any): Query
|
||||
-- breaking
|
||||
if (...) == nil then
|
||||
error("Missing components")
|
||||
end
|
||||
|
||||
compatible_archetypes = {}
|
||||
local length = 0
|
||||
|
||||
local components = { ... } :: any
|
||||
A, B, C, D, E, F, G, H, I = ...
|
||||
|
||||
local archetypes = world.archetypes
|
||||
|
||||
local firstArchetypeMap: ArchetypeMap
|
||||
local componentIndex = world.componentIndex
|
||||
|
||||
for _, componentId in components do
|
||||
local map = componentIndex[componentId]
|
||||
if not map then
|
||||
return EmptyQuery
|
||||
end
|
||||
|
||||
if firstArchetypeMap == nil or map.size < firstArchetypeMap.size then
|
||||
firstArchetypeMap = map
|
||||
end
|
||||
end
|
||||
|
||||
for id in firstArchetypeMap.cache do
|
||||
local compatibleArchetype = archetypes[id]
|
||||
local archetypeRecords = compatibleArchetype.records
|
||||
|
||||
local skip = false
|
||||
|
||||
for i, componentId in components do
|
||||
local index = archetypeRecords[componentId]
|
||||
if not index then
|
||||
skip = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if skip then
|
||||
continue
|
||||
end
|
||||
|
||||
length += 1
|
||||
compatible_archetypes[length] = compatibleArchetype
|
||||
end
|
||||
|
||||
drain = false
|
||||
init = false
|
||||
ids = components
|
||||
|
@ -1204,7 +1207,6 @@ do
|
|||
world_query_iter_create()
|
||||
|
||||
return it
|
||||
end
|
||||
end
|
||||
|
||||
type WorldIterator = (() -> (i53, { [unknown]: unknown? })) & (() -> ()) & (() -> i53)
|
||||
|
|
Loading…
Reference in a new issue