mirror of
				https://github.com/Ukendio/jecs.git
				synced 2025-11-04 10:59:18 +00:00 
			
		
		
		
	Merge branch 'Ukendio:main' into main
This commit is contained in:
		
						commit
						6a907db5e1
					
				
					 2 changed files with 64 additions and 57 deletions
				
			
		| 
						 | 
				
			
			@ -7,7 +7,7 @@ local Matter = require(ReplicatedStorage.DevPackages["_Index"]["matter-ecs_matte
 | 
			
		|||
local ecr = require(ReplicatedStorage.DevPackages["_Index"]["centau_ecr@0.8.0"].ecr)
 | 
			
		||||
local newWorld = Matter.World.new()
 | 
			
		||||
 | 
			
		||||
local jecs = require(ReplicatedStorage.Shim)
 | 
			
		||||
local jecs = require(ReplicatedStorage.Lib)
 | 
			
		||||
local mirror = require(ReplicatedStorage.mirror)
 | 
			
		||||
local mcs = mirror.World.new()
 | 
			
		||||
local ecs = jecs.World.new()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										115
									
								
								lib/init.lua
									
									
									
									
									
								
							
							
						
						
									
										115
									
								
								lib/init.lua
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -716,61 +716,11 @@ 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
 | 
			
		||||
local function PreparedQuery(
 | 
			
		||||
	compatibleArchetypes: { CompatibleArchetype } , components: { i53 }) 
 | 
			
		||||
 | 
			
		||||
	local compatibleArchetypes: { CompatibleArchetype } = {} 
 | 
			
		||||
	local length = 0
 | 
			
		||||
 | 
			
		||||
	local components = { ... }
 | 
			
		||||
	local archetypes = world.archetypes
 | 
			
		||||
	local queryLength = #components
 | 
			
		||||
	
 | 
			
		||||
	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 archetype = archetypes[id]
 | 
			
		||||
		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
 | 
			
		||||
			-- index should be index.offset
 | 
			
		||||
			indices[i] = index
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		if skip then
 | 
			
		||||
			continue
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		length += 1
 | 
			
		||||
		compatibleArchetypes[length] = {
 | 
			
		||||
			archetype = archetype,
 | 
			
		||||
			indices = indices,
 | 
			
		||||
		}
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	local lastArchetype = 1
 | 
			
		||||
	local compatibleArchetype: CompatibleArchetype = compatibleArchetypes[lastArchetype]
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -854,7 +804,7 @@ function World.query(world: World, ...): Query
 | 
			
		|||
			queryOutput[i] = columns[tr[i]][row]
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		return entityId, unpack(queryOutput :: any, 1, queryLength)
 | 
			
		||||
		return entityId, unpack(queryOutput, 1, queryLength)
 | 
			
		||||
	end
 | 
			
		||||
	
 | 
			
		||||
	function preparedQuery:__iter()
 | 
			
		||||
| 
						 | 
				
			
			@ -884,7 +834,7 @@ function World.query(world: World, ...): Query
 | 
			
		|||
			end
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		lastArchetype, compatibleArchetype = next(compatibleArchetypes :: any)
 | 
			
		||||
		lastArchetype, compatibleArchetype = next(compatibleArchetypes)
 | 
			
		||||
		if not lastArchetype then
 | 
			
		||||
			return EmptyQuery
 | 
			
		||||
		end
 | 
			
		||||
| 
						 | 
				
			
			@ -895,6 +845,63 @@ function World.query(world: World, ...): Query
 | 
			
		|||
	return setmetatable({}, preparedQuery) :: any
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function World.query(world: World, ...): Query
 | 
			
		||||
	-- breaking?
 | 
			
		||||
	if (...) == nil then
 | 
			
		||||
		error("Missing components")
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	local compatibleArchetypes: { CompatibleArchetype } = {} 
 | 
			
		||||
	local length = 0
 | 
			
		||||
 | 
			
		||||
	local components = { ... } :: any
 | 
			
		||||
	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 archetype = archetypes[id]
 | 
			
		||||
		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
 | 
			
		||||
			-- index should be index.offset
 | 
			
		||||
			indices[i] = index
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		if skip then
 | 
			
		||||
			continue
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		length += 1
 | 
			
		||||
		compatibleArchetypes[length] = {
 | 
			
		||||
			archetype = archetype,
 | 
			
		||||
			indices = indices,
 | 
			
		||||
		}
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	return PreparedQuery(compatibleArchetypes, components)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function World.__iter(world: World): () -> any 
 | 
			
		||||
	local dense = world.entityIndex.dense
 | 
			
		||||
	local sparse = world.entityIndex.sparse
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue