-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsceneman.lua
175 lines (143 loc) · 3.29 KB
/
sceneman.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
local sceneman = {}
sceneman.scenes = {}
sceneman.active = nil
-- Create a new scene
function sceneman:newscene(base)
local scene = base or {}
scene.loaded = false
scene.started = false
scene.load = scene.load or function() end
scene.start = scene.start or function() end
scene.update = scene.update or function() end
scene.draw = scene.draw or function() end
scene.tofront = scene.tofront or function() end
scene.toback = scene.toback or function() end
scene.stop = scene.stop or function() end
scene.quit = scene.quit or function() end
function scene:isloaded()
return self.loaded
end
function scene:isactive()
return sceneman.active == self
end
function scene:isstarted()
return self.started
end
return scene
end
-- Create and register a new scene
function sceneman:new(name, base)
local scene = self:newscene(base)
self.scenes[name] = scene
return scene
end
function sceneman:get(name)
if type(name) ~= 'string' then
error('sceneman: scene name must be a string')
end
local scene = self.scenes[name]
if scene == nil then
error('sceneman: scene "' .. name .. '" not found"')
end
return scene
end
-- Call the load function of a
-- a single scene
function sceneman:load(name, ...)
local scene = self:get(name)
scene:load(...)
scene.loaded = true
return self
end
-- Call the load function
-- of every scenes
function sceneman:loadall(...)
for name, scene in pairs(self.scenes) do
sceneman:load(name, ...)
end
return self
end
-- Check wether or not an active
-- scene is set
function sceneman:hasactive()
return self.active ~= nil
end
-- Set a scene has the active scene
-- and call it's start function
function sceneman:start(name, ...)
local scene = self:get(name)
scene:start(...)
scene.started = true
self.active = scene
return self
end
-- Set a scene has the active scene
-- and call it's tofront function
function sceneman:tofront(name, ...)
local scene = self:get(name)
scene:tofront(...)
self.active = scene
return self
end
-- Call the toback function of the active
-- scene and set to nil the active scene
function sceneman:toback(...)
if self:hasactive() then
self.active:toback(...)
self.active = nil
end
return self
end
function sceneman:update(...)
if self:hasactive() then
self.active:update(...)
end
end
function sceneman:draw(...)
if self:hasactive() then
self.active:draw(...)
end
end
-- Stop the current active scene
function sceneman:stop(...)
if self:hasactive() then
self.active:stop(...)
self.active.started = false
end
self.active = nil
return self
end
-- Stop all scenes wich
-- are started
function sceneman:stopall(...)
for _, scene in pairs(self.scenes) do
if scene:isstarted() then
scene:stop(...)
scene.started = false
end
end
self.active = nil
return self
end
-- Call the quit function of
-- the given scene
function sceneman:quit(name, ...)
local scene = self:get(name)
if scene == self.active then
error('sceneman: can\'t quit active scene')
end
if scene:isloaded() then
scene:quit(...)
end
scene.loaded = false
return self
end
-- Call the quit method of
-- every scene
function sceneman:quitall(...)
for name, _ in pairs(self.scenes) do
self:quit(name, ...)
end
return self
end
return sceneman