-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainAct.lua
578 lines (494 loc) · 16.5 KB
/
mainAct.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
-----------------------------------------------------------------------------------------
--
-- mainAct.lua
--
-- The main activity (map, etc.) for the Mars App
-----------------------------------------------------------------------------------------
-- Get local reference to the game globals
local game = globalGame
-- Load ship gem data
local gems = require( "gems" )
-- Create the act object
local act = game.newAct()
------------------------- Start of Activity --------------------------------
-- Constants
local zoomTime = 500 -- time for zoom in/out transition (ms)
local walkSpeed = 0.1 -- user's walking speed factor
-- Act variables
local spaceBgs -- space background (array of 2 images)
local marsBg -- Mars background image
local shipGroup -- display group centered on ship
local iconGroup -- display group for map icons, within shipGroup
local dot -- user's position dot on map
local roomInside -- room the user is inside or nil if none
local titleBar -- title bar used when map is zoomed
local yTitleBar -- y position of title bar when visible
local map -- the map image
-- Main Ship coordinates
local ship = {
-- Vertical hallway
vHall = { left = -12, top = -160, right = 12, bottom = 180 },
-- Top Horizontal hallway
thHall = { left = -43, top = -146, right = 43, bottom = -130},
-- Bottom Horizontal hallway
bhHall = { left = -133, top = -10, right = 132, bottom = 1 },
-- Rooms: name, rectangle bounds, zoom factor,
-- x, y position just outside the door, delta to inside
rooms = {
{
name = "Bridge",
left = -48, top = -248, right = 49, bottom = -168, zoom = 3,
x = 0, y = -160, dx = 1, dy = -25,
},
{
name = "Greenhouse",
left = -139, top = -190, right = -52, bottom = -83, zoom = 3,
x = -43, y = -138, dx = -20, dy = 3, sound = "HarpPiano.mp3",
},
{
name = "Lounge",
left = 49, top = -188, right = 137, bottom = -85, zoom = 3,
x = 43, y = -138, dx = 20, dy = 3,
},
{
name = "Jordan",
left = -80, top = -58, right = -21, bottom = -18, zoom = 4,
x = -50, y = -10, dx = -4, dy = -30, doorCode = "2439",
},
{
name = "Maxwell",
left = 20, top = -58, right = 77, bottom = -19, zoom = 4,
x = 49, y = -10, dx = 10, dy = -30,
},
{
name = "Graham",
left = -141, top = -58, right = -82, bottom = -19, zoom = 4,
x = -111, y = -10, dx = -4, dy = -30,
},
{
name = "Moore",
left = 81, top = -58, right = 138, bottom = -19, zoom = 4,
x = 110, y = -10, dx = 5, dy = -30,
},
{
name = "Ellis",
left = -141, top = 9, right = -81, bottom = 48, zoom = 4,
x = -111, y = 1, dx = -3, dy = 30,
},
{
name = "Shaw",
left = -79, top = 9, right = -22, bottom = 48, zoom = 4,
x = -50, y = 1, dx = -4, dy = 30,
},
{
name = "Webb",
left = 20, top = 9, right = 76, bottom = 48, zoom = 4,
x = 49, y = 1, dx = 6, dy = 30,
},
{
name = "Your Quarters",
left = 81, top = 9, right = 138, bottom = 48, zoom = 4,
x = 110, y = 1, dx = 7, dy = 30,
},
{
name = "Rover Bay",
left = -142, top = 88, right = -22, bottom = 184, zoom = 2,
x = -12, y = 138, dx = -20, dy = 4,
},
{
name = "Lab",
left = 20, top = 86, right = 141, bottom = 183, zoom = 2,
x = 12, y = 138, dx = 30, dy = 3,
},
{
name = "Engineering",
left = -77, top = 187, right = 74, bottom = 257, zoom = 2,
x = 0, y = 180, dy = 30, doorCode = "1010", sound = "Engine Hum.mp3",
},
},
}
-- Return the name of the room the user is in, or nil if none
function game.roomName()
if roomInside then
return roomInside.name
end
end
-- Return true if the user has entered the given room name
function game.roomEntered( roomName )
for _, room in ipairs( ship.rooms ) do
if room.name == roomName and room.entered then
return true
end
end
return false
end
-- Return the x, y destination constrained to the hallways of the ship,
-- taking into account the current position of the dot.
local function constrainToHalls( x, y )
-- Test to see which hall(s) the dot is in
local inVHall = game.xyInRect( dot.x, dot.y, ship.vHall )
local inBHHall = game.xyInRect( dot.x, dot.y, ship.bhHall )
local inTHHall = game.xyInRect( dot.x, dot.y, ship.thHall)
-- If dot is currently in the intersection of both the vertical and bottom horizontal hall,
-- determine which direction is closer to the destination.
if inBHHall and inVHall then
if math.abs( x - dot.x) > math.abs( y - dot.y ) then
inVHall = false -- will prefer horizontal movement
else
inBHHall = false -- will prefer vertical movement
end
end
-- If dot is currently in the intersection of both the vertical and top horizontal hall,
-- determine which direction is closer to the destination.
if inTHHall and inVHall then
if math.abs( x - dot.x) > math.abs( y - dot.y ) then
inVHall = false -- will prefer horizontal movement
else
inTHHall = false -- will prefer vertical movement
end
end
-- If dot is currently in a horizontal hall, then prefer horizontal movement,
-- else prefer vertical movement
if inBHHall then
-- Constrain to the horizontal hall
x = game.pinValue( x, ship.bhHall.left, ship.bhHall.right )
y = game.pinValue( y, ship.bhHall.top, ship.bhHall.bottom )
elseif inTHHall then
-- Constrain to the top horizontal hall
x = game.pinValue( x, ship.thHall.left, ship.thHall.right )
y = game.pinValue( y, ship.thHall.top, ship.thHall.bottom )
else
-- Constrain to the vertical hall
x = game.pinValue( x, ship.vHall.left, ship.vHall.right )
y = game.pinValue( y, ship.vHall.top, ship.vHall.bottom )
end
return x, y
end
-- Walk the dot to the given position on the map.
-- If the time is not given, use a default walking speed (time proportional to distance)
local function walkTo( x, y, time )
-- Compute time from distance from current location to destination, if needed
if not time then
local d = math.sqrt( (x - dot.x)^2 + (y - dot.y)^2 )
time = d / walkSpeed
end
-- Move dot to destination
transition.cancel( dot ) -- stop previous movement if any
transition.to( dot, { x = x, y = y, time = time, transition = easing.inOutSin } )
-- Count total moves
game.moves = game.moves + 1
-- Use a little food and water
game.addWater( -0.5 )
game.addFood( -0.5 )
--print( "Water = " .. game.water() .. ", food = " .. game.food() )
end
-- Handle touch on a map gem icon
local function gemTouched( event )
if event.phase == "began" then
local icon = event.target
local gem = icon.gem
if gem.t == "act" then
-- Run the linked activity
game.actGemName = icon.name
game.actParam = gem.param
game.gotoAct( gem.act, { effect = "crossFade", time = 500 } )
elseif gem.t == "doc" then
-- Get the document
game.foundDocument( gem.file, gem.ext )
gems.grabGemIcon( icon )
elseif gem.t == "res" then
-- Add the resource
if gem.res == "h2o" then
game.addWater( gem.amount )
elseif gem.res == "food" then
game.addFood( gem.amount )
elseif gem.res == "kWh" then
game.addEnergy( gem.amount )
end
gems.grabGemIcon( icon )
end
end
return true
end
-- Update the ambient sound depending on the room
local function updateAmbientSound()
if roomInside and roomInside.sound then
game.playAmbientSound( roomInside.sound )
else
game.playAmbientSound( "Ship Ambience.mp3" )
end
end
-- Make and return a display group of the active icons for the given room
local function makeIconGroup( room )
-- Find all active gems that are in the bounds of the room
local group = act:newGroup( shipGroup ) -- icons are centered on the ship
for name, gem in pairs( gems.onShip ) do
if gems.shipGemIsActive( name ) and game.xyInRect( gem.x, gem.y, room ) then
local icon = gems.newGemIcon( group, name, gem )
icon.xScale = 1 / room.zoom -- icon size does not zoom
icon.yScale = icon.xScale
icon:addEventListener( "touch", gemTouched )
end
end
return group
end
-- Change to the zoomed view for the given room
local function zoomToRoom( room )
-- Fade in icons for gems in the room
assert( iconGroup == nil )
iconGroup = makeIconGroup( room )
iconGroup.alpha = 0 -- will be faded in
transition.fadeIn( iconGroup, { time = zoomTime, transition = easing.inCubic } )
-- Animate the dot walking into the room
local x = room.x + (room.dx or 0)
local y = room.y + (room.dy or 0)
walkTo( x, y, zoomTime )
roomInside = room
room.entered = true
-- Zoom the map in, centered at the room's center
local scale = room.zoom
local x = act.xCenter - scale * (room.left + room.right) / 2
local y = act.yCenter - scale * (room.top + room.bottom) / 2
transition.to( shipGroup, { x = x, y = y, xScale = scale, yScale = scale,
time = zoomTime, onComplete = zoomDone } )
transition.to( dot, { xScale = 1/scale, yScale = 1/scale; time = zoomTime } ) -- keep dot original size
-- Show the title bar with this room name
act.title.text = room.name
titleBar.isVisible = true
transition.to( titleBar, { y = yTitleBar, time = zoomTime } )
-- Update the ambient sound when we enter the room
timer.performWithDelay( zoomTime, updateAmbientSound )
end
-- Called when a zoom out of a room is complete
local function zoomOutDone()
-- Remove gem icons (they are done fading out)
if iconGroup then
iconGroup:removeSelf()
iconGroup = nil
end
end
-- Hide the title bar
local function hideTitleBar()
titleBar.isVisible = false
end
-- Exit the currently zoomed room
local function exitRoom()
if roomInside then
-- Walk to just outside the door of the room we are in
walkTo( roomInside.x, roomInside.y, zoomTime )
roomInside = nil
-- Remove any active message box
game.endMessageBox()
-- Fade out then delete any gem icons
if iconGroup then
transition.fadeOut( iconGroup, { time = zoomTime, transition = easing.outCubic,
onComplete = zoomOutDone } )
end
-- Zoom the map out
transition.to( shipGroup, { x = act.xCenter, y = act.yCenter, xScale = 1, yScale = 1; time = zoomTime })
transition.to( dot, { xScale = 1, yScale = 1; time = zoomTime } )
-- Hide the title bar
transition.to( titleBar, { y = yTitleBar - act.dyTitleBar, time = zoomTime, onComplete = hideTitleBar })
-- Update the ambient sound when we exit the room
timer.performWithDelay( zoomTime, updateAmbientSound )
end
end
-- Handle touch event on the map
local function touchMap( event )
if event.phase == "began" then
-- Get tap position in shipGroup coords
local x, y = shipGroup:contentToLocal( event.x, event.y )
-- Are we currently zoomed inside a room?
if roomInside then
-- If the click is outside the room, go outside, else ignore it
if not game.xyInRect( x, y, roomInside ) then
exitRoom()
end
return true
end
-- If dot is near a door and the touch is inside that room then go inside
for i = 1, #ship.rooms do
local room = ship.rooms[i]
if game.xyHitTest( dot.x, dot.y, room.x, room.y, 10 ) then
if game.xyInRect( x, y, room ) then
-- Is this room locked?
if room.doorCode then
-- Use the doorLock act
game.lockedRoom = room
game.doorCode = room.doorCode
game.gotoAct( "doorLock", { effect = "slideLeft", time = 500 } )
else
-- Not locked, just go inside
zoomToRoom( room )
end
return true
end
end
end
-- If the touch is inside a room then walk to just outside the door
for i = 1, #ship.rooms do
local room = ship.rooms[i]
if game.xyInRect( x, y, room ) then
x, y = room.x, room.y
end
end
-- Constrain position to walkable portion of the ship and walk there
x, y = constrainToHalls( x, y )
walkTo( x, y )
end
return true
end
-- Handle tap on the back button in the title bar (when zoomed)
local function backTapped()
exitRoom()
return true
end
-- Handle new frame events
function act:enterFrame()
-- Continuous scroll of the endless space background
for i = 1, 2 do
local bg = spaceBgs[i]
bg.y = bg.y + 0.5
if bg.y > act.yMax then
bg.y = act.yMin - act.height
end
end
end
-- Make map background visible and remove ship outside image
local function removeShipOutside( shipOutside )
transition.fadeOut( shipOutside, { time = 1000, onComplete = game.removeObj } )
map.isVisible = true
end
-- Init the act
function act:init()
-- Space background images (2 for continuous scrolling)
spaceBgs = {
act:newImage( "space.jpg", { y = act.yMin, anchorY = 0, height = act.height } ),
act:newImage( "space.jpg", { y = act.yMin - act.height, anchorY = 0, height = act.height } ),
}
-- Mars background image
marsBg = act:newImage( "mars.jpg", { height = act.height } )
-- Display group for ship elements (centered on ship)
shipGroup = act:newGroup()
shipGroup.x = act.xCenter
shipGroup.y = act.yCenter
-- Start outside of the ship and zooms into it
local shipOutside = act:newImage( "shipOutside.png", { width = act.width - 10 } )
local params = {
delay = 1000,
time = 1500,
xScale = 1.9,
yScale = 1.9,
y = act.yCenter + 80,
transition = easing.inOutSine,
onComplete = removeShipOutside
}
transition.to( shipOutside, params )
-- Map background with touch listener
map = act:newImage( "shipPlan2.png", { parent = shipGroup, width = act.width, x = 0, y = 0 } )
map:addEventListener( "touch", touchMap )
map.isVisible = false
--[[ Display rectangles in the walkable parts of the hallways (testing only)
local r = display.newRect( shipGroup, ship.vHall.left, ship.vHall.top,
ship.vHall.right - ship.vHall.left, ship.vHall.bottom - ship.vHall.top )
r.anchorX = 0
r.anchorY = 0
r:setFillColor( 0.5 )
r.alpha = 0.5
r = display.newRect( shipGroup, ship.bhHall.left, ship.bhHall.top,
ship.bhHall.right - ship.bhHall.left, ship.bhHall.bottom - ship.bhHall.top )
r.anchorX = 0
r.anchorY = 0
r:setFillColor( 0.3 )
r.alpha = 0.5
r = display.newRect( shipGroup, ship.thHall.left, ship.thHall.top,
ship.thHall.right - ship.thHall.left, ship.thHall.bottom - ship.thHall.top )
r.anchorX = 0
r.anchorY = 0
r:setFillColor( 0.3 )
r.alpha = 0.5
--]]
--[[ Display room bounds and door locations (testing only)
for _, room in pairs(ship.rooms) do
local r = display.newRect( shipGroup, room.left, room.top,
room.right - room.left, room.bottom - room.top )
r.anchorX = 0
r.anchorY = 0
r:setFillColor( 0.5, 0.5, 0 )
r.alpha = 0.5
local c = display.newCircle( shipGroup, room.x, room.y, 5 )
c:setFillColor( 1, 0, 0 )
c.alpha = 0.5
end
--]]
-- Blue position dot, starting just outside the lab
dot = act:newImage( "blueDot.png", { parent = shipGroup } )
local lab = ship.rooms[1]
dot.x, dot.y = 9, 138 -- outside the Lab
-- Title bar to use when map is zoomed, invisible and off screen when unzoomed
titleBar = act:makeTitleBar( "", backTapped )
yTitleBar = titleBar.y -- remember normal (visible) position
titleBar.y = titleBar.y - act.dyTitleBar -- move off screen upwards
titleBar.isVisible = false
end
-- Land the ship and update ship state as necessary
function game.landShip()
-- Update state variable
game.saveState.onMars = true
-- Remove and disable ship acts no longer needed
gems.enableShipGem( "fly1", false )
gems.enableShipGem( "panel1", false )
gems.enableShipGem( "panel2", false )
gems.enableShipGem( "panel3", false )
gems.enableShipGem( "landing", false )
game.removeAct( "thrustNav" )
game.removeAct( "circuit" )
game.removeAct( "wireCut" )
game.removeAct( "shipLanding" )
-- Enable Mars acts
gems.enableShipGem( "rover" )
gems.enableShipGem( "recharge" )
gems.enableShipGem( "plants" )
gems.enableShipGem( "bugs")
game.sendMessage( "mars2", 11000 )
end
-- Select the proper background image
local function selectBackground()
local onMars = game.saveState.onMars or game.shipLanded
spaceBgs[1].isVisible = not onMars
spaceBgs[2].isVisible = not onMars
marsBg.isVisible = onMars
end
-- Prepare the view before it shows
function act:prepare()
selectBackground() -- Select correct background image
-- Are we zoomed inside a room?
if roomInside then
-- Reload the room's icons in case the enabled state of any changed
if iconGroup then
iconGroup:removeSelf()
iconGroup = makeIconGroup( roomInside )
end
else
-- If we just unlocked a door (coming back from doorLock act) then go in
if game.lockedRoom and game.doorUnlocked then
zoomToRoom( game.lockedRoom )
end
-- Reset for next door
game.lockedRoom = nil
game.doorCode = nil
game.doorUnlocked = nil
end
end
-- Start the act
function act:start()
game.paused = false
updateAmbientSound()
end
-- Stop the act
function act:stop()
game.endMessageBox()
end
------------------------- End of Activity --------------------------------
-- Corona needs the scene object returned from the act file
return act.scene