-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSceneManager.lua
More file actions
86 lines (72 loc) · 1.93 KB
/
Copy pathSceneManager.lua
File metadata and controls
86 lines (72 loc) · 1.93 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
-- manages different game scenes and transitions between them
SceneManager = {}
SceneManager.__index = SceneManager
function SceneManager:new(ctx)
return setmetatable({
scenes = {},
sceneStack = {}, -- stack to manage overlay scenes
paused = false, -- if paused, only update the top scene
ctx = ctx
}, self)
end
function SceneManager:addScene(scene)
self.scenes[scene.name] = scene
scene.sceneManager = self
end
-- change to a new scene, clearing the scene stack
function SceneManager:changeScene(sceneId)
while #self.sceneStack > 0 do
self:popScene()
end
self:pushScene(sceneId)
end
function SceneManager:pushScene(sceneId)
local scene = self.scenes[sceneId]
if not scene then
error("Scene not found: " .. tostring(sceneId))
end
table.insert(self.sceneStack, scene)
scene:enter()
end
function SceneManager:popScene()
if #self.sceneStack == 0 then
return
end
local scene = table.remove(self.sceneStack)
scene:exit()
end
function SceneManager:getScene(name)
return self.scenes[name]
end
function SceneManager:getCurrentScene()
return self.sceneStack[#self.sceneStack]
end
function SceneManager:update(dt)
if self.paused then
self:getCurrentScene():update(dt)
else
for _, scene in ipairs(self.sceneStack) do
scene:update(dt)
end
end
self:getCurrentScene():updateCursorBlink(dt)
end
function SceneManager:draw()
lg.setColor(COLORS.WHITE)
for _, scene in ipairs(self.sceneStack) do
scene:draw()
end
self:getCurrentScene():drawInputInterface()
end
function SceneManager:keypressed(key)
self:getCurrentScene():keypressed(key)
end
function SceneManager:textinput(t)
self:getCurrentScene():textinput(t)
end
function SceneManager:wheelmoved(x, y)
self:getCurrentScene():wheelmoved(x, y)
end
function SceneManager:pause(val)
self.paused = val
end