-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.lua
executable file
·736 lines (562 loc) · 18.2 KB
/
main.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
--***********************************************************************************************--
--***********************************************************************************************--
-- ===========
-- PUZZLE APP
-- ===========
-- Created by Jonathan Beebe.
-- http://jonbeebe.net
-- @jonathanbeebe
-- File: main.lua
--
-- Version 1.0
--
-- Copyright (C) 2011 ANSCA Inc. All Rights Reserved.
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in the
-- Software without restriction, including without limitation the rights to use, copy,
-- modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
-- and to permit persons to whom the Software is furnished to do so, subject to the
-- following conditions:
--
-- The above copyright notice and this permission notice shall be included in all copies
-- or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
-- INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
-- PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
-- FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-- DEALINGS IN THE SOFTWARE.
--
-- Published changes made to this software and associated documentation and module files (the
-- "Software") may be used and distributed by ANSCA, Inc. without notification. Modifications
-- made to this software and associated documentation and module files may or may not become
-- part of an official software release. All modifications made to the software will be
-- licensed under these same terms and conditions.
--***********************************************************************************************--
--***********************************************************************************************--
-- Set below to false if performance lags
local shadowOn = true
-- When you place a puzzle piece, it checks to see if you're within a certain
-- pixel range to the final spot that piece is supposed to go. If you place
-- the piece within that range, the app assumes you meant to put it at exactly
-- the right spot and auto-adjusts to fit at exactly the right location.
-- Value below needs to be adjusted once tested on device with proper dimensions
-- (I only had an iPod touch to test on).
local placementBuffer = 8
-- Below is another "cushion" setting so when you tap the piece, it will rotate.
-- Because a finger is subject to much more movement than a mouse pointer, this
-- also needs to be adjusted once tested on the proper device:
local rotateBuffer = 7
--
local ui = require( "ui" )
local screenGroup = display.newGroup()
local puzzleNum = 1
--
local loadNewPuzzle = function()
math.randomseed(os.time())
local mRand = math.random
local pzObject = {}
local pzShadow = {}
local pzMask = {}
local pzGlow, glowTween
local bgImage
local puzzlePreview
local dimRect
-- ui buttons
local newPuzzleBtn
local previewBtn
local solveItBtn
local isDragging
local isSelected = false
local lastPiece, finalPiece
local lastShadow, finalShadow
local placedCount = 0
local maxPieces = 25
local isReady = false
local halfW = display.contentCenterX
local halfH = display.contentCenterY
--===================================================================================
--
--
--===================================================================================
local resetReference = function( index )
local i = index
-- SET PUZZLE REFERENCE POINT DEPENDING ON MASK PIECE
local max1 = 5
local max2 = 10
local max3 = 15
local max4 = 20
local max5 = 25
local xRef, yRef
if i <= max1 then
xRef = -250 + ( i * 100 - 50 )
yRef = -250 + 50
elseif i > max1 and i <= max2 then
xRef = -250 + ( (i - max1) * 100 - 50 )
yRef = -250 + ( 2 * 100 - 50 )
elseif i > max2 and i <= max3 then
xRef = -250 + ( (i - max2) * 100 - 50 )
yRef = -250 + ( 3 * 100 - 50 )
elseif i > max3 and i <= max4 then
xRef = -250 + ( (i - max3) * 100 - 50 )
yRef = -250 + ( 4 * 100 - 50 )
elseif i > max4 and i <= max5 then
xRef = -250 + ( (i - max4) * 100 - 50 )
yRef = -250 + ( 5 * 100 - 50 )
end
pzObject[i].xReference = xRef
pzObject[i].yReference = yRef
--SHADOW
if shadowOn then
pzShadow[i].xReference = xRef
pzShadow[i].yReference = yRef
end
pzObject[i].top = pzObject[i].y - 50
pzObject[i].bottom = pzObject[i].y + 50
pzObject[i].left = pzObject[i].x - 50
pzObject[i].right = pzObject[i].x + 50
end
--===================================================================================
--
--
--===================================================================================
local checkLocation = function( index )
local i = index
pzObject[i].xReference = 0
pzObject[i].yReference = 0
local xCenter = 300
local yCenter = 674--512
-- Allow a little deviation (so they don't have to be pixel perfect)
local left = xCenter - placementBuffer
local right = xCenter + placementBuffer
local top = yCenter - placementBuffer
local bottom = yCenter + placementBuffer
--print( "Object[" .. i .. "] x: " .. pzObject[i].x .. ", but needs: " .. left .. " - " .. right )
--print( "Object[" .. i .. "] y: " .. pzObject[i].y .. ", but needs: " .. top .. " - " .. bottom )
if pzObject[i].x > left and pzObject[i].x < right and pzObject[i].y > top and pzObject[i].y < bottom
and pzObject[i].rotation == 0 and pzObject[i] == finalPiece then
-- PUZZLE PIECE IS IN CORRECT SPOT!
print( "Puzzle piece placed!" )
-- Increment placed count
placedCount = placedCount + 1
-- Check to see if puzzle is complete
if placedCount == maxPieces then
--THE PUZZLE IS FINISHED! DO COOL EFFECT
solveItBtn.isActive = false
previewBtn.isActive = false
local i = 1
local function nextPiece()
i = i + 1
if i <= maxPieces then
transition.to( pzObject[i], { time=50, alpha=1.0, onComplete=nextPiece } )
end
end
transition.to( pzObject[i], { time=50, alpha=1.0, onComplete=nextPiece } )
end
-- Correct any deviation
pzObject[i].canMove = false
pzObject[i].x = xCenter
pzObject[i].y = yCenter
pzObject[i].rotation = 0
pzObject[i].alpha = 0.65
-- Reset reference points again
resetReference( i )
return true
else
-- NOT PLACED IN PROPER LOCATION:
resetReference( i )
return false
end
end
--===================================================================================
--
--
--===================================================================================
local createPuzzle = function()
local i
local puzzleFile = "puzzle" .. puzzleNum .. ".png"
puzzlePreview = display.newImageRect( puzzleFile, 500, 500 )
puzzlePreview.x = 300; puzzlePreview.y = 512
puzzlePreview.alpha = 0
puzzlePreview.isVisible = false
for i=1,maxPieces,1 do
pzObject[i] = display.newImageRect( puzzleFile, 500, 500 )
--SHADOW
if shadowOn then
pzShadow[i] = display.newImageRect( "shadow.png", 500, 500 )
--SHADOW
screenGroup:insert( pzShadow[i] )
end
screenGroup:insert( pzObject[i] )
pzObject[i].x = 300; pzObject[i].y = 674 --512
--SHADOW
if shadowOn then
pzShadow[i].x = 300; pzShadow[i].y = 674
pzShadow[i].alpha = 0.5
pzShadow[i].isVisible = false
end
-- SET MASK
local maskFile = "mask" .. i .. ".png"
pzMask[i] = graphics.newMask( maskFile )
pzObject[i]:setMask( pzMask[i] )
--SHADOW
if shadowOn then
pzShadow[i]:setMask( pzMask[i] )
end
pzObject[i].canMove = true
resetReference( i )
local touchPz = function( event )
if not isSelected and event.phase == "began" and event.x > pzObject[i].left and event.x < pzObject[i].right and
event.y > pzObject[i].top and event.y < pzObject[i].bottom and pzObject[i].canMove then
isSelected = true
lastPiece = pzObject[i]
--SHADOW
if shadowOn then
lastShadow = pzShadow[i]
end
isDragging = true
elseif event.phase == "ended" then
-- ROTATE IF TAPPED IN PLACE:
local left = event.xStart - rotateBuffer
local right = event.xStart + rotateBuffer
local top = event.yStart - rotateBuffer
local bottom = event.yStart + rotateBuffer
if finalPiece and finalPiece == pzObject[i] and event.x > left and event.x < right
and event.y > top and event.y < bottom then
local curRot = pzObject[i].rotation
curRot = curRot + 45
if curRot >= 360 then curRot = 0; end
pzObject[i].rotation = curRot
--SHADOW
if shadowOn then
pzShadow[i].rotation = curRot
end
end
lastPiece = nil
--SHADOW
if shadowOn then
lastShadow = nil
end
checkLocation( i )
--resetReference( i )
end
end
pzObject[i]:addEventListener( "touch", touchPz )
end
end
--===================================================================================
--
--
--===================================================================================
local startDrag = function( event )
if isReady then
if event.phase == "began" then
finalPiece = lastPiece
if shadowOn then
finalShadow = lastShadow
end
if finalPiece then
--SHADOW
if shadowOn then
finalShadow.isVisible = true
finalShadow:toFront()
end
finalPiece:toFront()
pzGlow:toFront()
finalPiece.xScale = 1.02
finalPiece.yScale = 1.02
--SHADOW
if shadowOn then
finalShadow.xScale = 1.02
finalShadow.yScale = 1.02
end
if glowTween then transition.cancel( glowTween ); end
pzGlow.alpha = 0
pzGlow.isVisible = true
glowTween = transition.to( pzGlow, { time=500, alpha=0.4 } )
end
elseif event.phase == "ended" then
isDragging = false
isSelected = false
pzGlow.isVisible = false
pzGlow.alpha = 0
if glowTween then transition.cancel( glowTween ); end
if finalPiece then
finalPiece.xScale = 1.0
finalPiece.yScale = 1.0
end
--SHADOW
if finalShadow then
finalShadow.isVisible = false
finalShadow.xScale = 1.0
finalShadow.yScale = 1.0
end
end
if isDragging and finalPiece then
finalPiece.x = event.x
finalPiece.y = event.y
if shadowOn then
finalShadow.x = event.x + 2
finalShadow.y = event.y + 3
end
pzGlow.x = event.x
pzGlow.y = event.y
end
end
end
--===================================================================================
--
--
--===================================================================================
local scatterPuzzle = function()
local i = 1
local randX = mRand( 100, 500 )
local randY = mRand( 100, 400 )
local j = mRand( 1, 8 )
local endRot = 0
if j == 1 then
endRot = 0
elseif j == 2 then
endRot = 45
elseif j == 3 then
endRot = 90
elseif j == 4 then
endRot = 135
elseif j == 5 then
endRot = 180
elseif j == 6 then
endRot = 225
elseif j == 7 then
endRot = 270
elseif j == 8 then
endRot = 315
end
local function nextPiece()
local j = mRand( 1, 8 )
local endRot = 0
if j == 1 then
endRot = 0
elseif j == 2 then
endRot = 45
elseif j == 3 then
endRot = 90
elseif j == 4 then
endRot = 135
elseif j == 5 then
endRot = 180
elseif j == 6 then
endRot = 225
elseif j == 7 then
endRot = 270
elseif j == 8 then
endRot = 315
end
i = i + 1
if i <= maxPieces then
local randX = mRand( 100, 500 )
local randY = mRand( 100, 350 )
transition.to( pzObject[i], { time=50, x=randX, y=randY, rotation=endRot, onComplete=nextPiece } )
if shadowOn then
transition.to( pzShadow[i], { time=50, x=randX, y=randY, rotation=endRot, onComplete=nextPiece } )
end
else
-- activate buttons
newPuzzleBtn.isActive = true
previewBtn.isActive = true
solveItBtn.isActive = true
-- ready for dragging pieces around
isReady = true
Runtime:addEventListener( "touch", startDrag )
end
end
transition.to( pzObject[i], { time=50, x=randX, y=randY, rotation=endRot, onComplete=nextPiece } )
if shadowOn then
transition.to( pzShadow[i], { time=50, x=randX, y=randY, rotation=endRot, onComplete=nextPiece } )
end
end
--===================================================================================
--
--
--===================================================================================
local drawBackground = function()
bgImage = display.newImageRect( "greybackground.png", 600, 1024 )
bgImage.x = 300; bgImage.y = 512
screenGroup:insert( bgImage )
dimRect = display.newRect( screenGroup, 0, 0, display.contentWidth, display.contentHeight )
dimRect:setFillColor( 0, 0, 0, 255 )
dimRect.alpha = 0
dimRect.isVisible = false
end
--===================================================================================
--
--
--===================================================================================
local createGlow = function()
pzGlow = display.newImage( "glow.png", 200, 200 )
pzGlow.alpha = 0
pzGlow.isVisible = false
screenGroup:insert( pzGlow )
end
--===================================================================================
--
--
--===================================================================================
local touchNewPuzzleBtn = function( event )
if event.phase == "release" and newPuzzleBtn.isActive then
newPuzzleBtn.isActive = false
nextPuzzle()
end
end
local touchPreviewBtn = function( event )
if event.phase == "release" and previewBtn.isActive then
previewBtn.isActive = false
Runtime:removeEventListener( "touch", startDrag )
local startTouchEvent = function()
local function closePreview( event )
if event.phase == "ended" and not previewBtn.isActive then
previewBtn.isActive = true
dimRect.alpha = 0
dimRect.isVisible = false
puzzlePreview.alpha = 0
puzzlePreview.isVisible = false
Runtime:removeEventListener( "touch", closePreview )
Runtime:addEventListener( "touch", startDrag )
end
end
Runtime:addEventListener( "touch", closePreview )
puzzlePreview.isVisible = true
transition.to( puzzlePreview, { time=200, alpha=1.0 } )
end
dimRect:toFront()
puzzlePreview:toFront()
dimRect.isVisible = true
transition.to( dimRect, { time=500, alpha=0.85, onComplete=startTouchEvent } )
end
end
local touchSolveItBtn = function( event )
if event.phase == "release" and solveItBtn.isActive then
solveItBtn.isActive = false
previewBtn.isActive = false
local i = maxPieces
local xCenter = 300
local yCenter = 674
local function nextPiece()
finalPiece = pzObject[i]
checkLocation( i )
i = i - 1
if i > 0 then
if pzObject[i].canMove then
pzObject[i].xReference = 0
pzObject[i].yReference = 0
transition.to( pzObject[i], { time=50, x=xCenter, y=yCenter, rotation=0, onComplete=nextPiece } )
else
nextPiece()
end
else
i = i + 1
placedCount = maxPieces
checkLocation( i )
end
end
pzObject[i].xReference = 0
pzObject[i].yReference = 0
transition.to( pzObject[i], { time=50, x=xCenter, y=yCenter, rotation=0, onComplete=nextPiece } )
end
end
local createMenu = function()
-- NEW PUZZLE BUTTON
newPuzzleBtn = ui.newButton{
defaultSrc = "newpuzzlebtn.png",
defaultX = 162,
defaultY = 56,
overSrc = "newpuzzlebtn-over.png",
overX = 162,
overY = 56,
onEvent = touchNewPuzzleBtn,
id = "newPuzzleButton",
text = "",
font = "Helvetica",
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
newPuzzleBtn.x = 129; newPuzzleBtn.y = 975
screenGroup:insert( newPuzzleBtn )
-- PREVIEW BUTTON
previewBtn = ui.newButton{
defaultSrc = "previewbtn.png",
defaultX = 162,
defaultY = 56,
overSrc = "previewbtn-over.png",
overX = 162,
overY = 56,
onEvent = touchPreviewBtn,
id = "previewButton",
text = "",
font = "Helvetica",
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
previewBtn.x = 300; previewBtn.y = 975
screenGroup:insert( previewBtn )
-- SOLVE IT BUTTON
solveItBtn = ui.newButton{
defaultSrc = "solveitbtn.png",
defaultX = 162,
defaultY = 56,
overSrc = "solveitbtn-over.png",
overX = 162,
overY = 56,
onEvent = touchSolveItBtn,
id = "solveItButton",
text = "",
font = "Helvetica",
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
solveItBtn.x = 471; solveItBtn.y = 975
screenGroup:insert( solveItBtn )
end
--===================================================================================
--
--
--===================================================================================
local appInit = function()
drawBackground()
createPuzzle()
createGlow()
createMenu(); newPuzzleBtn.isActive = false; previewBtn.isActive = false; solveItBtn.isActive = false
timer.performWithDelay( 3000, scatterPuzzle, 1 )
--Runtime:addEventListener( "touch", startDrag )
dimRect:toFront()
puzzlePreview:toFront()
end
appInit()
end
loadNewPuzzle()
nextPuzzle = function()
if puzzleNum == 4 then
puzzleNum = 1
else
puzzleNum = puzzleNum + 1
end
-- Remove event listeners:
Runtime:removeEventListener( "touch", startDrag )
local unloadScreenGroup = function()
-- Remove objects from screenGroup:
local i
for i=screenGroup.numChildren,1,-1 do
local child = screenGroup[i]
child.parent:remove( child )
child = nil
end
loadNewPuzzle()
transition.to( screenGroup, { time=500, alpha=1.0 } )
end
-- Fade screenGroup Away
transition.to( screenGroup, { time=500, alpha=0, onComplete=unloadScreenGroup } )
end