mirror of
https://github.com/BlackMATov/evolved.lua.git
synced 2026-09-27 11:07:08 +07:00
remove registry.apply
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
```
|
||||
defers.defer -> (defer)
|
||||
defers.set -> defer -> entity -> entity -> any -> (defer)
|
||||
defers.apply -> defer -> entity -> {any -> any} -> entity -> (defer)
|
||||
defers.assign -> defer -> entity -> entity -> any -> (defer)
|
||||
defers.insert -> defer -> entity -> entity -> any -> (defer)
|
||||
defers.remove -> defer -> entity -> entity... -> (defer)
|
||||
@@ -18,7 +17,6 @@ defers.playback -> defer -> (defer)
|
||||
|
||||
```
|
||||
defer:set -> entity -> entity -> any -> (defer)
|
||||
defer:apply -> entity -> {any -> any} -> entity -> (defer)
|
||||
defer:assign -> entity -> entity -> any -> (defer)
|
||||
defer:insert -> entity -> entity -> any -> (defer)
|
||||
defer:remove -> entity -> entity... -> (defer)
|
||||
@@ -61,9 +59,6 @@ registry.has_any -> entity -> entity... -> (boolean)
|
||||
registry.set -> entity -> entity -> any -> (entity)
|
||||
registry.chunk_set -> chunk -> entity -> any -> (integer, integer)
|
||||
registry.query_set -> query -> entity -> any -> (integer, integer)
|
||||
registry.apply -> entity -> {any -> any} -> entity -> (boolean)
|
||||
registry.chunk_apply -> chunk -> {any -> any} -> entity -> (integer)
|
||||
registry.query_apply -> query -> {any -> any} -> entity -> (integer)
|
||||
registry.assign -> entity -> entity -> any -> (boolean)
|
||||
registry.chunk_assign -> chunk -> entity -> any -> (integer)
|
||||
registry.query_assign -> query -> entity -> any -> (integer)
|
||||
@@ -99,7 +94,6 @@ entity:has_all -> entity... -> (boolean)
|
||||
entity:has_any -> entity... -> (boolean)
|
||||
|
||||
entity:set -> entity -> any -> (entity)
|
||||
entity:apply -> {any -> any} -> entity -> (boolean)
|
||||
entity:assign -> entity -> any -> (boolean)
|
||||
entity:insert -> entity -> any -> (boolean)
|
||||
entity:remove -> entity... -> (boolean)
|
||||
@@ -111,7 +105,6 @@ entity:destroy -> (entity)
|
||||
|
||||
```
|
||||
query:set -> entity -> any -> (integer, integer)
|
||||
query:apply -> {any -> any} -> entity -> (integer)
|
||||
query:assign -> entity -> any -> (integer)
|
||||
query:insert -> entity -> any -> (integer)
|
||||
query:remove -> entity... -> (integer)
|
||||
@@ -127,7 +120,6 @@ query:execute -> ({execution_state? -> chunk?}, execution_state?)
|
||||
|
||||
```
|
||||
chunk:set -> entity -> any -> (integer, integer)
|
||||
chunk:apply -> {any -> any} -> entity -> (integer)
|
||||
chunk:assign -> entity -> any -> (integer)
|
||||
chunk:insert -> entity -> any -> (integer)
|
||||
chunk:remove -> entity... -> (integer)
|
||||
|
||||
+1
-2
@@ -14,7 +14,6 @@
|
||||
- [ ] add assertions for input arguments
|
||||
- [x] registry.assign should not change chunks' tree
|
||||
- [x] add deferred changes api
|
||||
- [ ] add multi apply/batch_apply
|
||||
- [ ] add batch vector operations
|
||||
- [ ] add inplace vector operations
|
||||
- [x] cache chunk lists in batch operations
|
||||
@@ -22,7 +21,7 @@
|
||||
- [x] add registry.batch_set
|
||||
- [x] rename include/exclude/execute to query_include/exclude/execute
|
||||
- [x] rename entities/components to chunk_entities/components
|
||||
- [ ] remove registry.apply to avoid encouraging incorrect patterns
|
||||
- [x] remove registry.apply to avoid encouraging incorrect patterns
|
||||
- [ ] add on_set, on_assign, on_insert, on_remove callbacks
|
||||
- [ ] add initial component fragment
|
||||
- [ ] add constructor component fragment
|
||||
@@ -13,32 +13,6 @@ do
|
||||
assert(e:get(f) == 84)
|
||||
end
|
||||
|
||||
do
|
||||
local mul2 = function(v) return v * 2 end
|
||||
|
||||
local f1, f2 = evo.registry.entity(), evo.registry.entity()
|
||||
local e = evo.registry.entity():set(f1, 21)
|
||||
local d = evo.defers.defer():apply(e, mul2, f1)
|
||||
assert(e:get(f1) == 21)
|
||||
assert(d == d:playback())
|
||||
assert(e:get(f1) == 42)
|
||||
|
||||
evo.defers.defer():apply(e, mul2, f2):playback()
|
||||
assert(not e:has(f2) and e:get(f1) == 42)
|
||||
end
|
||||
|
||||
do
|
||||
local mul2 = function(v) return v * 2 end
|
||||
local mul3 = function(v) return v * 3 end
|
||||
|
||||
local f1, f2 = evo.registry.entity(), evo.registry.entity()
|
||||
local e = evo.registry.entity():set(f1, 21):set(f2, 42)
|
||||
local d = evo.defers.defer():apply(e, mul2, f1):apply(e, mul3, f2)
|
||||
assert(e:get(f1) == 21 and e:get(f2) == 42)
|
||||
assert(d == d:playback())
|
||||
assert(e:get(f1) == 42 and e:get(f2) == 126)
|
||||
end
|
||||
|
||||
do
|
||||
local f = evo.registry.entity()
|
||||
local e = evo.registry.entity():set(f, 21)
|
||||
|
||||
@@ -345,56 +345,6 @@ 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(mul2, f1))
|
||||
assert(e.__chunk == nil)
|
||||
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(mul2, f1))
|
||||
assert(e:get(f1) == 42)
|
||||
assert(e.__chunk == evo.registry.chunk(f1))
|
||||
|
||||
assert(e:apply(null, f1))
|
||||
assert(e:get(f1) == true)
|
||||
assert(e.__chunk == evo.registry.chunk(f1))
|
||||
end
|
||||
|
||||
do
|
||||
assert(not e:apply(mul2, f2))
|
||||
assert(e:get(f1) == true)
|
||||
assert(e.__chunk == evo.registry.chunk(f1))
|
||||
assert(not e:apply(null, f2))
|
||||
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(mul2, f2))
|
||||
assert(e:get(f1) == true)
|
||||
assert(e:get(f2) == 8)
|
||||
assert(e.__chunk == evo.registry.chunk(f1, f2))
|
||||
|
||||
assert(e:apply(null, f2))
|
||||
assert(e:get(f1) == true)
|
||||
assert(e:get(f2) == true)
|
||||
assert(e.__chunk == evo.registry.chunk(f1, f2))
|
||||
end
|
||||
end
|
||||
|
||||
do
|
||||
local f1, f2, f3 = evo.registry.entity(), evo.registry.entity(), evo.registry.entity()
|
||||
|
||||
@@ -481,53 +431,6 @@ do
|
||||
end
|
||||
end
|
||||
|
||||
do
|
||||
local f1, f2 = evo.registry.entity(), evo.registry.entity()
|
||||
|
||||
local function mul2(v) return v * 2 end
|
||||
local function null(_) end
|
||||
|
||||
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)
|
||||
assert(2 == q:apply(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 == q:apply(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 == q:apply(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 == q:apply(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 == q:apply(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
|
||||
end
|
||||
|
||||
do
|
||||
local f1, f2 = evo.registry.entity(), evo.registry.entity()
|
||||
|
||||
|
||||
+5
-32
@@ -13,12 +13,11 @@ local defers = {}
|
||||
---@enum evolved.defer_op
|
||||
local evolved_defer_op = {
|
||||
set = 1,
|
||||
apply = 2,
|
||||
assign = 3,
|
||||
insert = 4,
|
||||
remove = 5,
|
||||
detach = 6,
|
||||
destroy = 7,
|
||||
assign = 2,
|
||||
insert = 3,
|
||||
remove = 4,
|
||||
detach = 5,
|
||||
destroy = 6,
|
||||
}
|
||||
|
||||
---@class (exact) evolved.__defer
|
||||
@@ -44,13 +43,6 @@ local __operation_processors = {
|
||||
registry.set(entity, fragment, component)
|
||||
return 4
|
||||
end,
|
||||
[evolved_defer_op.apply] = function(ops, idx)
|
||||
local entity = ops[idx + 1]
|
||||
local apply = ops[idx + 2]
|
||||
local fragment = ops[idx + 3]
|
||||
registry.apply(entity, apply, fragment)
|
||||
return 4
|
||||
end,
|
||||
[evolved_defer_op.assign] = function(ops, idx)
|
||||
local entity = ops[idx + 1]
|
||||
local fragment = ops[idx + 2]
|
||||
@@ -119,24 +111,6 @@ function defers.set(defer, entity, fragment, component)
|
||||
return defer
|
||||
end
|
||||
|
||||
---@param defer evolved.defer
|
||||
---@param entity evolved.entity
|
||||
---@param apply fun(any): any
|
||||
---@param fragment evolved.entity
|
||||
---@return evolved.defer
|
||||
function defers.apply(defer, entity, apply, fragment)
|
||||
local operations = defer.operations
|
||||
local operation_count = defer.operation_count
|
||||
|
||||
operations[operation_count + 1] = evolved_defer_op.apply
|
||||
operations[operation_count + 2] = entity
|
||||
operations[operation_count + 3] = apply
|
||||
operations[operation_count + 4] = fragment
|
||||
|
||||
defer.operation_count = operation_count + 4
|
||||
return defer
|
||||
end
|
||||
|
||||
---@param defer evolved.defer
|
||||
---@param entity evolved.entity
|
||||
---@param fragment evolved.entity
|
||||
@@ -246,7 +220,6 @@ end
|
||||
---
|
||||
|
||||
evolved_defer_mt.set = defers.set
|
||||
evolved_defer_mt.apply = defers.apply
|
||||
evolved_defer_mt.assign = defers.assign
|
||||
evolved_defer_mt.insert = defers.insert
|
||||
evolved_defer_mt.remove = defers.remove
|
||||
|
||||
@@ -622,80 +622,6 @@ function registry.query_set(query, fragment, component)
|
||||
return assigned_count, inserted_count
|
||||
end
|
||||
|
||||
---@param entity evolved.entity
|
||||
---@param apply fun(any): any
|
||||
---@param fragment evolved.entity
|
||||
---@return boolean is_applied
|
||||
function registry.apply(entity, apply, fragment)
|
||||
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
|
||||
|
||||
do
|
||||
local component = components[entity.__index_in_chunk]
|
||||
|
||||
component = apply(component)
|
||||
component = component == nil and true or component
|
||||
|
||||
components[entity.__index_in_chunk] = component
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
---@param chunk evolved.chunk
|
||||
---@param apply fun(any): any
|
||||
---@param fragment evolved.entity
|
||||
---@return integer applied_count
|
||||
function registry.chunk_apply(chunk, apply, fragment)
|
||||
local chunk_size = #chunk.__entities
|
||||
local chunk_components = chunk.__components
|
||||
local chunk_fragment_components = chunk_components[fragment]
|
||||
|
||||
if chunk_size == 0 or chunk_fragment_components == nil then
|
||||
return 0
|
||||
end
|
||||
|
||||
for i = 1, chunk_size do
|
||||
local component = chunk_fragment_components[i]
|
||||
|
||||
component = apply(component)
|
||||
component = component == nil and true or component
|
||||
|
||||
chunk_fragment_components[i] = component
|
||||
end
|
||||
|
||||
return chunk_size
|
||||
end
|
||||
|
||||
---@param query evolved.query
|
||||
---@param apply fun(any): any
|
||||
---@param fragment evolved.entity
|
||||
---@return integer applied_count
|
||||
function registry.query_apply(query, apply, fragment)
|
||||
local chunks = __execution_stack_acquire()
|
||||
|
||||
for chunk in registry.query_execute(query) do
|
||||
chunks[#chunks + 1] = chunk
|
||||
end
|
||||
|
||||
local applied_count = 0
|
||||
|
||||
for i = 1, #chunks do
|
||||
local applied = registry.chunk_apply(chunks[i], apply, fragment)
|
||||
applied_count = applied_count + applied
|
||||
end
|
||||
|
||||
__execution_stack_release(chunks)
|
||||
return applied_count
|
||||
end
|
||||
|
||||
---@param entity evolved.entity
|
||||
---@param fragment evolved.entity
|
||||
---@param component any
|
||||
@@ -1406,7 +1332,6 @@ evolved_entity_mt.has_all = registry.has_all
|
||||
evolved_entity_mt.has_any = registry.has_any
|
||||
|
||||
evolved_entity_mt.set = registry.set
|
||||
evolved_entity_mt.apply = registry.apply
|
||||
evolved_entity_mt.assign = registry.assign
|
||||
evolved_entity_mt.insert = registry.insert
|
||||
evolved_entity_mt.remove = registry.remove
|
||||
@@ -1434,7 +1359,6 @@ function evolved_query_mt:__tostring()
|
||||
end
|
||||
|
||||
evolved_query_mt.set = registry.query_set
|
||||
evolved_query_mt.apply = registry.query_apply
|
||||
evolved_query_mt.assign = registry.query_assign
|
||||
evolved_query_mt.insert = registry.query_insert
|
||||
evolved_query_mt.remove = registry.query_remove
|
||||
@@ -1463,7 +1387,6 @@ function evolved_chunk_mt:__tostring()
|
||||
end
|
||||
|
||||
evolved_chunk_mt.set = registry.chunk_set
|
||||
evolved_chunk_mt.apply = registry.chunk_apply
|
||||
evolved_chunk_mt.assign = registry.chunk_assign
|
||||
evolved_chunk_mt.insert = registry.chunk_insert
|
||||
evolved_chunk_mt.remove = registry.chunk_remove
|
||||
|
||||
Reference in New Issue
Block a user