set/assign/insert hooks

This commit is contained in:
BlackMATov
2024-12-18 22:44:15 +07:00
parent 782178bdbb
commit 79dc14a250
2 changed files with 100 additions and 10 deletions

View File

@@ -386,3 +386,74 @@ do
assert(evo.get(e, f1) == 42)
assert(evo.get(e, f2) == false)
end
do
local f = evo.id()
local e = evo.id()
local set_count = 0
local assign_count = 0
local insert_count = 0
local last_set_component = nil
local last_assign_component = nil
local last_insert_component = nil
evo.set(f, evo.ON_SET, function(entity, fragment, component)
assert(entity == e)
assert(fragment == f)
set_count = set_count + 1
last_set_component = component
end)
evo.set(f, evo.ON_ASSIGN, function(entity, fragment, component)
assert(entity == e)
assert(fragment == f)
assign_count = assign_count + 1
last_assign_component = component
end)
evo.set(f, evo.ON_INSERT, function(entity, fragment, component)
assert(entity == e)
assert(fragment == f)
insert_count = insert_count + 1
last_insert_component = 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(evo.assign(e, f, 42))
assert(set_count == 2)
assert(assign_count == 1)
assert(insert_count == 1)
assert(last_assign_component == 42)
assert(evo.assign(e, f, 43))
assert(set_count == 3)
assert(assign_count == 2)
assert(insert_count == 1)
assert(last_assign_component == 43)
evo.clear(e)
assert(set_count == 3)
assert(assign_count == 2)
assert(insert_count == 1)
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)
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)
end

View File

@@ -119,6 +119,10 @@ end
evolved.DEFAULT = __acquire_id()
evolved.CONSTRUCT = __acquire_id()
evolved.ON_SET = __acquire_id()
evolved.ON_ASSIGN = __acquire_id()
evolved.ON_INSERT = __acquire_id()
---
---
---
@@ -131,19 +135,30 @@ evolved.CONSTRUCT = __acquire_id()
---@param ... any construct additional parameters
---@return evolved.component
local function __construct(entity, fragment, component, ...)
local construct = evolved.get(fragment, evolved.CONSTRUCT)
if construct ~= nil then
component = construct(entity, component, ...)
end
if component == nil then
component = evolved.get(fragment, evolved.DEFAULT)
end
local default, construct = evolved.get(fragment, evolved.DEFAULT, evolved.CONSTRUCT)
if construct ~= nil then component = construct(entity, component, ...) end
if component == nil then component = default end
return component == nil and true or component
end
---@param entity evolved.entity
---@param fragment evolved.fragment
---@param component evolved.component
local function __on_assign(entity, fragment, 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
end
---@param entity evolved.entity
---@param fragment evolved.fragment
---@param component evolved.component
local function __on_insert(entity, fragment, 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
end
---
---
---
@@ -590,6 +605,7 @@ function evolved.set(entity, fragment, component, ...)
if old_chunk == new_chunk then
local old_chunk_fragment_components = old_chunk.__components[fragment]
old_chunk_fragment_components[old_place] = component
__on_assign(entity, fragment, component)
return true
end
@@ -635,6 +651,7 @@ function evolved.set(entity, fragment, component, ...)
__structural_changes = __structural_changes + 1
__on_insert(entity, fragment, component)
return true
end
@@ -667,6 +684,7 @@ function evolved.assign(entity, fragment, component, ...)
chunk_fragment_components[place] = component
__on_assign(entity, fragment, component)
return true
end
@@ -736,6 +754,7 @@ function evolved.insert(entity, fragment, component, ...)
__structural_changes = __structural_changes + 1
__on_insert(entity, fragment, component)
return true
end