local _class = {}
ClassType = {
class = 1,
instance = 2,
}
function systemclass(classname, super)
assert(type(classname) == "string" and #classname > 0)
-- 生成一个类类型
local class_type = {}
-- 在创建对象的时候自动调用
class_type.__cname = classname
class_type.__ctype = ClassType.class
class_type.super = super
class_type.new = function(...)
-- 生成一个类对象
local obj = {}
obj._class_type = class_type
obj.__ctype = ClassType.instance
obj.__cname = class_type.__cname
obj.__valid = true
if device.isLocalEditor then
setmetatable(obj, {
__index = _class[class_type],
__newindex = function(t, k, v)
if type(v) ~= "function" then
error(t.__cname.." System不允许定义任何变量,只允许定义方法, 请在对应的Manager/Component中定义:"..k.."\n"..table.dump(t), 2)
end
end
})
else
setmetatable(obj, {
__index = _class[class_type]
})
end
-- 调用初始化方法
do
local create
create = function(c, ...)
if c.super then
create(c.super, ...)
end
if c.ctor then
c.ctor(obj, ...)
end
end
create(class_type, ...)
end
-- 注册一个delete方法
obj.delete = function(self)
local now_super = self._class_type
while now_super ~= nil do
if now_super.dtor then
now_super.dtor(self)
end
now_super = now_super.super
end
self.__valid = false
end
return obj
end
local vtbl = {}
assert(_class[class_type] == nil, "Aready defined class : ", classname)
_class[class_type] = vtbl
setmetatable(class_type, {
__newindex = function(t,k,v)
if device.isLocalEditor then
if type(v) ~= "function" then
error("System不允许定义任何变量,只允许定义方法, 请在对应的Manager/Component中定义 "..k..(v or ""))
end
end
vtbl[k] = v
end,
__index = vtbl,
})
if super then
setmetatable(vtbl, {
__index = function(t,k)
return _class[super][k]
end
})
end
return class_type
end
local BaseWorld = systemclass("BaseWorld")
return BaseWorld
local base = BaseWorld
local BattleWorld = systemclass("BattleWorld",base)
return BattleWorld
local world = BattleWorld.new()