registry.batch_detach/destroy impl

This commit is contained in:
BlackMATov
2024-11-29 16:59:08 +07:00
parent 007ba7905b
commit 229872908f
2 changed files with 143 additions and 2 deletions

View File

@@ -564,6 +564,80 @@ do
end
end
do
local f1, f2, f3 = evo.registry.entity(), evo.registry.entity(), evo.registry.entity()
do
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):set(f3, 55)
local e5 = evo.registry.entity():set(f3, 65)
do
local q = evo.registry.query(f2):exclude(f3)
assert(1 == evo.registry.batch_detach(q))
assert(e1.__chunk == evo.registry.chunk(f1))
assert(e2.__chunk == evo.registry.chunk(f1))
assert(e3.__chunk == nil)
assert(e4.__chunk == evo.registry.chunk(f1, f2, f3))
assert(e5.__chunk == evo.registry.chunk(f3))
assert(#evo.registry.chunk(f1, f2):entities() == 0)
assert(#evo.registry.chunk(f1, f2):components(f1) == 0)
assert(#evo.registry.chunk(f1, f2):components(f2) == 0)
assert(e1:alive() and e2:alive() and e3:alive() and e4:alive() and e5:alive())
end
do
local q = evo.registry.query(f1)
assert(3 == evo.registry.batch_detach(q))
assert(e1.__chunk == nil)
assert(e2.__chunk == nil)
assert(e3.__chunk == nil)
assert(e4.__chunk == nil)
assert(e5.__chunk == evo.registry.chunk(f3))
assert(e1:alive() and e2:alive() and e3:alive() and e4:alive() and e5:alive())
end
end
end
do
local f1, f2, f3 = evo.registry.entity(), evo.registry.entity(), evo.registry.entity()
do
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):set(f3, 55)
local e5 = evo.registry.entity():set(f3, 65)
do
local q = evo.registry.query(f2):exclude(f3)
assert(1 == evo.registry.batch_destroy(q))
assert(e1.__chunk == evo.registry.chunk(f1))
assert(e2.__chunk == evo.registry.chunk(f1))
assert(e3.__chunk == nil)
assert(e4.__chunk == evo.registry.chunk(f1, f2, f3))
assert(e5.__chunk == evo.registry.chunk(f3))
assert(#evo.registry.chunk(f1, f2):entities() == 0)
assert(#evo.registry.chunk(f1, f2):components(f1) == 0)
assert(#evo.registry.chunk(f1, f2):components(f2) == 0)
assert(e1:alive() and e2:alive() and not e3:alive() and e4:alive() and e5:alive())
end
do
local q = evo.registry.query(f1)
assert(3 == evo.registry.batch_destroy(q))
assert(e1.__chunk == nil)
assert(e2.__chunk == nil)
assert(e3.__chunk == nil)
assert(e4.__chunk == nil)
assert(e5.__chunk == evo.registry.chunk(f3))
assert(not e1:alive() and not e2:alive() and not e3:alive() and not e4:alive() and e5:alive())
end
end
end
for _ = 1, 100 do
local insert_fragments = {} ---@type evolved.entity[]
local insert_fragment_count = math.random(0, 10)

View File

@@ -379,7 +379,41 @@ end
---@param query evolved.query
---@return integer destroyed_count
function registry.batch_destroy(query)
error('not impl yet', 2)
---@type evolved.chunk[]
local chunks = {}
for chunk in registry.execute(query) do
chunks[#chunks + 1] = chunk
end
local destroyed_count = 0
for _, chunk in ipairs(chunks) do
do
local changes = #chunk.__entities
__changes = __changes + changes
destroyed_count = destroyed_count + changes
chunk.__changes = chunk.__changes + changes
end
for _, entity in ipairs(chunk.__entities) do
entity.__chunk = nil
entity.__index_in_chunk = 0
idpools.release(__guids, entity.__guid)
end
do
chunk.__entities = {}
for f, _ in pairs(chunk.__components) do
chunk.__components[f] = {}
end
end
end
return destroyed_count
end
---@param entity evolved.entity
@@ -842,7 +876,40 @@ end
---@param query evolved.query
---@return integer detached_count
function registry.batch_detach(query)
error('not impl yet', 2)
---@type evolved.chunk[]
local chunks = {}
for chunk in registry.execute(query) do
chunks[#chunks + 1] = chunk
end
local detached_count = 0
for _, chunk in ipairs(chunks) do
do
local changes = #chunk.__entities
__changes = __changes + changes
detached_count = detached_count + changes
chunk.__changes = chunk.__changes + changes
end
for _, entity in ipairs(chunk.__entities) do
entity.__chunk = nil
entity.__index_in_chunk = 0
end
do
chunk.__entities = {}
for f, _ in pairs(chunk.__components) do
chunk.__components[f] = {}
end
end
end
return detached_count
end
---@param ... evolved.entity fragments