]> git.lizzy.rs Git - dragonblocks3d-lua.git/blob - util/objectmgr.lua
31409155d5dd8a06e5daa4b0efa67ae38014d0c3
[dragonblocks3d-lua.git] / util / objectmgr.lua
1 local ObjectRef = {}
2
3 function ObjectRef:init()
4         for _, p in ipairs(self._proto) do
5                 if p ~= ObjectRef and p.init then
6                         p.init(self)
7                 end
8         end
9 end
10
11 function ObjectRef:add_proto(p)
12         table.insert(self._proto, p)
13 end
14
15 ObjectMgr = {}
16
17 ObjectMgr.metatable = {
18         __index = function(t, k)
19                 for _, p in ipairs(t._proto) do
20                         local v = p[k]
21                         if v then
22                                 return v
23                         end
24                 end
25         end,
26         __call = function(t, ...)
27                 return t:_call()
28         end,
29         __tostring = function(t)
30                 return t.serialize and t:serialize() or "<not serializable>"
31         end,
32 }
33
34 function ObjectMgr.create()
35         local o = {}
36         o._proto = {ObjectRef}
37         setmetatable(o, ObjectMgr.metatable)
38         return o
39 end