-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.lua
54 lines (45 loc) · 1.03 KB
/
scene.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
function Scene()
local scene = {
entities = {}
}
function scene:add(e)
table.insert(self.entities, e)
end
function scene:clear()
self.entities = {}
end
function scene:click(mx, my, button)
for _,e in ipairs(self.entities) do
if e.click then e:click(mx, my, button) end
end
end
function scene:declick(mx, my, button)
for _,e in ipairs(self.entities) do
if e.declick then e:declick(mx, my, button) end
end
end
function scene:draw()
for _,e in ipairs(self.entities) do
if e.draw then e:draw() end
end
end
function scene:remove(e)
e.removed = true
end
function scene:update(dtime)
local ents = {}
for _,e in ipairs(self.entities) do
if e.update then e:update(dtime) end
if not e.removed then
table.insert(ents, e)
end
end
self.entities = ents
table.sort(self.entities, function(l, r)
if not l.layer then l.layer = 0 end
if not r.layer then r.layer = 0 end
return l.layer < r.layer
end)
end
return scene
end