-
Notifications
You must be signed in to change notification settings - Fork 0
/
shipLanding.lua
481 lines (392 loc) · 14.8 KB
/
shipLanding.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
-----------------------------------------------------------------------------------------
--
-- shipLanding.lua
--
-- Ship Landing Activity by Ryan Bains-Jordan
-----------------------------------------------------------------------------------------
-- Get local reference to the game globals
local game = globalGame
-- Create the act object
local act = game.newAct()
-- Act Requisites
local physics = require( "physics" )
local widget = require( "widget" )
------------------------- Start of Activity --------------------------------
-- Display Groups
local map
local ship
local endGame
-- Sound
local sound = {}
local soundChannel = {}
local musicChannel = {}
-- Boolean Variables that may loop in newFrame
local thrustOn = false
local isEnded = false
local isRotateC = false
local isRotateCC = false
local gameEnded = false
-- Numberic Variables and Constants
local MAPXMIN, MAPXMAX = 20, 660 -- Playable area of the map
local LANDXMIN, LANDXMAX = 350, 550 -- Borders of the successful land area
local LANDDETECTOR = 3520 -- Height of the landing detector
local CRASHDETECTOR = 3500 -- Height of the crash detector
local LANDHEIGHT = 3600 -- Height of the physical land
local SHIPSTARTX, SHIPSTARTY -- Start position of the ship
-- Scales
local verticalScale, horizontalScale
--------------------
-- Listener Functions
--------------------
-- Listener for Back Button
local function back()
game.gotoAct( "mainAct", { effect = "zoomOutIn", time = 500 } )
end
-- Listener for Thrust Button
local function thrust( event )
if event.phase == "began" then
thrustOn = true
ship.leftThrust.isVisible = true
ship.midLeftThrust.isVisible = true
ship.midRightThrust.isVisible = true
ship.rightThrust.isVisible = true
soundChannel.ignite = game.playSound( sound.ignite )
soundChannel.thrust = game.playSound( sound.thrust, { loops = -1 } )
elseif event.phase == "ended" then
thrustOn = false
ship.leftThrust.isVisible = false
ship.midLeftThrust.isVisible = false
ship.midRightThrust.isVisible = false
ship.rightThrust.isVisible = false
game.stopSound( soundChannel.thrust )
end
return true
end
-- Listener for clockwise rotation button
local function rotateC( event )
if event.phase == "began" then
isRotateC = true
ship.rightThrust.isVisible = true
soundChannel.ignite = game.playSound( sound.ignite )
soundChannel.thrust = game.playSound( sound.thrust, { loops = -1 } )
else
isRotateC = false
ship.rightThrust.isVisible = false
game.stopSound( soundChannel.thrust )
end
return true
end
-- Listener for counter-clockwise rotation button
local function rotateCC( event )
if event.phase == "began" then
isRotateCC = true
ship.leftThrust.isVisible = true
soundChannel.ignite = game.playSound( sound.ignite )
soundChannel.thrust = game.playSound( sound.thrust, { loops = -1 } )
else
isRotateCC = false
ship.leftThrust.isVisible = false
game.stopSound( soundChannel.thrust )
end
return true
end
-- Function to Correct Landing
local function autopilot( event )
ship.autopilot = true
physics.removeBody( ship )
-- Function to turn off Autopilot
local autoPilotOff = function( obj )
physics.addBody( ship, { density = 0.6 } )
ship.leftThrust.isVisible, ship.midLeftThrust.isVisible = false, false
ship.rightThrust.isVisible, ship.midRightThrust.isVisible = false, false
game.stopSound( soundChannel.thrust )
ship.autopilot = false
end
-- Autopilot Transition
transition.to( ship, { y = ship.y - 1000, rotation = 0, time = 3000, onComplete = autoPilotOff } )
ship.leftThrust.isVisible, ship.midLeftThrust.isVisible = true, true
ship.rightThrust.isVisible, ship.midRightThrust.isVisible = true, true
soundChannel.ignite = game.playSound( sound.ignite )
soundChannel.thrust = game.playSound( sound.thrust, { loops = -1 } )
end
-- Function for Ending the Game
local function endTheGame( )
endGame.isVisible = true
physics.pause( )
gameEnded = true
game.shipLanded = true
end
--------------------
-- Looped Functions
--------------------
-- Function to adjust map objects according to ship
local function moveCamera()
if ( ship.y > 200 and ship.y < 3355 ) then --3275
map.y = -ship.y + 200
end
if ( ship.x > 200 and ship.x < 540 ) then
map.x = -ship.x + 200
end
end
-- Function to test whether the landing is succsessful or not
local function testLanding()
if ship.y > LANDDETECTOR then
local xv, yv = ship:getLinearVelocity( )
-- Successful Landing
if ship.x > LANDXMIN and ship.x < LANDXMAX -- Is the ship in the x landing range
and math.abs( ship.rotation ) < 10 -- Is the ship rotation somewhat flat
and math.abs( xv ) < 30 -- Is the ships xVelocity less than 30
--and math.abs( yv ) < 20 -- Is the ships yVelocity less than 20
then
--print("successful landing")
if gameEnded == false then
endTheGame()
end
-- Unsuccessful Landing
else
ship.autopilot = true
end
end
end
-- Function to test whether or not to fire Autopilot
local function testWillCrash()
-- If the ship has passed the CRASHDETECTOR and is about to crash
if ship.y > CRASHDETECTOR then
local xv, yv = ship:getLinearVelocity( )
print( yv )
if yv > 75 then
print( yv )
return true
end
end
-- If the ship has passed the CRASHDETECTOR but is not about to crash
return false
end
--------------------
-- Convenience Functions
--------------------
-- Convenience Function for creating Rotation buttons
local function createSideThrustButton( scene, x, y, vertices, listener )
local b = widget.newButton {
x = x, y = y,
shape = "polygon",
vertices = vertices,
fillColor = { default = { game.themeColor.r, game.themeColor.g, game.themeColor.b },
over = { game.themeHighlightColor.r, game.themeHighlightColor.g, game.themeHighlightColor.b } },
labelColor = { default={ 1, 1, 1 } },
onEvent = listener
}
scene:insert(b)
return b
end
-----------------------------------------------------------------------------------------
-- Init Game
-----------------------------------------------------------------------------------------
function act:init( )
SHIPSTARTX, SHIPSTARTY = act.xCenter, 80
-- Scale used to determine altitude of ship
verticalScale = act:newGroup( )
verticalScale.top = act.yMin + 40
verticalScale.bottom = act.yMax - 100
-- Scale used to determine linear location of ship
horizontalScale = act:newGroup( )
horizontalScale.left = act.xMin + 100
horizontalScale.right = act.xMax - 40
-- Physics
physics.start( )
physics.pause( )
physics.setScale( 10 )
-- Sound
sound.ignite = act:loadSound( "ignite.wav" )
sound.thrust = act:loadSound( "thrust.wav" )
-- Background
map = act:newGroup( )
local background = act:newImage( "background.png", { parent = map } )
background.anchorX, background.anchorY = 0, 0
background.x, background.y = -20, -40
-- Ground Object
local ground = display.newRect( map, 0, LANDHEIGHT, MAPXMAX, 50 )
ground.anchorX = 0
ground.anchorY = 1
ground:setFillColor( 1, 1, 1, 0 )
physics.addBody( ground, "static" )
-- Torches
local torch1 = act:newImage( "torch.png", { parent = map, width = 25 } )
local torch2 = act:newImage( "torch.png", { parent = map, width = 25 } )
torch1.x, torch1.y = LANDXMIN, LANDHEIGHT - 50
torch2.x, torch2.y = LANDXMAX, LANDHEIGHT - 50
-- End Game
endGame = act:newGroup( )
endGame.header = display.newText( endGame, "Successful Landing", act.xCenter, act.yCenter, native.systemFontBold, 30 )
endGame.header:setFillColor( 0 )
endGame.isVisible = false
--------------------
-- Ship
--------------------
-- Ship
ship = act:newGroup( map )
ship.x, ship.y = SHIPSTARTX, SHIPSTARTY
ship.image = act:newImage( "ship.png", { parent = ship, width = 150 } )
ship.image.x, ship.image.y = 0, 0
-- Thrusters
ship.leftThrust = act:newImage( "rocketfire.png", { parent = ship } )
ship.leftThrust.x, ship.leftThrust.y, ship.leftThrust.rotation = -55, 15, 45
ship.midLeftThrust = act:newImage( "rocketfire.png", { parent = ship } )
ship.midLeftThrust.x, ship.midLeftThrust.y = 4, 20
ship.midRightThrust = act:newImage( "rocketfire.png", { parent = ship } )
ship.midRightThrust.x, ship.midRightThrust.y = 22, 20
ship.rightThrust = act:newImage( "rocketfire.png", { parent = ship } )
ship.rightThrust.x, ship.rightThrust.y, ship.rightThrust.rotation = 65, 15, -45
-- Thrust Visability
ship.leftThrust.isVisible = false
ship.midLeftThrust.isVisible = false
ship.midRightThrust.isVisible = false
ship.rightThrust.isVisible = false
-- Ship Physics
physics.addBody( ship, { density = 0.6 } )
-- Autopilot
ship.autopilot = false
--------------------
-- Scales
--------------------
-- Vertical Scale Line
local vsty, vsby = verticalScale.top, verticalScale.bottom
verticalScale.x = act.xMax - 20
verticalScale.mainLine = display.newLine( verticalScale, 0, vsty, 0, vsby ) -- The large line on the side of the screen
verticalScale.topLine = display.newLine( verticalScale, -10, vsty, 10, vsty ) -- The top barrier for the line
verticalScale.bottomLine = display.newLine( verticalScale, -10, vsby, 10, vsby ) -- The bottom barrier for the line
verticalScale.positionLine = display.newLine( verticalScale, -10, vsty, 10, vsty ) -- The moving altitude line
act.group:insert( verticalScale )
-- Horizontal Scale Line
local hslx, hsrx = horizontalScale.left, horizontalScale.right
horizontalScale.y = act.yMin + 20
horizontalScale.mainLine = display.newLine( horizontalScale, hslx, 0, hsrx, 0 ) -- The large line at the top of the screen
horizontalScale.leftLine = display.newLine( horizontalScale, hslx, -10, hslx, 10 ) -- The left most barrier for the line
horizontalScale.rightLine = display.newLine( horizontalScale, hsrx, -10, hsrx, 10 ) -- The right most barrier for the line
horizontalScale.positionLine = display.newLine( horizontalScale, hslx, -10, hslx, 10 ) -- The moving linear distance line
act.group:insert( horizontalScale )
--display.newLine( map, 0, LANDDETECTOR, 700, LANDDETECTOR )
--display.newLine( map, 0, CRASHDETECTOR, 700, CRASHDETECTOR )
--------------------
-- Buttons
--------------------
-- Back Button
local backBtn = widget.newButton {
label = "Back",
x = act.xMin + 40, y = act.yMin + 30,
shape = "roundedRect",
width = 60, height = 40,
fillColor = { default = { game.themeColor.r, game.themeColor.g, game.themeColor.b },
over = { game.themeHighlightColor.r, game.themeHighlightColor.g, game.themeHighlightColor.b } },
labelColor = { default={ 1, 1, 1 } },
onEvent = back
}
act.group:insert( backBtn )
-- Thrust Button
local thrustBtn = widget.newButton {
label = "Thrust",
x = act.xMin + 40, y = act.yMax - 35,
shape = "circle",
radius = 30,
fillColor = { default = { game.themeColor.r, game.themeColor.g, game.themeColor.b },
over = { game.themeHighlightColor.r, game.themeHighlightColor.g, game.themeHighlightColor.b } },
labelColor = { default={ 1, 1, 1 } },
onEvent = thrust
}
act.group:insert( thrustBtn )
-- Rotation Buttons
local rotateCBtn = createSideThrustButton( act.group, act.xMax - 80, act.yMax - 35,
{ 0, -30, -40, 0, 0, 30 }, rotateC )
local rotateCCBtn = createSideThrustButton( act.group, act.xMax - 30, act.yMax - 35,
{ 0, -30, 40, 0, 0, 30 }, rotateCC )
--physics.setDrawMode( "hybrid" )
end
-----------------------------------------------------------------------------------------
-- EnterFrame Loop
-----------------------------------------------------------------------------------------
function act:enterFrame()
-- When the user is about to crash
if ship.autopilot == true then
moveCamera()
--TODO: Find a better way to adjust the scales (Function not working)
-- Vertical Scale
verticalScale.positionLine:removeSelf( )
verticalScale.distance = LANDHEIGHT - ship.y
verticalScale.ratio = verticalScale.distance / ( LANDHEIGHT - SHIPSTARTY )
verticalScale.size = verticalScale.bottom - verticalScale.top
verticalScale.indicator = verticalScale.bottom - ( verticalScale.ratio * verticalScale.size )
verticalScale.positionLine = display.newLine( verticalScale, -10, verticalScale.indicator, 10, verticalScale.indicator )
-- Horizontal Scale
horizontalScale.positionLine:removeSelf( )
horizontalScale.distance = MAPXMAX - ship.x
horizontalScale.ratio = horizontalScale.distance / ( MAPXMAX - MAPXMIN )
horizontalScale.size = horizontalScale.right - horizontalScale.left
horizontalScale.indicator = horizontalScale.right - ( horizontalScale.ratio * horizontalScale.size )
horizontalScale.positionLine = display.newLine( horizontalScale, horizontalScale.indicator, -10, horizontalScale.indicator, 10 )
-- When the user is not about to crash
else
if thrustOn then
local a = ship.rotation * math.pi / 180
local xf = math.sin(a) * 1000
local yf = math.cos(a) * -1000
ship:applyForce( xf, yf, ship.x, ship.y)
end
if isRotateC then
ship:rotate(-1)
elseif isRotateCC then
ship:rotate(1)
end
-- Stop the ship from flying past boundries
if ship.x > MAPXMAX then
ship.x = MAPXMAX
local xv, yv = ship:getLinearVelocity( )
ship:setLinearVelocity( 0, xy )
elseif ship.x < MAPXMIN then
ship.x = MAPXMIN
local xv, yv = ship:getLinearVelocity( )
ship:setLinearVelocity( 0, xy )
elseif ship.y < SHIPSTARTY then
ship.y = SHIPSTARTY
local xv, yv = ship:getLinearVelocity( )
ship:setLinearVelocity( xv, 0 )
end
if testWillCrash() then
autopilot()
end
--------------------
-- Scales
--------------------
-- Vertical Scale
verticalScale.positionLine:removeSelf( )
verticalScale.distance = LANDHEIGHT - ship.y
verticalScale.ratio = verticalScale.distance / ( LANDHEIGHT - SHIPSTARTY )
verticalScale.size = verticalScale.bottom - verticalScale.top
verticalScale.indicator = verticalScale.bottom - ( verticalScale.ratio * verticalScale.size )
verticalScale.positionLine = display.newLine( verticalScale, -10, verticalScale.indicator, 10, verticalScale.indicator )
-- Horizontal Scale
horizontalScale.positionLine:removeSelf( )
horizontalScale.distance = MAPXMAX - ship.x
horizontalScale.ratio = horizontalScale.distance / ( MAPXMAX - MAPXMIN )
horizontalScale.size = horizontalScale.right - horizontalScale.left
horizontalScale.indicator = horizontalScale.right - ( horizontalScale.ratio * horizontalScale.size )
horizontalScale.positionLine = display.newLine( horizontalScale, horizontalScale.indicator, -10, horizontalScale.indicator, 10 )
--------------------
-- Looped Functions
--------------------
moveCamera()
testLanding()
end
end
-----------------------------------------------------------------------------------------
-- Scene Control
-----------------------------------------------------------------------------------------
function act:prepare( )
musicChannel = game.playAmbientSound( "Tension.mp3" )
physics.start( )
end
function act:stop( )
physics.pause( )
end
------------------------- End of Activity --------------------------------
-- Corona needs the scene object returned from the act file
return act.scene