mirror of
				https://github.com/Ukendio/jecs.git
				synced 2025-11-03 18:39:19 +00:00 
			
		
		
		
	Add queryNext
This commit is contained in:
		
							parent
							
								
									ea12df96a4
								
							
						
					
					
						commit
						5bd43bddd4
					
				
					 1 changed files with 85 additions and 72 deletions
				
			
		
							
								
								
									
										41
									
								
								lib/init.lua
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								lib/init.lua
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -652,14 +652,15 @@ setmetatable(EmptyQuery, EmptyQuery)
 | 
			
		|||
 | 
			
		||||
export type Query = typeof(EmptyQuery)
 | 
			
		||||
 | 
			
		||||
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
 | 
			
		||||
 | 
			
		||||
function World.query(world: World, ...): Query
 | 
			
		||||
	-- breaking?
 | 
			
		||||
	if (...) == nil then
 | 
			
		||||
		error("Missing components")
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	type CompatibleArchetype = { archetype: Archetype, indices: { number } }
 | 
			
		||||
	local compatibleArchetypes = {} :: { CompatibleArchetype }
 | 
			
		||||
	local compatibleArchetypes: { CompatibleArchetype } = {} 
 | 
			
		||||
	local length = 0
 | 
			
		||||
 | 
			
		||||
	local components = { ... }
 | 
			
		||||
| 
						 | 
				
			
			@ -708,33 +709,38 @@ function World.query(world: World, ...): Query
 | 
			
		|||
		}
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	local lastArchetype, compatibleArchetype: CompatibleArchetype = next(compatibleArchetypes :: any)
 | 
			
		||||
	local lastArchetype = 1
 | 
			
		||||
 	local compatibleArchetype: CompatibleArchetype = compatibleArchetypes[lastArchetype]
 | 
			
		||||
 | 
			
		||||
	if not lastArchetype then
 | 
			
		||||
	if not compatibleArchetype then
 | 
			
		||||
		return EmptyQuery
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	local preparedQuery = {}
 | 
			
		||||
	preparedQuery.__index = preparedQuery
 | 
			
		||||
	
 | 
			
		||||
	local lastRow
 | 
			
		||||
	local queryOutput = {}
 | 
			
		||||
 | 
			
		||||
	function preparedQuery:__iter()
 | 
			
		||||
		return function() 
 | 
			
		||||
	local i = 1
 | 
			
		||||
 | 
			
		||||
	local function queryNext() 
 | 
			
		||||
		local archetype = compatibleArchetype.archetype
 | 
			
		||||
			local row: number = next(archetype.entities, lastRow)
 | 
			
		||||
			while row == nil do
 | 
			
		||||
				lastArchetype, compatibleArchetype = next(compatibleArchetypes, lastArchetype)
 | 
			
		||||
				if lastArchetype == nil then
 | 
			
		||||
		local entityId = archetype.entities[i]
 | 
			
		||||
 | 
			
		||||
		while entityId == nil do 
 | 
			
		||||
			lastArchetype += 1
 | 
			
		||||
			if lastArchetype > #compatibleArchetypes then 
 | 
			
		||||
				return
 | 
			
		||||
			end
 | 
			
		||||
			compatibleArchetype = compatibleArchetypes[lastArchetype]
 | 
			
		||||
			archetype = compatibleArchetype.archetype
 | 
			
		||||
				row = next(archetype.entities, row)
 | 
			
		||||
			i = 1
 | 
			
		||||
			entityId = archetype.entities[i]
 | 
			
		||||
		end
 | 
			
		||||
			lastRow = row
 | 
			
		||||
 | 
			
		||||
			local entityId = archetype.entities[row]
 | 
			
		||||
		local row = i
 | 
			
		||||
		i+=1
 | 
			
		||||
 | 
			
		||||
		local columns = archetype.columns
 | 
			
		||||
		local tr = compatibleArchetype.indices
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -788,6 +794,13 @@ function World.query(world: World, ...): Query
 | 
			
		|||
 | 
			
		||||
		return entityId, unpack(queryOutput :: any, 1, queryLength)
 | 
			
		||||
	end
 | 
			
		||||
	
 | 
			
		||||
	function preparedQuery:__iter()
 | 
			
		||||
		return queryNext
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	function preparedQuery:next() 
 | 
			
		||||
		return queryNext()	
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	function preparedQuery:without(...)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue