diff --git a/develop/untests/registry_untests.lua b/develop/untests/registry_untests.lua index 438c982..5a2f69c 100644 --- a/develop/untests/registry_untests.lua +++ b/develop/untests/registry_untests.lua @@ -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) diff --git a/evolved/registry.lua b/evolved/registry.lua index 0aff1bd..4bfe353 100644 --- a/evolved/registry.lua +++ b/evolved/registry.lua @@ -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