forked from n8fr8/Ghosts-vs.-Monsters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel1.lua
1703 lines (1340 loc) · 46.7 KB
/
level1.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- Abstract: Ghosts Vs Monsters sample project
-- Designed and created by Jonathan and Biffy Beebe of Beebe Games exclusively for Ansca, Inc.
-- http://beebegamesonline.appspot.com/
-- (This is easiest to play on iPad or other large devices, but should work on all iOS and Android devices)
--
-- Version: 1.1
--
-- Sample code is MIT licensed, see http://developer.anscamobile.com/code/license
-- Copyright (C) 2010 ANSCA Inc. All Rights Reserved.
module(..., package.seeall)
--***********************************************************************************************--
--***********************************************************************************************--
-- LEVEL MODULE
-- To create a new level, change "MODULE-SPECIFIC VARIABLES" (below) and also the
-- createLevel() function. Everything else should be identical between level modules.
--***********************************************************************************************--
--***********************************************************************************************--
-- Main function - MUST return a display.newGroup()
function new()
local hudGroup = display.newGroup()
local gameGroup = display.newGroup()
gameGroup.x = -480
local trailGroup = display.newGroup()
local dotTimer
local levelGroup = display.newGroup()
-- MODULE-SPECIFIC VARIABLES
local backgroundFilename1 = "altbackground1.png"
local backgroundFilename2 = "altbackground2.png"
-- EXTERNAL MODULES / LIBRARIES
local movieclip = movieclip --require( "movieclip" )
local physics = require "physics"
local ui = ui --require("ui")
--local facebook = require "facebook"
local mCeil = math.ceil
local mAtan2 = math.atan2
local mPi = math.pi
local mSqrt = math.sqrt
-- OBJECTS
local backgroundImage1
local backgroundImage2
local clouds1
local clouds2
local clouds3
local clouds4
local groundLight1
local groundObject1
local groundObject2
local shotOrb
local shotArrow
local blastGlow
local ghostObject
local poofObject
local greenPoof; local poofTween
local life1; local life2; local life3; local life4
local scoreText; local bestScoreText
local continueText; local continueTimer
local pauseMenuBtn; local pauseBtn; local pauseShade
-- VARIABLES
local gameIsActive = false
local waitingForNewRound
local restartTimer
local ghostTween
local screenPosition = "left" --> "left" or "right"
local canSwipe = true
local swipeTween
local gameLives = 4
local gameScore = 0
local bestScore
local monsterCount
-- LEVEL SETTINGS
local restartModule
local nextModule
local woodDensity = 2.0
local vPlankShape = { -6,-48, 6,-48, 6,48, -6,48 }
local hPlankShape = { -48,-6, 48,-6, 48,6, -48,6 }
local stoneDensity = 5.0
local vSlabShape = { -12,-26, 12,-26, 12,26, -12,26 }
local tombDensity = 5.5
local tombShape = { -18,-21, 18,-21, 18,21, -18,21 }
local monsterDensity = 1.0
local monsterShape = { -12,-13, 12,-13, 12,13, -12,13 }
-- AUDIO
local tapSound = audio.loadSound( "tapsound.wav" )
local blastOffSound = audio.loadSound( "blastoff.wav" )
local ghostPoofSound = audio.loadSound( "ghostpoof.wav" )
local monsterPoofSound = audio.loadSound( "monsterpoof.wav" )
local impactSound = audio.loadSound( "impact.wav" )
local weeSound = audio.loadSound( "wee.wav" )
local newRoundSound = audio.loadSound( "newround.wav" )
local youWinSound = audio.loadSound( "youwin.wav" )
local youLoseSound = audio.loadSound( "youlose.wav" )
--***************************************************
-- saveValue() --> used for saving high score, etc.
--***************************************************
local saveValue = function( strFilename, strValue )
-- will save specified value to specified file
local theFile = strFilename
local theValue = strValue
local path = system.pathForFile( theFile, system.DocumentsDirectory )
-- io.open opens a file at path. returns nil if no file found
local file = io.open( path, "w+" )
if file then
-- write game score to the text file
file:write( theValue )
io.close( file )
end
end
--***************************************************
-- loadValue() --> load saved value from file (returns loaded value as string)
--***************************************************
local loadValue = function( strFilename )
-- will load specified file, or create new file if it doesn't exist
local theFile = strFilename
local path = system.pathForFile( theFile, system.DocumentsDirectory )
-- io.open opens a file at path. returns nil if no file found
local file = io.open( path, "r" )
if file then
-- read all contents of file into a string
local contents = file:read( "*a" )
io.close( file )
return contents
else
-- create file b/c it doesn't exist yet
file = io.open( path, "w" )
file:write( "0" )
io.close( file )
return "0"
end
end
local startNewRound = function()
if ghostObject then
local activateRound = function()
canSwipe = true
waitingForNewRound = false
if restartTimer then
timer.cancel( restartTimer )
end
groundLight1:toFront()
groundObject1:toFront()
groundObject2:toFront()
ghostObject.x = 150; --ghostObject.y = 195
ghostObject.y = 300;
ghostObject:stopAtFrame( 1 )
ghostObject.rotation = 0
ghostObject.isVisible = true
ghostObject.isBodyActive = true
audio.play( newRoundSound )
local ghostLoaded = function()
gameIsActive = true
ghostObject.inAir = false
ghostObject.isHit = false
ghostObject:toFront()
ghostObject.bodyType = "static"
-- Show the pause button
pauseBtn.isVisible = true
pauseBtn.isActive = true
-- START up and down animation
if ghostTween then
transition.cancel( ghostTween )
end
local function ghostAnimation()
local animUp = function()
if ghostObject.inAir or shotOrb.isVisible then
transition.cancel( ghostTween )
else
ghostTween = transition.to( ghostObject, { time=375, y=190, onComplete=ghostAnimation })
end
end
if ghostObject.inAir or shotOrb.isVisible then
transition.cancel( ghostTween )
else
ghostTween = transition.to( ghostObject, { time=375, y=200, onComplete=animUp })
end
end
ghostTween = transition.to( ghostObject, { time=375, y=190, onComplete=ghostAnimation })
-- END up and down animation
end
transition.to( ghostObject, { time=1000, y=195, onComplete=ghostLoaded } )
end
-- reset camera
if gameGroup.x < 0 then
transition.to( gameGroup, { time=1000, x=0, transition=easing.inOutExpo, onComplete=activateRound } )
else
gameGroup.x = 0
activateRound()
end
end
end
local comma_value = function(amount)
local formatted = amount
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
if (k==0) then
break
end
end
return formatted
end
local setScore = function( scoreNum )
local newScore = scoreNum
gameScore = newScore
if gameScore < 0 then gameScore = 0; end
scoreText.text = comma_value(gameScore)
scoreText.xScale = 0.5; scoreText.yScale = 0.5 --> for clear retina display text
scoreText.x = (480 - (scoreText.contentWidth * 0.5)) - 15
scoreText.y = 20
end
local callGameOver = function( isWin )
local isWin = isWin
if isWin == "yes" then
audio.play( youWinSound )
else
audio.play( youLoseSound )
end
gameIsActive = false --> temporarily disable gameplay touches, enterFrame listener, etc.
physics.pause()
-- Make sure pause button is hidden/inactive
pauseBtn.isVisible = false
pauseBtn.isActive = false
if continueTimer then timer.cancel( continueTimer ); end
continueText.isVisible = false
-- Create all game over objects and insert them into the HUD group
-- SHADE
local shadeRect = display.newRect( 0, 0, 480, 320 )
shadeRect:setFillColor( 0, 0, 0, 255 )
shadeRect.alpha = 0
-- GAME OVER WINDOW
local gameOverDisplay
if isWin == "yes" then
gameOverDisplay = display.newImageRect( "youwin.png", 390, 154 )
-- Give score bonus depending on how many ghosts left
local ghostBonus = gameLives * 20000
local newScore = gameScore + ghostBonus
setScore( newScore )
else
gameOverDisplay = display.newImageRect( "youlose.png", 390, 154 )
end
gameOverDisplay.x = 240; gameOverDisplay.y = 165
gameOverDisplay.alpha = 0
-- MENU BUTTON
local onMenuTouch = function( event )
if event.phase == "release" then
audio.play( tapSound )
director:changeScene( "loadmainmenu" )
end
end
local menuBtn = ui.newButton{
defaultSrc = "menubtn.png",
defaultX = 60,
defaultY = 60,
overSrc = "menubtn-over.png",
overX = 60,
overY = 60,
onEvent = onMenuTouch,
id = "MenuButton",
text = "",
font = "Helvetica",
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
if isWin == "yes" then
menuBtn.x = 227
else
menuBtn.x = 266
end
menuBtn.y = 186
menuBtn.alpha = 0
-- RESTART BUTTON
local onRestartTouch = function( event )
if event.phase == "release" then
audio.play( tapSound )
local theModule = "load" .. restartModule
director:changeScene( theModule )
end
end
local restartBtn = ui.newButton{
defaultSrc = "restartbtn.png",
defaultX = 60,
defaultY = 60,
overSrc = "restartbtn-over.png",
overX = 60,
overY = 60,
onEvent = onRestartTouch,
id = "RestartButton",
text = "",
font = "Helvetica",
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
restartBtn.x = menuBtn.x + 72; restartBtn.y = 186
restartBtn.alpha = 0
-- NEXT BUTTON
local onNextTouch = function( event )
if event.phase == "release" then
audio.play( tapSound )
local theModule = "load" .. nextModule
director:changeScene( theModule )
end
end
local nextBtn = ui.newButton{
defaultSrc = "nextlevelbtn.png",
defaultX = 60,
defaultY = 60,
overSrc = "nextlevelbtn-over.png",
overX = 60,
overY = 60,
onEvent = onNextTouch,
id = "NextButton",
text = "",
font = "Helvetica",
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
nextBtn.x = restartBtn.x + 72; nextBtn.y = 186
nextBtn.alpha = 0
if isWin ~= "yes" then nextBtn.isVisible = false; end
-- OPENFEINT BUTTON
local onOFTouch = function( event )
if event.phase == "release" then
audio.play( tapSound )
-- Launch OpenFeint Leaderboards Panel:
--openfeint.launchDashboard("leaderboards")
end
end
local ofBtn = ui.newButton{
defaultSrc = "openfeintbtn.png",
defaultX = 168,
defaultY = 40,
overSrc = "openfeintbtn-over.png",
overX = 168,
overY = 40,
onEvent = onOFTouch,
id = "OpenfeintButton",
text = "",
font = "Helvetica",
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
ofBtn.x = 168; ofBtn.y = 110
ofBtn.alpha = 0
local fbBtn
-- FACEBOOK BUTTON
local onFBTouch = function( event )
if event.phase == "release" and fbBtn.isActive then
audio.play( tapSound )
-- Code to Post Status to Facebook (don't forget the 'require "facebook"' line at top of module)
-- The Code below is fully functional as long as you replace the fbAppID var with valid app ID.
--[[
local fbAppID = "1234567890" --> (string) Your FB App ID from facebook developer's panel
local facebookListener = function( event )
if ( "session" == event.type ) then
-- upon successful login, update their status
if ( "login" == event.phase ) then
local scoreToPost = comma_value(gameScore)
local statusUpdate = "just scored a " .. gameScore .. " on Ghosts v.s Monsters!"
facebook.request( "me/feed", "POST", {
message=statusUpdate,
name="Download Ghosts vs. Monsters to Compete with Me!",
caption="Ghosts vs. Monsters - Sample app created with the Corona SDK by Ansca Mobile.",
link="http://itunes.apple.com/us/app/your-app-name/id382456881?mt=8",
picture="http://www.yoursite.com/link-to-90x90-image.png" } )
end
end
end
facebook.login( fbAppID, facebookListener, { "publish_stream" } )
]]--
end
end
fbBtn = ui.newButton{
defaultSrc = "facebookbtn.png",
defaultX = 302,
defaultY = 40,
overSrc = "facebookbtn-over.png",
overX = 302,
overY = 40,
onEvent = onFBTouch,
id = "FacebookButton",
text = "",
font = "Helvetica",
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
fbBtn.x = 240; fbBtn.y = 220
fbBtn.alpha = 0
if isWin == "yes" then
fbBtn.isVisible = true
fbBtn.isActive = true
else
fbBtn.isVisible = false
fbBtn.isActive = false
end
-- INSERT ALL ITEMS INTO GROUP
hudGroup:insert( shadeRect )
hudGroup:insert( ofBtn )
hudGroup:insert( fbBtn )
hudGroup:insert( gameOverDisplay )
hudGroup:insert( menuBtn )
hudGroup:insert( restartBtn )
if isWin == "yes" then hudGroup:insert( nextBtn ); end
-- FADE IN ALL GAME OVER ELEMENTS
transition.to( shadeRect, { time=200, alpha=0.65 } )
transition.to( gameOverDisplay, { time=500, alpha=1 } )
transition.to( menuBtn, { time=500, alpha=1 } )
transition.to( restartBtn, { time=500, alpha=1 } )
if isWin == "yes" then transition.to( nextBtn, { time=500, alpha=1 } ); end
transition.to( ofBtn, { time=500, alpha=1, y=68, transition=easing.inOutExpo } )
if isWin == "yes" then transition.to( fbBtn, { time=500, alpha=1, y=255, transition=easing.inOutExpo } ); end
-- MAKE SURE SCORE TEXT IS VISIBLE (if player won the round)
if isWin == "yes" then
scoreText.isVisible = false
local oldScoreText = scoreText.text
scoreText.text = "Score: " .. oldScoreText
scoreText.xScale = 0.5; scoreText.yScale = 0.5 --> for clear retina display text
scoreText.x = (480 - (scoreText.contentWidth * 0.5)) - 30
scoreText.y = 30
scoreText:toFront()
timer.performWithDelay( 1000, function() scoreText.isVisible = true; end, 1 )
else
--scoreText:removeSelf()
display.remove( scoreText )
scoreText = nil
end
-- Update Best Score
if gameScore > bestScore then
bestScore = gameScore
local bestScoreFilename = restartModule .. ".data"
saveValue( bestScoreFilename, tostring(bestScore) )
end
-- MAKE SURE BEST SCORE TEXT IS VISIBLE
bestScoreText = display.newText( "0", 10, 300, "Helvetica-Bold", 32 )
bestScoreText:setTextColor( 228, 228, 228, 255 ) --> white
bestScoreText.text = "Best Score For This Level: " .. comma_value( bestScore )
bestScoreText.xScale = 0.5; bestScoreText.yScale = 0.5 --> for clear retina display text
bestScoreText.x = (bestScoreText.contentWidth * 0.5) + 15
bestScoreText.y = 304
hudGroup:insert( bestScoreText )
end
local callNewRound = function( shouldPoof, instantPoof )
local shouldPoof = shouldPoof
local instantPoof = instantPoof
local isGameOver = false
if blastGlow.isVisible then
blastGlow.isVisible = false
end
if gameLives >= 1 then
gameLives = gameLives - 1
if gameLives == 3 then
life4.alpha = 0.3
if monsterCount < 1 then isGameOver = true; end
elseif gameLives == 2 then
life4.alpha = 0.3
life3.alpha = 0.3
if monsterCount < 1 then isGameOver = true; end
elseif gameLives == 1 then
life4.alpha = 0.3
life3.alpha = 0.3
life2.alpha = 0.3
if monsterCount < 1 then isGameOver = true; end
elseif gameLives == 0 then
life4.alpha = 0.3
life3.alpha = 0.3
life2.alpha = 0.3
life1.alpha = 0.3
isGameOver = true
end
elseif gameLives < 0 then
gameLives = 0
life4.alpha = 0.3
life3.alpha = 0.3
life2.alpha = 0.3
life1.alpha = 0.3
isGameOver = true
else
life4.alpha = 0.3
life3.alpha = 0.3
life2.alpha = 0.3
life1.alpha = 0.3
isGameOver = true
end
if shouldPoof then
local poofTheGhost = function()
local theDelay = 300
-- Make ghost disappear and show "poof" animation
ghostObject:setLinearVelocity( 0, 0 )
ghostObject.bodyType = "static"
ghostObject.isVisible = false
ghostObject.isBodyActive = false
ghostObject.rotation = 0
-- Poof code below --
audio.play( ghostPoofSound )
poofObject.x = ghostObject.x; poofObject.y = ghostObject.y
poofObject.alpha = 0
poofObject.isVisible = true
local fadePoof = function()
transition.to( poofObject, { time=2000, alpha=0 } )
end
transition.to( poofObject, { time=100, alpha=1.0, onComplete=fadePoof } )
-- Move camera to far right to see effect
if gameGroup.x > -480 then
local camTween = transition.to( gameGroup, { time=500, x=-480 } )
end
local continueBlink = function()
local startBlinking = function()
if continueText.isVisible then
continueText.isVisible = false
else
continueText.isVisible = true
end
end
continueTimer = timer.performWithDelay( 350, startBlinking, 0 )
end
restartTimer = timer.performWithDelay( theDelay, function()
waitingForNewRound = true;
continueBlink();
end, 1 )
--[[
if not isGameOver then
restartTimer = timer.performWithDelay( theDelay, function() waitingForNewRound = true; end, 1 )
else
if monsterCount > 0 then
restartTimer = timer.performWithDelay( theDelay, function() callGameOver( "no" ); end, 1 )
else
restartTimer = timer.performWithDelay( 3000, function() callGameOver( "yes" ); end, 1 )
end
end
]]--
end
if instantPoof == "yes" then
local poofTimer = timer.performWithDelay( 500, poofTheGhost, 1 )
else
local poofTimer = timer.performWithDelay( 1700, poofTheGhost, 1 )
end
else
ghostObject:setLinearVelocity( 0, 0 )
ghostObject.bodyType = "static"
ghostObject.isVisible = false
ghostObject.isBodyActive = false
ghostObject.rotation = 0
--restartTimer = timer.performWithDelay( 300, startNewRound, 1 )
if not isGameOver then
restartTimer = timer.performWithDelay( 300, startNewRound, 1 )
else
if monsterCount > 0 then
restartTimer = timer.performWithDelay( 300, function() callGameOver( "no" ); end, 1 )
else
restartTimer = timer.performWithDelay( 300, function() callGameOver( "yes" ); end, 1 )
end
end
end
end
local drawBackground = function()
-- Background gets drawn in this order: backdrop, clouds, trees, red glow
-- BACKDROP
backgroundImage1 = display.newImageRect( backgroundFilename1, 480, 320 )
backgroundImage1:setReferencePoint( display.CenterLeftReferencePoint )
backgroundImage1.x = 0; backgroundImage1.y = 160
backgroundImage2 = display.newImageRect( backgroundFilename2, 480, 320 )
backgroundImage2:setReferencePoint( display.CenterLeftReferencePoint )
backgroundImage2.x = 480; backgroundImage2.y = 160
gameGroup:insert( backgroundImage1 )
gameGroup:insert( backgroundImage2 )
-- CLOUDS
clouds1 = display.newImageRect( "clouds-left.png", 480, 320 )
clouds1.x = 240; clouds1.y = 160
clouds2 = display.newImageRect( "clouds-right.png", 480, 320 )
clouds2.x = 720; clouds2.y = 160
clouds3 = display.newImageRect( "clouds-left.png", 480, 320 )
clouds3.x = 1200; clouds3.y = 160
clouds4 = display.newImageRect( "clouds-right.png", 480, 320 )
clouds4.x = 1680; clouds4.y = 160
gameGroup:insert( clouds1 )
gameGroup:insert( clouds2 )
gameGroup:insert( clouds3 )
gameGroup:insert( clouds4 )
-- TREES
local treesLeft = display.newImageRect( "trees-left.png", 480, 320 )
treesLeft.x = 240; treesLeft.y = 160
local treesRight = display.newImageRect( "trees-right.png", 480, 320 )
treesRight.x = 720; treesRight.y = 160
gameGroup:insert( treesLeft )
gameGroup:insert( treesRight )
-- RED GLOW
--[[
local redGlow = display.newImageRect( "redglow.png", 480, 320 )
redGlow.x = 725; redGlow.y = 160
redGlow.alpha = 0.5
gameGroup:insert( redGlow )
]]--
end
local drawHUD = function()
-- TWO BLACK RECTANGLES AT TOP AND BOTTOM (for those viewing from iPad)
local topRect = display.newRect( 0, -160, 480, 160 )
topRect:setFillColor( 0, 0, 0, 255 )
local bottomRect = display.newRect( 0, 320, 480, 160 )
bottomRect:setFillColor( 0, 0, 0, 255 )
hudGroup:insert( topRect )
hudGroup: insert( bottomRect )
-- LIVES DISPLAY
life1 = display.newImageRect( "lifeicon.png", 22, 22 )
life1.x = 20; life1.y = 18
life2 = display.newImageRect( "lifeicon.png", 22, 22 )
life2.x = life1.x + 25; life2.y = 18
life3 = display.newImageRect( "lifeicon.png", 22, 22 )
life3.x = life2.x + 25; life3.y = 18
life4 = display.newImageRect( "lifeicon.png", 22, 22 )
life4.x = life3.x + 25; life4.y = 18
hudGroup:insert( life1 )
hudGroup:insert( life2 )
hudGroup:insert( life3 )
hudGroup:insert( life4 )
-- SCORE DISPLAY
scoreText = display.newText( "0", 470, 22, "Helvetica-Bold", 52 )
scoreText:setTextColor( 255, 255, 255, 255 ) --> white
scoreText.text = gameScore
scoreText.xScale = 0.5; scoreText.yScale = 0.5 --> for clear retina display text
scoreText.x = (480 - (scoreText.contentWidth * 0.5)) - 15
scoreText.y = 20
hudGroup:insert( scoreText )
-- TAP TO CONTINUE DISPLAY
continueText = display.newText( "TAP TO CONTINUE", 240, 18, "Helvetica", 36 )
continueText:setTextColor( 249, 203, 64, 255 )
continueText.xScale = 0.5; continueText.yScale = 0.5
continueText.x = 240; continueText.y = 18
continueText.isVisible = false
hudGroup:insert( continueText )
-- PAUSE BUTTON
local onPauseTouch = function( event )
if event.phase == "release" and pauseBtn.isActive then
audio.play( tapSound )
-- Pause the game
if gameIsActive then
gameIsActive = false
physics.pause()
-- SHADE
if not shadeRect then
shadeRect = display.newRect( 0, 0, 480, 320 )
shadeRect:setFillColor( 0, 0, 0, 255 )
hudGroup:insert( shadeRect )
end
shadeRect.alpha = 0.5
-- SHOW MENU BUTTON
if pauseMenuBtn then
pauseMenuBtn.isVisible = true
pauseMenuBtn.isActive = true
pauseMenuBtn:toFront()
end
pauseBtn:toFront()
-- STOP GHOST ANIMATION
if ghostTween then
transition.cancel( ghostTween )
end
else
if shadeRect then
--shadeRect:removeSelf()
display.remove( shadeRect )
shadeRect = nil
end
if pauseMenuBtn then
pauseMenuBtn.isVisible = false
pauseMenuBtn.isActive = false
end
gameIsActive = true
physics.start()
-- START Ghost animation back up
if ghostTween then
transition.cancel( ghostTween )
end
local function ghostAnimation()
local animUp = function()
if ghostObject.inAir or shotOrb.isVisible then
transition.cancel( ghostTween )
else
ghostTween = transition.to( ghostObject, { time=375, y=190, onComplete=ghostAnimation })
end
end
if ghostObject.inAir or shotOrb.isVisible then
transition.cancel( ghostTween )
else
ghostTween = transition.to( ghostObject, { time=375, y=200, onComplete=animUp })
end
end
ghostTween = transition.to( ghostObject, { time=375, y=190, onComplete=ghostAnimation })
end
end
end
pauseBtn = ui.newButton{
defaultSrc = "pausebtn.png",
defaultX = 44,
defaultY = 44,
overSrc = "pausebtn-over.png",
overX = 44,
overY = 44,
onEvent = onPauseTouch,
id = "PauseButton",
text = "",
font = "Helvetica",
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
pauseBtn.x = 442; pauseBtn.y = 288
pauseBtn.isVisible = false
pauseBtn.isActive = false
hudGroup:insert( pauseBtn )
-- MENU BUTTON (on Pause Display)
local onMenuPauseTouch = function( event )
if event.phase == "release" and pauseMenuBtn.isActive then
audio.play( tapSound )
local onComplete = function ( event )
if "clicked" == event.action then
local i = event.index
if i == 2 then
-- Player click 'Cancel'; do nothing, just exit the dialog
elseif i == 1 then
-- Player clicked Yes, go to main menu
director:changeScene( "loadmainmenu" )
end
end
end
-- Show alert with two buttons
local alert = native.showAlert( "Are You Sure?", "Your current game will end.",
{ "Yes", "Cancel" }, onComplete )
end
end
pauseMenuBtn = ui.newButton{
defaultSrc = "pausemenubtn.png",
defaultX = 44,
defaultY = 44,
overSrc = "pausemenubtn-over.png",
overX = 44,
overY = 44,
onEvent = onMenuPauseTouch,
id = "PauseMenuButton",
text = "",
font = "Helvetica",
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
pauseMenuBtn.x = 38; pauseMenuBtn.y = 288
pauseMenuBtn.isVisible = false
pauseMenuBtn.isActive = false
hudGroup:insert( pauseMenuBtn )
end
local createGround = function()
groundLight1 = display.newImageRect( "groundlight.png", 228, 156 )
groundLight1.x = 150; groundLight1.y = 190
groundObject1 = display.newImageRect( "ground1.png", 480, 76 )
groundObject1:setReferencePoint( display.BottomLeftReferencePoint )
groundObject1.x = 0; groundObject1.y = 320
groundObject2 = display.newImageRect( "ground2.png", 480, 76 )
groundObject2:setReferencePoint( display.BottomLeftReferencePoint )
groundObject2.x = 480; groundObject2.y = 320
groundObject1.myName = "ground"
groundObject2.myName = "ground"
local groundShape = { -240,-18, 240,-18, 240,18, -240,18 }
physics.addBody( groundObject1, "static", { density=1.0, bounce=0, friction=0.5, shape=groundShape } )
physics.addBody( groundObject2, "static", { density=1.0, bounce=0, friction=0.5, shape=groundShape } )
gameGroup:insert( groundLight1 )
gameGroup:insert( groundObject1 )
gameGroup:insert( groundObject2 )
end
local createShotOrb = function()
shotOrb = display.newImageRect( "orb.png", 96, 96 )
shotOrb.xScale = 1.0; shotOrb.yScale = 1.0
shotOrb.isVisible = false
gameGroup:insert( shotOrb )
end
local createGhost = function()
local onGhostCollision = function( self, event )
if event.phase == "began" then
audio.play( impactSound )
if ghostObject.isHit == false then
if blastGlow.isVisible then
blastGlow.isVisible = false
end
if dotTimer then timer.cancel( dotTimer ); end
ghostObject.isHit = true
if event.other.myName == "wood" or event.other.myName == "stone" or event.other.myName == "tomb" or event.other.myName == "monster" then
callNewRound( true, "yes" )
else
callNewRound( true, "no" )
end
local newScore = gameScore + 500
setScore( newScore )
elseif ghostObject.isHit then
return true
end
end
end
-- first, create the transparent arrow that shows up
shotArrow = display.newImageRect( "arrow.png", 240, 240 )
shotArrow.x = 150; shotArrow.y = 195
shotArrow.isVisible = false
gameGroup:insert( shotArrow )
ghostObject = movieclip.newAnim({ "ghost1-waiting.png", "ghost1.png" }, 26, 26 )
ghostObject.x = 150; ghostObject.y = 195
ghostObject.isVisible = false