registry.apply impl

This commit is contained in:
BlackMATov
2024-11-29 11:31:19 +07:00
parent 28e322c518
commit dd6a6579dd
2 changed files with 76 additions and 10 deletions
+50
View File
@@ -365,6 +365,56 @@ do
assert(e.__chunk == nil)
end
do
local f1, f2 = evo.registry.entity(), evo.registry.entity()
local e = evo.registry.entity()
local function mul2(v) return v * 2 end
local function null(_) end
do
assert(not e:apply(f1, mul2))
assert(e.__chunk == nil)
assert(not e:apply(f1, null))
assert(e.__chunk == nil)
assert(e:insert(f1, 21))
assert(e:get(f1) == 21)
assert(e.__chunk == evo.registry.chunk(f1))
assert(e:apply(f1, mul2))
assert(e:get(f1) == 42)
assert(e.__chunk == evo.registry.chunk(f1))
assert(e:apply(f1, null))
assert(e:get(f1) == true)
assert(e.__chunk == evo.registry.chunk(f1))
end
do
assert(not e:apply(f2, mul2))
assert(e:get(f1) == true)
assert(e.__chunk == evo.registry.chunk(f1))
assert(not e:apply(f2, null))
assert(e:get(f1) == true)
assert(e.__chunk == evo.registry.chunk(f1))
assert(e:insert(f2, 4))
assert(e:get(f2) == 4)
assert(e.__chunk == evo.registry.chunk(f1, f2))
assert(e:apply(f2, mul2))
assert(e:get(f1) == true)
assert(e:get(f2) == 8)
assert(e.__chunk == evo.registry.chunk(f1, f2))
assert(e:apply(f2, null))
assert(e:get(f1) == true)
assert(e:get(f2) == true)
assert(e.__chunk == evo.registry.chunk(f1, f2))
end
end
for _ = 1, 100 do
local insert_fragments = {} ---@type evolved.entity[]
local insert_fragment_count = math.random(0, 10)
+26 -10
View File
@@ -465,9 +465,9 @@ end
---@return boolean
---@nodiscard
function registry.has(entity, fragment)
local cur_chunk = entity.__chunk
if cur_chunk == nil then return false end
return __chunk_has_fragment(cur_chunk, fragment)
local chunk = entity.__chunk
if chunk == nil then return false end
return __chunk_has_fragment(chunk, fragment)
end
---@param entity evolved.entity
@@ -475,9 +475,9 @@ end
---@return boolean
---@nodiscard
function registry.has_all(entity, ...)
local cur_chunk = entity.__chunk
if cur_chunk == nil then return select('#', ...) == 0 end
return __chunk_has_all_fragments(cur_chunk, ...)
local chunk = entity.__chunk
if chunk == nil then return select('#', ...) == 0 end
return __chunk_has_all_fragments(chunk, ...)
end
---@param entity evolved.entity
@@ -485,9 +485,9 @@ end
---@return boolean
---@nodiscard
function registry.has_any(entity, ...)
local cur_chunk = entity.__chunk
if cur_chunk == nil then return false end
return __chunk_has_any_fragments(cur_chunk, ...)
local chunk = entity.__chunk
if chunk == nil then return false end
return __chunk_has_any_fragments(chunk, ...)
end
---@param entity evolved.entity
@@ -495,7 +495,23 @@ end
---@param transform fun(any): any
---@return boolean is_applied
function registry.apply(entity, fragment, transform)
error('not impl yet', 2)
if not idpools.alive(__guids, entity.__guid) then
return false
end
local chunk = entity.__chunk
if chunk == nil then return false end
local components = chunk.__components[fragment]
if components == nil then return false end
local component = components[entity.__index_in_chunk]
component = transform(component)
component = component == nil and true or component
components[entity.__index_in_chunk] = component
return true
end
---@param query evolved.query