Allow elements to have multiple parents

This commit is contained in:
Fierelier 2022-12-01 23:59:42 +01:00
parent cccc8d1b4d
commit 3460748bc8
1 changed files with 33 additions and 25 deletions

View File

@ -41,14 +41,17 @@ function engine.element.create(elementType)
local element = {
memlo = {
_type = engine.elementTypes[elementType],
_children = {},
_type = engine.elementTypes[elementType]
_parents = {}
},
memhi = {
_id = id,
_type = elementType,
_children = {},
_childrenCount = 0
_childrenCount = 0,
_parents = {},
_parentsCount = 0
}
}
@ -59,43 +62,48 @@ function engine.element.create(elementType)
end
function engine.element.attach(child,parent)
if child.memlo._parent ~= nil then
child.memlo._parent.memlo._children[child.memhi._id] = nil
child.memlo._parent.memhi._children[child.memhi._id] = nil
child.memlo._parent.memhi._childrenCount = child.memlo._parent.memhi._childrenCount - 1
end
child.memlo._parent = parent
if parent == nil then
child.memhi._parent = nil
return
end
child.memhi._parent = parent.memhi._id
parent.memhi._children[child.memhi._id] = true
if child.memlo._parents[parent.memhi._id] ~= nil then return end
child.memlo._parents[parent.memhi._id] = parent
child.memhi._parents[parent.memhi._id] = true
child.memhi._parentsCount = child.memhi._parentsCount + 1
parent.memlo._children[child.memhi._id] = child
parent.memhi._children[child.memhi._id] = true
parent.memhi._childrenCount = parent.memhi._childrenCount + 1
end
function engine.element.detach(child,parent)
if child.memlo._parents[parent.memhi._id] == nil then return end
child.memlo._parents[parent.memhi._id] = nil
child.memhi._parents[parent.memhi._id] = nil
child.memhi._parentsCount = child.memhi._parentsCount - 1
parent.memlo._children[child.memhi._id] = nil
parent.memhi._children[child.memhi._id] = nil
parent.memhi._childrenCount = parent.memhi._childrenCount - 1
end
function engine.element.destroy(element,destroyChildren)
if destroyChildren == nil then destroyChildren = true end
engine.element.attach(element)
if destroyChildren == true then
if element.memhi._childrenCount > 0 then
for i,v in pairs(element.memlo._children) do
engine.element.destroy(v)
for _,parent in pairs(element.memlo._parents) do
engine.element.detach(element,parent)
end
for _,child in pairs(element.memlo._children) do
engine.element.detach(child,element)
if destroyChildren == true then
if child.memhi._parentsCount == 0 then
engine.element.destroy(child,true)
end
end
end
engine.elementTypes[element.memhi._type].destroy(element)
element.memlo._type.destroy(element)
engine.elements[element.memhi._id] = nil
engine.elementsByType[element.memhi._type][element.memhi._id] = nil
engine.elementTypes[element.memhi._type]._references = engine.elementTypes[element.memhi._type]._references - 1
if engine.elementTypes[element.memhi._type]._references == 0 then
engine.elementTypes[element.memhi._type].unload()
element.memlo._type._references = engine.elementTypes[element.memhi._type]._references - 1
if element.memlo._type._references == 0 then
element.memlo._type.unload()
engine.elementTypes[element.memhi._type] = nil
engine.elementsByType[element.memhi._type] = nil
end