forked from n8fr8/Ghosts-vs.-Monsters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
director.lua
592 lines (514 loc) · 15.6 KB
/
director.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
module(..., package.seeall)
--====================================================================--
-- DIRECTOR CLASS
--====================================================================--
--
-- Version: 1.2
-- Made by Ricardo Rauber Pereira @ 2010
-- Blog: http://rauberlabs.blogspot.com/
-- Mail: ricardorauber@gmail.com
--
-- This class is free to use, feel free to change but please send new versions
-- or new features like new effects to me and help us to make it better!
--
--====================================================================--
-- CHANGES
--====================================================================--
--
-- 06-OCT-2010 - Ricardo Rauber - Created
-- 07-OCT-2010 - Ricardo Rauber - Functions loadScene and fxEnded were
-- taken off from the changeScene function;
-- Added function cleanGroups for best
-- memory clean up;
-- Added directorView and effectView groups
-- for better and easier control;
-- Please see INFORMATION to know how to use it
-- 14-NOV-2010 - Ricardo Rauber - Bux fixes and new getScene function to get
-- the name of the active scene (lua file)
-- 14-FEB-2011 - Ricardo Rauber - General Bug Fixes
--
--====================================================================--
-- INFORMATION
--====================================================================--
--
-- * For best practices, use fps=60, scale = "zoomStretch" on config.lua
--
-- * In main.lua file, you have to import the class like this:
--
-- director = require( "director" )
-- local mainGroup = display.newGroup()
-- mainGroup:insert( director.directorView )
--
-- * To change scenes, use this command [use the effect of your choice]
--
-- director:changeScene("settings","moveFromLeft")
--
-- * Every scene is a lua module file and must have a new() function that
-- must return a local display group, like this: [see template.lua]
--
-- module(..., package.seeall)
-- local localGroup = display.newGroup()
-- function new()
-- ------ Your code here ------
-- return localGroup
-- end
--
-- * Every display object must be inserted on the local display group
--
-- local background = display.newImage( "background.png" )
-- localGroup:insert( background )
--
-- * This class doesn't clean timers! If you want to stop timers when
-- change scenes, you'll have to do it manually creating a clean() function.
--
--====================================================================--
directorView = display.newGroup()
currView = display.newGroup()
nextView = display.newGroup()
effectView = display.newGroup()
--
local currScreen, nextScreen
local currScene, nextScene = "main", "main"
local newScene
local fxTime = 200
local safeDelay = 50
local isChangingScene = false
--
directorView:insert(currView)
directorView:insert(nextView)
directorView:insert(effectView)
--
currView.x = 0
currView.y = 0
nextView.x = display.contentWidth
nextView.y = 0
------------------------------------------------------------------------
-- GET COLOR
------------------------------------------------------------------------
local function getColor ( arg1, arg2, arg3 )
--
local r, g, b
--
if type(arg1) == "nil" then
arg1 = "black"
end
--
if string.lower(arg1) == "red" then
r=255
g=0
b=0
elseif string.lower(arg1) == "green" then
r=0
g=255
b=0
elseif string.lower(arg1) == "blue" then
r=0
g=0
b=255
elseif string.lower(arg1) == "yellow" then
r=255
g=255
b=0
elseif string.lower(arg1) == "pink" then
r=255
g=0
b=255
elseif string.lower(arg1) == "white" then
r=255
g=255
b=255
elseif type (arg1) == "number"
and type (arg2) == "number"
and type (arg3) == "number" then
r=arg1
g=arg2
b=arg3
else
r=0
g=0
b=0
end
--
return r, g, b
--
end
------------------------------------------------------------------------
-- CHANGE CONTROLS
------------------------------------------------------------------------
-- fxTime
function director:changeFxTime ( newFxTime )
if type(newFxTime) == "number" then
fxTime = newFxTime
end
end
-- safeDelay
function director:changeSafeDelay ( newSafeDelay )
if type(newSafeDelay) == "number" then
safeDelay = newSafeDelay
end
end
------------------------------------------------------------------------
-- GET SCENES
------------------------------------------------------------------------
function director:getCurrScene ()
return currScene
end
--
function director:getNextScene ()
return nextScene
end
------------------------------------------------------------------------
-- CLEAN GROUP
------------------------------------------------------------------------
local function cleanGroups ( curGroup, level )
if curGroup.numChildren then
while curGroup.numChildren > 0 do
cleanGroups ( curGroup[curGroup.numChildren], level+1 )
end
if level > 0 then
display.remove( curGroup )
--curGroup:removeSelf()
end
else
--curGroup:removeSelf()
display.remove( curGroup )
curGroup = nil
return true
end
end
------------------------------------------------------------------------
-- CALL CLEAN FUNCTION
------------------------------------------------------------------------
local function callClean ( moduleName )
if type(package.loaded[moduleName]) == "table" then
if string.lower(moduleName) ~= "main" then
for k,v in pairs(package.loaded[moduleName]) do
if k == "clean" and type(v) == "function" then
package.loaded[moduleName].clean()
end
end
end
end
end
------------------------------------------------------------------------
-- UNLOAD SCENE
------------------------------------------------------------------------
local function unloadScene ( moduleName )
if moduleName ~= "main" and type(package.loaded[moduleName]) == "table" then
package.loaded[moduleName] = nil
local function garbage ( event )
collectgarbage("collect")
end
garbage()
timer.performWithDelay(fxTime,garbage)
end
end
------------------------------------------------------------------------
-- LOAD SCENE
------------------------------------------------------------------------
local function loadScene ( moduleName, target )
-- Test parameters
if type(moduleName) == "nil" then
return true
end
if type(target) == "nil" then
target = "next"
end
-------------------------------------
-- Load choosed scene
-------------------------------------
-- Prev
if string.lower(target) == "curr" then
--
callClean ( moduleName )
--
cleanGroups(currView,0)
--
if nextScene == moduleName then
cleanGroups(nextView,0)
end
--
unloadScene( moduleName )
--
currScreen = require(moduleName).new()
currView:insert(currScreen)
currScene = moduleName
-- Next
else
--
callClean ( moduleName )
--
cleanGroups(nextView,0)
--
if currScene == moduleName then
cleanGroups(currView,0)
end
--
unloadScene( moduleName )
--
nextScreen = require(moduleName).new()
nextView:insert(nextScreen)
nextScene = moduleName
end
end
-- Load curr screen
function director:loadCurrScene ( moduleName )
loadScene ( moduleName, "curr" )
end
-- Load next screen
function director:loadNextScene ( moduleName )
loadScene ( moduleName, "next" )
end
------------------------------------------------------------------------
-- EFFECT ENDED
------------------------------------------------------------------------
local function fxEnded ( event )
currView.x = 0
currView.y = 0
currView.xScale = 1
currView.yScale = 1
--
callClean ( currScene )
cleanGroups( currView ,0)
unloadScene( currScene )
--
currScreen = nextScreen
currScene = newScene
currView:insert(currScreen)
--
nextView.x = display.contentWidth
nextView.y = 0
nextView.xScale = 1
nextView.yScale = 1
--
isChangingScene = false
end
------------------------------------------------------------------------
-- CHANGE SCENE
------------------------------------------------------------------------
function director:changeScene(nextLoadScene,
effect,
arg1,
arg2,
arg3)
-----------------------------------
-- If is changing scene, return without do anything
-----------------------------------
if isChangingScene then
return true
else
isChangingScene = true
end
-----------------------------------
-- If is the same, don't change
-----------------------------------
if currScene then
if string.lower(currScene) == string.lower(nextLoadScene) then
return true
end
end
newScene = nextLoadScene
local showFx
-----------------------------------
-- EFFECT: Move From Right
-----------------------------------
if effect == "moveFromRight" then
nextView.x = display.contentWidth
nextView.y = 0
--
loadScene (newScene)
--
showFx = transition.to ( nextView, { x=0, time=fxTime } )
showFx = transition.to ( currView, { x=display.contentWidth*-1, time=fxTime } )
--
timer.performWithDelay( fxTime+safeDelay, fxEnded )
-----------------------------------
-- EFFECT: Over From Right
-----------------------------------
elseif effect == "overFromRight" then
nextView.x = display.contentWidth
nextView.y = 0
--
loadScene (newScene)
--
showFx = transition.to ( nextView, { x=0, time=fxTime } )
--
timer.performWithDelay( fxTime+safeDelay, fxEnded )
-----------------------------------
-- EFFECT: Move From Left
-----------------------------------
elseif effect == "moveFromLeft" then
nextView.x = display.contentWidth*-1
nextView.y = 0
--
loadScene (newScene)
--
showFx = transition.to ( nextView, { x=0, time=fxTime } )
showFx = transition.to ( currView, { x=display.contentWidth, time=fxTime } )
--
timer.performWithDelay( fxTime+safeDelay, fxEnded )
-----------------------------------
-- EFFECT: Over From Left
-----------------------------------
elseif effect == "overFromLeft" then
nextView.x = display.contentWidth*-1
nextView.y = 0
--
loadScene (newScene)
--
showFx = transition.to ( nextView, { x=0, time=fxTime } )
--
timer.performWithDelay( fxTime+safeDelay, fxEnded )
-----------------------------------
-- EFFECT: Move From Top
-----------------------------------
elseif effect == "moveFromTop" then
nextView.x = 0
nextView.y = display.contentHeight*-1
--
loadScene (newScene)
--
showFx = transition.to ( nextView, { y=0, time=fxTime } )
showFx = transition.to ( currView, { y=display.contentHeight, time=fxTime } )
--
timer.performWithDelay( fxTime+safeDelay, fxEnded )
-----------------------------------
-- EFFECT: Over From Top
-----------------------------------
elseif effect == "overFromTop" then
nextView.x = 0
nextView.y = display.contentHeight*-1
--
loadScene (newScene)
--
showFx = transition.to ( nextView, { y=0, time=fxTime } )
--
timer.performWithDelay( fxTime+safeDelay, fxEnded )
-----------------------------------
-- EFFECT: Move From Bottom
-----------------------------------
elseif effect == "moveFromBottom" then
nextView.x = 0
nextView.y = display.contentHeight
--
loadScene (newScene)
--
showFx = transition.to ( nextView, { y=0, time=fxTime } )
showFx = transition.to ( currView, { y=display.contentHeight*-1, time=fxTime } )
--
timer.performWithDelay( fxTime+safeDelay, fxEnded )
-----------------------------------
-- EFFECT: Over From Bottom
-----------------------------------
elseif effect == "overFromBottom" then
nextView.x = 0
nextView.y = display.contentHeight
--
loadScene (newScene)
--
showFx = transition.to ( nextView, { y=0, time=fxTime } )
--
timer.performWithDelay( fxTime+safeDelay, fxEnded )
-----------------------------------
-- EFFECT: Crossfade
-----------------------------------
elseif effect == "crossfade" then
nextView.x = display.contentWidth
nextView.y = 0
--
loadScene (newScene)
--
nextView.alpha = 0
nextView.x = 0
--
showFx = transition.to ( nextView, { alpha=1, time=fxTime*2 } )
--
timer.performWithDelay( fxTime*2+safeDelay, fxEnded )
-----------------------------------
-- EFFECT: Fade
-----------------------------------
-- ARG1 = color [string]
-----------------------------------
-- ARG1 = red [number]
-- ARG2 = green [number]
-- ARG3 = blue [number]
-----------------------------------
elseif effect == "fade" then
local r, g, b = getColor ( arg1, arg2, arg3 )
--
nextView.x = display.contentWidth
nextView.y = 0
--
loadScene (newScene)
--
local fade = display.newRect( 0 - display.contentWidth, 0 - display.contentHeight, display.contentWidth * 3, display.contentHeight * 3 )
fade.alpha = 0
fade:setFillColor( r,g,b )
effectView:insert(fade)
--
showFx = transition.to ( fade, { alpha=1.0, time=fxTime } )
--
timer.performWithDelay( fxTime+safeDelay, fxEnded )
--
local function returnFade ( event )
showFx = transition.to ( fade, { alpha=0, time=fxTime } )
--
local function removeFade ( event )
fade:removeSelf()
end
--
timer.performWithDelay( fxTime+safeDelay, removeFade )
end
--
timer.performWithDelay( fxTime+safeDelay+1, returnFade )
-----------------------------------
-- EFFECT: Flip
-----------------------------------
elseif effect == "flip" then
showFx = transition.to ( currView, { xScale=0.001, time=fxTime } )
showFx = transition.to ( currView, { x=display.contentWidth*0.5, time=fxTime } )
--
loadScene (newScene)
--
nextView.xScale=0.001
nextView.x=display.contentWidth*0.5
--
showFx = transition.to ( nextView, { xScale=1, delay=fxTime, time=fxTime } )
showFx = transition.to ( nextView, { x=0, delay=fxTime, time=fxTime } )
--
timer.performWithDelay( fxTime*2+safeDelay, fxEnded )
-----------------------------------
-- EFFECT: Down Flip
-----------------------------------
elseif effect == "downFlip" then
showFx = transition.to ( currView, { xScale=0.7, time=fxTime } )
showFx = transition.to ( currView, { yScale=0.7, time=fxTime } )
showFx = transition.to ( currView, { x=display.contentWidth*0.15, time=fxTime } )
showFx = transition.to ( currView, { y=display.contentHeight*0.15, time=fxTime } )
showFx = transition.to ( currView, { xScale=0.001, delay=fxTime, time=fxTime } )
showFx = transition.to ( currView, { x=display.contentWidth*0.5, delay=fxTime, time=fxTime } )
--
loadScene (newScene)
--
nextView.x = display.contentWidth*0.5
nextView.xScale=0.001
nextView.yScale=0.7
nextView.y=display.contentHeight*0.15
--
showFx = transition.to ( nextView, { x=display.contentWidth*0.15, delay=fxTime*2, time=fxTime } )
showFx = transition.to ( nextView, { xScale=0.7, delay=fxTime*2, time=fxTime } )
showFx = transition.to ( nextView, { xScale=1, delay=fxTime*3, time=fxTime } )
showFx = transition.to ( nextView, { yScale=1, delay=fxTime*3, time=fxTime } )
showFx = transition.to ( nextView, { x=0, delay=fxTime*3, time=fxTime } )
showFx = transition.to ( nextView, { y=0, delay=fxTime*3, time=fxTime } )
--
timer.performWithDelay( fxTime*4+safeDelay, fxEnded )
-----------------------------------
-- EFFECT: None
-----------------------------------
else
timer.performWithDelay( 0, fxEnded )
loadScene (newScene)
end
return true
end