112 lines
3.0 KiB
Lua
112 lines
3.0 KiB
Lua
local engine = {}
|
|
|
|
function engine.null() end
|
|
|
|
engine.elements = {}
|
|
engine.elementsByType = {}
|
|
engine.elementTypes = {}
|
|
engine.element = {}
|
|
|
|
engine.paths = { mainScriptPath .. "/engine" }
|
|
function engine.path(path)
|
|
for i,v in pairs(engine.paths) do
|
|
local fpath = v .. "/" .. path
|
|
local f = io.open(fpath)
|
|
if f ~= nil then
|
|
io.close(f)
|
|
return fpath
|
|
end
|
|
end
|
|
error("File not found: " .. path)
|
|
end
|
|
|
|
function engine.element.create(elementType)
|
|
if engine.elementTypes[elementType] == nil then
|
|
engine.elementTypes[elementType] = {
|
|
_type = elementType,
|
|
_references = 0
|
|
}
|
|
|
|
engine.elementsByType[elementType] = {}
|
|
assert(loadfile(engine.path(elementType.. ".lua")))(engine.elementTypes[elementType])
|
|
end
|
|
|
|
engine.elementTypes[elementType]._references = engine.elementTypes[elementType]._references + 1
|
|
|
|
local id = 1
|
|
while true do
|
|
id = math.random(1,2147483647)
|
|
if engine.elements[id] == nil then break end
|
|
end
|
|
|
|
local element = {
|
|
memlo = {
|
|
_type = engine.elementTypes[elementType],
|
|
_children = {},
|
|
_parents = {}
|
|
},
|
|
memhi = {
|
|
_id = id,
|
|
_type = elementType,
|
|
_children = {},
|
|
_childrenCount = 0,
|
|
_parents = {},
|
|
_parentsCount = 0
|
|
}
|
|
}
|
|
|
|
engine.elementTypes[elementType].create(element)
|
|
engine.elements[id] = element
|
|
engine.elementsByType[elementType][id] = element
|
|
return element
|
|
end
|
|
|
|
function engine.element.attach(child,parent)
|
|
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
|
|
|
|
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
|
|
|
|
element.memlo._type.destroy(element)
|
|
engine.elements[element.memhi._id] = nil
|
|
engine.elementsByType[element.memhi._type][element.memhi._id] = nil
|
|
|
|
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
|
|
end
|
|
|
|
return engine |