mirror of
				https://github.com/Ukendio/jecs.git
				synced 2025-10-31 17:20:32 +00:00 
			
		
		
		
	Prepared query
This commit is contained in:
		
							parent
							
								
									b75dc91a6a
								
							
						
					
					
						commit
						52cf531764
					
				
					 1 changed files with 50 additions and 36 deletions
				
			
		
							
								
								
									
										86
									
								
								lib/init.lua
									
									
									
									
									
								
							
							
						
						
									
										86
									
								
								lib/init.lua
									
									
									
									
									
								
							|  | @ -361,44 +361,14 @@ local function getSmallestMap(componentIndex, components) | ||||||
| 	return s.sparse | 	return s.sparse | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
| function World.query(world: World, ...: i53): (() -> (number, ...any)) | () -> () | local PreparedQuery = {} | ||||||
| 	 | PreparedQuery.__index = PreparedQuery | ||||||
| 	local compatibleArchetypes = {} |  | ||||||
| 	local components = { ... } |  | ||||||
| 	local archetypes = world.archetypes |  | ||||||
| 	local queryLength = #components |  | ||||||
| 	local firstArchetypeMap = getSmallestMap(world.componentIndex, components) |  | ||||||
| 
 | 
 | ||||||
| 	if not firstArchetypeMap then  | function PreparedQuery:__iter()  | ||||||
| 		return noop() | 	local compatibleArchetypes = self.compatibleArchetypes | ||||||
| 	end | 	local queryLength = self.queryLength | ||||||
|  | 	local components = self.components | ||||||
| 
 | 
 | ||||||
| 	for id in firstArchetypeMap do |  | ||||||
| 		local archetype = archetypes[id] |  | ||||||
|         local columns = archetype.columns |  | ||||||
| 		local archetypeRecords = archetype.records |  | ||||||
|         local indices = {} |  | ||||||
| 		local skip = false |  | ||||||
| 		 |  | ||||||
| 		for i, componentId in components do  |  | ||||||
| 			local index = archetypeRecords[componentId] |  | ||||||
| 			if not index then  |  | ||||||
| 				skip = true |  | ||||||
| 				break |  | ||||||
| 			end |  | ||||||
| 			indices[i] = columns[index] |  | ||||||
| 		end |  | ||||||
| 
 |  | ||||||
| 		if skip then  |  | ||||||
| 			continue |  | ||||||
| 		end |  | ||||||
| 
 |  | ||||||
| 		table.insert(compatibleArchetypes, { |  | ||||||
| 			archetype = archetype, |  | ||||||
| 			indices = indices |  | ||||||
| 		}) |  | ||||||
| 	end |  | ||||||
| 	 |  | ||||||
| 	local lastArchetype, compatibleArchetype = next(compatibleArchetypes) | 	local lastArchetype, compatibleArchetype = next(compatibleArchetypes) | ||||||
| 	if not compatibleArchetype then  | 	if not compatibleArchetype then  | ||||||
| 		return noop() | 		return noop() | ||||||
|  | @ -482,6 +452,50 @@ function World.query(world: World, ...: i53): (() -> (number, ...any)) | () -> ( | ||||||
| 	end | 	end | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
|  | function World.query(world: World, ...: i53) | ||||||
|  | 	local compatibleArchetypes = {} | ||||||
|  | 	local components = { ... } | ||||||
|  | 	local archetypes = world.archetypes | ||||||
|  | 	local queryLength = #components | ||||||
|  | 	local firstArchetypeMap = getSmallestMap(world.componentIndex, components) | ||||||
|  | 
 | ||||||
|  | 	if not firstArchetypeMap then  | ||||||
|  | 		return noop() | ||||||
|  | 	end | ||||||
|  | 
 | ||||||
|  | 	for id in firstArchetypeMap do | ||||||
|  | 		local archetype = archetypes[id] | ||||||
|  |         local columns = archetype.columns | ||||||
|  | 		local archetypeRecords = archetype.records | ||||||
|  |         local indices = {} | ||||||
|  | 		local skip = false | ||||||
|  | 		 | ||||||
|  | 		for i, componentId in components do  | ||||||
|  | 			local index = archetypeRecords[componentId] | ||||||
|  | 			if not index then  | ||||||
|  | 				skip = true | ||||||
|  | 				break | ||||||
|  | 			end | ||||||
|  | 			indices[i] = columns[index] | ||||||
|  | 		end | ||||||
|  | 
 | ||||||
|  | 		if skip then  | ||||||
|  | 			continue | ||||||
|  | 		end | ||||||
|  | 
 | ||||||
|  | 		table.insert(compatibleArchetypes, { | ||||||
|  | 			archetype = archetype, | ||||||
|  | 			indices = indices | ||||||
|  | 		}) | ||||||
|  | 	end | ||||||
|  | 	 | ||||||
|  | 	return setmetatable({ | ||||||
|  | 		queryLength = queryLength, | ||||||
|  | 		compatibleArchetypes = compatibleArchetypes, | ||||||
|  | 		components = components  | ||||||
|  | 	}, PreparedQuery) | ||||||
|  | end | ||||||
|  | 
 | ||||||
| function World.component(world: World)  | function World.component(world: World)  | ||||||
| 	local id = world.nextId + 1 | 	local id = world.nextId + 1 | ||||||
| 	if id > HI_COMPONENT_ID then  | 	if id > HI_COMPONENT_ID then  | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue