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