-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsceneManager.lua
More file actions
77 lines (60 loc) · 1.77 KB
/
sceneManager.lua
File metadata and controls
77 lines (60 loc) · 1.77 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
local sceneManager = {}
local emptyFunc = function()
end
local screenStack = {}
function sceneManager._validateScene(s)
s = s or {}
s.load = s.load or emptyFunc
s.unload = s.unload or emptyFunc
s.update = s.update or emptyFunc
s.draw = s.draw or emptyFunc
s.resize = s.resize or emptyFunc
s.keypressed = s.keypressed or emptyFunc
s.textinput = s.textinput or emptyFunc
s.mousepressed = s.mousepressed or emptyFunc
s.name = s.name or nil
s.comingBack = s.comingBack or emptyFunc
return s
end
function sceneManager.changeScene(s, option)
s = s or error("ChangeScene requires a Scene")
for i = 1, #screenStack do
screenStack[i].unload()
end
screenStack = {s}
sceneManager.currentScene = sceneManager._validateScene(s)
sceneManager.currentScene.load(option)
end
function sceneManager.pushScene(s, option)
s = s or error("pushScene requires a scene")
table.insert(screenStack, s)
sceneManager.currentScene = sceneManager._validateScene(s)
sceneManager.currentScene.load(option)
end
function sceneManager.popScene(load)
if #screenStack < 2 then
error("popScene requires at least one extra scene to go back to")
end
sceneManager.currentScene.unload()
table.remove(screenStack, #screenStack)
sceneManager.currentScene = screenStack[#screenStack]
sceneManager.currentScene.comingBack()
if load then
sceneManager.currentScene.load()
end
end
function sceneManager.getCurrentScene()
return sceneManager.currentScene
end
function sceneManager.draw()
for i = 1, #screenStack do
print(screenStack[i].name)
screenStack[i].draw()
end
end
function sceneManager.resetScene()
sceneManager.currentScene.unload()
sceneManager.currentScene.load()
end
sceneManager.currentScene = sceneManager._validateScene(nil)
return sceneManager