registry.batch_assign impl

This commit is contained in:
BlackMATov
2024-11-29 13:49:49 +07:00
parent 38a8ee5195
commit fafd8d2566
2 changed files with 69 additions and 1 deletions

View File

@@ -415,6 +415,50 @@ do
end
end
do
local f1, f2 = evo.registry.entity(), evo.registry.entity()
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_assign(q, f1, 42))
assert(e1:get(f1) == 10 and e2:get(f1) == 15 and e3:get(f1) == 42 and e4:get(f1) == 42)
assert(e3:get(f2) == 40 and e4:get(f2) == 45)
end
do
local q = evo.registry.query(f1)
assert(4 == evo.registry.batch_assign(q, f1, 21))
assert(e1:get(f1) == 21 and e2:get(f1) == 21 and e3:get(f1) == 21 and e4:get(f1) == 21)
assert(e3:get(f2) == 40 and e4:get(f2) == 45)
end
do
local q = evo.registry.query(f1, f2)
assert(2 == evo.registry.batch_assign(q, f1, nil))
assert(e1:get(f1) == 21 and e2:get(f1) == 21 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_assign(q, f2, 42))
assert(e1:get(f1) == 21 and e2:get(f1) == 21 and e3:get(f1) == true and e4:get(f1) == true)
assert(e3:get(f2) == 42 and e4:get(f2) == 42)
end
do
local q = evo.registry.query(f1):exclude(f2)
assert(2 == evo.registry.batch_assign(q, f1, 84))
assert(e1:get(f1) == 84 and e2:get(f1) == 84 and e3:get(f1) == true and e4:get(f1) == true)
assert(e3:get(f2) == 42 and e4:get(f2) == 42)
end
end
do
local f1, f2 = evo.registry.entity(), evo.registry.entity()

View File

@@ -575,7 +575,31 @@ end
---@param component any
---@return integer assigned_count
function registry.batch_assign(query, fragment, component)
error('not impl yet', 2)
component = component == nil and true or component
---@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 assigned_count = 0
for _, chunk in ipairs(chunks) do
local components = chunk.__components[fragment]
local component_count = #components
assigned_count = assigned_count + component_count
for i = 1, component_count do
components[i] = component
end
end
return assigned_count
end
---@param entity evolved.entity