summaryrefslogtreecommitdiff
path: root/Data/BuiltIn/Libraries/GameLab/Class.lua
blob: 96865eba446096e75104d7c0b7d80c03b361b564 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-- Declare class

-- 保留方法:New, Ctor, Is
-- 保留字段:_type, _base

local TrackClassInstances = false

local _class = function (name, ctor)
    local c = {}

    local isstring = type(name) == "string"
    local pkg = "unknown"
    local short = "unknown" 
    if isstring then 
        pkg = string.match(name, "^(.+)%.%w+$")
        short = string.match(name, "%.*(%w+)$")
    end 
    
    c._type = { 
        mode = "lua",
        name = short,
        package = pkg,
        fullName = name
    }

    c.__index = c
    
    c.New = function(...) 
        local obj = {} 
        setmetatable(obj, c)
        obj:Ctor(...)
        return obj
    end

    c.Is = function(self, klass) 
        local m = getmetatable(self)
        while m do 
            if m == klass then 
                return true 
            end
            m = m._base
        end
        return false
    end 

    if ctor and type(ctor) == "function" then 
        c.Ctor = ctor
    end 

    local mt = {} 

    mt.__call = function(dummy, ...)
        return c.New(...)
    end 

    setmetatable(c, mt)

    return c
end

local class = function(name, ctor)
    local c = _class(name, ctor)
    
    c.Extend = function(childName)
        local child = _class(childName) 
        if c then 
            setmetatable(child, c)
            child._base = c
        end 
        return child
    end

    c._base = nil

    return c
end  

GameLab.Class = class

return class