mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-24 17:10:03 +00:00
Add without (#3)
This commit is contained in:
parent
b75dc91a6a
commit
13c703211d
2 changed files with 106 additions and 81 deletions
|
@ -196,21 +196,21 @@ return {
|
||||||
Functions = {
|
Functions = {
|
||||||
Mater = function()
|
Mater = function()
|
||||||
local matched = 0
|
local matched = 0
|
||||||
for entityId, firstComponent in newWorld:query(A2, A4, A6, A8) do
|
for entityId, firstComponent in newWorld:query(A1, A4, A6, A8) do
|
||||||
matched += 1
|
matched += 1
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
ECR = function()
|
ECR = function()
|
||||||
local matched = 0
|
local matched = 0
|
||||||
for entityId, firstComponent in registry2:view(B2, B4, B6, B8) do
|
for entityId, firstComponent in registry2:view(B1, B4, B6, B8) do
|
||||||
matched += 1
|
matched += 1
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
Jecs = function()
|
Jecs = function()
|
||||||
local matched = 0
|
local matched = 0
|
||||||
for entityId, firstComponent in ecs:query(D2, D4, D6, D8) do
|
for entityId, firstComponent in ecs:query(D1, D4, D6, D8) do
|
||||||
matched += 1
|
matched += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
181
lib/init.lua
181
lib/init.lua
|
@ -361,8 +361,7 @@ local function getSmallestMap(componentIndex, components)
|
||||||
return s.sparse
|
return s.sparse
|
||||||
end
|
end
|
||||||
|
|
||||||
function World.query(world: World, ...: i53): (() -> (number, ...any)) | () -> ()
|
function World.query(world: World, ...: i53): any
|
||||||
|
|
||||||
local compatibleArchetypes = {}
|
local compatibleArchetypes = {}
|
||||||
local components = { ... }
|
local components = { ... }
|
||||||
local archetypes = world.archetypes
|
local archetypes = world.archetypes
|
||||||
|
@ -398,88 +397,114 @@ function World.query(world: World, ...: i53): (() -> (number, ...any)) | () -> (
|
||||||
indices = indices
|
indices = indices
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
local lastArchetype, compatibleArchetype = next(compatibleArchetypes)
|
local lastArchetype, compatibleArchetype = next(compatibleArchetypes)
|
||||||
if not compatibleArchetype then
|
if not lastArchetype then
|
||||||
return noop()
|
return noop()
|
||||||
end
|
end
|
||||||
|
|
||||||
local lastRow
|
local preparedQuery = {}
|
||||||
|
preparedQuery.__index = preparedQuery
|
||||||
return function()
|
|
||||||
local archetype = compatibleArchetype.archetype
|
function preparedQuery:without(...)
|
||||||
local indices = compatibleArchetype.indices
|
local components = { ... }
|
||||||
local row = next(archetype.entities, lastRow)
|
for i, component in components do
|
||||||
while row == nil do
|
components[i] = #component
|
||||||
lastArchetype, compatibleArchetype = next(compatibleArchetypes, lastArchetype)
|
end
|
||||||
if lastArchetype == nil then
|
for i = #compatibleArchetypes, 1, - 1 do
|
||||||
return
|
local archetype = compatibleArchetypes[i].archetype
|
||||||
|
local shouldRemove = false
|
||||||
|
for _, componentId in components do
|
||||||
|
if archetype.records[componentId] then
|
||||||
|
shouldRemove = true
|
||||||
|
break
|
||||||
|
end
|
||||||
end
|
end
|
||||||
archetype = compatibleArchetype.archetype
|
if shouldRemove then
|
||||||
row = next(archetype.entities, row)
|
table.remove(compatibleArchetypes, i)
|
||||||
end
|
end
|
||||||
lastRow = row
|
end
|
||||||
|
|
||||||
local entityId = archetype.entities[row :: number]
|
|
||||||
|
|
||||||
if queryLength == 1 then
|
|
||||||
return entityId, indices[1][row]
|
|
||||||
elseif queryLength == 2 then
|
|
||||||
return entityId, indices[1][row], indices[2][row]
|
|
||||||
elseif queryLength == 3 then
|
|
||||||
return entityId,
|
|
||||||
indices[1][row],
|
|
||||||
indices[2][row],
|
|
||||||
indices[3][row]
|
|
||||||
elseif queryLength == 4 then
|
|
||||||
return entityId,
|
|
||||||
indices[1][row],
|
|
||||||
indices[2][row],
|
|
||||||
indices[3][row],
|
|
||||||
indices[4][row]
|
|
||||||
elseif queryLength == 5 then
|
|
||||||
return entityId,
|
|
||||||
indices[1][row],
|
|
||||||
indices[2][row],
|
|
||||||
indices[3][row],
|
|
||||||
indices[4][row]
|
|
||||||
elseif queryLength == 6 then
|
|
||||||
return entityId,
|
|
||||||
indices[1][row],
|
|
||||||
indices[2][row],
|
|
||||||
indices[3][row],
|
|
||||||
indices[4][row],
|
|
||||||
indices[5][row],
|
|
||||||
indices[6][row]
|
|
||||||
elseif queryLength == 7 then
|
|
||||||
return entityId,
|
|
||||||
indices[1][row],
|
|
||||||
indices[2][row],
|
|
||||||
indices[3][row],
|
|
||||||
indices[4][row],
|
|
||||||
indices[5][row],
|
|
||||||
indices[6][row],
|
|
||||||
indices[7][row]
|
|
||||||
|
|
||||||
elseif queryLength == 8 then
|
|
||||||
return entityId,
|
|
||||||
indices[1][row],
|
|
||||||
indices[2][row],
|
|
||||||
indices[3][row],
|
|
||||||
indices[4][row],
|
|
||||||
indices[5][row],
|
|
||||||
indices[6][row],
|
|
||||||
indices[7][row],
|
|
||||||
indices[8][row]
|
|
||||||
end
|
|
||||||
|
|
||||||
local queryOutput = {}
|
|
||||||
for i, componentId in components do
|
|
||||||
queryOutput[i] = indices[i][row]
|
|
||||||
end
|
|
||||||
|
|
||||||
return entityId, unpack(queryOutput, 1, queryLength)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local lastRow
|
||||||
|
function preparedQuery:__iter()
|
||||||
|
return function()
|
||||||
|
local archetype = compatibleArchetype.archetype
|
||||||
|
local indices = compatibleArchetype.indices
|
||||||
|
local row = next(archetype.entities, lastRow)
|
||||||
|
while row == nil do
|
||||||
|
lastArchetype, compatibleArchetype = next(compatibleArchetypes, lastArchetype)
|
||||||
|
if lastArchetype == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
archetype = compatibleArchetype.archetype
|
||||||
|
row = next(archetype.entities, row)
|
||||||
|
end
|
||||||
|
lastRow = row
|
||||||
|
|
||||||
|
local entityId = archetype.entities[row :: number]
|
||||||
|
|
||||||
|
if queryLength == 1 then
|
||||||
|
return entityId, indices[1][row]
|
||||||
|
elseif queryLength == 2 then
|
||||||
|
return entityId, indices[1][row], indices[2][row]
|
||||||
|
elseif queryLength == 3 then
|
||||||
|
return entityId,
|
||||||
|
indices[1][row],
|
||||||
|
indices[2][row],
|
||||||
|
indices[3][row]
|
||||||
|
elseif queryLength == 4 then
|
||||||
|
return entityId,
|
||||||
|
indices[1][row],
|
||||||
|
indices[2][row],
|
||||||
|
indices[3][row],
|
||||||
|
indices[4][row]
|
||||||
|
elseif queryLength == 5 then
|
||||||
|
return entityId,
|
||||||
|
indices[1][row],
|
||||||
|
indices[2][row],
|
||||||
|
indices[3][row],
|
||||||
|
indices[4][row]
|
||||||
|
elseif queryLength == 6 then
|
||||||
|
return entityId,
|
||||||
|
indices[1][row],
|
||||||
|
indices[2][row],
|
||||||
|
indices[3][row],
|
||||||
|
indices[4][row],
|
||||||
|
indices[5][row],
|
||||||
|
indices[6][row]
|
||||||
|
elseif queryLength == 7 then
|
||||||
|
return entityId,
|
||||||
|
indices[1][row],
|
||||||
|
indices[2][row],
|
||||||
|
indices[3][row],
|
||||||
|
indices[4][row],
|
||||||
|
indices[5][row],
|
||||||
|
indices[6][row],
|
||||||
|
indices[7][row]
|
||||||
|
|
||||||
|
elseif queryLength == 8 then
|
||||||
|
return entityId,
|
||||||
|
indices[1][row],
|
||||||
|
indices[2][row],
|
||||||
|
indices[3][row],
|
||||||
|
indices[4][row],
|
||||||
|
indices[5][row],
|
||||||
|
indices[6][row],
|
||||||
|
indices[7][row],
|
||||||
|
indices[8][row]
|
||||||
|
end
|
||||||
|
|
||||||
|
local queryOutput = {}
|
||||||
|
for i, componentId in components do
|
||||||
|
queryOutput[i] = indices[i][row]
|
||||||
|
end
|
||||||
|
|
||||||
|
return entityId, unpack(queryOutput, 1, queryLength)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return setmetatable({}, preparedQuery)
|
||||||
end
|
end
|
||||||
|
|
||||||
function World.component(world: World)
|
function World.component(world: World)
|
||||||
|
|
Loading…
Reference in a new issue