return an EmptyQuery when querying for components that are not tied to any entities (#12)

* Update init.lua

* export Query type

* replace EmptyQuery with static reference
This commit is contained in:
Shane 2024-04-30 18:45:49 -07:00 committed by GitHub
parent 1b1ae947d9
commit d0fb1a1b1d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -360,8 +360,9 @@ local EmptyQuery = {
without = noop without = noop
} }
EmptyQuery.__index = EmptyQuery EmptyQuery.__index = EmptyQuery
setmetatable(EmptyQuery, EmptyQuery)
export type Query = typeof(setmetatable({}, EmptyQuery)) export type Query = typeof(EmptyQuery)
function World.query(world: World, ...: i53): Query function World.query(world: World, ...: i53): Query
local compatibleArchetypes = {} local compatibleArchetypes = {}
@ -379,7 +380,7 @@ function World.query(world: World, ...: i53): Query
for i, componentId in components do for i, componentId in components do
local map = componentIndex[componentId] local map = componentIndex[componentId]
if not map then if not map then
return setmetatable({}, EmptyQuery) return EmptyQuery
end end
if firstArchetypeMap == nil or map.size < firstArchetypeMap.size then if firstArchetypeMap == nil or map.size < firstArchetypeMap.size then
@ -415,7 +416,7 @@ function World.query(world: World, ...: i53): Query
local lastArchetype, compatibleArchetype = next(compatibleArchetypes) local lastArchetype, compatibleArchetype = next(compatibleArchetypes)
if not lastArchetype then if not lastArchetype then
return setmetatable({}, EmptyQuery) return EmptyQuery
end end
local preparedQuery = {} local preparedQuery = {}
@ -439,7 +440,7 @@ function World.query(world: World, ...: i53): Query
lastArchetype, compatibleArchetype = next(compatibleArchetypes) lastArchetype, compatibleArchetype = next(compatibleArchetypes)
if not lastArchetype then if not lastArchetype then
return setmetatable({}, EmptyQuery) return EmptyQuery
end end
return self return self