-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.lua
2144 lines (1726 loc) · 57.2 KB
/
game.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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local composer = require( "composer" )
local scene = composer.newScene()
--if false jump to monster
local build_release = true
--usefull for debug, invincible
local ship_body_active = false
local physics = require("physics")
local font = require("font")
local popup = require("popup")
physics.start()
physics.setGravity(0,0)
local fieldRadius = 120
local fieldPower = 0.4
--TODO: finish this data type.....
local sheetSyringes =
{
frames =
{
{ --1) INFECTED
x = 95,
y = 4,
width = 28,
height = 67
},
{ --2) NOT INFECTED
x = 95,
y = 132,
width = 28,
height = 67
},
},
}
local bossLifeUI =
{
INFECTED,
NOT_INFECTED,
}
local sheetCommons =
{
frames =
{
{ -- 1) CV
x = 0,
y = 0,
width = 173,
height = 172
},
{ -- 2) laser
x = 173,
y = 0,
width = 10,
height = 38
},
{ -- 3) missile
x = 183,
y = 0,
width = 131,
height = 300
},
{ -- 4) flying missile
x = 314,
y = 0,
width = 138,
height = 517
},
{ -- 5) burner
x = 0,
y = 517,
width = 51,
height = 161
},
{ -- 6) starship
x = 51,
y = 517,
width = 340,
height = 258
},
},
}
local sheetAnimationOptionsLaserExplosion =
{
width = 300,
height = 300,
numFrames = 36,
sheetContentWidth = 1800,
sheetContentHeight = 1800
}
local sequences_laserExplosion =
{
-- consecutive frames sequence
{
name = "normalExplosion",
start = 1,
count = 36,
time = 800,
loopCount = 1,
loopDirection = "forward"
}
}
local sheetAnimationOptionsShipExplosion =
{
width = 128,
height = 128,
numFrames = 3,
sheetContentWidth = 384,
sheetContentHeight = 128
}
local sequences_shipExplosion =
{
-- consecutive frames sequence
{
name = "normalExplosion",
start = 1,
count = 3,
time = 500,
loopCount = 1,
loopDirection = "forward"
}
}
local sequences_shield =
{
{
name = "normalShield",
start = 1,
count = 5,
time = 800,
loopCount = 0,
loopDirection = "forward"
}
}
local sheetAnimationOptionsShield =
{
width = 280,
height = 280,
numFrames = 5,
sheetContentWidth = 1400,
sheetContentHeight = 280
}
local sheetAnimationOptionsMissileExplosion =
{
width = 200,
height = 125,
numFrames = 12,
sheetContentWidth = 600,
sheetContentHeight = 538
}
local sequences_missileExplosion =
{
-- consecutive frames sequence
{
name = "normalExplosion",
start = 1,
count = 12,
time = 1200,
loopCount = 1,
loopDirection = "forward"
}
}
local gameplayTime
local previousTime
local newTime
local deltaTime
local sheet_laserExplosion
local sheet_shipExplosion
local sheet_missileExplosion
local PATHS =
{
path_laserExplosion = "imgs/explosion/explosion_laser.png",
path_missileExplosion = "imgs/explosion/explosion_missile.png",
path_background = "imgs/background.png",
path_sheet_commons = "imgs/gameObjects.png",
path_sheet_syringes = "imgs/Syringe.png",
path_shipExplosion = "imgs/explosion/explosion_ship.png",
path_shipAcceleration = "imgs/flame_sheet.png",
path_shield = "imgs/shield_sheet.png",
path_plasma = "imgs/plasma_red.png",
path_score = "imgs/score_little.png",
path_pickup = "imgs/pickup_beam.png",
path_explosion = "audio/explosion.wav",
path_fire = "audio/fire.wav",
path_keystroke = "audio/typewriter-key-1.wav",
path_track = "audio/80s-Space-Game_Looping.wav",
path_hourglass = "imgs/clessidra.png",
path_heart = "imgs/heart.png",
path_avatar = "imgs/esplorando-il-corpo-umano.png",
path_boss = "imgs/CV_boss.png",
path_popup = "imgs/popup_menu_backgrounded.png",
}
--load the sheets
local objectSheet = graphics.newImageSheet(PATHS.path_sheet_commons,sheetCommons)
local syringeSheet = graphics.newImageSheet(PATHS.path_sheet_syringes,sheetSyringes)
--global game variables, game states
local GAME_VARS =
{
lives = 1,
score = 0,
died = false,
survived = false
}
--array di virus
local virusesTable = {}
local boss
local STATIC_IMGS =
{
avatar,
scorePrefix,
heart,
hourglass,
}
--array di laser
local lasersTable = {}
local SCALE_MISSILE
local gameEnded
local ship
local leftMissileShip
local rigthMissileShip
local leftMissileFlying
local rigthMissileiFlyng
local leftMissileDestroyed
local firingLeftMissile
local rigthMissileDestroyed
local firingRigthMissile
local gameLoopTimer
--local livesText
local scoreText
local antivirusText
--local scorePrefix
local timeText
local backGroup
local mainGroup
local uiGroup
--local heart
--local hourglass
local shield
local GAME_SOUNDS =
{
enemyDeadSound,
playerDeadSound,
fireLaserSound,
musicTrack,
enemyDeadSoundHandle,
playerDeadSoundHandle,
fireLaserSoundHandle,
musicTrackHandle,
}
--fire variable
local lastFireSide
local shieldVisible = true
local bossInfo =
{
BOSS_WIDTH = 140,
BOSS_HEIGTH = 140,
bossLife = 10,
bossDamage = 0
}
local FIRE_OPTIONS =
{
FIRE_RIGTH,
FIRE_LEFT,
FIRE_ALTERNATE,
FIRE_CENTER,
FIRE_BOTH,
OPT_FIRE_BOTH_OFFSET,
LASER_X_SPEED,
LASER_Y_SPEED
}
local SECONDS_TRANSITION_BOSS = 6
local SECONDS_TO_GAMEOVER = 19
local thresholdSpawning = SECONDS_TO_GAMEOVER - SECONDS_TRANSITION_BOSS
local g_fireMode
local halfShipBoundsX
local halfShipBoundsY
local SCALE_TIME
local laser_event = "tap"
local missile_event = "tap"
local move_event = "touch"
local ship_name_1 = "ship"
local missile_name_left_1 = "missile_left"
local missile_name_rigth_1 = "missile_rigth"
--local message_live_rip = "R.I.P"
--local message_live_1 = "Lives: "
local message_score_1 = "Score: "
local message_time_1 = " "
local message_time_2 = " 0"
local menu_scene = "menu"
local gameover_scene = "gameover"
local game_scene = "game"
local laser_name_1 = "laser1"
local laser_name_2 = "laser2"
local enemy_name_1 = "virus"
local enemy_boss_name_1 = "COVID_BOSS_1"
local enemy_laser_name_1 = "laser3"
local spawn_X_1 = -60
local spawn_Y_2 = -60
local spawn_X_3 = 60
local spawn_Y_RANGE_3 = 500
local spawn_Y_RANGE_1 = 500
local speed_X_RANGE_FROM_1 = 40
local speed_X_RANGE_TO_1 = 120
local speed_Y_RANGE_FROM_1 = 20
local speed_Y_RANGE_TO_1 = 60
local speed_X_RANGE_FROM_2 = -40
local speed_X_RANGE_TO_2 = 40
local speed_Y_RANGE_FROM_2 = 40
local speed_Y_RANGE_TO_2 = 120
local speed_X_RANGE_FROM_3 = -120
local speed_X_RANGE_TO_3 = -40
local speed_Y_RANGE_FROM_3 = 20
local speed_Y_RANGE_TO_3 = 60
local torque_x = -6
local torque_y = 6
local bg1
local bg2
local scroll
local scrool_speedUp
local doUpdatePlayTime = true
local doUpdatePlayerMovements = true
local doUpdateFire = true
local BOSS_FIRE_OPTIONS =
{
BOSS_BULLETS_WAVE = 10,
BOSS_BULLETS_LINE = 5,
BOSS_BULLETS_SPHERE = 10,
BOSS_FIRE_LINE = 1,
BOSS_FIRE_WAVE = 2,
BOSS_FIRE_SPHERE = 3
}
local BOSS_FIRE_MODE = BOSS_FIRE_OPTIONS.BOSS_FIRE_SPHERE
local fireDirection = 1
local power = 400
local SPAWN_RANDOM = 4
local missileRadius = 300
local MidX
local MidY
local function setDebugOptions()
shieldVisible = true
gameplayTime = thresholdSpawning
SCALE_TIME = 0.2
ship.isBodyActive = ship_body_active
end
local function pausePlayerMovements()
doUpdatePlayerMovements = false
end
local function playPlayerMovements()
doUpdatePlayerMovements = true
end
local function pauseTime()
doUpdatePlayTime = false
end
local function playTime()
doUpdatePlayTime = true
end
local function pauseFire()
doUpdateFire = false
end
local function playFire()
doUpdateFire = true
end
local function addScore(iScore)
GAME_VARS.score = GAME_VARS.score + iScore
scoreText.text = string.format("%07d",GAME_VARS.score)
end
local function bgScroll (event)
bg1.y = bg1.y + scroll + scrool_speedUp
bg2.y = bg2.y + scroll + scrool_speedUp
if bg1.y >= display.contentHeight * 1.5 then
bg1.y = display.contentHeight * -.5
end
if bg2.y >= display.contentHeight * 1.5 then
bg2.y = display.contentHeight * -.5
end
end
-- place ship
local function placeShip()
ship.x = display.contentCenterX
ship.y = display.contentHeight - 100
end
local function placeShield()
if(shieldVisible == true)
then
shield.x = ship.x
shield.y = ship.y
end
end
local function loadAudio()
GAME_SOUNDS.enemyDeadSound = audio.loadSound(PATHS.path_explosion)
GAME_SOUNDS.playerDeadSound = audio.loadSound(PATHS.path_explosion)
GAME_SOUNDS.fireLaserSound = audio.loadSound(PATHS.path_fire)
GAME_SOUNDS.musicTrack = audio.loadStream(PATHS.path_track)
GAME_SOUNDS.keyStrokeSound = audio.loadSound(PATHS.path_keystroke)
end
-- place missiles
local function placeMissiles()
leftMissileFlying.x = ship.x - 38
rigthMissileFlying.x = ship.x + 38
leftMissileFlying.y = ship.y + 12
rigthMissileFlying.y = ship.y + 12
leftMissileShip.x = ship.x - 38
rigthMissileShip.x = ship.x + 38
leftMissileShip.y = ship.y + 12
rigthMissileShip.y = ship.y + 12
end
local function createLittleVirus()
--get an image from the sheet and resize it
newVirus = display.newImageRect(mainGroup,objectSheet,1,51,51)
--add to the array
table.insert(virusesTable,newVirus)
physics.addBody(newVirus,"dynamic",{radius=25.5,bounce=0.8})
newVirus.myName = enemy_name_1
newVirus.x = spawn_X_1
newVirus.y = math.random(spawn_Y_RANGE_1)
newVirus:setLinearVelocity(math.random(speed_X_RANGE_FROM_1,speed_X_RANGE_TO_1),math.random(speed_Y_RANGE_FROM_1,speed_Y_RANGE_TO_1))
--apply a rotation
newVirus:applyTorque(math.random(torque_x,torque_y))
end
local function createMediumVirus()
--get an image from the sheet and resize it
newVirus = display.newImageRect(mainGroup,objectSheet,1,60,60)
--add to the array
table.insert(virusesTable,newVirus)
physics.addBody(newVirus,"dynamic",{radius=30,bounce=0.8})
newVirus.myName = enemy_name_1
--spawn from top side all width
newVirus.x = math.random(display.contentWidth)
newVirus.y = spawn_Y_2
newVirus:setLinearVelocity(math.random(speed_X_RANGE_FROM_2,speed_X_RANGE_TO_2),math.random(speed_Y_RANGE_FROM_2,speed_Y_RANGE_TO_2))
--apply a rotation
newVirus:applyTorque(math.random(torque_x,torque_y))
end
local function createBigVirus()
--get an image from the sheet and resize it
newVirus = display.newImageRect(mainGroup,objectSheet,1,70,70)
--add to the array
table.insert(virusesTable,newVirus)
physics.addBody(newVirus,"dynamic",{radius=35,bounce=0.8})
newVirus.myName = enemy_name_1
--spawn from right side
newVirus.x = display.contentWidth + 60
newVirus.y = math.random(spawn_Y_RANGE_3)
newVirus:setLinearVelocity(math.random(speed_X_RANGE_FROM_3,speed_X_RANGE_TO_3),math.random(speed_Y_RANGE_FROM_3,speed_Y_RANGE_TO_3))
newVirus:applyTorque(math.random(torque_x,torque_y))
end
local function createBossWithTransition(where,seconds,actionBefore,actionAfter)
boss = display.newImageRect(mainGroup,PATHS.path_boss,bossInfo.BOSS_WIDTH,bossInfo.BOSS_HEIGTH)
boss:rotate( -60 )
physics.addBody(boss,"static",{radius=bossInfo.BOSS_WIDTH/2,bounce = 0.0})
boss.myName = enemy_boss_name_1
boss.x = display.contentCenterX
boss.y = -bossInfo.BOSS_HEIGTH
actionBefore()
boss.alpha = 0
transition.to( boss, {y=where,alpha = 1, time = seconds,onComplete=actionAfter})
end
local function hideViruses(seconds)
for i = #virusesTable,1,-1 do
local thisVirus = virusesTable[i]
transition.to(thisVirus,{alpha=0,time=seconds})
end
end
local function moveVirusesCenterTop(seconds)
for i = #virusesTable,1,-1 do
local thisVirus = virusesTable[i]
thisVirus.isBodyActive = false
transition.to(thisVirus,{x=display.contentCenterX,y=-40,time=seconds})
end
end
local function movePlayerCenterBottom(seconds)
transition.to(ship,{x=display.contentCenterX,y=display.contentHeight - ship.height/2 - 20,time=seconds})
transition.to(leftMissileShip,{x=display.contentCenterX - 38,y=display.contentHeight - ship.height/2 - 20 + 12,time=seconds})
transition.to(leftMissileFlying,{x=display.contentCenterX - 38,y=display.contentHeight - ship.height/2 - 20 + 12,time=seconds})
transition.to(rigthMissileShip,{x=display.contentCenterX + 38,y=display.contentHeight - ship.height/2 - 20 + 12,time=seconds})
transition.to(rigthMissileFlying,{x=display.contentCenterX + 38,y=display.contentHeight - ship.height/2 - 20 + 12,time=seconds})
end
--spawn asteroids
local function createVirus(randomize)
local newVirus
local whereFrom = randomize
--spawn positions
if(randomize == SPAWN_RANDOM)
then
whereFrom = math.random(3)
else
whereFrom = randomize
end
if(whereFrom == 1) then
createLittleVirus()
elseif (whereFrom == 2) then
createMediumVirus()
elseif(whereFrom == 3) then
createBigVirus()
end
end
local function createViruses()
createVirus(SPAWN_RANDOM)
for i = 0, math.floor(gameplayTime / 3), 1 do
createVirus(SPAWN_RANDOM)
end
end
--remove viruses offscreen
local function removeViruses()
for i = #virusesTable, 1, -1 do
local thisVirus = virusesTable[i]
if(
thisVirus.x < -100 or thisVirus.x > display.contentWidth + 100
or thisVirus.y < -100 or thisVirus.y > display.contentHeight + 100
)
then
--remove the object from the display
display.remove(thisVirus)
--remove from array
table.remove(virusesTable,i)
end
end
end
local removed = 0;
local function removeLasers()
for i = #lasersTable, 1, -1 do
local thisLaser = lasersTable[i]
if(
thisLaser.x < -100 or thisLaser.x > display.contentWidth + 100
or thisLaser.y < -100 or thisLaser.y > display.contentHeight + 100
)
then
display.remove(thisLaser)
table.remove(lasersTable,i)
removed = removed + 1;
if(removed > 1000)
then
removed = 0
end
--scoreText.text = "removed " .. removed
end
end
end
--spawn laser
local function fireLaser()
if(doUpdateFire == false)
then
return
end
--load the laser
-- Play fire sound!
audio.play( GAME_SOUNDS.fireLaserSound )
local newLaser = display.newImageRect(mainGroup,objectSheet,2,14,40)
table.insert(lasersTable,newLaser)
--add the laser as sensor, it can read events
physics.addBody(newLaser,"dynamic",{isSensor=true})
newLaser.isBullet = true
newLaser.myName = laser_name_1
local newlaser2
if(g_fireMode == FIRE_OPTIONS.FIRE_ALTERNATE)
then
if(lastFireSide == FIRE_OPTIONS.FIRE_LEFT)
then
lastFireSide = FIRE_OPTIONS.FIRE_RIGTH
else
lastFireSide = FIRE_OPTIONS.FIRE_LEFT
end
newLaser.x = ship.x + FIRE_OPTIONS.OPT_FIRE_BOTH_OFFSET * lastFireSide
newLaser.y = ship.y
elseif (g_fireMode == FIRE_OPTIONS.FIRE_CENTER)
then
newLaser.x = ship.x
newLaser.y = ship.y
elseif (g_fireMode == FIRE_OPTIONS.FIRE_BOTH)
then
newLaser2 = display.newImageRect(mainGroup,objectSheet,2,14,40)
table.insert(lasersTable,newLaser2)
--add the laser as sensor, it can read events
physics.addBody(newLaser2,"dynamic",{isSensor=true})
newLaser2.isBullet = true
newLaser2.myName = laser_name_2
newLaser.x = ship.x - FIRE_OPTIONS.OPT_FIRE_BOTH_OFFSET
newLaser.y = ship.y
newLaser2.x = ship.x + FIRE_OPTIONS.OPT_FIRE_BOTH_OFFSET
newLaser2.y = ship.y
else
newLaser.x = ship.x
newLaser.y = ship.y
end
--put the laser backside
newLaser:toBack()
--move the laser forward to the end position
--transition.to(newLaser, {y=-40,time=500,
-- onComplete = function() display.remove(newLaser) end
--})
newLaser:setLinearVelocity(FIRE_OPTIONS.LASER_X_SPEED,FIRE_OPTIONS.LASER_Y_SPEED);
if(g_fireMode == FIRE_OPTIONS.FIRE_BOTH)
then
--put the laser backside
newLaser2:toBack()
--move the laser forward to the end position
-- transition.to(newLaser2, {y=-40,time=500,
-- onComplete = function() display.remove(newLaser2) end
--})
newLaser2:setLinearVelocity(FIRE_OPTIONS.LASER_X_SPEED,FIRE_OPTIONS.LASER_Y_SPEED);
end
end
local function fireLeftMissile()
if(doUpdateFire == false)
then
return
end
if(firingLeftMissile == true) then
return
end
firingLeftMissile = true
leftMissileShip.alpha = 0
leftMissileFlying.alpha = 1
transition.to(leftMissileFlying, {y=-40,time=500,
onComplete = function()
display.remove(leftMissileFlying)
leftMissileDestroyed = true
firingLeftMissile = false
end
})
leftMissileFlying.isBodyActive = true
end
local function fireRigthMissile()
if(doUpdateFire == false)
then
return
end
if(firingRigthMissile == true) then
return
end
firingRigthMissile = true
rigthMissileShip.alpha = 0
rigthMissileFlying.alpha = 1
transition.to(
rigthMissileFlying,
{y=-40,time=500,
onComplete = function()
display.remove(rigthMissileFlying)
rigthMissileDestroyed = true
firingRigthMissile = false
end
})
rigthMissileFlying.isBodyActive = true
end
local function createLeftMissile()
if(leftMissileShip == nil)
then
leftMissileShip = display.newImageRect( mainGroup,objectSheet,3,131 * SCALE_MISSILE,300 * SCALE_MISSILE)
end
leftMissileFlying = display.newImageRect( mainGroup,objectSheet,4,138 * SCALE_MISSILE,517 * SCALE_MISSILE)
leftMissileShip.alpha = 1
leftMissileFlying.alpha = 0
leftMissileDestroyed = false
firingLeftMissile = false
physics.addBody(leftMissileFlying,"dynamic",{isSensor=true})
leftMissileFlying.myName = missile_name_left_1
leftMissileFlying.isBullet = true
leftMissileFlying.isBodyActive = false
leftMissileShip:addEventListener(missile_event,fireLeftMissile)
leftMissileShip:toBack( )
leftMissileFlying:toBack( )
end
local function createRigthMissile()
if(rigthMissileShip == nil)
then
rigthMissileShip = display.newImageRect( mainGroup,objectSheet,3,131 * SCALE_MISSILE,300 * SCALE_MISSILE)
end
rigthMissileFlying = display.newImageRect( mainGroup,objectSheet,4,138 * SCALE_MISSILE,517 * SCALE_MISSILE)
rigthMissileShip.alpha = 1
rigthMissileFlying.alpha = 0
rigthMissileDestroyed = false
firingRigthMissile = false
physics.addBody(rigthMissileFlying,"dynamic",{isSensor=true})
rigthMissileFlying.myName = missile_name_rigth_1
rigthMissileFlying.isBullet = true
rigthMissileFlying.isBodyActive = false
rigthMissileShip:addEventListener(missile_event,fireRigthMissile)
rigthMissileShip:toBack( )
rigthMissileFlying:toBack()
end
--ship movement
local function dragShip(event)
if(doUpdatePlayerMovements == false)
then
return
end
local ship = event.target
local phase = event.phase
if("began" == phase) then
display.currentStage:setFocus(ship)
ship.touchOffsetX = event.x - ship.x
ship.touchOffsetY = event.y - ship.y
elseif ("moved" == phase) then
local shipx = event.x - ship.touchOffsetX
local shipy = event.y - ship.touchOffsetY
if(
shipx < display.contentWidth - halfShipBoundsX*2 and shipx > halfShipBoundsX*2 and
shipy < display.contentHeight - halfShipBoundsY and shipy > halfShipBoundsY
)
then
ship.x = shipx
ship.y = shipy
placeMissiles()
placeShield()
end
elseif ("ended" == phase) then
display.currentStage:setFocus(nil)
end
return true
end
--recreate a new ship and weapons
local function effectRestoreShipAndWeapons()
ship.isBodyActive = false
placeShip()
if(leftMissileDestroyed==true) then
createLeftMissile()
end
if(rigthMissileDestroyed==true) then
createRigthMissile()
end
placeMissiles()
transition.to(ship, {alpha=1,time=3000,onComplete = function()
leftMissileShip.alpha = 1
rigthMissileShip.alpha = 1
ship.isBodyActive = true
GAME_VARS.died = false
end
})
end
local function effectDeadShipAndWeapons()
ship.alpha = 0
if(leftMissileDestroyed == false and firingLeftMissile == false) then
leftMissileShip.alpha = 0
end
if(rigthMissileDestroyed == false and firingRigthMissile == false) then
rigthMissileShip.alpha = 0
end
local shipExplosion = display.newSprite( mainGroup,sheet_shipExplosion, sequences_shipExplosion)
shipExplosion:setSequence( "normalExplosion")
shipExplosion.x = ship.x
shipExplosion.y = ship.y
shipExplosion:play()
local function mySpriteListener( event )
if ( event.phase == "ended" ) then
shipExplosion:removeSelf()
shipExplosion = nil
end
end
shipExplosion:addEventListener( "sprite", mySpriteListener )
-- Play explosion sound!
audio.play( GAME_SOUNDS.enemyDeadSound )
end
local function explodeLaserAndRemove(obj1,obj2)
local midX = (obj1.x + obj2.x)/2
local midY = (obj2.y + obj2.y)/2
if obj1.myName == enemy_name_1 or obj1.myName == laser_name_1 or obj1.myName == enemy_laser_name_1
then
display.remove(obj1)
end
if obj2.myName == enemy_name_1 or obj2.myName == laser_name_1 or obj2.myName == enemy_laser_name_1
then
display.remove(obj2)
end
local laserExplosion = display.newSprite( mainGroup,sheet_laserExplosion, sequences_laserExplosion)
laserExplosion:setSequence( "normalExplosion")
laserExplosion.x = midX
laserExplosion.y = midY
laserExplosion:play()
local function mySpriteListener( event )
if ( event.phase == "ended" ) then
laserExplosion:removeSelf()
laserExplosion = nil
end
end
laserExplosion:addEventListener( "sprite", mySpriteListener )
-- Play explosion sound!
audio.play( GAME_SOUNDS.enemyDeadSound )
end
local shieldPower = 4
local function explodeShieldAndRemove(obj1,obj2)
local midX
local midY
if(obj1.myName == enemy_name_1)
then
midX = obj1.x
midY = obj1.y
display.remove(obj1)
end
if(obj2.myName == enemy_name_1)
then
midX = obj2.x
midY = obj2.y
display.remove(obj2)
end
shieldPower = shieldPower - 1
if(shieldPower <= 0)
then
if(obj1.myName == "shield")
then
display.remove(obj1)
end
if(obj2.myName == "shield")
then
display.remove(obj2)
end
end
local shieldExplosion = display.newSprite( mainGroup,sheet_laserExplosion, sequences_laserExplosion)
shieldExplosion:setSequence( "normalExplosion")
shieldExplosion.x = midX
shieldExplosion.y = midY
shieldExplosion:play()
local function mySpriteListener( event )
if ( event.phase == "ended" ) then
shieldExplosion:removeSelf()
shieldExplosion = nil
end
end
shieldExplosion:addEventListener( "sprite", mySpriteListener )
-- Play explosion sound!
audio.play( GAME_SOUNDS.enemyDeadSound )
end
local function explodeAndRemove(obj)
display.remove(obj)
local laserExplosion = display.newSprite( mainGroup,sheet_laserExplosion, sequences_laserExplosion)
laserExplosion:setSequence( "normalExplosion")
laserExplosion.x = obj.x
laserExplosion.y = obj.y
laserExplosion:play()
local function mySpriteListener( event )
if ( event.phase == "ended" ) then
laserExplosion:removeSelf()
laserExplosion = nil
end
end
laserExplosion:addEventListener( "sprite", mySpriteListener )
-- Play explosion sound!
audio.play( GAME_SOUNDS.enemyDeadSound )
end
local function explodeBlastAndRemoveEnemies(obj1,obj2)
if(obj1.myName == enemy_name_1)