forked from tutsplus/Corona-PlaneFightingGamePart4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamelevel.lua
executable file
·583 lines (524 loc) · 16 KB
/
gamelevel.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
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local playerSpeedY = 0
local playerSpeedX = 0
local playerMoveSpeed = 7
local playerWidth = 60
local playerHeight = 48
local bulletWidth = 8
local bulletHeight = 19
local islandHeight = 81
local islandWidth = 100
local numberofEnemysToGenerate = 0
local numberOfEnemysGenerated = 0
local playerBullets = {}
local enemyBullets = {}
local islands = {}
local planeGrid = {}
local enemyPlanes = {}
local livesImages = {}
local numberOfLives = 3
local freeLifes = {}
local playerIsInvincible = false
local gameOver = false
local numberOfTicks = 0
local islandGroup
local planeGroup
local player
local planeSoundChannel
local firePlayerBulletTimer
local generateIslandTimer
local fireEnemyBulletsTimer
local generateFreeLifeTimer
local rectUp
local rectDown
local rectLeft
local rectRight
function scene:createScene( event )
local group = self.view
setupBackground()
setupGroups()
setupDisplay()
setupPlayer()
setupLivesImages()
setupDPad()
resetPlaneGrid()
end
-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )
function scene:enterScene( event )
local group = self.view
local previousScene = storyboard.getPrevious()
storyboard.removeScene(previousScene)
rectUp:addEventListener( "touch", movePlane)
rectDown:addEventListener( "touch", movePlane)
rectLeft:addEventListener( "touch", movePlane)
rectRight:addEventListener( "touch", movePlane)
local planeSound = audio.loadStream("planesound.mp3")
planeSoundChannel = audio.play( planeSound, {loops=-1} )
Runtime:addEventListener("enterFrame", gameLoop)
startTimers()
generateEnemys()
end
scene:addEventListener( "enterScene", scene )
function setupBackground ()
local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight)
background:setFillColor( 0,0,1)
scene.view:insert(background)
end
function setupGroups()
islandGroup = display.newGroup()
planeGroup = display.newGroup()
scene.view:insert(islandGroup)
scene.view:insert(planeGroup)
end
function setupDisplay ()
local tempRect = display.newRect(0,display.contentHeight-70,display.contentWidth,124);
tempRect:setFillColor(0,0,0);
scene.view:insert(tempRect)
local logo = display.newImage("logo.png",display.contentWidth-139,display.contentHeight-70);
scene.view:insert(logo)
local dpad = display.newImage("dpad.png",10,display.contentHeight - 70)
scene.view:insert(dpad)
end
function setupPlayer()
player = display.newImage("player.png",(display.contentWidth/2)-(playerWidth/2),(display.contentHeight - 70)-playerHeight)
player.name = "Player"
scene.view:insert(player)
end
function setupLivesImages()
for i = 1, 6 do
local tempLifeImage = display.newImage("life.png", 40* i - 20, 10)
table.insert(livesImages,tempLifeImage)
scene.view:insert(tempLifeImage)
if( i > 3) then
tempLifeImage.isVisible = false;
end
end
end
function setupDPad()
rectUp = display.newRect( 34, display.contentHeight-70, 23, 23)
rectUp:setFillColor(1,0,0)
rectUp.id ="up"
rectUp.isVisible = false;
rectUp.isHitTestable = true;
scene.view:insert(rectUp)
rectDown = display.newRect( 34,display.contentHeight-23, 23,23)
rectDown:setFillColor(1,0,0)
rectDown.id ="down"
rectDown.isVisible = false;
rectDown.isHitTestable = true;
scene.view:insert(rectDown)
rectLeft = display.newRect( 10,display.contentHeight-47,23, 23)
rectLeft:setFillColor(1,0,0)
rectLeft.id ="left"
rectLeft.isVisible = false;
rectLeft.isHitTestable = true;
scene.view:insert(rectLeft)
rectRight= display.newRect( 58,display.contentHeight-47, 23,23)
rectRight:setFillColor(1,0,0)
rectRight.id ="right"
rectRight.isVisible = false;
rectRight.isHitTestable = true;
scene.view:insert(rectRight)
end
function resetPlaneGrid()
planeGrid = {}
for i=1, 11 do
table.insert(planeGrid,0)
end
end
function movePlane(event)
if event.phase == "began" then
if(event.target.id == "up") then
playerSpeedY = -playerMoveSpeed
end
if(event.target.id == "down") then
playerSpeedY = playerMoveSpeed
end
if(event.target.id == "left") then
playerSpeedX = -playerMoveSpeed
end
if(event.target.id == "right") then
playerSpeedX = playerMoveSpeed
end
elseif event.phase == "ended" then
playerSpeedX = 0
playerSpeedY = 0
end
end
function movePlayer()
player.x = player.x + playerSpeedX
player.y = player.y + playerSpeedY
if(player.x < 0) then
player.x = 0
end
if(player.x > display.contentWidth - playerWidth) then
player.x = display.contentWidth - playerWidth
end
if(player.y < 0) then
player.y = 0
end
if(player.y > display.contentHeight - 70- playerHeight) then
player.y = display.contentHeight - 70 - playerHeight
end
end
function gameLoop()
numberOfTicks = numberOfTicks + 1
movePlayer()
movePlayerBullets()
checkPlayerBulletsOutOfBounds()
moveIslands()
checkIslandsOutOfBounds()
moveFreeLifes()
checkFreeLifesOutOfBounds()
checkPlayerCollidesWithFreeLife()
moveEnemyPlanes()
moveEnemyBullets()
checkEnemyBulletsOutOfBounds()
checkEnemyPlanesOutOfBounds()
checkPlayerBulletsCollideWithEnemyPlanes()
checkEnemyBulletsCollideWithPlayer()
checkEnemyPlaneCollideWithPlayer()
end
function firePlayerBullet()
local tempBullet = display.newImage("bullet.png",(player.x+playerWidth/2) - bulletWidth,player.y-bulletHeight)
table.insert(playerBullets,tempBullet);
planeGroup:insert(tempBullet)
end
function startTimers()
firePlayerBulletTimer = timer.performWithDelay(2000, firePlayerBullet ,-1)
generateIslandTimer = timer.performWithDelay( 5000, generateIsland ,-1)
generateFreeLifeTimer = timer.performWithDelay(7000,generateFreeLife, - 1)
fireEnemyBulletsTimer = timer.performWithDelay(2000,fireEnemyBullets,-1)
end
function movePlayerBullets()
if(#playerBullets > 0) then
for i=1,#playerBullets do
playerBullets[i]. y = playerBullets[i].y - 7
end
end
end
function checkPlayerBulletsOutOfBounds()
if(#playerBullets > 0) then
for i=#playerBullets,1,-1 do
if(playerBullets[i].y < -18) then
playerBullets[i]:removeSelf()
playerBullets[i] = nil
table.remove(playerBullets,i)
end
end
end
end
function generateIsland()
local tempIsland = display.newImage("island1.png", math.random(0,display.contentWidth - islandWidth),-islandHeight)
table.insert(islands,tempIsland)
islandGroup:insert( tempIsland )
end
function moveIslands()
if(#islands > 0) then
for i=1, #islands do
islands[i].y = islands[i].y + 3
end
end
end
function checkIslandsOutOfBounds()
if(#islands > 0) then
for i=#islands,1,-1 do
if(islands[i].y > display.contentHeight) then
islands[i]:removeSelf()
islands[i] = nil
table.remove(islands,i)
end
end
end
end
function generateFreeLife ()
if(numberOfLives >= 6) then
return
end
local freeLife = display.newImage("newlife.png", math.random(0,display.contentWidth - 40), 0);
table.insert(freeLifes,freeLife)
planeGroup:insert(freeLife)
end
function moveFreeLifes()
if(#freeLifes > 0) then
for i=1,#freeLifes do
freeLifes[i].y = freeLifes[i].y +5
end
end
end
function checkFreeLifesOutOfBounds()
if(#freeLifes > 0) then
for i=#freeLifes,1,-1 do
if(freeLifes[i].y > display.contentHeight) then
freeLifes[i]:removeSelf()
freeLifes[i] = nil
table.remove(freeLifes,i)
print("REMOVING FREE LIFE")
end
end
end
end
function hasCollided( obj1, obj2 )
if ( obj1 == nil ) then
return false
end
if ( obj2 == nil ) then
return false
end
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
return (left or right) and (up or down)
end
function checkPlayerCollidesWithFreeLife()
if(#freeLifes > 0) then
for i=#freeLifes,1,-1 do
if(hasCollided(freeLifes[i], player)) then
freeLifes[i]:removeSelf()
freeLifes[i] = nil
table.remove(freeLifes, i)
numberOfLives = numberOfLives + 1
hideLives()
showLives()
end
end
end
end
function hideLives()
for i=1, 6 do
livesImages[i].isVisible = false
end
end
function showLives()
for i=1, numberOfLives do
livesImages[i].isVisible = true;
end
end
function generateEnemys()
numberOfEnemysToGenerate = math.random(3,7)
timer.performWithDelay( 2000, generateEnemyPlane,numberOfEnemysToGenerate)
end
function generateEnemyPlane()
if(gameOver ~= true) then
local randomGridSpace = math.random(11)
local randomEnemyNumber = math.random(3)
local tempEnemy
if(planeGrid[randomGridSpace]~=0) then
generateEnemyPlane()
return
else
if(randomEnemyNumber == 1)then
tempEnemy = display.newImage("enemy1.png", (randomGridSpace*65)-28,-60)
tempEnemy.type = "regular"
elseif(randomEnemyNumber == 2) then
tempEnemy = display.newImage("enemy2.png", display.contentWidth/2 - playerWidth/2,-60)
tempEnemy.type = "waver"
else
tempEnemy = display.newImage("enemy3.png", (randomGridSpace*65)-28,-60)
tempEnemy.type = "chaser"
end
planeGrid[randomGridSpace] = 1
table.insert(enemyPlanes,tempEnemy)
planeGroup:insert(tempEnemy)
numberOfEnemysGenerated = numberOfEnemysGenerated+1;
end
if(numberOfEnemysGenerated == numberOfEnemysToGenerate)then
numberOfEnemysGenerated = 0;
resetPlaneGrid()
timer.performWithDelay(2000,generateEnemys,1)
end
end
end
function moveEnemyPlanes()
if(#enemyPlanes > 0) then
for i=1, #enemyPlanes do
if(enemyPlanes[i].type == "regular") then
moveRegularPlane(enemyPlanes[i])
elseif(enemyPlanes[i].type == "waver") then
moveWaverPlane(enemyPlanes[i])
else
moveChaserPlane(enemyPlanes[i])
end
end
end
end
function moveRegularPlane(plane)
plane.y = plane.y+4
end
function moveWaverPlane(plane)
plane.y =plane.y+4
plane.x = (display.contentWidth/2)+ 250* math.cos(numberOfTicks * 0.5 * math.pi/30) ;
end
function moveChaserPlane(plane)
if(plane.x < player.x)then
plane.x =plane.x +4
end
if(plane.x > player.x)then
plane.x = plane.x - 4
end
plane.y = plane.y + 4
end
function fireEnemyBullets()
if(#enemyPlanes >= 2) then
local numberOfEnemyPlanesToFire = math.floor(#enemyPlanes/2)
local tempEnemyPlanes = table.copy(enemyPlanes)
local function fireBullet()
local randIndex = math.random(#tempEnemyPlanes)
local tempBullet = display.newImage("bullet.png",(tempEnemyPlanes[randIndex].x+playerWidth/2) + bulletWidth,tempEnemyPlanes[randIndex].y+playerHeight+bulletHeight)
tempBullet.rotation = 180
planeGroup:insert(tempBullet)
table.insert(enemyBullets,tempBullet);
table.remove(tempEnemyPlanes,randIndex)
end
for i = 0, numberOfEnemyPlanesToFire do
fireBullet()
end
end
end
function moveEnemyBullets()
if(#enemyBullets > 0) then
for i=1,#enemyBullets do
enemyBullets[i]. y = enemyBullets[i].y + 7
end
end
end
function checkEnemyBulletsOutOfBounds()
if(#enemyBullets > 0) then
for i=#enemyBullets,1,-1 do
if(enemyBullets[i].y > display.contentHeight) then
enemyBullets[i]:removeSelf()
enemyBullets[i] = nil
table.remove(enemyBullets,i)
end
end
end
end
function checkEnemyPlanesOutOfBounds()
if(#enemyPlanes> 0) then
for i=#enemyPlanes,1,-1 do
if(enemyPlanes[i].y > display.contentHeight) then
enemyPlanes[i]:removeSelf()
enemyPlanes[i] = nil
table.remove(enemyPlanes,i)
end
end
end
end
function checkPlayerBulletsCollideWithEnemyPlanes()
if(#playerBullets > 0 and #enemyPlanes > 0) then
for i=#playerBullets,1,-1 do
for j=#enemyPlanes,1,-1 do
if(hasCollided(playerBullets[i], enemyPlanes[j])) then
playerBullets[i]:removeSelf()
playerBullets[i] = nil
table.remove(playerBullets,i)
generateExplosion(enemyPlanes[j].x,enemyPlanes[j].y)
enemyPlanes[j]:removeSelf()
enemyPlanes[j] = nil
table.remove(enemyPlanes,j)
local explosion = audio.loadStream("explosion.mp3")
local backgroundMusicChannel = audio.play( explosion, {fadein=1000 } )
end
end
end
end
end
function generateExplosion(xPosition , yPosition)
local options = { width = 60,height = 49,numFrames = 6}
local explosionSheet = graphics.newImageSheet( "explosion.png", options )
local sequenceData = {
{ start=1, count=6, time=400, loopCount=1 }
}
local explosionSprite = display.newSprite( explosionSheet, sequenceData )
explosionSprite.x = xPosition
explosionSprite.y = yPosition
explosionSprite:addEventListener( "sprite", explosionListener )
explosionSprite:play()
end
function explosionListener( event )
if ( event.phase == "ended" ) then
local explosion = event.target
explosion:removeSelf()
explosion = nil
end
end
function checkEnemyBulletsCollideWithPlayer()
if(#enemyBullets > 0) then
for i=#enemyBullets,1,-1 do
if(hasCollided(enemyBullets[i],player)) then
enemyBullets[i]:removeSelf()
enemyBullets[i] = nil
table.remove(enemyBullets,i)
if(playerIsInvincible == false) then
killPlayer()
end
end
end
end
end
function killPlayer()
numberOfLives = numberOfLives- 1;
if(numberOfLives == 0) then
gameOver = true
doGameOver()
else
spawnNewPlayer()
hideLives()
showLives()
playerIsInvincible = true
end
end
function doGameOver()
storyboard.gotoScene("gameover")
end
function spawnNewPlayer()
local numberOfTimesToFadePlayer = 5
local numberOfTimesPlayerHasFaded = 0
local function fadePlayer()
player.alpha = 0;
transition.to( player, {time=400, alpha=1, })
numberOfTimesPlayerHasFaded = numberOfTimesPlayerHasFaded + 1
if(numberOfTimesPlayerHasFaded == numberOfTimesToFadePlayer) then
playerIsInvincible = false
end
end
fadePlayer()
timer.performWithDelay(400, fadePlayer,numberOfTimesToFadePlayer)
end
function checkEnemyPlaneCollideWithPlayer()
if(#enemyPlanes > 0) then
for i=#enemyPlanes,1,-1 do
if(hasCollided(enemyPlanes[i], player)) then
enemyPlanes[i]:removeSelf()
enemyPlanes[i] = nil
table.remove(enemyPlanes,i)
if(playerIsInvincible == false) then
killPlayer()
end
end
end
end
end
-- Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
rectUp:removeEventListener( "touch", movePlane)
rectDown:removeEventListener( "touch", movePlane)
rectLeft:removeEventListener( "touch", movePlane)
rectRight:removeEventListener( "touch", movePlane)
audio.stop(planeSoundChannel)
audio.dispose(planeSoundChannel)
Runtime:removeEventListener("enterFrame", gameLoop)
cancelTimers()
end
scene:addEventListener( "exitScene", scene )
function cancelTimers()
timer.cancel( firePlayerBulletTimer )
timer.cancel(generateIslandTimer)
timer.cancel(fireEnemyBulletsTimer)
timer.cancel(generateFreeLifeTimer)
end
return scene