-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.lua
More file actions
777 lines (614 loc) · 21.1 KB
/
Copy pathmenu.lua
File metadata and controls
777 lines (614 loc) · 21.1 KB
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
local composer = require( "composer" )
local scene = composer.newScene()
local json = require( "json" )
local userClass = require("user")
local sceneGroup
local bombsGroup
local noAdsGroup
local user
local noTouch
local battleIdentifier = ''
local gotoRandomBattle
local battleClass
local allBombs
local noAdsButton
local i=0
local k=0
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
local function enableTouches()
noTouch = false
end
local function stopAnimations()
timer.cancelAll()
transition.cancelAll()
end
function chooseMiniGame()
if (noTouch) then
-- do nothing
else
if (not loadMiniGame()) then
local textBox
noTouch = true
local messageGroup = display.newGroup()
sceneGroup:insert( messageGroup )
local function hideMe()
for i = messageGroup.numChildren, 1, -1 do
messageGroup[i]:removeSelf()
messageGroup[1] = nil
end
textBox:removeSelf()
timer.performWithDelay( 500, enableTouches )
end
local function selectRandomBattle()
hideMe()
--noTouch = false
timer.performWithDelay( 1000, enableTouches )
gotoRandomBattle()
end
messageGroup.x = display.contentCenterX
messageGroup.y = display.contentCenterY
messageGroup.width = display.contentWidth-200
messageGroup.height = display.contentHeight/3
local msgY = -210
local msgBackground = showBox(650, 650, 0, 0, messageGroup, "boxLightBlue")
local closeForm = display.newImageRect( messageGroup, uIimageSheet100, 13, 64, 64 )
closeForm.x = 260
closeForm.y = -260
closeForm:addEventListener( "tap", hideMe )
local infoText = display.newText( messageGroup, "Please provide a valid mini game code", 0, msgY, 400, 100, fontText )
infoText:setFillColor( 0,0,0 )
msgY = msgY + 110
local notValidText = display.newText( messageGroup, "Not a valid code", 0, msgY, 400, 100, fontText )
notValidText:setFillColor( unpack(color.red) )
notValidText.isVisible = false
local function goToSpecificBattle()
local battleString = textBox.text
if (battleString == nil or battleString == '') then
--do nothing
else
--We don't set the battleIdentifier directly from the text box, because it is a global
--variable for this scene, and we don't want to store it unless it is correct
--check if this code returns a valid minigame
local wave = battleClass:getBattleFromString(battleString)
if (#wave == 3) then
battleIdentifier = battleString
hideMe()
--noTouch = false
notValidText.isVisible = false
gotoRandomBattle()
else
notValidText.isVisible = true
end
end
end
local function goToDailyBattle()
local battleString = dailyBattle
if (battleString == nil or battleString == '') then
--do nothing
else
--We don't set the battleIdentifier directly from the text box, because it is a global
--variable for this scene, and we don't want to store it unless it is correct
--check if this code returns a valid minigame
local wave = battleClass:getBattleFromString(battleString)
if (#wave == 3) then
battleIdentifier = battleString
hideMe()
--noTouch = false
notValidText.isVisible = false
gotoRandomBattle()
else
notValidText.isVisible = true
end
end
end
msgY = msgY + 20
local textBoxBackground = display.newImageRect( messageGroup, 'images/ui/form_field.png', 326, 88 )
textBoxBackground.x = -50
textBoxBackground.y = msgY
textBox = native.newTextField(-50,msgY, 326, 88 )
textBox.hasBackground = false
textBox.isEditable = true
textBox.font = native.newFont( native.systemFont, 52 )
textBox:setTextColor( 0, 0, 0 )
messageGroup:insert( textBox )
local msgOkButton = showButton(100, 100, 170, msgY, messageGroup)
msgOkButton:addEventListener( "tap", goToSpecificBattle )
local msgTextOk = display.newText( messageGroup, "GO", 170, msgY, fontText, 36 )
msgTextOk:setTextColor( 0, 0, 0 )
msgY = msgY + 140
local seeCampaignButton = display.newText( messageGroup, "Or", -200, msgY, fontText )
seeCampaignButton:setFillColor( 0.82, 0.86, 1 )
local msgRandomButton = showButton(440, 100, 0, msgY, messageGroup)
--msgRandomButton:setFillColor( 1, 1, 1 )
msgRandomButton.comingFrom = "msgRandomButton"
msgRandomButton:addEventListener( "tap", selectRandomBattle )
local msgTextRandom = display.newText( messageGroup, "Get Random Battle", 0, msgY, fontText, 36 )
msgTextRandom:setTextColor( 0, 0, 0 )
msgY = msgY + 140
local orText = display.newText( messageGroup, "Or", -200, msgY, fontText )
orText:setFillColor( 0.82, 0.86, 1 )
--local msgDailyButton = display.newRect( messageGroup, 0, msgY, 320, 80 )
local msgDailyButton = showButton(440, 100, 0, msgY, messageGroup)
msgDailyButton.comingFrom = "msgDailyBattle"
msgDailyButton:addEventListener( "tap", goToDailyBattle )
--msgDailyButton.isVisible = false
local msgTextDaily = display.newText( messageGroup, "Play daily battle", 0, msgY, fontText, 36 )
msgTextDaily:setTextColor( 0, 0, 0 )
local function getDailyBattle()
-- The following sample code contacts Google's encrypted search over SSL
-- and prints the response (in this case, the HTML source of the home page)
-- to the Corona terminal.
local function networkListener( event )
if ( event.isError ) then
print( "Network error: ", event.response )
else
--print ( "RESPONSE: " .. event.response )
local responceTable = json.decode( event.response )
if (false ~= responceTable) then
dailyBattle = responceTable[1]
print(dailyBattle)
msgDailyButton.isVisible = true
end
end
end
-- Access Google over SSL:
network.request( "https://www.cardbasher.com/php/daily.php", "GET", networkListener )
end
--Because calling the api works asynchrounsly, get the daily battle if possible right now
if ('' == dailyBattle) then
getDailyBattle()
else
msgDailyButton.isVisible = true
end
end
end
end
function editUsername()
noTouch = true
local textBox
local messageGroup = display.newGroup()
sceneGroup:insert( messageGroup )
local function saveName (event)
local username = textBox.text
if (username == nil or username == '') then
--do nothing
else
user.username = username
user:save()
-- And also hide this message
for i = messageGroup.numChildren, 1, -1 do
messageGroup[i]:removeSelf()
messageGroup[1] = nil
end
textBox:removeSelf()
native.setKeyboardFocus( nil )
--noTouch = false
timer.performWithDelay( 1000, enableTouches )
end
end
messageGroup.x = display.contentCenterX
messageGroup.y = display.contentCenterY
messageGroup.width = display.contentWidth-200
messageGroup.height = display.contentHeight/3
local msgBackground = showBox(650, 650, 0, 0, messageGroup, "boxLightBlue")
local descriptionText = display.newText( messageGroup, "Please provide a player name", 0, -150, fontText )
descriptionText:setTextColor( 0, 0, 0 )
local msgOkButton = showButton(100, 100, 0, 130, messageGroup)
msgOkButton:addEventListener( "tap", saveName )
local msgTextOk = display.newText( messageGroup, "OK", 0, 130, globalFont, 36 )
msgTextOk:setTextColor( 0, 0, 0 )
local textBoxBackground = display.newImageRect( messageGroup, 'images/ui/form_field.png', 426, 88 )
textBoxBackground.x = 0
textBoxBackground.y = 0
textBox = native.newTextField(0,0, 386, 68 )
textBox.hasBackground = false
textBox.isEditable = true
textBox.font = native.newFont( native.systemFont, 52 )
textBox:setTextColor( 0, 0, 0 )
messageGroup:insert( textBox )
end
local function gotoDifficultBattle()
stopMenuMusic()
if (noTouch) then
-- We would do a card Tap here, but we have noTouch!
return true
else
local options = {
effect = "crossFade",
time = 800,
params = {
gameType="DevTesting"
}
}
composer.setVariable( "gameType" , "DevTesting" )
composer.setVariable( "campaignBattleKey" , 0 )
composer.setVariable( "campaignBattles" , {} )
composer.setVariable("user",user)
composer.setVariable( "comingFrom" , "menu" )
stopAnimations()
composer.gotoScene( "battle", options )
end
end
function gotoRandomBattle()
local options = {
effect = "crossFade",
time = 800,
params = {
gameType="RandomMini"
}
}
composer.setVariable( "gameType" , "RandomMini" )
composer.setVariable( "campaignBattleKey" , 0 )
composer.setVariable( "campaignBattles" , {} )
composer.setVariable( "battleIdentifier" , battleIdentifier )
-- Reset the battleIdentifier so that if we come back to the menu, we don't have it stored
battleIdentifier = ''
composer.setVariable("user",user)
composer.setVariable( "comingFrom" , "menu" )
stopAnimations()
composer.gotoScene( "battle", options )
end
local function gotoTutorial()
if (noTouch) then
-- We would do a card Tap here, but we have noTouch!
return true
else
local options = {
effect = "crossFade",
time = 800
}
stopAnimations()
composer.gotoScene( "tutorialnew", options )
end
end
local function gotoCampaignBattle()
if (noTouch) then
-- We would do a card Tap here, but we have noTouch!
return true
else
stopMenuMusic()
local options = {
effect = "crossFade",
time = 800
}
composer.setVariable( "gameType" , "CampaignEasy" )
composer.setVariable( "campaignBattleKey" , 0 )
composer.setVariable( "campaignBattles" , {} )
composer.setVariable( "user" , user)
composer.setVariable( "comingFrom" , "menu" )
stopAnimations()
composer.gotoScene( "battle", options )
end
end
function loadMiniGame()
local filePath = system.pathForFile( "battle_RandomMini.json", system.DocumentsDirectory )
local file = io.open( filePath, "r" )
local fileBattle
if file then
local contents = file:read( "*a" )
io.close( file )
fileBattle = json.decode( contents )
end
if ( fileBattle == nil or next(fileBattle) == nil) then
-- Do nothing
return false
--battle = battle
else
local toLoad = {}
battleIdentifier = fileBattle['battleIdentifier']
if ("" == battleIdentifier) then
return false
else
gotoRandomBattle()
return true
end
end
end
local function gotoCampaignView()
if (noTouch) then
-- We would do a card Tap here, but we have noTouch!
return true
else
local options = {
effect = "crossFade",
time = 800,
params = {
gameType="CampaignEasy"
}
}
composer.setVariable( "comingFrom" , "menu" )
composer.setVariable( "user" , user )
composer.setVariable("newSkillToUnlock", '')
stopAnimations()
composer.gotoScene( "campaign", options )
end
end
local function gotoTest()
if (noTouch) then
-- We would do a card Tap here, but we have noTouch!
return true
else
local options = {
effect = "crossFade",
time = 800,
params = {
gameType="CampaignEasy"
}
}
composer.setVariable( "comingFrom" , "menu" )
composer.setVariable("user",user)
stopAnimations()
composer.gotoScene( "test", options )
end
end
local function showSettings()
if (noTouch) then
-- We would do a card Tap here, but we have noTouch!
return true
else
local options = {
effect = "crossFade",
time = 800
}
stopAnimations()
composer.gotoScene( "settings", options )
end
end
local function showGlossary()
if (noTouch) then
-- We would do a card Tap here, but we have noTouch!
return true
else
local options = {
effect = "crossFade",
time = 800
}
stopAnimations()
composer.gotoScene( "glossary", options )
end
end
local function showChallenges()
if (noTouch) then
-- We would do a card Tap here, but we have noTouch!
return true
else
local options = {
effect = "crossFade",
time = 800
}
stopAnimations()
composer.gotoScene( "challenges", options )
end
end
local function showAchievements()
if (noTouch) then
-- We would do a card Tap here, but we have noTouch!
return true
else
local options = {
effect = "crossFade",
time = 800
}
stopAnimations()
composer.gotoScene( "achievements", options )
end
end
local function showMenuButton(buttonY, buttonListener, buttonText, parentGroup)
parentGroup = parentGroup or sceneGroup
local seeMenuButton = display.newImageRect( parentGroup, "images/ui/main_menu_button.png", 470, 85 )
seeMenuButton.x = display.contentCenterX
seeMenuButton.y = buttonY
seeMenuButton:addEventListener( "tap", buttonListener )
local seeMenuText = display.newText( parentGroup, buttonText, display.contentCenterX, buttonY, fontText, 48 )
seeMenuText:setFillColor( 0, 0, 0 )
local buttonElements = {}
buttonElements.button = seeMenuButton
buttonElements.text = seeMenuText
return buttonElements
end
local function purchaseNoAds()
if (storeIsActive) then
print('we will try to purchase the no ads now')
store.purchase( 'com.koyan.cardbasher.noads' )
else
print('store not initialised')
end
end
local function goToReview()
local targetAppStore = system.getInfo( "targetAppStore" )
--print("targetAppStore is ", targetAppStore)
if ( "apple" == targetAppStore ) then -- iOS
system.openURL( "https://apps.apple.com/us/app/cardbasher/id1578705117" )
elseif ( "google" == targetAppStore ) then -- Android
system.openURL( "https://play.google.com/store/apps/details?id=com.koyan.cardbasher" )
else
system.openURL( "https://play.google.com/store/apps/details?id=com.koyan.cardbasher" )
end
end
local function showNoAds()
if (noTouch) then
-- We would do a card Tap here, but we have noTouch!
return true
else
local noAdsLocalGroup = display.newGroup()
noAdsGroup:insert( noAdsLocalGroup )
local background = display.newImageRect( noAdsLocalGroup, "images/ui/main_menu_background.png", 720, 1560 )
background.x = display.contentCenterX
background.y = display.contentCenterY
local backButton = display.newImageRect( noAdsLocalGroup, uIimageSheet100, 14, 100, 100 )
backButton.x = display.contentWidth - 80
backButton.y = 680
local textOptions =
{
parent = noAdsLocalGroup,
text = "You can play the game with no ads by purchasing the NoAds upgrade",
x = display.contentCenterX,
y = 780,
width = 440,
height = 200,
font = fontText,
fontSize = 34,
align = "center" -- Alignment parameter
}
local seeMenuText = display.newText( textOptions )
seeMenuText:setFillColor( 0, 0, 0 )
local function closeNoAds()
display.remove( noAdsLocalGroup )
print('here')
end
backButton:addEventListener( "tap", closeNoAds )
showMenuButton(1000, purchaseNoAds, "Purchase", noAdsLocalGroup)
end
end
local function showRandomBomb()
local startX = math.random(10, 190)
if (startX >100) then
startX = startX + 500
i = i+1
else
k = k+1
end
local startY = math.random(-300, 0)
local size = math.random(6,10) * 0.1
local newBomb = display.newImageRect( bombsGroup, "images/ui/main_menu_bomb.png", 18*size, 81*size )
newBomb.x = startX
newBomb.y = startY
local function onArrive(item)
item:removeSelf()
end
--local fallSpeed = math.random(7000, 10000)
local fallSpeed = 18000 / size
transition.moveTo( newBomb, { x=startX, y=1520, time=fallSpeed, onComplete=onArrive} )
timer.performWithDelay( 1500, showRandomBomb )
end
local function hideNoAdsButton()
if (user.noAds == 0 and storeIsActive) then
timer.performWithDelay(1000, hideNoAdsButton)
print('here')
else
noAdsButton.button:removeSelf()
noAdsButton.text:removeSelf()
end
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
battleClass = require("battleClass")
user = userClass:new()
user:load()
sceneGroup = self.view
composer.setVariable( "menuMusic" , audio.loadStream('sounds/Menu_START.mp3') )
composer.setVariable( "menuMusicStatus" , 'playing' )
audio.play(composer.getVariable( "menuMusic" ),{
channel = musicChannel,
onComplete = loopMenuMusic
})
-- Code here runs when the scene is first created but has not yet appeared on screen
local background = display.newImageRect( sceneGroup, "images/ui/main_menu_background.png", 720, 1560 )
background.x = display.contentCenterX
background.y = display.contentCenterY
local battle = battleClass:new('',0,'')
local buttonDistance = 110
local buttonY = display.contentCenterY
showMenuButton(buttonY, gotoTutorial, "Tutorial")
buttonY = buttonY + buttonDistance
showMenuButton(buttonY, gotoCampaignView, "Play Campaign")
--buttonY = buttonY + buttonDistance
--showMenuButton(buttonY, gotoDifficultBattle, "Play Dev Battle")
--buttonY = buttonY + buttonDistance
--showMenuButton(buttonY, chooseMiniGame, "Play Random Battle")
--buttonY = buttonY + buttonDistance
--showMenuButton(buttonY, gotoTest, "Test")
buttonY = buttonY + buttonDistance
showMenuButton(buttonY, showChallenges, "Challenges")
--buttonY = buttonY + buttonDistance
--showMenuButton(buttonY, showGlossary, "Glossary")
buttonY = buttonY + buttonDistance
showMenuButton(buttonY, showAchievements, "Achievements")
--printTable('user', user)
if (storeIsActive) then
if (user.noAds == 0) then
buttonY = buttonY + buttonDistance
noAdsButton = showMenuButton(buttonY, showNoAds, "No Ads")
timer.performWithDelay(1000, hideNoAdsButton)
end
end
if (user.showReview == 1) then
buttonY = buttonY + buttonDistance
reviewButton = showMenuButton(buttonY, goToReview, "Review")
end
buttonY = buttonY + 60
local versionText = display.newText( sceneGroup, "Version " .. battle.version, display.contentCenterX, buttonY, globalFont, 30 )
versionText:setFillColor( 0, 0, 0 )
local settingsButton = display.newImageRect( sceneGroup, uIimageSheet100, 11, 86, 86 )
settingsButton.x = display.contentWidth - 60
settingsButton.y = display.contentHeight - 100
settingsButton:addEventListener( "tap", showSettings )
bombsGroup = display.newGroup()
sceneGroup:insert( bombsGroup )
showRandomBomb()
noAdsGroup = display.newGroup()
sceneGroup:insert( noAdsGroup )
if (user.username == '' or user.username == nil) then
editUsername()
end
end
function loopMenuMusic( event )
if event.completed then
composer.setVariable( "menuMusic" , audio.loadStream('sounds/Menu_LOOP.mp3') )
audio.play(composer.getVariable( "menuMusic" ),{
channel = musicChannel,
loops = -1
})
end
end
-- show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
if composer.getVariable( "menuMusicStatus" ) == 'silent' then
composer.setVariable( "menuMusic" , audio.loadStream('sounds/Menu_START.mp3') )
composer.setVariable( "menuMusicStatus" , 'playing' )
audio.play(composer.getVariable( "menuMusic" ),{
channel = musicChannel,
onComplete = loopMenuMusic
})
end
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
local stopBombs = true
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
composer.removeScene( "menu" )
scene:destroy()
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene