Compare commits

...

3 Commits

Author SHA1 Message Date
Fierelier cccc8d1b4d Add basic script element 2022-11-26 19:27:58 +01:00
Fierelier 5bfe0ed61d Add element.memlo._type 2022-11-26 19:27:13 +01:00
Fierelier 31e446f232 Add engine.elementsByType 2022-11-26 19:26:43 +01:00
2 changed files with 34 additions and 1 deletions

27
engine/script.lua Normal file
View File

@ -0,0 +1,27 @@
local self = ...
local engine = require("engine")
function self.create(element)
element.memhiOriginal = element.memhi
element.memhi = {}
local mt = {
__index = function(t,k)
return element.memhiOriginal[k]
end,
__newindex = function(t,k,v)
element.memhiOriginal[k] = v
if k == "file" then
if element.memlo.script ~= nil then self.destroy(element) end
element.memlo.script = loadfile(engine.path(self._type.. "/" ..v))(element)
end
end
}
setmetatable(element.memhi,mt)
end
function self.destroy(element)
element.memlo.script.destroy(element)
end
function self.unload()
end

View File

@ -3,6 +3,7 @@ local engine = {}
function engine.null() end
engine.elements = {}
engine.elementsByType = {}
engine.elementTypes = {}
engine.element = {}
@ -26,6 +27,7 @@ function engine.element.create(elementType)
_references = 0
}
engine.elementsByType[elementType] = {}
assert(loadfile(engine.path(elementType.. ".lua")))(engine.elementTypes[elementType])
end
@ -39,7 +41,8 @@ function engine.element.create(elementType)
local element = {
memlo = {
_children = {}
_children = {},
_type = engine.elementTypes[elementType]
},
memhi = {
_id = id,
@ -51,6 +54,7 @@ function engine.element.create(elementType)
engine.elementTypes[elementType].create(element)
engine.elements[id] = element
engine.elementsByType[elementType][id] = element
return element
end
@ -87,11 +91,13 @@ function engine.element.destroy(element,destroyChildren)
engine.elementTypes[element.memhi._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()
engine.elementTypes[element.memhi._type] = nil
engine.elementsByType[element.memhi._type] = nil
end
end