mirror of
https://github.com/Ukendio/jecs.git
synced 2025-04-26 01:50:01 +00:00
add replace method
This commit is contained in:
parent
ea89be96c2
commit
5e3f0485d3
2 changed files with 94 additions and 42 deletions
|
@ -8,7 +8,7 @@ local function TITLE(title: string)
|
||||||
print(testkit.color.white(title))
|
print(testkit.color.white(title))
|
||||||
end
|
end
|
||||||
|
|
||||||
local jecs = require("../lib/init")
|
local jecs = require("@jecs")
|
||||||
local mirror = require("../mirror/init")
|
local mirror = require("../mirror/init")
|
||||||
|
|
||||||
type i53 = number
|
type i53 = number
|
||||||
|
|
|
@ -662,16 +662,24 @@ local function noop(_self: Query, ...): () -> ()
|
||||||
end
|
end
|
||||||
|
|
||||||
local EmptyQuery = {
|
local EmptyQuery = {
|
||||||
__iter = noop,
|
__iter = iterNoop,
|
||||||
without = noop,
|
next = noop,
|
||||||
|
replace = noop,
|
||||||
|
without = function()
|
||||||
|
return EmptyQuery
|
||||||
|
end
|
||||||
}
|
}
|
||||||
EmptyQuery.__index = EmptyQuery
|
|
||||||
setmetatable(EmptyQuery, EmptyQuery)
|
|
||||||
|
|
||||||
export type Query = typeof(EmptyQuery)
|
export type Query = typeof(EmptyQuery)
|
||||||
|
|
||||||
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
|
type CompatibleArchetype = { archetype: Archetype, indices: { number } }
|
||||||
|
|
||||||
|
local function replaceColumns(row, columns, ...)
|
||||||
|
for i, column in columns do
|
||||||
|
column[row] = select(i, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function preparedQuery(compatibleArchetypes: { Archetype },
|
local function preparedQuery(compatibleArchetypes: { Archetype },
|
||||||
components: { i53? }, indices: { { number } })
|
components: { i53? }, indices: { { number } })
|
||||||
|
|
||||||
|
@ -783,20 +791,64 @@ local function preparedQuery(compatibleArchetypes: { Archetype },
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
local it = {
|
local function iter()
|
||||||
__iter = function()
|
lastArchetype = 1
|
||||||
lastArchetype = 1
|
archetype = compatibleArchetypes[1]
|
||||||
archetype = compatibleArchetypes[1]
|
entities = archetype.entities
|
||||||
entities = archetype.entities
|
i = #entities
|
||||||
i = #entities
|
|
||||||
|
|
||||||
return queryNext
|
return queryNext
|
||||||
end,
|
end
|
||||||
|
|
||||||
|
local function replace()
|
||||||
|
for _, compatibleArchetype in compatibleArchetypes do
|
||||||
|
local archetype = compatibleArchetype.archetype
|
||||||
|
local tr = compatibleArchetype.indices
|
||||||
|
local columns = archetype.columns
|
||||||
|
|
||||||
|
for row in archetype.entities do
|
||||||
|
if queryLength == 1 then
|
||||||
|
local a = columns[tr[1]]
|
||||||
|
local pa = fn(a[row])
|
||||||
|
|
||||||
|
a[row] = pa
|
||||||
|
elseif queryLength == 2 then
|
||||||
|
local a = columns[tr[1]]
|
||||||
|
local b = columns[tr[2]]
|
||||||
|
|
||||||
|
a[row], b[row] = fn(a[row], b[row])
|
||||||
|
elseif queryLength == 3 then
|
||||||
|
local a = columns[tr[1]]
|
||||||
|
local b = columns[tr[2]]
|
||||||
|
local c = columns[tr[3]]
|
||||||
|
|
||||||
|
a[row], b[row], c[row] = fn(a[row], b[row], c[row])
|
||||||
|
elseif queryLength == 4 then
|
||||||
|
local a = columns[tr[1]]
|
||||||
|
local b = columns[tr[2]]
|
||||||
|
local c = columns[tr[3]]
|
||||||
|
local d = columns[tr[4]]
|
||||||
|
|
||||||
|
a[row], b[row], c[row], d[row] = fn(
|
||||||
|
a[row], b[row], c[row], d[row])
|
||||||
|
else
|
||||||
|
for i = 1, queryLength do
|
||||||
|
queryOutput[i] = columns[tr[i]][row]
|
||||||
|
end
|
||||||
|
replace(row, columns, fn(unpack(queryOutput)))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local it = {
|
||||||
|
__iter = iter,
|
||||||
next = queryNext,
|
next = queryNext,
|
||||||
without = without
|
without = without,
|
||||||
|
replace = replace
|
||||||
}
|
}
|
||||||
|
|
||||||
return setmetatable(it, it) :: any
|
return it
|
||||||
end
|
end
|
||||||
|
|
||||||
local function query(world: World, ...: number): Query
|
local function query(world: World, ...: number): Query
|
||||||
|
|
Loading…
Reference in a new issue