mirror of
https://github.com/BlackMATov/evolved.lua.git
synced 2025-12-16 14:11:16 +07:00
add on_remove hook
This commit is contained in:
@@ -2,5 +2,4 @@
|
||||
|
||||
## Backlog
|
||||
|
||||
- add on_remove hook
|
||||
|
||||
- add tag-fragment trait
|
||||
@@ -458,6 +458,49 @@ do
|
||||
assert(last_assign_component == 45)
|
||||
end
|
||||
|
||||
do
|
||||
local f1, f2 = evo.id(2)
|
||||
local e = evo.id()
|
||||
|
||||
local remove_count = 0
|
||||
local last_removed_component = nil
|
||||
|
||||
evo.set(f1, evo.ON_REMOVE, function(entity, fragment, component)
|
||||
assert(entity == e)
|
||||
assert(fragment == f1)
|
||||
remove_count = remove_count + 1
|
||||
last_removed_component = component
|
||||
end)
|
||||
|
||||
evo.set(f2, evo.ON_REMOVE, function(entity, fragment, component)
|
||||
assert(entity == e)
|
||||
assert(fragment == f2)
|
||||
remove_count = remove_count + 1
|
||||
last_removed_component = component
|
||||
end)
|
||||
|
||||
assert(evo.insert(e, f1, 42))
|
||||
evo.remove(e, f1, f2)
|
||||
assert(remove_count == 1)
|
||||
assert(last_removed_component == 42)
|
||||
|
||||
assert(evo.insert(e, f1, 42))
|
||||
assert(evo.insert(e, f2, 43))
|
||||
evo.remove(e, f1, f2)
|
||||
assert(remove_count == 3)
|
||||
assert(last_removed_component == 43)
|
||||
|
||||
assert(evo.insert(e, f1, 44))
|
||||
assert(evo.insert(e, f2, 45))
|
||||
evo.clear(e)
|
||||
assert(remove_count == 5)
|
||||
|
||||
assert(evo.insert(e, f1, 46))
|
||||
assert(evo.insert(e, f2, 47))
|
||||
evo.destroy(e)
|
||||
assert(remove_count == 7)
|
||||
end
|
||||
|
||||
do
|
||||
local f = evo.id()
|
||||
local e = evo.id()
|
||||
|
||||
32
evolved.lua
32
evolved.lua
@@ -122,6 +122,7 @@ evolved.CONSTRUCT = __acquire_id()
|
||||
evolved.ON_SET = __acquire_id()
|
||||
evolved.ON_ASSIGN = __acquire_id()
|
||||
evolved.ON_INSERT = __acquire_id()
|
||||
evolved.ON_REMOVE = __acquire_id()
|
||||
|
||||
---
|
||||
---
|
||||
@@ -159,6 +160,14 @@ local function __on_insert(entity, fragment, component)
|
||||
if on_insert then on_insert(entity, fragment, component) end
|
||||
end
|
||||
|
||||
---@param entity evolved.entity
|
||||
---@param fragment evolved.fragment
|
||||
---@param component evolved.component
|
||||
local function __on_remove(entity, fragment, component)
|
||||
local on_remove = evolved.get(fragment, evolved.ON_REMOVE)
|
||||
if on_remove then on_remove(entity, fragment, component) end
|
||||
end
|
||||
|
||||
---
|
||||
---
|
||||
---
|
||||
@@ -787,8 +796,21 @@ function evolved.remove(entity, ...)
|
||||
|
||||
local old_chunk_size = #old_chunk.__entities
|
||||
local old_chunk_entities = old_chunk.__entities
|
||||
local old_chunk_fragments = old_chunk.__fragments
|
||||
local old_chunk_components = old_chunk.__components
|
||||
|
||||
for i = 1, select('#', ...) do
|
||||
local old_f = select(i, ...)
|
||||
if old_chunk_fragments[old_f] then
|
||||
local old_cs = old_chunk_components[old_f]
|
||||
__on_remove(entity, old_f, old_cs[old_place])
|
||||
end
|
||||
end
|
||||
|
||||
if old_chunk ~= __entity_chunks[index] then
|
||||
error('entity structural changes are prohibited in ON_REMOVE hooks', 2)
|
||||
end
|
||||
|
||||
if new_chunk and assert(new_place) then
|
||||
local new_chunk_entities = new_chunk.__entities
|
||||
local new_chunk_components = new_chunk.__components
|
||||
@@ -844,8 +866,18 @@ function evolved.clear(entity)
|
||||
|
||||
local chunk_size = #chunk.__entities
|
||||
local chunk_entities = chunk.__entities
|
||||
local chunk_fragments = chunk.__fragments
|
||||
local chunk_components = chunk.__components
|
||||
|
||||
for f, _ in pairs(chunk_fragments) do
|
||||
local cs = chunk_components[f]
|
||||
__on_remove(entity, f, cs[place])
|
||||
end
|
||||
|
||||
if chunk ~= __entity_chunks[index] then
|
||||
error('entity structural changes are prohibited in ON_REMOVE hooks', 2)
|
||||
end
|
||||
|
||||
if place == chunk_size then
|
||||
chunk_entities[place] = nil
|
||||
|
||||
|
||||
Reference in New Issue
Block a user