mirror of
https://github.com/BlackMATov/evolved.lua.git
synced 2025-12-14 12:10:23 +07:00
pass old_component to on_set/assign hooks
This commit is contained in:
@@ -394,68 +394,106 @@ do
|
||||
local set_count = 0
|
||||
local assign_count = 0
|
||||
local insert_count = 0
|
||||
local remove_count = 0
|
||||
|
||||
local last_set_component = nil
|
||||
local last_assign_component = nil
|
||||
local last_insert_component = nil
|
||||
local last_set_new_component = nil
|
||||
local last_set_old_component = nil
|
||||
local last_assign_new_component = nil
|
||||
local last_assign_old_component = nil
|
||||
local last_insert_new_component = nil
|
||||
local last_remove_old_component = nil
|
||||
|
||||
evo.set(f, evo.ON_SET, function(entity, fragment, component)
|
||||
evo.set(f, evo.ON_SET, function(entity, fragment, new_component, old_component)
|
||||
assert(entity == e)
|
||||
assert(fragment == f)
|
||||
set_count = set_count + 1
|
||||
last_set_component = component
|
||||
last_set_new_component = new_component
|
||||
last_set_old_component = old_component
|
||||
end)
|
||||
|
||||
evo.set(f, evo.ON_ASSIGN, function(entity, fragment, component)
|
||||
evo.set(f, evo.ON_ASSIGN, function(entity, fragment, new_component, old_component)
|
||||
assert(entity == e)
|
||||
assert(fragment == f)
|
||||
assign_count = assign_count + 1
|
||||
last_assign_component = component
|
||||
last_assign_new_component = new_component
|
||||
last_assign_old_component = old_component
|
||||
end)
|
||||
|
||||
evo.set(f, evo.ON_INSERT, function(entity, fragment, component)
|
||||
evo.set(f, evo.ON_INSERT, function(entity, fragment, new_component)
|
||||
assert(entity == e)
|
||||
assert(fragment == f)
|
||||
insert_count = insert_count + 1
|
||||
last_insert_component = component
|
||||
last_insert_new_component = new_component
|
||||
end)
|
||||
|
||||
evo.set(f, evo.ON_REMOVE, function(entity, fragment, old_component)
|
||||
assert(entity == e)
|
||||
assert(fragment == f)
|
||||
remove_count = remove_count + 1
|
||||
last_remove_old_component = old_component
|
||||
end)
|
||||
|
||||
assert(evo.insert(e, f, 21))
|
||||
assert(set_count == 1)
|
||||
assert(assign_count == 0)
|
||||
assert(insert_count == 1)
|
||||
assert(last_insert_component == 21)
|
||||
assert(remove_count == 0)
|
||||
assert(last_set_old_component == nil)
|
||||
assert(last_set_new_component == 21)
|
||||
assert(last_insert_new_component == 21)
|
||||
|
||||
assert(evo.assign(e, f, 42))
|
||||
assert(set_count == 2)
|
||||
assert(assign_count == 1)
|
||||
assert(insert_count == 1)
|
||||
assert(last_assign_component == 42)
|
||||
assert(remove_count == 0)
|
||||
assert(last_set_new_component == 42)
|
||||
assert(last_set_old_component == 21)
|
||||
assert(last_assign_new_component == 42)
|
||||
assert(last_assign_old_component == 21)
|
||||
|
||||
assert(evo.assign(e, f, 43))
|
||||
assert(set_count == 3)
|
||||
assert(assign_count == 2)
|
||||
assert(insert_count == 1)
|
||||
assert(last_assign_component == 43)
|
||||
assert(remove_count == 0)
|
||||
assert(last_set_new_component == 43)
|
||||
assert(last_set_old_component == 42)
|
||||
assert(last_assign_new_component == 43)
|
||||
assert(last_assign_old_component == 42)
|
||||
|
||||
evo.clear(e)
|
||||
assert(set_count == 3)
|
||||
assert(assign_count == 2)
|
||||
assert(insert_count == 1)
|
||||
assert(remove_count == 1)
|
||||
assert(last_remove_old_component == 43)
|
||||
|
||||
evo.set(e, f, 44)
|
||||
assert(set_count == 4)
|
||||
assert(assign_count == 2)
|
||||
assert(insert_count == 2)
|
||||
assert(last_set_component == 44)
|
||||
assert(last_insert_component == 44)
|
||||
assert(remove_count == 1)
|
||||
assert(last_set_new_component == 44)
|
||||
assert(last_set_old_component == nil)
|
||||
assert(last_insert_new_component == 44)
|
||||
|
||||
evo.set(e, f, 45)
|
||||
assert(set_count == 5)
|
||||
assert(assign_count == 3)
|
||||
assert(insert_count == 2)
|
||||
assert(last_set_component == 45)
|
||||
assert(last_assign_component == 45)
|
||||
assert(remove_count == 1)
|
||||
assert(last_set_new_component == 45)
|
||||
assert(last_set_old_component == 44)
|
||||
assert(last_assign_new_component == 45)
|
||||
assert(last_assign_old_component == 44)
|
||||
|
||||
evo.destroy(e)
|
||||
assert(set_count == 5)
|
||||
assert(assign_count == 3)
|
||||
assert(insert_count == 2)
|
||||
assert(remove_count == 2)
|
||||
assert(last_remove_old_component == 45)
|
||||
end
|
||||
|
||||
do
|
||||
|
||||
29
evolved.lua
29
evolved.lua
@@ -161,28 +161,29 @@ end
|
||||
|
||||
---@param entity evolved.entity
|
||||
---@param fragment evolved.fragment
|
||||
---@param component evolved.component
|
||||
local function __on_assign(entity, fragment, component)
|
||||
---@param new_component evolved.component
|
||||
---@param old_component evolved.component
|
||||
local function __on_assign(entity, fragment, new_component, old_component)
|
||||
local on_set, on_assign = evolved.get(fragment, evolved.ON_SET, evolved.ON_ASSIGN)
|
||||
if on_set then on_set(entity, fragment, component) end
|
||||
if on_assign then on_assign(entity, fragment, component) end
|
||||
if on_set then on_set(entity, fragment, new_component, old_component) end
|
||||
if on_assign then on_assign(entity, fragment, new_component, old_component) end
|
||||
end
|
||||
|
||||
---@param entity evolved.entity
|
||||
---@param fragment evolved.fragment
|
||||
---@param component evolved.component
|
||||
local function __on_insert(entity, fragment, component)
|
||||
---@param new_component evolved.component
|
||||
local function __on_insert(entity, fragment, new_component)
|
||||
local on_set, on_insert = evolved.get(fragment, evolved.ON_SET, evolved.ON_INSERT)
|
||||
if on_set then on_set(entity, fragment, component) end
|
||||
if on_insert then on_insert(entity, fragment, component) end
|
||||
if on_set then on_set(entity, fragment, new_component) end
|
||||
if on_insert then on_insert(entity, fragment, new_component) end
|
||||
end
|
||||
|
||||
---@param entity evolved.entity
|
||||
---@param fragment evolved.fragment
|
||||
---@param component evolved.component
|
||||
local function __on_remove(entity, fragment, component)
|
||||
---@param old_component evolved.component
|
||||
local function __on_remove(entity, fragment, old_component)
|
||||
local on_remove = evolved.get(fragment, evolved.ON_REMOVE)
|
||||
if on_remove then on_remove(entity, fragment, component) end
|
||||
if on_remove then on_remove(entity, fragment, old_component) end
|
||||
end
|
||||
|
||||
---
|
||||
@@ -688,8 +689,9 @@ function evolved.set(entity, fragment, component, ...)
|
||||
|
||||
if old_chunk == new_chunk then
|
||||
local old_chunk_fragment_components = old_chunk.__components[fragment]
|
||||
local old_component = old_chunk_fragment_components[old_place]
|
||||
old_chunk_fragment_components[old_place] = component
|
||||
__on_assign(entity, fragment, component)
|
||||
__on_assign(entity, fragment, component, old_component)
|
||||
return true, false
|
||||
end
|
||||
|
||||
@@ -747,8 +749,9 @@ function evolved.assign(entity, fragment, component, ...)
|
||||
end
|
||||
|
||||
local old_chunk_fragment_components = old_chunk.__components[fragment]
|
||||
local old_component = old_chunk_fragment_components[old_place]
|
||||
old_chunk_fragment_components[old_place] = component
|
||||
__on_assign(entity, fragment, component)
|
||||
__on_assign(entity, fragment, component, old_component)
|
||||
return true, false
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user