-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.lua
52 lines (45 loc) · 1.11 KB
/
entity.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
local utils = require "utils"
local setmetatable = setmetatable
local table = table
local pairs = pairs
local entity = {}
setfenv (1, entity)
Entity = utils.defineClass(
function (self)
self.componentList = {}
self.componentId = 1
end
)
function Entity:addComponent (component)
self.componentList[self.componentId] = component
component.componentId = self.componentId
self.componentId = self.componentId + 1
if component.added then
component:added(self)
end
return component
end
function Entity:removeComponent (component)
if component.componentId then
self.componentList[component.componentId] = nil
component.componentId = nil
if component.removed then
component:removed()
end
end
end
function Entity:update()
for id, component in pairs(self.componentList) do
if component.update then
component:update()
end
end
end
function Entity:draw()
for id, component in pairs(self.componentList) do
if component.draw then
component:draw()
end
end
end
return entity