-
Notifications
You must be signed in to change notification settings - Fork 0
/
house.lua
324 lines (250 loc) · 10.7 KB
/
house.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
local composer = require( "composer" )
local scene = composer.newScene()
local tiled = require "com.ponywolf.ponytiled"
local physics = require "physics"
local json = require "json"
local persistence = require "persistence"
local sceneTransition = require "sceneTransition"
local gamePanel = require "gamePanel"
local instructions = require "instructions"
local gameState = require "gameState"
local path = require "path"
local gameScene = require "gameScene"
local houseFSM = require "fsm.miniGames.houseFSM"
local listenersModule = require "listeners"
local listeners = listenersModule:new()
physics.start()
physics.setGravity( 0, 0 )
-- -----------------------------------------------------------------------------------
-- Declaração das variáveis
-- -----------------------------------------------------------------------------------
local house
local character
local mom
local tilesSize = 32
local stepDuration = 50
local puzzle = { bigPieces = { }, littlePieces = { count }, puzzleSensors = { }, collectedPieces = { count = 0 } }
local collision = false
local miniGameData
local originalMiniGameData
local messageBubble
local animation = {}
local message = {}
local function setPuzzle()
local bigPiecesLayer = house:findLayer("big puzzle")
local littlePiecesLayer = house:findLayer("little puzzle")
local puzzleSensorsLayer = house:findLayer("puzzle sensors")
for i = 1, bigPiecesLayer.numChildren do
puzzle.bigPieces[ bigPiecesLayer[i].myName ] = bigPiecesLayer[i]
puzzle.puzzleSensors[ puzzleSensorsLayer[i].puzzleNumber ] = puzzleSensorsLayer[i]
physics.addBody( puzzleSensorsLayer[i], { bodyType = "static", isSensor = true } )
littlePiecesLayer[i].alpha = 1
puzzle.littlePieces[ littlePiecesLayer[i].myName ] = littlePiecesLayer[i]
end
puzzle.littlePieces.count = bigPiecesLayer.numChildren
end
-- -----------------------------------------------------------------------------------
-- Listeners
-- -----------------------------------------------------------------------------------
-- Trata dos tipos de colisão da casa
local function onCollision( event )
phase = event.phase
local obj1 = event.object1
local obj2 = event.object2
if ( event.phase == "began" ) then
if ( ( ( obj1.myName == "puzzle" ) and ( obj2.isCharacter ) ) or ( ( obj1.isCharacter ) and ( obj2.myName == "puzzle" ) ) ) then
if ( obj1.myName == "puzzle" ) then obj = obj1 else obj = obj2 end
if ( puzzle.collectedPieces[obj.puzzleNumber] == nil ) then
puzzle.bigPieces[obj.puzzleNumber].alpha = 1
puzzle.littlePieces[ obj.puzzleNumber ].alpha = 0
puzzle.collectedPieces[ obj.puzzleNumber ] = puzzle.littlePieces[ obj.puzzleNumber ]
local remainingPieces = puzzle.littlePieces.count - (puzzle.collectedPieces.count + 1)
puzzle.collectedPieces.last = obj.puzzleNumber
if ( ( puzzle.collectedPieces.count ~= 0 ) and ( remainingPieces > 0 ) ) then
houseFSM.update()
elseif ( remainingPieces <= 0 ) then
houseFSM.update( _, "transitionEvent" )
end
puzzle.collectedPieces.count = puzzle.collectedPieces.count + 1
end
-- Volta para o mapa quando o personagem chega na saída/entrada da casa
elseif ( ( ( obj1.myName == "exit" ) and ( obj2.isCharacter ) ) or ( ( obj1.isCharacter ) and ( obj2.myName == "exit" ) ) ) then
if ( miniGameData.isComplete == true ) then
transition.cancel( character )
character.stepping.point = "exit"
instructions:destroyInstructionsTable()
gamePanel:stopAllListeners()
timer.performWithDelay( 1000, sceneTransition.gotoMap )
elseif ( ( miniGameData.controlsTutorial == "complete" ) and ( houseFSM.fsm ) ) then
local _, animationName = houseFSM.fsm.current:match( "([^,]+)_([^,]+)" )
if ( animationName == "handExitAnimation" ) then
transition.fadeOut( gamePanel.exitHand, { time = 450 } )
houseFSM.update()
end
end
elseif ( ( ( obj1.myName == "entrance" ) and ( obj2.isCharacter ) ) or ( ( obj1.isCharacter ) and ( obj2.myName == "entrance" ) ) ) then
if ( miniGameData.isGameComplete == true ) then
transition.cancel( character )
character.stepping.point = "entrance"
instructions:destroyInstructionsTable()
gamePanel:stopAllListeners()
timer.performWithDelay( 1000, sceneTransition.gotoMap )
end
-- Colisão entre o personagem e os sensores dos tiles do caminho
elseif ( ( ( obj1.isCharacter ) and ( obj2.isPath ) ) or ( ( obj2.isCharacter ) and ( obj1.isPath ) ) ) then
local obj
if ( obj1.isPath ) then obj = obj1 else obj = obj2 end
character.stepping = obj
character.stepping.point = "point"
path:showTile( obj.myName )
-- Colisão com os demais objetos e o personagem (rope nesse caso)
elseif ( ( ( obj1.isCollision ) and ( obj2.isCharacter ) ) or ( ( obj1.isCharacter ) and ( obj2.isCollision ) ) ) then
local obj
if ( obj1.isCollision ) then obj = obj1 else obj = obj2 end
transition.cancel( character )
if ( ( obj.direction == "right" ) ) then
transition.to( character, { time = 0, x = character.x + .20 * tilesSize } )
elseif ( ( obj.direction == "left" ) ) then
transition.to( character, { time = 0, x = character.x - .20 * tilesSize } )
elseif ( ( obj.direction == "up" ) ) then
transition.to( character, { time = 0, y = character.y - .23 * tilesSize } )
elseif ( ( obj.direction == "down" ) ) then
transition.to( character, { time = 0, y = character.y + .22 * tilesSize } )
end
if ( ( miniGameData.isComplete == false ) and ( houseFSM.fsm.current ~= "feedbackAnimation" ) ) then
if ( obj.isWall ) then
local message = { "Ei, você não pode andar por aí!", "Cuidado com as paredes." }
houseFSM.showText( house:findObject( "momBubble" ), message, house:findObject( "mom" ) )
elseif ( obj.isFloor ) then
local message = { "Ei, você não pode andar por aí!", "Ande apenas nos quadrados", "azuis." }
houseFSM.showText( house:findObject( "momBubble" ), message, house:findObject( "mom" ) )
end
end
end
end
return true
end
-- -----------------------------------------------------------------------------------
-- Remoções para limpar a tela
-- -----------------------------------------------------------------------------------
local function destroyScene()
gamePanel:destroy()
instructions:destroyInstructionsTable()
house:removeSelf()
house = nil
if ( ( messageBubble ) and ( messageBubble.text ) ) then
messageBubble.text:removeSelf()
messageBubble.text = nil
end
if ( ( houseFSM ) and ( houseFSM.destroy ) ) then
houseFSM.destroy()
end
end
-- -----------------------------------------------------------------------------------
-- Cenas
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
local sceneGroup = self.view
house, character, gamePanel, gameState, path, instructions, instructionsTable, miniGameData = gameScene:set( "house" )
sceneGroup:insert( house )
sceneGroup:insert( gamePanel.tiled )
if ( miniGameData.onRepeat == true ) then
if ( ( miniGameData.isGameComplete == false ) or ( ( miniGameData.isGameComplete == true ) and ( miniGameData.shownCompletion == true ) ) ) then
miniGameData.controlsTutorial = "incomplete"
miniGameData.bikeTutorial = "incomplete"
miniGameData.isComplete = false
originalMiniGameData = miniGameData
end
end
if ( miniGameData.controlsTutorial == "incomplete" ) then
setPuzzle()
end
end
-- show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
mom = house:findObject( "mom" )
if ( phase == "will" ) then
if ( miniGameData.isComplete == true ) then
if ( ( miniGameData.isGameComplete == true ) and ( miniGameData.shownCompletion == false ) ) then
local brother, brotherPosition
path:hidePath()
if ( character == house:findObject( "ada") ) then
brother = house:findObject( "turing" )
else
brother = house:findObject( "ada")
end
brotherPosition = house:findObject( "brotherEnding" )
if ( miniGameData.wonSurprise == false ) then
brother.x, brother.y = brotherPosition.x, brotherPosition.y
brother.xScale = -1
brother.alpha = 1
end
else
mom.alpha = 1
character.alpha = 1
gamePanel:addDirectionListeners()
end
elseif ( miniGameData.controlsTutorial == "complete" ) then
gamePanel:addDirectionListeners()
else
gamePanel.bikeWheel.alpha = 0
mom.alpha = 1
character.alpha = 1
end
listeners:add( Runtime, "collision", onCollision )
elseif ( phase == "did" ) then
if ( ( miniGameData.isGameComplete == true ) and ( miniGameData.shownCompletion == false ) ) then
houseFSM.new( house, character, listeners, _, miniGameData, gameState, gamePanel, path )
houseFSM.completeGame()
elseif ( miniGameData.controlsTutorial == "complete" ) then
gamePanel:addButtonsListeners()
gamePanel:addInstructionPanelListeners()
if ( miniGameData.bikeTutorial == "incomplete" ) then
mom.alpha = 1
character.alpha = 1
gamePanel:showBikewheel ( false )
houseFSM.new( house, character, listeners, puzzle, miniGameData, gameState, gamePanel, path )
houseFSM.bikeTutorial()
end
else
if ( miniGameData.controlsTutorial == "incomplete" ) then
houseFSM.new( house, character, listeners, puzzle, miniGameData, gameState, gamePanel, path )
houseFSM.controlsTutorial()
end
end
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
if ( miniGameData.onRepeat == true ) then
miniGameData.onRepeat = false
if ( miniGameData.isComplete == false ) then
miniGameData = originalMiniGameData
end
end
gameState:save( miniGameData )
destroyScene()
listeners:destroy()
elseif ( phase == "did" ) then
composer.removeScene( "house" )
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene