add registry.batch_set

This commit is contained in:
BlackMATov
2024-12-03 23:36:26 +07:00
parent beaf4eb5e1
commit 00cc01b34f
4 changed files with 71 additions and 1 deletions
+2
View File
@@ -55,6 +55,7 @@ registry.entity -> (entity)
registry.guid -> entity -> (id)
registry.alive -> entity -> (boolean)
registry.set -> entity -> entity -> any -> (entity)
registry.batch_set -> query -> entity -> any -> (integer, integer)
registry.get -> entity -> entity... -> (any...)
registry.has -> entity -> entity -> (boolean)
registry.has_all -> entity -> entity... -> (boolean)
@@ -104,6 +105,7 @@ entity:destroy -> (boolean)
query:include -> entity... -> query
query:exclude -> entity... -> query
query:execute -> ({execution_state? -> chunk?}, execution_state?)
query:batch_set -> entity -> any -> (integer, integer)
query:batch_apply -> {any -> any} -> entity -> (integer)
query:batch_assign -> entity -> any -> (integer)
query:batch_insert -> entity -> any -> (integer)
+1 -1
View File
@@ -19,4 +19,4 @@
- [ ] add inplace vector operations
- [x] cache chunk lists in batch operations
- [ ] impl compat.move for 5.1 vanilla lua
- [ ] add registry.batch_set
- [x] add registry.batch_set
+42
View File
@@ -395,6 +395,48 @@ do
end
end
do
local f1, f2, f3 = evo.registry.entity(), 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)
local assigned, inserted = q:batch_set(f1, 42)
assert(assigned == 2 and inserted == 0)
assert(e1:get(f1) == 10 and e2:get(f1) == 15 and e3:get(f1) == 42 and e4:get(f1) == 42)
assert(e1:get(f2) == nil and e2:get(f2) == nil and e3:get(f2) == 40 and e4:get(f2) == 45)
end
do
local q = evo.registry.query(f1)
local assigned, inserted = q:batch_set(f1, 21)
assert(assigned == 4 and inserted == 0)
assert(e1:get(f1) == 21 and e2:get(f1) == 21 and e3:get(f1) == 21 and e4:get(f1) == 21)
assert(e1:get(f2) == nil and e2:get(f2) == nil and e3:get(f2) == 40 and e4:get(f2) == 45)
end
do
local q = evo.registry.query(f1)
local assigned, inserted = q:batch_set(f2, 84)
assert(assigned == 2 and inserted == 2)
assert(e1:get(f1) == 21 and e2:get(f1) == 21 and e3:get(f1) == 21 and e4:get(f1) == 21)
assert(e1:get(f2) == 84 and e2:get(f2) == 84 and e3:get(f2) == 84 and e4:get(f2) == 84)
end
do
local q = evo.registry.query(f1, f2)
local assigned, inserted = q:batch_set(f3, 22)
assert(assigned == 0 and inserted == 4)
assert(e1:get(f1) == 21 and e2:get(f1) == 21 and e3:get(f1) == 21 and e4:get(f1) == 21)
assert(e1:get(f2) == 84 and e2:get(f2) == 84 and e3:get(f2) == 84 and e4:get(f2) == 84)
assert(e1:get(f3) == 22 and e2:get(f3) == 22 and e3:get(f3) == 22 and e4:get(f3) == 22)
end
end
do
local f1, f2 = evo.registry.entity(), evo.registry.entity()
+26
View File
@@ -698,6 +698,31 @@ function registry.set(entity, fragment, component)
return entity
end
---@param query evolved.query
---@param fragment evolved.entity
---@param component any
---@return integer assigned_count
---@return integer inserted_count
function registry.batch_set(query, fragment, component)
local to_assign_chunks = __execution_stack_acquire()
local to_insert_chunks = __execution_stack_acquire()
for chunk in registry.execute(query) do
if chunk.__components[fragment] ~= nil then
to_assign_chunks[#to_assign_chunks + 1] = chunk
else
to_insert_chunks[#to_insert_chunks + 1] = chunk
end
end
local assigned_count = __batch_assign(to_assign_chunks, fragment, component)
local inserted_count = __batch_insert(to_insert_chunks, fragment, component)
__execution_stack_release(to_assign_chunks)
__execution_stack_release(to_insert_chunks)
return assigned_count, inserted_count
end
---@param entity evolved.entity
---@param ... evolved.entity fragments
---@return any ... components
@@ -1284,6 +1309,7 @@ end
evolved_query_mt.include = registry.include
evolved_query_mt.exclude = registry.exclude
evolved_query_mt.execute = registry.execute
evolved_query_mt.batch_set = registry.batch_set
evolved_query_mt.batch_apply = registry.batch_apply
evolved_query_mt.batch_assign = registry.batch_assign
evolved_query_mt.batch_insert = registry.batch_insert