-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwacky-ecs.lua
308 lines (273 loc) · 6.97 KB
/
wacky-ecs.lua
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
local PATH = (...):match('(.-)[^%.]+$')
local HashList = require(PATH .. '.data.hashlist')
local ArrayList = require(PATH .. '.data.arraylist')
local Vector = require(PATH .. '.data.vector')
local VectorLight = require(PATH .. '.util.vector-light')
local Pool = require(PATH .. '.util.pool')
local Debug = require(PATH .. '.util.Debug')
-- jprof
PROF_CAPTURE = false
prof = require(PATH .. '.jprof')
local Entity, Component, System, World = {}, {}, {}, {}
-- multiple filters per system
-- enable/disable systems?
-- STUFF
local function deepCopy(table)
local copy = {}
for k,v in pairs(table) do
if type(v) == 'table' then v = deepCopy(v) end
copy[k] = v
end
return copy
end
local idCounter = 0
local function getId()
idCounter = idCounter + 1
return idCounter
end
-- ENTITY
Entity.__index = Entity
function Entity.new(world)
if world and getmetatable(world) ~= World then error("Entity.new() - Passed variable is not of type World") end
local e = setmetatable({ __id = getId() }, Entity)
if world then world:addEntity(e) end
return e
end
function Entity:add(component, ...)
self[component] = Component.__get(component, ...)
if self.__world then
self.__world:__updateEntity(self)
end
return self
end
function Entity:remove(component)
self[component] = nil
if self.__world then
self.__world:__updateEntity(self)
end
end
function Entity:has(filter)
if not filter then return false end
if type(filter) == 'string' then
return self[filter] ~= nil
else
for _,comp in ipairs(filter) do
if self[comp] == nil then
return false
end
end
return true
end
end
function Entity:getComponents()
local components = {}
for k, v in pairs(self) do
if k:sub(1, 2) ~= '__' then components[k] = v end
end
return components
end
function Entity:destroy()
if self.__world then self.__world:removeEntity(self) end
end
function Entity:getWorld()
return self.__world
end
-- COMPONENT
function Component.new(name, initFunc)
Component[name] = initFunc or 'nil'
end
function Component.__get(name, ...)
if type(Component[name]) == 'function' then
return Component[name](...)
end
return {}
end
-- SYSTEM
System.__index = System
function System.new(name, filter, events, singleInstance)
local s = { __filter = filter, __singleInstance = singleInstance or false }
System[name] = setmetatable(s, System)
if events then
for event, func in pairs(events) do
s[event] = func
end
end
return s
end
function System:getFilter()
return self.__filter
end
function System:getWorld()
return self.__world
end
function System.__createInstance(name)
if not System[name] then return nil end
if System[name].__singleInstance then
return System[name]
else
return setmetatable(deepCopy(System[name]), System)
end
end
-- WORLD
World.__index = World
function World.new()
return setmetatable({
entities = HashList.new(),
__add = HashList.new(),
__remove = HashList.new(),
__update = HashList.new(),
systems = ArrayList.new(),
systemNames = {},
entityCount = 0
}, World)
end
function World:clear(filter)
if filter then
local matches = self:getEntities(filter)
for _,entity in ipairs(matches) do
self:removeEntity(entity)
end
else
for _, system in ipairs(self.systems) do
system.cache:clear()
end
self.entities:clear()
end
return self
end
function World:commit()
for _,entity in ipairs(self.__add) do
self.entities:add(entity)
entity.__world = self
for _,worldSystem in ipairs(self.systems) do
local system = worldSystem.system
if system.__filter and entity:has(system.__filter) then
worldSystem.cache:add(entity)
if type(system.wacky_entity_add) == 'function' then
system:wacky_entity_add(entity)
end
end
end
end
self.__add:clear()
for _,entity in ipairs(self.__remove) do
self.entities:remove(entity)
entity.__world = nil
for _,worldSystem in ipairs(self.systems) do
local system = worldSystem.system
worldSystem.cache:remove(entity)
if type(system.wacky_entity_remove) == 'function' then
system:wacky_entity_remove(entity)
end
end
end
self.__remove:clear()
for _,entity in ipairs(self.__update) do
for _,worldSystem in ipairs(self.systems) do
local system = worldSystem.system
if entity:has(system.__filter) then
worldSystem.cache:add(entity)
if type(system.wacky_entity_add) == 'function' then
system:wacky_entity_add(entity)
end
else
system.cache:remove(entity)
if type(system.wacky_entity_remove) == 'function' then
system:wacky_entity_remove(entity)
end
end
end
end
self.__update:clear()
end
function World:addSystem(name)
local system = System.__createInstance(name)
if not system then error("World:addSystem() - System not found: " .. name) end
system.__world = self
local idx = self.systems:add({
system = system,
name = name,
cache = HashList.new(self:getEntities(system.__filter))
})
self.systemNames[name] = idx
if type(system.wacky_init) == 'function' then
system:wacky_init(self)
end
return self
end
function World:removeSystem(name)
local idx = self.systemNames[name]
self.systems:remove(idx)
self.systems:compact(true)
self.systemNames[name] = nil
end
function World:getEntityCount()
return self.entities.size
end
function World:addEntity(entity)
if not entity then error("World:addEntity() - Entity is nil") end
self.__add:add(entity, true)
return self
end
function World:removeEntity(entity)
if not entity then error("World:removeEntity() - Entity is nil") end
self.__remove:add(entity, true)
return self
end
function World:__updateEntity(entity)
self.__update:add(entity, true)
end
function World:getEntities(filter)
local matches = {}
local i = 1
if not filter then
for id,entity in ipairs(self.entities) do
matches[i] = entity
i = i + 1
end
else
for id, entity in ipairs(self.entities) do
if entity:has(filter) then
matches[i] = entity
i = i + 1
end
end
end
return matches
end
function World:hasSystem(name)
return self.systemNames[name] ~= nil
end
function World:getSystem(name)
local idx = self.systemNames[name]
if not idx then error("World:getSystem() - System not found: " .. name) end
return self.systems:get(idx).system
end
function World:call(event, ...)
prof.push(event)
for _, worldSystem in ipairs(self.systems) do
local system = worldSystem.system
if type(system[event]) == 'function' then
prof.push(worldSystem.name)
system[event](system, worldSystem.cache, ...)
prof.pop(worldSystem.name)
end
end
prof.pop(event)
end
return {
Entity = Entity,
Component = Component,
System = System,
World = World,
Data = {
HashList = HashList,
ArrayList = ArrayList,
Vector = Vector
},
Util = {
VectorLight = VectorLight,
Pool = Pool,
Debug = Debug
}
}