-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjBase.lua
More file actions
142 lines (122 loc) · 3.32 KB
/
ObjBase.lua
File metadata and controls
142 lines (122 loc) · 3.32 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
local _NOOP = function () end
local ObjBase = Class.new {
persistent = false,
world = function () return Game.world end,
type = "ObjBase",
-- overridable methods
create = _NOOP,
draw = _NOOP,
onCollide = _NOOP,
postCollide = _NOOP,
}
function ObjBase:addModule( newModule )
if not self.modules then self.modules = {} end
if not self.removableModules then self.removableModules = {} end
if not self.allFuncts then
self.allFuncts = {}
self.allFuncts["create"]= {}
self.allFuncts["create"]["default"] = self.create
self.allFuncts["tick"]= {}
self.allFuncts["tick"]["default"] = self.default
self.allFuncts["destroy"]= {}
self.allFuncts["destroy"]["default"] = self.destroy
self.allFuncts["onRemove"]= {}
self.allFuncts["onRemove"]["default"] = self.onRemove
end
if not self.overRideFuncts then
self.overRideFuncts = {"create","tick","destroy","onRemove"}
end
local modName = newModule.type
if newModule.dependencies then
for i,v in ipairs(newModule.dependencies) do
if not self:hasModule(v) then
-- lume.trace("module: ",v,"added through dependency to module: ", modName)
local dependencyPath = "modules." .. v
self:addModule(require(dependencyPath))
end
end
end
if newModule.trackFunctions then
for i,v in ipairs(newModule.trackFunctions) do
if not self.allFuncts[v] then
self:trackFunction(v)
end
end
end
for i,v in ipairs(self.overRideFuncts) do
if newModule[v] then
if not self.allFuncts[v] then
self.allFuncts[v] = {}
end
self.allFuncts[v][modName] = newModule[v]
end
end
Class.include(self,newModule)
if newModule.create then
newModule.create(self)
end
if newModule.onRemove then
self.allFuncts["onRemove"][modName] = newModule.onRemove
end
for i,v in ipairs(self.overRideFuncts) do
local function iterateFunctions( self, ... )
local returnVals = {}
if self.allFuncts[v] then
for k,funct in pairs(self.allFuncts[v]) do
if funct then
local ret = funct(self,...)
if ret then
table.insert(returnVals,ret)
end
end
end
end
return returnVals
end
--local funct = lume.fn(iterateFunctions, self)
self[v] = iterateFunctions
end
self.modules[modName] = true
if newModule.removable then
self.removableModules[modName] = true
end
end
function ObjBase:hasModule( modName )
return self.modules[modName]
end
function ObjBase:removeModule(modName )
if self.allFuncts["onRemove"] and self.allFuncts["onRemove"][modName] then
self.allFuncts["onRemove"][modName](self)
end
for k,v in pairs(self.allFuncts) do
v[modName] = nil
end
self.modules[modName] = nil
self.removableModules[modName] = nil
end
function ObjBase:getModules()
return self.modules
end
function ObjBase:getAllRemovableModules()
local newTable = {}
for k,v in pairs(self.removableModules) do
table.insert(newTable,k)
end
return newTable
end
function ObjBase:trackFunction( functionName )
table.insert(self.overRideFuncts,functionName)
if self[functionName] and self[functionName] ~= _NOOP then
if not self.allFuncts[functionName] then
self.allFuncts[functionName] = {}
end
self.allFuncts[functionName]["default"] = self[functionName]
end
local function iterateFunctions( self, ... )
for k,funct in pairs(self.allFuncts[functionName]) do
funct(self,...)
end
end
self[functionName] = iterateFunctions
end
return ObjBase