swap apply arguments

This commit is contained in:
BlackMATov
2024-11-29 17:41:20 +07:00
parent 345dc22225
commit 76d99852f4
4 changed files with 24 additions and 23 deletions

View File

@@ -36,8 +36,8 @@ registry.get_or -> entity -> entity -> any -> (any)
registry.has -> entity -> entity -> (boolean)
registry.has_all -> entity -> entity... -> (boolean)
registry.has_any -> entity -> entity... -> (boolean)
registry.apply -> entity -> entity -> (any -> any) -> (boolean)
registry.batch_apply -> query -> entity -> (any -> any) -> (integer)
registry.apply -> entity -> (any -> any) -> entity -> (boolean)
registry.batch_apply -> query -> (any -> any) -> entity -> (integer)
registry.assign -> entity -> entity -> any -> (boolean)
registry.batch_assign -> query -> entity -> any -> (integer)
registry.insert -> entity -> entity -> any -> (boolean)
@@ -68,6 +68,7 @@ entity:get_or -> entity -> any -> (any)
entity:has -> entity -> (boolean)
entity:has_all -> entity... -> (boolean)
entity:has_any -> entity... -> (boolean)
entity:apply -> (any -> any) -> entity -> (boolean)
entity:assign -> entity -> any -> (boolean)
entity:insert -> entity -> any -> (boolean)
entity:remove -> entity... -> (boolean)

View File

@@ -14,4 +14,4 @@
- [ ] add assertions for input arguments
- [x] registry.assign should not change chunks' tree
- [ ] add deferred changes api
- [ ] add multi batch_apply
- [ ] add multi apply/batch_apply

View File

@@ -373,29 +373,29 @@ do
local function null(_) end
do
assert(not e:apply(f1, mul2))
assert(not e:apply(mul2, f1))
assert(e.__chunk == nil)
assert(not e:apply(f1, null))
assert(not e:apply(null, f1))
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:apply(mul2, f1))
assert(e:get(f1) == 42)
assert(e.__chunk == evo.registry.chunk(f1))
assert(e:apply(f1, null))
assert(e:apply(null, f1))
assert(e:get(f1) == true)
assert(e.__chunk == evo.registry.chunk(f1))
end
do
assert(not e:apply(f2, mul2))
assert(not e:apply(mul2, f2))
assert(e:get(f1) == true)
assert(e.__chunk == evo.registry.chunk(f1))
assert(not e:apply(f2, null))
assert(not e:apply(null, f2))
assert(e:get(f1) == true)
assert(e.__chunk == evo.registry.chunk(f1))
@@ -403,12 +403,12 @@ do
assert(e:get(f2) == 4)
assert(e.__chunk == evo.registry.chunk(f1, f2))
assert(e:apply(f2, mul2))
assert(e:apply(mul2, f2))
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:apply(null, f2))
assert(e:get(f1) == true)
assert(e:get(f2) == true)
assert(e.__chunk == evo.registry.chunk(f1, f2))
@@ -472,35 +472,35 @@ do
do
local q = evo.registry.query(f2)
assert(2 == evo.registry.batch_apply(q, f1, mul2))
assert(2 == evo.registry.batch_apply(q, mul2, f1))
assert(e1:get(f1) == 10 and e2:get(f1) == 15 and e3:get(f1) == 40 and e4:get(f1) == 50)
assert(e3:get(f2) == 40 and e4:get(f2) == 45)
end
do
local q = evo.registry.query(f1)
assert(4 == evo.registry.batch_apply(q, f1, mul2))
assert(4 == evo.registry.batch_apply(q, mul2, f1))
assert(e1:get(f1) == 20 and e2:get(f1) == 30 and e3:get(f1) == 80 and e4:get(f1) == 100)
assert(e3:get(f2) == 40 and e4:get(f2) == 45)
end
do
local q = evo.registry.query(f1, f2)
assert(2 == evo.registry.batch_apply(q, f1, null))
assert(2 == evo.registry.batch_apply(q, null, f1))
assert(e1:get(f1) == 20 and e2:get(f1) == 30 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_apply(q, f2, mul2))
assert(2 == evo.registry.batch_apply(q, mul2, f2))
assert(e1:get(f1) == 20 and e2:get(f1) == 30 and e3:get(f1) == true and e4:get(f1) == true)
assert(e3:get(f2) == 80 and e4:get(f2) == 90)
end
do
local q = evo.registry.query(f1):exclude(f2)
assert(2 == evo.registry.batch_apply(q, f1, mul2))
assert(2 == evo.registry.batch_apply(q, mul2, f1))
assert(e1:get(f1) == 40 and e2:get(f1) == 60 and e3:get(f1) == true and e4:get(f1) == true)
assert(e3:get(f2) == 80 and e4:get(f2) == 90)
end

View File

@@ -525,10 +525,10 @@ function registry.has_any(entity, ...)
end
---@param entity evolved.entity
---@param apply fun(any): any
---@param fragment evolved.entity
---@param transform fun(any): any
---@return boolean is_applied
function registry.apply(entity, fragment, transform)
function registry.apply(entity, apply, fragment)
if not idpools.alive(__guids, entity.__guid) then
return false
end
@@ -541,7 +541,7 @@ function registry.apply(entity, fragment, transform)
local component = components[entity.__index_in_chunk]
component = transform(component)
component = apply(component)
component = component == nil and true or component
components[entity.__index_in_chunk] = component
@@ -549,10 +549,10 @@ function registry.apply(entity, fragment, transform)
end
---@param query evolved.query
---@param apply fun(any): any
---@param fragment evolved.entity
---@param transform fun(any): any
---@return integer applied_count
function registry.batch_apply(query, fragment, transform)
function registry.batch_apply(query, apply, fragment)
---@type evolved.chunk[]
local chunks = {}
@@ -573,7 +573,7 @@ function registry.batch_apply(query, fragment, transform)
for i = 1, component_count do
local component = components[i]
component = transform(components[i])
component = apply(components[i])
component = component == nil and true or component
components[i] = component
@@ -694,7 +694,7 @@ function registry.batch_insert(query, fragment, component)
local chunks = {}
for chunk in registry.execute(query) do
if not __chunk_has_fragment(chunk, fragment) then
if chunk.__components[fragment] == nil then
chunks[#chunks + 1] = chunk
end
end