mirror of
https://github.com/BlackMATov/evolved.lua.git
synced 2025-12-16 22:19:25 +07:00
registry.batch_apply impl
This commit is contained in:
@@ -415,6 +415,53 @@ do
|
||||
end
|
||||
end
|
||||
|
||||
do
|
||||
local f1, f2 = evo.registry.entity(), evo.registry.entity()
|
||||
|
||||
local function mul2(v) return v * 2 end
|
||||
local function null(_) end
|
||||
|
||||
local e1 = evo.registry.entity():set(f1, 10)
|
||||
local e2 = evo.registry.entity():set(f1, 15)
|
||||
local e3 = evo.registry.entity():set(f1, 20):set(f2, 40)
|
||||
local e4 = evo.registry.entity():set(f1, 25):set(f2, 45)
|
||||
|
||||
do
|
||||
local q = evo.registry.query(f2)
|
||||
assert(2 == evo.registry.batch_apply(q, f1, mul2))
|
||||
assert(e1:get(f1) == 10 and e2:get(f1) == 15 and e3:get(f1) == 40 and e4:get(f1) == 50)
|
||||
assert(e3:get(f2) == 40 and e4:get(f2) == 45)
|
||||
end
|
||||
|
||||
do
|
||||
local q = evo.registry.query(f1)
|
||||
assert(4 == evo.registry.batch_apply(q, f1, mul2))
|
||||
assert(e1:get(f1) == 20 and e2:get(f1) == 30 and e3:get(f1) == 80 and e4:get(f1) == 100)
|
||||
assert(e3:get(f2) == 40 and e4:get(f2) == 45)
|
||||
end
|
||||
|
||||
do
|
||||
local q = evo.registry.query(f1, f2)
|
||||
assert(2 == evo.registry.batch_apply(q, f1, null))
|
||||
assert(e1:get(f1) == 20 and e2:get(f1) == 30 and e3:get(f1) == true and e4:get(f1) == true)
|
||||
assert(e3:get(f2) == 40 and e4:get(f2) == 45)
|
||||
end
|
||||
|
||||
do
|
||||
local q = evo.registry.query(f1)
|
||||
assert(2 == evo.registry.batch_apply(q, f2, mul2))
|
||||
assert(e1:get(f1) == 20 and e2:get(f1) == 30 and e3:get(f1) == true and e4:get(f1) == true)
|
||||
assert(e3:get(f2) == 80 and e4:get(f2) == 90)
|
||||
end
|
||||
|
||||
do
|
||||
local q = evo.registry.query(f1):exclude(f2)
|
||||
assert(2 == evo.registry.batch_apply(q, f1, mul2))
|
||||
assert(e1:get(f1) == 40 and e2:get(f1) == 60 and e3:get(f1) == true and e4:get(f1) == true)
|
||||
assert(e3:get(f2) == 80 and e4:get(f2) == 90)
|
||||
end
|
||||
end
|
||||
|
||||
for _ = 1, 100 do
|
||||
local insert_fragments = {} ---@type evolved.entity[]
|
||||
local insert_fragment_count = math.random(0, 10)
|
||||
|
||||
@@ -519,7 +519,34 @@ end
|
||||
---@param transform fun(any): any
|
||||
---@return integer applied_count
|
||||
function registry.batch_apply(query, fragment, transform)
|
||||
error('not impl yet', 2)
|
||||
---@type evolved.chunk[]
|
||||
local chunks = {}
|
||||
|
||||
for chunk in registry.execute(query) do
|
||||
if chunk.__components[fragment] ~= nil then
|
||||
chunks[#chunks + 1] = chunk
|
||||
end
|
||||
end
|
||||
|
||||
local applied_count = 0
|
||||
|
||||
for _, chunk in ipairs(chunks) do
|
||||
local components = chunk.__components[fragment]
|
||||
local component_count = #components
|
||||
|
||||
applied_count = applied_count + component_count
|
||||
|
||||
for i = 1, component_count do
|
||||
local component = components[i]
|
||||
|
||||
component = transform(components[i])
|
||||
component = component == nil and true or component
|
||||
|
||||
components[i] = component
|
||||
end
|
||||
end
|
||||
|
||||
return applied_count
|
||||
end
|
||||
|
||||
---@param entity evolved.entity
|
||||
|
||||
Reference in New Issue
Block a user