forked from dmccuskey/Ghost-vs-Monsters---OOP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene-menu.lua
344 lines (239 loc) · 7.4 KB
/
scene-menu.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
--====================================================================--
-- scene-menu.lua
--
-- by David McCuskey
--
-- Sample code is MIT licensed, the same license which covers Lua itself
-- http://en.wikipedia.org/wiki/MIT_License
-- Copyright (C) 2011 David McCuskey. All Rights Reserved.
-- Copyright (C) 2010 ANSCA Inc. All Rights Reserved.
--====================================================================--
module(..., package.seeall)
--====================================================================--
-- Imports
--====================================================================--
local director = require( "director" )
local ui = require( "ui" )
local HUDFactory = require( "hud_objects" )
local levelMgr = require( "level_manager" )
--====================================================================--
-- Setup, Constants
--====================================================================--
local app_token
local sceneGroup
local loadScreenHUD
local loadTimer
local ghostTween
local ofTween
local playTween
local tapSound = audio.loadSound( "assets/sounds/tapsound.wav" )
--====================================================================--
-- Main
--====================================================================--
local drawScreen
local startLoadingMenu
local function unloadScene()
if ghostTween then transition.cancel( ghostTween ); end
if ofTween then transition.cancel( ofTween ); end
if playTween then transition.cancel( playTween ); end
if sceneGroup then
sceneGroup:removeSelf()
sceneGroup = nil
end
loadScreenHUD = nil
end
--== Event and Button Handlers ==--
local function openFeintButtonTouchHandler( event )
if event.phase == "release" then
audio.play( tapSound )
print( "OpenFeint Button Pressed." )
-- Will display OpenFeint dashboard when uncommented (if OpenFeint was properly initialized in main.lua)
--app_token.openfeint.launchDashboard()
end
return true
end
local function levelSelectHandler( event )
levelMgr:hide()
levelMgr:removeEventListener( "level", levelSelectHandler )
if event.type == "selected" then
unloadScene()
app_token.data = event.data
director:changeScene( app_token, "scene-game" )
end
return true
end
local function playButtonTouchHandler( event )
if event.phase == "release" then
audio.play( tapSound )
levelMgr:show()
levelMgr:toFront()
levelMgr:addEventListener( "level", levelSelectHandler )
end
return true
end
drawScreen = function()
-- BACKGROUND IMAGE
local backgroundImage = display.newImageRect( "assets/backgrounds/mainmenu.png", 480, 320 )
backgroundImage.x = 240; backgroundImage.y = 160
sceneGroup:insert( backgroundImage )
-- GHOST
local menuGhost = display.newImageRect( "assets/characters/menughost.png", 50, 62 )
menuGhost.x = 240; menuGhost.y = 188
sceneGroup:insert( menuGhost )
-- GHOST ANIMATION
if ghostTween then
transition.cancel( ghostTween )
end
local function ghostAnimation()
local animUp = function()
ghostTween = transition.to( menuGhost, { time=400, y=193, onComplete=ghostAnimation })
end
ghostTween = transition.to( menuGhost, { time=400, y=183, onComplete=animUp })
end
ghostAnimation()
-- END GHOST ANIMATION
-- OPENFEINT BUTTON
local ofBtn = ui.newButton{
defaultSrc = "assets/buttons/menuofbtn.png",
defaultX = 118,
defaultY = 88,
overSrc = "assets/buttons/menuofbtn-over.png",
overX = 118,
overY = 88,
onEvent = openFeintButtonTouchHandler,
id = "OpenfeintButton",
text = "",
font = "Helvetica",
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
ofBtn:setReferencePoint( display.BottomCenterReferencePoint )
ofBtn.x = 281 ofBtn.y = 410
sceneGroup:insert( ofBtn )
-- PLAY BUTTON
local playBtn = ui.newButton{
defaultSrc = "assets/buttons/playbtn.png",
defaultX = 146,
defaultY = 116,
overSrc = "assets/buttons/playbtn-over.png",
overX = 146,
overY = 116,
onEvent = playButtonTouchHandler,
id = "PlayButton",
text = "",
font = "Helvetica",
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
playBtn:setReferencePoint( display.BottomCenterReferencePoint )
playBtn.x = 365 playBtn.y = 440
sceneGroup:insert( playBtn )
-- SLIDE PLAY AND OPENFEINT BUTTON FROM THE BOTTOM:
local setPlayBtn = function()
playTween = transition.to( playBtn, { time=100, x=378, y=325 } )
local setOfBtn = function()
ofTween = transition.to( ofBtn, { time=100, x=268, y=325 } )
end
ofTween = transition.to( ofBtn, { time=500, y=320, onComplete=setOfBtn, transition=easing.inOutExpo } )
end
playTween = transition.to( playBtn, { time=500, y=320, onComplete=setPlayBtn, transition=easing.inOutExpo } )
end
--== START: Loading Section ==--
local function loadCompleteHandler( event )
local f = function()
loadScreenHUD:removeEventListener( "complete", loadCompleteHandler )
loadScreenHUD:hide()
timer.cancel( loadTimer )
loadTimer = nil
drawScreen()
end
-- make it so we can see 100% load bar for a little bit
loadScreenHUD.text = "Loading Complete"
loadTimer = timer.performWithDelay( 200, f, 1 )
end
-- load_snailsAndTails
--
local function load_snailsAndTails()
--print("GameScene: load_snailsAndTails")
local contentToLoad = "Snails and Tails"
local contentPercent = 30
loadScreenHUD.text = contentToLoad
-- START LOAD
-- put stuff here to load
-- END LOAD
-- fake loading function
local f = function()
loadScreenHUD.percentComplete = loadScreenHUD.percentComplete + contentPercent
end
loadTimer = timer.performWithDelay( 500, f, 1 )
end
-- load_gatheringStoneAndWood
--
local function load_gatheringStoneAndWood()
--print("GameScene: load_gatheringStoneAndWood")
local contentToLoad = "Gathering Stone and Wood"
local contentPercent = 30
local loadNext = load_snailsAndTails
loadScreenHUD.text = contentToLoad
-- START LOAD
-- put stuff here to load
-- END LOAD
-- fake loading function
local f = function()
loadScreenHUD.percentComplete = loadScreenHUD.percentComplete + contentPercent
loadNext()
end
loadTimer = timer.performWithDelay( 500, f, 1 )
end
-- load_ghostsAndGhouls
--
local function load_ghostsAndGhouls()
--print("GameScene: load_ghostsAndGhouls")
local contentToLoad = "Ghosts and Ghouls"
local contentPercent = 40
local loadNext = load_gatheringStoneAndWood
loadScreenHUD.text = contentToLoad
-- START LOAD
-- put stuff here to load
-- END LOAD
-- fake loading function
local f = function()
loadScreenHUD.percentComplete = loadScreenHUD.percentComplete + contentPercent
loadNext()
end
loadTimer = timer.performWithDelay( 500, f, 1 )
end
startLoadingMenu = function()
-- setup loading screen
loadScreenHUD = app_token.loadScreenHUD
loadScreenHUD:addEventListener( "complete", loadCompleteHandler )
loadScreenHUD:clear()
loadScreenHUD:show()
-- start loading
load_ghostsAndGhouls()
end
--== END: Loading Section ==--
-- new()
-- Director function
--
function new( params )
print( "LOADING: Game Menu =============" )
app_token = params
-- create the display group for our content
sceneGroup = display.newGroup()
app_token.mainGroup:insert( sceneGroup )
startLoadingMenu()
--[[
local monitorMem = function()
collectgarbage()
print( "\nMemUsage: " .. collectgarbage("count") )
local textMem = system.getInfo( "textureMemoryUsed" ) / 1000000
print( "TexMem: " .. textMem )
end
Runtime:addEventListener( "enterFrame", monitorMem )
]]--
-- MUST return a display.newGroup()
return sceneGroup
end