-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMoonJohn.lua
188 lines (165 loc) · 7.34 KB
/
MoonJohn.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
176
177
178
179
180
181
182
183
184
185
186
187
188
local Stack = {}
function Stack:new()
local StackNode = function(data)
local self = {
next, previous, data, constructor = function(this, data)
this.next = nil; this.data = data; this.previous = nil
end
}
self.constructor(self, data)
local getNext = function() return self.next end
local setNext = function(next) self.next = next end
local getPrevious = function() return self.previous end
local setPrevious = function(previous) self.previous = previous end
local getData = function() return self.data end
local setData = function(data) self.data = data end
return {getNext = getNext, setNext = setNext, getPrevious = getPrevious, setPrevious = setPrevious, getData = getData, setData = setData}
end
local self = {
current, size, constructor = function(this)
this.current = nil; this.size = 0
end
}
self.constructor(self)
local peek = function() return self.current and self.current.getData() or nil end
local push = function(data)
local newNode = StackNode(data)
newNode.setNext(self.current)
self.current = newNode
self.size = self.size + 1
end
local pop = function()
if(self.current) then
local returnedData = self.current.getData()
self.current = self.current.getNext()
return returnedData
end
return nil
end
local isEmpty = function() return self.current == nil end
local size = function() return self.size or 0 end
return {peek = peek, push = push, pop = pop, isEmpty = isEmpty, size = size}
end
local MoonJohn = {}; MoonJohn.__index = MoonJohn
local function inputEvents(self, name, ...)
if not self.transition then
if not self.currentSubscene then
if self.currentScene[name] then
self.currentScene[name](self.currentScene, ...)
end
elseif self.currentSubscene[name] then
self.currentSubscene[name](self.currentSubscene, ...)
end
end
end
function MoonJohn:new(firstScene)
assert(firstScene, "MoonJohn needs a initial scene to work properly")
local this = setmetatable({
currentScene = firstScene, currentSubscene = nil,
sceneObjects = {}, subsceneObjects = {}, sceneNames = {},
sceneStack = Stack:new(), transition = nil, defaultTransition = nil
}, MoonJohn)
local events = {
"keypressed", "keyreleased", "textedited", "textinput", "mousemoved", "mousepressed",
"mousereleased", "touchmoved", "touchpressed", "touchreleased", "wheelmoved"
}
for _, eventName in pairs(events) do --[[ Here's all event functions --]]
this[eventName] = function(this, ...) inputEvents(this, eventName, ...) end
end
return setmetatable(this, MoonJohn)
end
function MoonJohn:reset(scene) --[[ A function that is called to reset the scene --]]
assert(self.sceneObjects[scene], "Unable to find required scene: '" .. tostring(scene) .. "'")
if self.sceneObjects[scene].reset then self.sceneObjects[scene]:reset() end
end
function MoonJohn:addScene(sceneName, sceneObject, override)
if override or not self.sceneObjects[sceneName] then
self.sceneObjects[sceneName] = sceneObject; self.sceneNames[sceneObject] = sceneName
end
end
function MoonJohn:addSubscene(subsceneName, subsceneObject, override)
if override or not self.subsceneObjects[subsceneName] then
self.subsceneObjects[subsceneName] = subsceneObject
end
end
local function callTransitionInScene(self, action, newOldScene)
--[[ print(self.sceneNames[self.currentScene], action, self.sceneNames[newOldScene]) -- print for debug purposes ]]
if self.currentScene and self.currentScene[action] then self.currentScene[action](self.currentScene, self.sceneNames[newOldScene]) end
end
local function switchScene(self, scene, message)
local toEnterScene = self.sceneObjects[scene] or self.currentScene; local oldScene = self.currentScene
callTransitionInScene(self, "goingOut", toEnterScene); self.sceneStack.push(self.currentScene)
self.currentScene = toEnterScene; self.currentScene.message = message
callTransitionInScene(self, "entering", oldScene); self.currentSubscene = nil
end
function MoonJohn:switchScene(scene, message)
assert(self.sceneObjects[scene], "Unable to find required scene: '" .. tostring(scene) .. "'")
if self.defaultTransition then
local update, draw = self.defaultTransition()
self:setTransition(update, draw, function() switchScene(self, scene, message) end)
else
switchScene(self, scene, message)
end
end
local function previousScene(self)
local toEnterScene = self.currentScene; callTransitionInScene(self, "goingOut", self.sceneStack.peek())
self.currentScene = self.sceneStack.pop(); callTransitionInScene(self, "entering", toEnterScene); self.currentSubscene = nil
end
function MoonJohn:previousScene()
if self.sceneStack.peek() then
if self.defaultTransition then
local update, draw = self.defaultTransition()
self:setTransition(update, draw, function() previousScene(self) end)
else
previousScene(self)
end
end
end
function MoonJohn:clearStack(scene)
assert(scene and self.sceneObjects[scene], "Unable to find required scene: '" .. tostring(scene) .. "'")
local oldScene = self.currentScene
while not self.sceneStack.isEmpty() do
self.currentScene = self.sceneStack.pop()
end
local toEnterScene = (scene and self.sceneObjects[scene]) or self.currentScene; self.currentScene = toEnterScene
callTransitionInScene(self, "entering", oldScene); self.currentScene = oldScene
callTransitionInScene(self, "goingOut", toEnterScene); self.currentScene = toEnterScene
self.sceneStack.push(self.currentScene); self.currentSubscene = nil
end
function MoonJohn:switchSubscene(subscene, args)
self.currentSubscene = self.subsceneObjects[subscene]
assert(self.currentSubscene, string.format("Unable to find requested subscene \"%s\"", tostring(subscene)))
self.currentSubscene.args = args
end
function MoonJohn:exitSubscene() self.currentSubscene = nil end
function MoonJohn:setTransition(update, draw, callWhenOver)
self.transition = {draw = draw, update = update, callback = callWhenOver}
end
function MoonJohn:isTransitionOver() return self.transition == nil end
function MoonJohn:setDefaultTransition(transition) self.defaultTransition = transition end
function MoonJohn:update(dt)
if self.transition then
local finished = self.transition.update(dt)
if finished then
if self.transition.callback then self.transition.callback() end
self.transition = nil
end
else
if not self.currentSubscene then
if self.currentScene.update then
self.currentScene:update(dt)
end
elseif self.currentSubscene.update then
self.currentSubscene:update(dt)
end
end
end
function MoonJohn:draw()
if self.currentScene.draw then self.currentScene:draw() end
if self.currentSubscene and self.currentSubscene.draw then self.currentSubscene:draw() end
if self.transition then self.transition.draw() end
end
function MoonJohn:resize(w, h)
if self.currentScene.resize then self.currentScene:resize(w, h) end
end
return MoonJohn