-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamePanel.lua
742 lines (606 loc) · 25.7 KB
/
gamePanel.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
module(..., package.seeall)
local json = require "json"
local tiled = require "com.ponywolf.ponytiled"
local sceneTransition = require "sceneTransition"
local fitScreen = require "fitScreen"
local physics = require "physics"
local listenersModule = require "listeners"
physics.start()
local listeners = listenersModule:new()
-- -----------------------------------------------------------------------------------
-- Declaração das variáveis
-- -----------------------------------------------------------------------------------
local tilesSize = 32
local M = { }
-- -----------------------------------------------------------------------------------
-- Funções do painel de instruções
-- -----------------------------------------------------------------------------------
function M.new( executeInstructions )
local gamePanelData
local instructions = { selectedBox = {}, boxes = { }, upArrows = { }, downArrows = { }, leftArrows = { }, rightArrows = { }, shownArrow = { }, shownInstruction = { }, texts = { }, shownBox = 0 }
local directionButtons = { up, down, left, right }
local bikeWheel
-- Carrega o arquivo tiled
gamePanelData = json.decodeFile(system.pathForFile("tiled/gamePanel.json", system.ResourceDirectory)) -- load from json export
gamePanel = tiled.new(gamePanelData, "tiled")
-- Cria referências para os quadros de instruções e suas setas
local instructionsLayer = gamePanel:findLayer("instructions")
local selectedInstructionLayer = gamePanel:findLayer("selectedInstruction")
local upArrowsLayer = gamePanel:findLayer("upArrows")
local downArrowsLayer = gamePanel:findLayer("downArrows")
local leftArrowsLayer = gamePanel:findLayer("leftArrows")
local rightArrowsLayer = gamePanel:findLayer("rightArrows")
for i = 1, instructionsLayer.numChildren do
instructions.boxes[i] = instructionsLayer[i]
instructions.selectedBox[i] = selectedInstructionLayer[i]
instructions.upArrows[i] = upArrowsLayer[i]
instructions.downArrows[i] = downArrowsLayer[i]
instructions.leftArrows[i] = leftArrowsLayer[i]
instructions.rightArrows[i] = rightArrowsLayer[i]
end
instructions.boxes[1].alpha = 1
-- Roda da bicicleta que aumenta o número de passos
bikeWheel = gamePanel:findObject("bikeWheel")
bikeWheel.radius = bikeWheel.width/2
bikeWheel.quadrant = 1
bikeWheel.steps = 1
bikeWheel.maxCount = math.huge
bikeWheel.count = math.huge
M.bikeWheel = bikeWheel
bikeLimit = gamePanel:findObject( "bikeLimit" )
-- Setas que definem a direção
directionButtons.right = gamePanel:findObject("directionRight")
directionButtons.right.originalX = directionButtons.right.x
directionButtons.right.originalY = directionButtons.right.y
directionButtons.left = gamePanel:findObject("directionLeft")
directionButtons.left.originalX = directionButtons.left.x
directionButtons.left.originalY = directionButtons.left.y
directionButtons.down = gamePanel:findObject("directionDown")
directionButtons.down.originalX = directionButtons.down.x
directionButtons.down.originalY = directionButtons.down.y
directionButtons.up = gamePanel:findObject("directionUp")
directionButtons.up.originalX = directionButtons.up.x
directionButtons.up.originalY = directionButtons.up.y
instructionsPanel = gamePanel:findObject("instructionsPanel")
executeButton = gamePanel:findObject("executeButton")
executeButton.executionsCount = 0
executeButton.instructionsCount = {}
executeButton.bikeCount = {}
M.executeButton = executeButton
gotoMenuButton = gamePanel:findObject("gotoMenuButton")
if ( display.actualContentWidth > 512 ) then
gotoMenuButton.x = gotoMenuButton.x - 32
executeButton.x = executeButton.x - 32
gamePanel.x = gamePanel.x + 32
end
-- -----------------------------------------------------------------------------------
-- Listeners do game panel
-- -----------------------------------------------------------------------------------
-- Atualiza quantidade de passos de acordo com giro da roda da bicicleta
local function updateSteps( circle )
stepsCount = math.floor(circle.steps)
instructionsTable.steps[instructionsTable.last] = stepsCount
for i = 0, instructions.shownBox do
if ( instructions.shownInstruction[i] == instructionsTable.last ) then
instructions.texts[i].text = instructionsTable.last .. ". " .. stepsCount
end
end
end
-- Retorna quadrante do ponto onde o jogador está tocando
local function getQuadrant( dx, dy )
if ( ( dx > 0) and ( dy > 0 ) ) then
return 2
elseif ( ( dx > 0 ) and ( dy < 0 ) ) then
return 1
elseif ( ( dx < 0 ) and ( dy > 0 ) ) then
return 3
elseif ( ( dx < 0 ) and ( dy < 0 ) ) then
return 4
end
end
function updateBikeCount( count )
if ( bikeWheel.maxCount ~= math.huge ) then
bikeWheel.count = count
if ( bikeLimit.text ) then
bikeLimit.text.text = count
else
bikeLimit.text = display.newText( gamePanel:findLayer( "bikeLimit" ), " ", bikeLimit.x, bikeLimit.y, system.nativeFontBold, 12 )
bikeLimit.text.alpha = 0
bikeLimit.text.text = count
end
transition.fadeIn( bikeLimit.text, { time = 400 } )
transition.fadeIn( bikeLimit, { time = 400 } )
else
transition.fadeOut( bikeLimit, { time = 400 } )
if ( bikeLimit.text ) then
transition.fadeOut( bikeLimit.text, { time = 400 } )
end
end
end
function M:updateBikeMaxCount( count )
bikeWheel.maxCount = count
bikeWheel.count = count
updateBikeCount( count )
end
-- Gira a roda da bicicleta
local function spinBikeWheel( event )
local circle = event.target
local phase = event.phase
local centerX, centerY = circle:localToContent( 0, 0 )
if ( "began" == phase ) then
display.currentStage:setFocus( circle )
local dx = event.x - centerX
local dy = event.y - centerY
local radius = math.sqrt( math.pow( dx, 2 ) + math.pow( dy, 2 ) )
local ds, dt = ( circle.radius * dx ) / radius, ( circle.radius * dy ) / radius
adjustment = math.atan2( dt, ds ) * 180 / math.pi - circle.rotation
circle.quadrant = getQuadrant( ds, dt )
elseif ( "moved" == phase ) then
if ( adjustment ) then
if ( bikeWheel.count > 0 ) then
local dx = event.x - centerX
local dy = event.y - centerY
local radius = math.sqrt( math.pow( dx, 2 ) + math.pow( dy, 2 ) )
local ds, dt = ( circle.radius * dx ) / radius, ( circle.radius * dy ) / radius
local quadrant = getQuadrant( dx, dy )
if ( ( quadrant ) and ( circle.quadrant ) ) then
if ( quadrant ~= circle.quadrant ) then
if ( ( circle.quadrant == 4 ) and ( quadrant == 1 ) ) then
circle.steps = circle.steps + 0.5
elseif ( ( circle.quadrant == 1 ) and ( quadrant == 4 ) ) then
if ( circle.steps > 0 ) then
circle.steps = circle.steps - 0.5
end
elseif ( quadrant > circle.quadrant ) then
circle.steps = circle.steps + 0.5
elseif ( quadrant < circle.quadrant ) then
if ( circle.steps > 0 ) then
circle.steps = circle.steps - 0.5
end
end
end
circle.quadrant = quadrant
if ( circle.steps > 0 ) then
circle.rotation = ( math.atan2( dt, ds ) * 180 / math.pi ) - adjustment
end
updateSteps( circle )
end
end
end
elseif ( "ended" == phase or "cancelled" == phase ) then
display.currentStage:setFocus( nil )
end
return true
end
local function betweenBounds( event, box )
if ( ( event.x > box.contentBounds.xMin ) and ( event.x < box.contentBounds.xMax )
and ( event.y > box.contentBounds.yMin ) and ( event.y < box.contentBounds.yMax ) ) then
return true
end
return false
end
local function deleteInstruction( boxNumber )
local pos = instructions.shownInstruction[boxNumber]
local isLastBoxEmpty
local last
local first
if ( ( instructionsTable.executing == 1 ) and ( pos ) ) then
if ( ( instructionsTable.last ~= 1 ) and ( instructionsTable.steps[pos] > 1 ) ) then
updateBikeCount( bikeWheel.count + 1 )
elseif ( instructionsTable.last == 1 ) then
updateBikeCount( bikeWheel.maxCount )
end
instructionsTable:remove( pos )
if ( instructionsTable.last < instructions.shownBox ) then
last = instructions.shownBox - 1
instructions.shownArrow[ instructions.shownBox ].alpha = 0
instructions.shownArrow[ instructions.shownBox ] = nil
instructions.shownInstruction[ instructions.shownBox ] = nil
instructions.texts[ instructions.shownBox ].text = " "
instructions.shownBox = instructions.shownBox - 1
if ( instructions.shownBox < #instructions.boxes - 1 ) then
instructions.boxes[ instructions.shownBox + 2 ].alpha = 0
end
first = boxNumber
else
pos = instructions.shownInstruction[1] - 1
if ( pos >= 1 ) then
first = 1
last = #instructions.boxes
else
pos = instructions.shownInstruction[boxNumber]
first = boxNumber
last = #instructions.boxes
end
end
for i = first, last do
local arrow = findInstructionArrow ( pos )
instructions.shownInstruction[i] = pos
instructions.shownArrow[i].alpha = 0
instructions.shownArrow[i] = arrow[i]
arrow[i].alpha = 1
local dir
if ( instructions.shownArrow[i] == instructions.upArrows[i] ) then dir = "up" end
if ( instructions.shownArrow[i] == instructions.downArrows[i] ) then dir = "down" end
if ( instructions.shownArrow[i] == instructions.leftArrows[i] ) then dir = "left" end
if ( instructions.shownArrow[i] == instructions.rightArrows[i] ) then dir = "right" end
instructions.texts[i].text = pos .. ". " .. instructionsTable.steps[pos]
pos = pos + 1
end
end
end
-- É o listener para quando o jogador aperta uma seta
-- Também adiciona a instrução na fila
local function createInstruction( event )
local phase = event.phase
local direction = event.target.myName
local directionButton = event.target
local box
local selectedBox
-- Descobre qual é a caixa de instrução que mostrará a nova instrução e as suas posições
if ( instructions.shownBox < #instructions.boxes ) then
box = instructions.boxes[ instructions.shownBox + 1 ], instructions.boxes[ instructions.shownBox + 1 ]:localToContent( 0, 0 )
selectedBox = instructions.selectedBox[ instructions.shownBox + 1 ]
else
box = instructions.boxes[ #instructions.boxes ], instructions.boxes[ #instructions.boxes ]:localToContent( 0, 0 )
selectedBox = instructions.selectedBox[ #instructions.boxes ]
end
if ( "began" == phase ) then
display.currentStage:setFocus( directionButton )
-- Seta selecionada é posicionada na frente de todos os outros objetos na tela
directionButton:toFront()
-- Cálculo do offset inicial
directionButton.touchOffsetX = event.x - directionButton.x
directionButton.touchOffsetY = event.y - directionButton.y
-- O loop começa com 1 passo e a rotação da roda de bicicleta volta para o início
bikeWheel.steps = 1
bikeWheel.rotation = 0
-- Esconde as instruções anteriores e reseta a lista de instruções após uma execução
if ( instructionsTable.executing ~= 1 ) then
M:hideInstructions()
instructionsTable:reset()
end
-- Verifica se a última caixa de instrução está mostrando alguma instrução
-- e caso esteja, houve scroll
if ( ( instructions.shownBox >= #instructions.boxes ) and ( instructions.shownInstruction[instructions.shownBox] ~= instructionsTable.last ) ) then
-- Caso tenha havido um scroll anteriormente, faz scroll para cima até a última
-- instrução feita ser mostrada na última caixa de instrução
while ( instructions.shownInstruction[instructions.shownBox] ~= instructionsTable.last ) do
scrollInstruction("up")
end
moveInstruction( 1, instructions.shownBox, 1, true )
elseif ( ( instructions.shownBox >= #instructions.boxes ) and ( instructions.texts[instructions.shownBox].text ~= " " ) ) then
-- Se a última instrução feita estiver na última caixa de instrução, faz um
-- scroll para cima para que a última caixa de instrução seja esvaziada
moveInstruction( 1, instructions.shownBox, 1, true )
end
elseif ( ( "moved" == phase ) and ( directionButton.touchOffsetX ) ) then
-- Move a seta
directionButton.x = event.x - directionButton.touchOffsetX
directionButton.y = event.y - directionButton.touchOffsetY
-- Mostra a "caixa selecionada", para reforçar que aquele é o local onde a seta deve ser
-- colocada
if ( ( event.x > box.contentBounds.xMin ) and ( event.x < box.contentBounds.xMax )
and ( event.y > box.contentBounds.yMin ) and ( event.y < box.contentBounds.yMax ) ) then
selectedBox.alpha = 1
else
selectedBox.alpha = 0
end
elseif ( ( "ended" == phase ) or ( "cancelled" == phase ) and ( directionButton.touchOffsetX ) ) then
-- Verifica se o local onde o movimento de "touch" terminou está dentro
-- da caixa de instrução atual
if ( ( event.x > box.contentBounds.xMin ) and ( event.x < box.contentBounds.xMax )
and ( event.y > box.contentBounds.yMin ) and ( event.y < box.contentBounds.yMax ) ) then
-- Caso esteja, a seta volta para sua posição original
directionButton.x = directionButton.originalX
directionButton.y = directionButton.originalY
-- Instrução começa com um passo para a direção escolhida
local stepsCount = 1
-- Instrução é adicionada à lista de instruções e mostrada na sua caixa de instrução correspondente
instructionsTable:add( direction, stepsCount )
showInstruction( direction, stepsCount )
-- Listener da roda de bicicleta é adicionado
if ( instructionsTable.last == 1 ) then
bikeWheel.alpha = 1
if ( bikeWheel.maxCount ~= math.huge ) then
bikeLimit.alpha = 1
end
listeners:add( bikeWheel, "touch", spinBikeWheel )
end
-- A caixa selecionada é escondida
selectedBox.alpha = 0
if ( ( bikeWheel.maxCount ~= math.huge ) and ( instructionsTable.last ~= 1 ) and ( instructionsTable.steps[ instructionsTable.last - 1 ] > 1 ) ) then
updateBikeCount( bikeWheel.count - 1 )
end
else
-- Caso o movimento de toque acabe e a seta não seja colocada na caixa correta, ela
-- volta para a posição original
transition.to( directionButton, { time = 400, x = directionButton.originalX, y = directionButton.originalY } )
end
display.currentStage:setFocus( nil )
end
return true
end
-- Faz o scroll das instruções
local function scrollInstructionsPanel( event )
local phase = event.phase
local xInstructionsPanel, yInstructionsPanel = instructionsPanel:localToContent( 0, instructionsPanel.height/2 )
if ( phase == "began" ) then
instructionsPanel.touchOffsetY = event.y
instructionsPanel.originalOffset = event.y
elseif ( phase == "moved" ) then
if ( instructionsPanel.touchOffsetY ) then
if ( ( instructionsPanel.touchOffsetY - event.y ) < -tilesSize ) then
scrollInstruction( "down" )
instructionsPanel.touchOffsetY = event.y
elseif ( ( instructionsPanel.touchOffsetY - event.y ) > tilesSize ) then
scrollInstruction( "up" )
instructionsPanel.touchOffsetY = event.y
end
end
elseif ( phase == "ended" ) then
if ( instructionsPanel.originalOffset ) then
if ( math.abs( instructionsPanel.originalOffset - event.y ) < 2 ) then
local pos
if ( instructions.shownBox >= #instructions.boxes ) then
pos = #instructions.boxes
else
pos = instructions.shownBox + 1
end
for i = 1, pos do
if ( betweenBounds( event, instructions.boxes[i] ) == true ) then
deleteInstruction( i )
break
end
end
end
end
end
return true
end
function M:removegotoMenuButton()
listeners:remove( gotoMenuButton, "tap", sceneTransition.gotoMenu )
gotoMenuButton = nil
end
function M:addDirectionListeners()
directionButtons.right.alpha = 1
directionButtons.left.alpha = 1
directionButtons.down.alpha = 1
directionButtons.up.alpha = 1
listeners:add( directionButtons.right, "touch", createInstruction )
listeners:add( directionButtons.left, "touch", createInstruction )
listeners:add( directionButtons.down, "touch", createInstruction )
listeners:add( directionButtons.up, "touch", createInstruction )
end
function M:resetExecutionButton()
executeButton.executionsCount = 0
executeButton.instructionsCount = { }
executeButton.bikeCount = { }
end
function M.executeInstructions()
local bikeCount = 0
if ( ( instructionsTable.last ~= 0 ) and ( instructionsTable.executing == 1 ) ) then
executeButton.executionsCount = executeButton.executionsCount + 1
table.insert( executeButton.instructionsCount, instructionsTable.last )
for i = 1, #instructionsTable.steps do
if ( instructionsTable.steps[i] ) > 1 then bikeCount = bikeCount + 1 end
end
table.insert( executeButton.bikeCount, bikeCount )
if ( instructionsTable.steps[ instructionsTable.last ] > 1 ) then
updateBikeCount( bikeWheel.count - 1 )
end
executeInstructions()
end
end
function M:addGotoMenuButtonListener()
gotoMenuButton.alpha = 1
listeners:add( gotoMenuButton, "tap", sceneTransition.gotoMenu )
end
function M:addButtonsListeners()
executeButton.alpha = 1
gotoMenuButton.alpha = 1
listeners:add( executeButton, "tap", M.executeInstructions )
listeners:add( gotoMenuButton, "tap", sceneTransition.gotoMenu )
end
function M:addInstructionPanelListeners()
listeners:add( instructionsPanel, "touch", scrollInstructionsPanel )
end
function M.stopExecutionListeners()
local nonDraggableLayer = gamePanel:findLayer( "non-draggable" )
for i = 1, nonDraggableLayer.numChildren do
nonDraggableLayer[i].alpha = 0.5
end
directionButtons.right.alpha = 0.5
directionButtons.left.alpha = 0.5
directionButtons.down.alpha = 0.5
directionButtons.up.alpha = 0.5
executeButton.alpha = 0.5
bikeWheel.alpha = 0.5
if ( bikeLimit.alpha ~= 0 ) then
bikeLimit.alpha = 0.5
end
gotoMenuButton.alpha = 0.5
listeners:remove( directionButtons.right, "touch", createInstruction )
listeners:remove( directionButtons.left, "touch", createInstruction )
listeners:remove( directionButtons.down, "touch", createInstruction )
listeners:remove( directionButtons.up, "touch", createInstruction )
listeners:remove( executeButton, "tap", M.executeInstructions )
listeners:remove( bikeWheel, "touch", spinBikeWheel )
listeners:remove( gotoMenuButton, "tap", sceneTransition.gotoMenu )
end
function M:stopAllListeners( )
directionButtons.right.alpha = 0.5
directionButtons.left.alpha = 0.5
directionButtons.down.alpha = 0.5
directionButtons.up.alpha = 0.5
executeButton.alpha = 0.5
bikeWheel.alpha = 0.5
if ( bikeLimit.alpha ~= 0 ) then
bikeLimit.alpha = 0.5
end
listeners:remove( directionButtons.right, "touch", createInstruction )
listeners:remove( directionButtons.left, "touch", createInstruction )
listeners:remove( directionButtons.down, "touch", createInstruction )
listeners:remove( directionButtons.up, "touch", createInstruction )
listeners:remove( executeButton, "tap", M.executeInstructions )
listeners:remove( bikeWheel, "touch", spinBikeWheel )
end
function M.restartExecutionListeners()
M:addDirectionListeners()
M:addButtonsListeners()
end
function M.hide()
gamePanel.alpha = 0
end
function M:destroy()
if ( gamePanel ) then
gamePanel:removeSelf()
M:stopAllListeners()
-- remove instruções
for k0, v0 in pairs( instructions ) do
if ( type(v0) == "table" ) then
for k1, v1 in pairs(v0) do
table.remove( v0, k1 )
end
end
instructions[k0] = nil
end
instructions = nil
-- remove botões de direção
for k, v in pairs( directionButtons ) do
directionButtons[k] = nil
end
directionButtons = nil
-- remove tabela de instruções
for k0, v0 in pairs( instructionsTable ) do
if ( type(v0) == "table" ) then
for k1, v1 in pairs(v0) do
table.remove( v0, k1 )
end
instructionsTable[k0] = nil
end
end
instructionsTable = nil
executeButton = nil
bikeWheel = nil
instructionsPanel = nil
gamePanel = nil
end
end
-- -----------------------------------------------------------------------------------
-- Funções relacionadas à amostra de instruções na tela
-- -----------------------------------------------------------------------------------
-- Acha qual seta deve ser mostrada nas caixas de instrução
function findInstructionArrow ( index )
local direction = instructionsTable.direction[index]
if ( direction == "right" ) then
arrow = instructions.rightArrows
elseif ( direction == "left" ) then
arrow = instructions.leftArrows
elseif ( direction == "down" ) then
arrow = instructions.downArrows
else
arrow = instructions.upArrows
end
return arrow
end
-- Move as instruções mostradas na tela da caixa firstBox até lastBox
-- Direction = 1 significa mover para cima e -1 para baixo.
function moveInstruction( firstBox, lastBox, direction, isLastBoxEmpty )
for i = firstBox, lastBox do
local instructionIndex = instructions.shownInstruction[i] + direction
if ( ( i < instructions.shownBox ) or ( isLastBoxEmpty == false ) ) then
local instructionIndex = instructions.shownInstruction[i] + direction
local steps = instructionsTable.steps[ instructionIndex ]
local arrow = findInstructionArrow( instructionIndex )
instructions.shownArrow[i].alpha = 0
instructions.shownArrow[i] = arrow[i]
arrow[i].alpha = 1
instructions.texts[i].text = instructionIndex .. ". " .. steps
instructions.shownInstruction[i] = instructions.shownInstruction[i] + direction
else
instructions.shownArrow[i].alpha = 0
instructions.texts[i].text = " "
end
end
end
-- Verifica se as instruções podem ser movidas para baixo/cima e chama
-- moveInstruction
function scrollInstruction ( direction )
if ( instructions.shownBox ~= 0 ) then
local firstBox = 1
local lastBox = instructions.shownBox
if ( ( direction == "down" ) and ( instructions.shownInstruction[1] > 1 ) ) then
moveInstruction( firstBox, lastBox, -1, false )
elseif ( ( direction == "up" ) and ( instructions.shownInstruction[instructions.shownBox] < instructionsTable.last ) ) then
moveInstruction( firstBox, lastBox, 1, false )
end
end
end
-- Esconde as instruções após a execução
function M:hideInstructions()
local boxNum = instructions.shownBox
if ( instructions.shownBox ~= 0 ) then
for i = 1, boxNum do
display.remove(instructions.texts[i])
instructions.shownArrow[i].alpha = 0
instructions.boxes[i].alpha = 0
instructions.shownBox = 0
end
for i = boxNum, #instructions.boxes do
instructions.boxes[i].alpha = 0
end
instructions.boxes[1].alpha = 1
end
end
-- Mostra as instruções à medida que são feitas
function showInstruction( direction, stepsCount )
local arrow = findInstructionArrow ( instructionsTable.last )
if ( instructions.shownBox < #instructions.boxes ) then
local boxNum = instructions.shownBox + 1
local box = instructions.boxes[boxNum]
instructions.shownBox = boxNum
if ( ( boxNum + 1 ) < #instructions.boxes + 1 ) then
instructions.boxes[ boxNum + 1 ].alpha = 1
end
arrow[ boxNum ].alpha = 1
instructions.shownArrow[boxNum] = arrow[boxNum]
instructions.texts[boxNum] = display.newText( gamePanel:findLayer("instructions"), boxNum .. ". " .. stepsCount, box.x - 10, box.y, system.nativeFontBold, 12)
instructions.shownInstruction[boxNum] = instructionsTable.last
else
local boxNum = instructions.shownBox
-- Verifica se a última instrução sendo mostrada na tela foi a feita anteriormente (se não for, houve scroll)
if ( ( instructionsTable.last - 1 ) == instructions.shownInstruction[boxNum] ) then
instructions.shownArrow[boxNum].alpha = 0
instructions.shownArrow[boxNum] = arrow[boxNum]
instructions.shownArrow[boxNum].alpha = 1
instructions.texts[boxNum].text = instructionsTable.last .. ". " .. stepsCount
instructions.shownInstruction[boxNum] = instructionsTable.last
end
end
end
function M.createInstruction( direction, steps )
local box
local selectedBox
-- Descobre qual é a caixa de instrução que mostrará a nova instrução e as suas posições
if ( instructions.shownBox < #instructions.boxes ) then
box = instructions.boxes[ instructions.shownBox + 1 ], instructions.boxes[ instructions.shownBox + 1 ]:localToContent( 0, 0 )
selectedBox = instructions.selectedBox[ instructions.shownBox + 1 ]
else
box = instructions.boxes[ #instructions.boxes ], instructions.boxes[ #instructions.boxes ]:localToContent( 0, 0 )
selectedBox = instructions.selectedBox[ #instructions.boxes ]
end
if ( ( instructions.shownBox >= #instructions.boxes ) and ( instructions.shownInstruction[instructions.shownBox] ~= instructionsTable.last ) ) then
while ( instructions.shownInstruction[instructions.shownBox] ~= instructionsTable.last ) do
scrollInstruction("up")
end
moveInstruction( 1, instructions.shownBox, 1, true )
elseif ( ( instructions.shownBox >= #instructions.boxes ) and ( instructions.texts[instructions.shownBox].text ~= " " ) ) then
moveInstruction( 1, instructions.shownBox, 1, true )
end
instructionsTable:add( direction, steps )
showInstruction( direction, steps)
end
return gamePanel
end
return M