-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscGameScreen.lua
executable file
·717 lines (543 loc) · 20 KB
/
scGameScreen.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
local levelCompletePraises = {"NICE JOB!", "GREAT!", "AMAZING!","EXCEPTIONAL!"}
local jsonUtils = require("jsonUtils")
local file = require("fileFunctions")
local widget = require("widget")
local composer = require "composer"
-- local ads = require("ads")
local dictionaryFunction = jsonUtils.init({jsonFileName = "words.json", path = system.ResourceDirectory})
local dictionary = dictionaryFunction:load().words
local scene = composer.newScene()
local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight
local boxSize = 70
local boxGap = 10
local cornerRadius = 10
local font = "Century Gothic"
--objects
local gameObjects
local letterGroup
local letters
local containerGroup, puzzleNoDisplay, containers, markers, markerGroup
--functions
local onTouch,createLevel,shuffleString,shufflePuzzle
--helpful functions **can be transfered to a utilities module sometime in the future
local function distBetween( x1, y1, x2, y2 ) --gets distance between two points
local xFactor = x2 - x1
local yFactor = y2 - y1
local dist = math.sqrt( (xFactor*xFactor) + (yFactor*yFactor) )
return dist
end
local function breath(obj,delay,time,increase)
transition.to(obj,{time=time,delay=delay,xScale=increase,yScale=increase,onComplete = function(obj)
transition.to(obj,{time=time,delay=delay,xScale=1,yScale=1,onComplete = function(obj)
breath(obj,delay,time,increase)
end})
end})
end
function shuffleString(inputStr)
local outputStr = "";
local strLength = string.len(inputStr);
while (strLength ~=0) do
--get a random character of the input string
local pos = math.random(strLength);
--insert into output string
outputStr = outputStr..string.sub(inputStr,pos,pos);
--remove the used character from input string
inputStr = inputStr:sub(1, pos-1) .. inputStr:sub(pos+1);
--get new length of the input string
strLength = string.len(inputStr);
end
return outputStr;
end
function shufflePuzzle(puzzle)
local fullString = ""
for i=1,#puzzle do
for k=1,#puzzle[i] do
fullString = fullString..string.sub(puzzle[i],k,k)
end
end
print(fullString)
return shuffleString(fullString)
end
function scene.scanForWords()
local lettersArray = {}
for i=1,#letters do
lettersArray[letters[i].i] = letters[i].letter.text
end
local words = {}
local correctWordsCount = 0
for i=1,3 do
words[i] = ""
for k=1,3 do
words[i] = words[i]..lettersArray[(i-1)*3+k]
end
--check on the database if the words exist
if dictionary[words[i]] ~= nil then
correctWordsCount = correctWordsCount + 1
for k=1,3 do
for j=1,#letters do
if letters[j].i == (i-1)*3+k then
letters[j].shape:setFillColor(unpack(colorPalette.dark))
letters[j].letter:setFillColor(unpack(colorPalette.primary))
end
end
end
markers[i].cross.alpha = 0
markers[i].check.alpha = 1
else
for k=1,3 do
for j=1,#letters do
if letters[j].i == (i-1)*3+k then
letters[j].shape:setFillColor(unpack(colorPalette.primary))
letters[j].letter:setFillColor(unpack(colorPalette.light))
end
end
end
markers[i].cross.alpha = 1
markers[i].check.alpha = 0
end
end
if correctWordsCount == 3 then
print("Well done!")
for i=1,#letters do
letters[i]:removeEventListener( "touch", onTouch )
end
local previousLevel = gameDataTable.currentLevel
local unlockedLevel = 0
if gameDataTable.currentLevel + 1 > #gameDataTable.levels then
print("maximum level reached. Till next update! :)")
else
--mark level as "done"
if gameDataTable.levels[gameDataTable.currentLevel].status == "unlocked" then
gameDataTable.levels[ gameDataTable.currentLevel].status = "done"
--unlock a new level (first locked level to encounter)
for i=1,#gameDataTable.levels do
if gameDataTable.levels[i].status == "locked" then
gameDataTable.levels[i].status = "unlocked"
unlockedLevel = i
break
end
end
end
--goto next level
for i=gameDataTable.currentLevel+1,#gameDataTable.levels do
if gameDataTable.levels[i].status == "unlocked" then
gameDataTable.currentLevel = i
break
end
end
gameData:save(gameDataTable)
local newLevel = display.newGroup()
newLevel.anchorChildren = true
gameObjects:insert(newLevel)
local function handleButtonEvent(event)
if event.phase == "ended" then
if event.target.id == "next" then
scene.createLevel()
display.remove(newLevel)
newLevel = nil
elseif event.target.id == "facebook" then
system.openURL( "http://on.fb.me/1QUoO4z" )
else
local options =
{
supportedAndroidStores = { "google", "amazon" }
}
native.showPopup( "appStore", options )
end
end
end
gamePlayCounter = gamePlayCounter + 1
if gamePlayCounter >= adsFrequency then
-- if ads.isLoaded("interstitial") then
-- gamePlayCounter = 0
-- ads.show("interstitial",{appId=admob_interstitial})
-- ads.load("interstitial",{appId=admob_interstitial})
-- end
end
transition.to(letterGroup,{delay=500,alpha=0,time=500,onComplete = function()
local newLevelbackground = display.newRect(newLevel,centerX,centerY,_W,_H)
newLevelbackground:setFillColor(unpack(colorPalette.background))
newLevelbackground.alpha = 0.9
local congratulations = display.newText(newLevel,levelCompletePraises[math.random(1,#levelCompletePraises)],centerX,60,font,25)
congratulations:setFillColor(unpack(colorPalette.light))
local ribbons = display.newImageRect(newLevel,"res/ribbons.png",344*0.3,212*0.3)
ribbons.x, ribbons.y = centerX,centerY - 70
local y = ribbons.y + ribbons.contentHeight/2 + 30
local x = centerX - 80
for i=1,#words do
local word = display.newText(newLevel,words[i],x,y,font,25)
x = x + 80
end
local button_next = widget.newButton
{
id = "next",
width = 396*0.3,
height = 174*0.3,
defaultFile = "res/next.png",
overFile = "res/next.png",
x = centerX,
y = centerY + 150,
onEvent = handleButtonEvent
}
newLevel:insert(button_next)
local levelDone = display.newText(newLevel,"PUZZLE "..previousLevel.." IS SOLVED!",centerX,congratulations.y + 30,font,15)
levelDone:setFillColor(unpack(colorPalette.light))
if unlockedLevel ~= 0 then
local levelUnlocked = display.newText(newLevel,"PUZZLE "..unlockedLevel.." UNLOCKED",centerX,levelDone.y + levelDone.contentHeight/2 + 10,font,15)
levelUnlocked:setFillColor(unpack(colorPalette.light))
end
--show promotional stuff
if 1 == math.random(1,3) then
local bar = display.newRect(newLevel,centerX,y + 25 + 35,_W,35)
bar:setFillColor(unpack(colorPalette.light))
if 1 == math.random(1,2) then
local button_promo = widget.newButton
{
id = "facebook",
width = 100*0.3,
height = 86*0.3,
defaultFile = "res/like.png",
overFile = "res/like.png",
x = bar.x,
y = bar.y,
onEvent = handleButtonEvent,
label = "Visit BlueboxLab",
font = font,
fontSize = bar.contentHeight/2 ,
labelColor = { default= {unpack(colorPalette.dark)}, over={ 0, 0, 0, 0.5 } },
labelAlign = "left",
labelXOffset = 30
}
newLevel:insert(button_promo)
else
local button_promo = widget.newButton
{
id = "rate",
width = 128*0.25,
height = 127*0.25,
defaultFile = "res/rate.png",
overFile = "res/rate.png",
x = bar.x,
y = bar.y,
onEvent = handleButtonEvent,
label = "Rate Us",
font = font,
fontSize = bar.contentHeight/2 ,
labelColor = { default= {unpack(colorPalette.dark)}, over={ 0, 0, 0, 0.5 } },
labelAlign = "left",
labelXOffset = 30
}
newLevel:insert(button_promo)
end
end
newLevel.x,newLevel.y = centerX,centerY
end })
end
end
end
function scene.createLevel()
--local words = {"LEG","ROW","HEN"}
--local puzzle = shufflePuzzle(words)
--print(puzzle)
local puzzle = shufflePuzzle(gameDataTable.levels[gameDataTable.currentLevel].words)
puzzleNoDisplay.text = "PUZZLE "..gameDataTable.currentLevel
--clean previous level
display.remove(letterGroup)
letterGroup = nil
display.remove(markerGroup)
markerGroup = nil
letterGroup = display.newGroup()
gameObjects:insert(letterGroup)
letterGroup.anchorChildren = true
letterGroup.x,letterGroup.y = containerGroup.x,containerGroup.y
letters = {}
for i=1,string.len(puzzle) do
letters[i] = display.newGroup()
letters[i].anchorChildren = true
local shape = display.newRoundedRect(letters[i],boxSize/2,boxSize/2,boxSize,boxSize,cornerRadius)
shape:setFillColor(unpack(colorPalette.primary))
letters[i].shape = shape
local letter = display.newText(letters[i],string.sub(puzzle,i,i),boxSize/2,boxSize/2,font,boxSize/2)
letter:setFillColor(unpack(colorPalette.light))
letters[i].letter = letter
letters[i].x,letters[i].y = containers[i].x,containers[i].y
letters[i].i = i --to know what container is associated with them
--letters[i].
letters[i]:addEventListener( "touch", onTouch )
letterGroup:insert(letters[i])
end
markerGroup = display.newGroup()
markerGroup.anchorChildren = true
gameObjects:insert(markerGroup)
markers = {}
for i=1,3 do
markers[i] = display.newGroup()
markers[i].anchorChildren = true
local check = display.newImageRect(markers[i],"res/check-white.png",64*0.3,57*0.3)
check.x, check.y = 16,16
markers[i].check = check
local cross = display.newImageRect(markers[i],"res/cross-white.png",64*0.3,64*0.3)
cross.x, cross.y = 16,16
markers[i].cross = cross
markers[i].x, markers[i].y = 16,containers[i*3].y
markerGroup:insert(markers[i])
end
markerGroup.x, markerGroup.y = containerGroup.x + containerGroup.contentWidth/2 +20, containerGroup.y
scene.scanForWords()
end
function scene:create( event )
local sceneGroup = self.view
gameObjects = display.newGroup()
-- Initialize the scene here
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
local background = display.newRect(sceneGroup,centerX,centerY,_W,_H)
background:setFillColor(unpack(colorPalette.background))
puzzleNoDisplay = display.newText(gameObjects,"PUZZLE "..gameDataTable.currentLevel,centerX,40,font,20)
puzzleNoDisplay:setFillColor(unpack(colorPalette.light))
--containers
containerGroup = display.newGroup()
gameObjects:insert(containerGroup)
containerGroup.anchorChildren = true
containerGroup.x, containerGroup.y = centerX,centerY - 30
containers = {}
local x = boxSize/2
local y = boxSize/2
for i=1,3 do
for k=1,3 do
containers[#containers+1] = display.newRoundedRect(containerGroup,x,y,boxSize+6,boxSize+6,cornerRadius+1)
x = x + boxGap + boxSize
end
x = boxSize/2
y = boxSize + boxGap + y
end
containerGroup.alpha = 0.1
--gameObjects:insert(letterGroup)
local function handleButtonEvent(event)
if event.phase == "ended" then
if event.target.id == "settings" then
composer.showOverlay("scSettings",{isModal = true})
elseif event.target.id == "levels" then
composer.gotoScene("scLevels")--,{isModal = true})
elseif event.target.id == "shuffle" then
scene.createLevel()
elseif event.target.id == "sharePuzzle" then
display.save( sceneGroup, "snap.png", system.CachesDirectory)
local serviceName = "facebook"
local msg = ""
local isAvailable = native.canShowPopup( "social", serviceName )
if isAvailable then
print("Social popup is available, serviceName: ",serviceName)
local listener = {}
function listener:popup( event )
print( "name(" .. event.name .. ") type(" .. event.type .. ") action(" .. tostring(event.action) .. ") limitReached(" .. tostring(event.limitReached) .. ")" )
end
-- Show the popup
native.showPopup( "social",
{
service = serviceName, -- The service key is ignored on Android.
message = msg,
listener = listener,
image =
{
{ filename = "snap.png", baseDir = system.CachesDirectory },
},
url =
{
""
}
})
else
if "simulator" == system.getInfo( "environment" ) then
native.showAlert( "Build for device", "This plugin is not supported on the Corona Simulator, please build for an iOS/Android device or the Xcode simulator", { "OK" } )
else
-- Popup isn't available.. Show error message
native.showAlert( "Cannot send " .. serviceName .. " message.", "Please setup your " .. serviceName .. " account or check your network connection", { "OK" } )
end
end
end
end
end
local button_settings = widget.newButton
{
id = "settings",
width = 128*0.3,
height = 127*0.3,
defaultFile = "res/settings.png",
overFile = "res/settings.png",
x = _W - 30 ,
y = 40,
onEvent = handleButtonEvent,
}
sceneGroup:insert(button_settings)
-- local button_sharePuzzle = widget.newButton
-- {
-- id = "sharePuzzle",
-- width = 128*0.35,
-- height = 127*0.35,
-- defaultFile = "res/sharePuzzle.png",
-- overFile = "res/sharePuzzle.png",
-- x = centerX - 50 ,
-- y = containerGroup.y + containerGroup.contentHeight/2 + 60,
-- onEvent = handleButtonEvent,
-- }
-- sceneGroup:insert(button_sharePuzzle)
local button_levels = widget.newButton
{
id = "levels",
width = 128*0.35,
height = 127*0.35,
defaultFile = "res/levels.png",
overFile = "res/levels.png",
x = centerX + 30 ,
y = containerGroup.y + containerGroup.contentHeight/2 + 60,
onEvent = handleButtonEvent,
}
sceneGroup:insert(button_levels)
local button_shuffle = widget.newButton
{
id = "shuffle",
width = 128*0.35,
height = 127*0.35,
defaultFile = "res/shuffle.png",
overFile = "res/shuffle.png",
x = centerX - 30,
y = containerGroup.y + containerGroup.contentHeight/2 + 60,
onEvent = handleButtonEvent,
}
sceneGroup:insert(button_shuffle)
sceneGroup:insert(gameObjects)
function onTouch( event )
local t = event.target
local phase = event.phase
if "began" == phase then
-- Make target the top-most object
local parent = t.parent
parent:insert( t )
display.getCurrentStage():setFocus( t )
-- Spurious events can be sent to the target, e.g. the user presses
-- elsewhere on the screen and then moves the finger over the target.
-- To prevent this, we add this flag. Only when it's true will "move"
-- events be sent to the target.
t.isFocus = true
t.xScale,t.yScale = 0.7,0.7
-- Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
elseif t.isFocus then
if "moved" == phase then
-- Make object move (we subtract t.x0,t.y0 so that moves are
-- relative to initial grab point, rather than object "snapping").
t.x = event.x - t.x0
t.y = event.y - t.y0
--[[ not needed for now
-- Gradually show the shape's stroke depending on how much pressure is applied.
if ( event.pressure ) then
t:setStrokeColor( 1, 1, 1, event.pressure )
end
--]]
for i=1,#letters do
if distBetween(letters[i].x,letters[i].y,t.x,t.y) < boxSize/2 + boxGap then
if letters[i] ~= t and i ~= letterGroup.toSwap then
transition.to(letters[i],{time=100,xScale=0.8,yScale=0.8})
if letterGroup.toSwap ~= nil then
transition.cancel(letters[letterGroup.toSwap])
letters[letterGroup.toSwap].xScale,letters[letterGroup.toSwap].yScale = 1,1
end
letterGroup.toSwap = i
break
end
else
if letters[letterGroup.toSwap] ~= nil then
transition.cancel(letters[letterGroup.toSwap])
letters[letterGroup.toSwap].xScale,letters[letterGroup.toSwap].yScale = 1,1
letterGroup.toSwap = nil
end
end
end
elseif "ended" == phase or "cancelled" == phase then
--find nearest container
local found = false
for i=1,#containers do
if distBetween(containers[i].x,containers[i].y,t.x,t.y) < boxSize/2 + boxGap then
--look for the letter in the container
local j
for k = 1,#letters do
if letters[k].i == i then
j = k
break
end
end
transition.to(letters[j],{time=200,xScale=1,yScale=1,x=containers[t.i].x,y=containers[t.i].y,onComplete = function()
local swappedI = letters[j].i
letters[j].i = t.i
t.i = swappedI
scene.scanForWords()
end })
transition.to(t,{time=200,xScale=1,yScale=1,x =containers[i].x,y=containers[i].y })
found = true
break
end
end
if not found then
transition.to(t,{time=200,xScale=1,yScale=1,x =containers[t.i].x,y=containers[t.i].y })
end
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
-- Important to return true. This tells the system that the event
-- should not be propagated to listeners of any objects underneath.
return true
end
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
-- Called when the scene is now on screen
-- Insert code here to make the scene come alive
-- Example: start timers, begin animation, play audio, etc.
local previousScene = composer.getSceneName( "previous" )
if previousScene ~= nil then
composer.removeScene( previousScene )
end
self.createLevel()
-- ads:setCurrentProvider( "admob" )
-- ads.show("banner",{x=0,y=100000, appId=admob_bannerAppID})
-- print("Admob: Try to load fullscreen ad for gameOver scene")
-- ads.load("interstitial",{appId=admob_interstitial})
end
end
-- "scene:hide()"
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is on screen (but is about to go off screen)
-- Insert code here to "pause" the scene
-- Example: stop timers, stop animation, stop audio, etc.
elseif ( phase == "did" ) then
-- Called immediately after scene goes off screen
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's view
-- Insert code here to clean up the scene
-- Example: remove display objects, save state, etc.
end
-- -------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -------------------------------------------------------------------------------
return scene