-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.lua
722 lines (621 loc) · 19 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
require ('slam')
-- local flux = require ("flux/flux")
-- TODO
-- Network play
-- Sword and boom power-ups
-- Certain enemies have certain drops
-- Enemies fight back
-- Save progress (high score, killed rare enemy etc.)
-- Weapon specific sprites
local game = {}
local cron = require "cron"
local utilities = require("utilities")
local resource_manager = require("resource_manager")
local Hero = require("hero")
local EnemyBlue= require("enemy_blue")
local EnemyRed = require("enemy_red")
local EnemySubBoss = require("enemy_sub_boss")
local EnemyCaterpillarBoss = require("enemy_caterpillar_boss")
local EnemyBoss = require("enemy_boss")
local EnemyBlack = require("enemy_black")
local EnemyPurple = require("enemy_purple")
local EnemyUrn = require("enemy_urn")
local EnemyRedUrn = require("enemy_redurn")
local ShotNormal = require("shot_normal")
local ShotHoming = require("shot_homing")
local ShotShuriken = require("shot_shuriken")
local Powerup = require("powerup")
local Effect = require("effect")
-- game objects
local shotStrings = { "Normal", "Triple", "Fast", "Homing", "Drone", "Boom", "Disable", "Shuriken", "Elite" }
local hero
local drone
local shotObjects
local enemies
local enemiesNextWave
local powerups
local effects
local maxShotNumber
local curShotSpeed
local weaponType
-- game state
local flagStopped
local flagGameover
local flagWin
local flagPaused
local groundHeight = 540
local winTime
local gameTime
local lastPowerupTime
local score
local level
local music
local totalShotCount
local totalEnemiesKilledThisLevel
local enemyKillTrigger
-- config
local joystickDeadzone = 0.20
local startLevel = 1
local powerupChance = 0.15
local droneShootPeriod = 0.6 -- seconds
-- local powerupDuration = 10
function game.toggleHitbox()
utilities.toggleHitbox()
end
function game.droneShoot()
if #shotObjects >= maxShotNumber then
return
end
if weaponType == 5 then
local dx = drone:getX()+drone:getWidth()/2
local dy = drone:getY()
table.insert(shotObjects, ShotNormal(dx, dy, curShotSpeed))
end
end
local timer = cron.every(droneShootPeriod, game.droneShoot)
function game.shoot()
if #shotObjects >= maxShotNumber then
return
end
totalShotCount = totalShotCount + 1
local hx = hero:getX()+hero:getWidth()/2
local hy = hero:getY()
-- shuriken, homing or normal
if(weaponType == 8) then
local dir = (((totalShotCount % 2) * 2) - 1) -- -1/1
table.insert(shotObjects, ShotShuriken(hx, hy, dir))
elseif(weaponType == 4 or weaponType == 9) then
table.insert(shotObjects, ShotHoming(hx, hy, curShotSpeed))
else
table.insert(shotObjects, ShotNormal(hx, hy, curShotSpeed, weaponType == 7))
end
-- extra for triple
if weaponType == 2 then
table.insert(shotObjects, ShotNormal(hx+10, hy+10, curShotSpeed))
table.insert(shotObjects, ShotNormal(hx-10, hy+10, curShotSpeed))
end
local instance = resource_manager.playSound("shot")
instance:setPitch(.5 + love.math.random() * .5)
end
function game.chooseWeaponType(mode)
if flagStopped then
return
end
lastPowerupTime = gameTime
mode = mode or love.math.random(1,#shotStrings)
weaponType = mode
local heroImage = 1
if weaponType == 1 then -- normal
curShotSpeed = 100
maxShotNumber = 5
elseif weaponType == 2 then -- triple shot
curShotSpeed = 130
maxShotNumber = 9
heroImage = 2
elseif weaponType == 3 then -- fast firing
curShotSpeed = 750
maxShotNumber = 3
heroImage = 4
elseif weaponType == 4 then -- homing bullets
curShotSpeed = 110
maxShotNumber = 5
heroImage = 3
elseif weaponType == 5 then -- drone
curShotSpeed = 100
maxShotNumber = 16
elseif weaponType == 6 then -- boom
curShotSpeed = 100
maxShotNumber = 5
elseif weaponType == 7 then -- disable
curShotSpeed = 120
maxShotNumber = 5
heroImage = 5
elseif weaponType == 8 then -- glaive
curShotSpeed = 0
maxShotNumber = 7
heroImage = 6
elseif weaponType == 9 then -- elite
curShotSpeed = 800
maxShotNumber = 1
heroImage = 7
else
curShotSpeed = 0
maxShotNumber = 0
end
hero:setQuadName('hero' .. heroImage)
end
function game.shotString(localWeaponType)
if localWeaponType >= 1 and localWeaponType <= #shotStrings then
return shotStrings[localWeaponType]
end
return "XXX"
end
function game.load(gameX, gameY)
resource_manager.load()
love.graphics.setNewFont("fonts/Deadly Advance.ttf", 20)
game.reload(gameX, gameY)
end
function game.incLevel(gameX, gameY, incLevel)
local newLevel = level + incLevel
if newLevel <=1 then newLevel = 1 end
game.reload(gameX, gameY, newLevel)
end
function game.reload(gameX, gameY, newLevel)
level = newLevel or startLevel
flagStopped = false
flagGameover = false
flagWin = false
flagPaused = false
score = 0
winTime = -1
gameTime = 0
lastPowerupTime = 0
totalShotCount = 0
hero = Hero(400, groundHeight-15, 150, "hero3")
game.chooseWeaponType(1)
drone = Hero(400, groundHeight-15, 450, "drone1")
enemies = {}
enemiesNextWave = {}
powerups = {}
effects = {}
game.spawnEnemies(gameX, gameY)
shotObjects = {}
end
function game.spreadEnemy(i,border,numEnemies,gameX)
if numEnemies == 1 then
return gameX/2
end
return i*((gameX - border)/(numEnemies-1)) + border/2
end
function game.spawnEnemies(gameX, gameY)
--x, y, speed, health, score, image, quad, quad2
totalEnemiesKilledThisLevel = 0
enemyKillTrigger = 0
if level == 1 then
music = "dramatic"
for i=0,6 do
table.insert(enemies, EnemyBlue(i*90 + 100, 180))
end
elseif level == 2 then
music = "dramatic"
for i=0,10 do
table.insert(enemies, EnemyRed(i*70 + 30, 120))
end
for i=0,12 do
table.insert(enemies, EnemyBlue(i*45 + 100, 180))
end
elseif level == 3 then
music = "dramatic"
for i=0,1 do
table.insert(enemies, EnemyBlack(i*250 + 250, 25))
end
elseif level == 4 then
music = "dramatic"
enemyKillTrigger = 15
for i=0,11 do
table.insert(enemies, EnemyBlue(i*((gameX - 100.0)/12) + 50, 25))
end
for i=0,9 do
table.insert(enemies, EnemyBlue(i*((gameX - 100.0)/10) + 50, 50))
end
for i=0,2 do
table.insert(enemiesNextWave, EnemyBlack(gameX - (i*110 + 100), 40))
end
elseif level == 5 then
music = "bossfight"
table.insert(enemies, EnemyBoss(gameX/2 - 32/2, 20) )
for i=0,6 do
table.insert(enemies, EnemyBlue(i*90 + 100, 100))
end
elseif level == 6 then
music = "dramatic"
for i=0,2 do
table.insert(enemies, EnemyUrn(game.spreadEnemy(i,400,3,gameX), 100))
end
elseif level == 7 then
music = "dramatic"
for i=0,4 do
table.insert(enemies, EnemyUrn(game.spreadEnemy(i,700,5,gameX), 25))
end
elseif level == 8 then
music = "dramatic"
enemyKillTrigger = 3
for i=0,2 do
table.insert(enemies, EnemyPurple(game.spreadEnemy(i,400,3,gameX), 100))
end
for i=0,4 do
table.insert(enemies, EnemyRed(game.spreadEnemy(i,150,5,gameX), 25))
end
for i=0,0 do
table.insert(enemiesNextWave, EnemyBlack(game.spreadEnemy(i,200,1,gameX), 100))
end
elseif level == 9 then
music = "dramatic"
enemyKillTrigger = 2
for i=0,2 do
table.insert(enemies, EnemyPurple(game.spreadEnemy(i,400,3,gameX), 100))
end
for i=0,2 do
table.insert(enemiesNextWave, EnemyUrn(game.spreadEnemy(i,400,3,gameX), 0))
end
for i=0,7 do
table.insert(enemiesNextWave, EnemyRed(game.spreadEnemy(i,200,8,gameX), 0))
end
elseif level == 10 then
music = "bossfight"
enemyKillTrigger = 3
table.insert(enemies, EnemyBoss(gameX/2 - 32/2, 20) )
for i=0,1 do
table.insert(enemies, EnemyUrn(game.spreadEnemy(i,500,2,gameX), -25))
table.insert(enemies, EnemyUrn(game.spreadEnemy(i,450,2,gameX), -50))
table.insert(enemies, EnemyUrn(game.spreadEnemy(i,400,2,gameX), -75))
table.insert(enemies, EnemyUrn(game.spreadEnemy(i,350,2,gameX), -100))
end
elseif level == 11 then
music = "dramatic"
for i=0,2 do
table.insert(enemies, EnemyRedUrn(game.spreadEnemy(i,500,2,gameX), 125))
end
elseif level == 12 then
music = "dramatic"
for i=0,11 do
table.insert(enemiesNextWave, EnemyBlue(game.spreadEnemy(i,300,12,gameX), 50))
end
for i=0,9 do
table.insert(enemiesNextWave, EnemyRed(game.spreadEnemy(i,300,10,gameX), 65))
end
for i=0,4 do
table.insert(enemiesNextWave, EnemyUrn(game.spreadEnemy(i,500,5,gameX), 80))
end
elseif level == 13 then
music = "bossfight"
enemyKillTrigger = 3
table.insert(enemies, EnemyBoss(gameX/2 - 32/2, 20) )
for i=0,2 do
table.insert(enemies, EnemyBlack(i*110 + 100, 40))
end
for i=0,2 do
table.insert(enemiesNextWave, EnemyBlack(gameX - (i*110 + 100), 40))
end
elseif level == 14 then
music = "dramatic"
for i=0,9 do
table.insert(enemies, EnemyUrn(game.spreadEnemy(i,500,10,gameX), 10))
end
for i=0,2 do
table.insert(enemies, EnemyRedUrn(game.spreadEnemy(i,500,3,gameX), 10))
end
elseif level == 15 then
music = "bossfight"
--table.insert(enemies, EnemyUrn(90 + 100, 180))
table.insert(enemies, EnemySubBoss(gameX/2 - 40, 50))
elseif level == 20 then
music = "bossfight"
--table.insert(enemies, EnemyUrn(90 + 100, 180))
table.insert(enemies, EnemyCaterpillarBoss(gameX/2 - 40, 50))
else
-- music = "win"
if(winTime < 0) then
winTime = gameTime
end
flagStopped = true
flagWin = true
end
resource_manager.playMusic(music)
end
-- ????
function utilities.findDroneTarget(objectX, enemiesLocal)
local enemyDist = nil
local enemyDir = 0
local enemyX = objectX
local foundBlack = false
-- find closest
-- find closest black
for ii,enemy in ipairs(enemiesLocal) do
ii = ii
if enemy:is(EnemyBlack) then
local thisEnemyX = enemy:getX() + enemy:getWidth()/2
if (enemyDist == nil or (math.abs(objectX - thisEnemyX) < enemyDist)) then
enemyDist = math.abs(objectX - thisEnemyX)
enemyX = thisEnemyX
foundBlack = true
end
end
end
if not foundBlack then
for ii,enemy in ipairs(enemiesLocal) do
ii = ii
local thisEnemyX = enemy:getX() + enemy:getWidth()/2
if (enemyDist == nil or (math.abs(objectX - thisEnemyX) < enemyDist)) then
enemyDist = math.abs(objectX - thisEnemyX)
enemyX = thisEnemyX
end
end
end
if(enemyDist < 3) then
-- stop oscillation
enemyDir = 0
elseif(objectX > enemyX) then
enemyDir = -1
elseif (objectX < enemyX) then
enemyDir = 1
end
return enemyDir
end
function game.update(dt, gameX, gameY)
-- if paused, then gametime should stop
if flagPaused then
return
end
gameTime = gameTime + dt
timer:update(dt)
-- if stopped (other than pause) then allow gameitme to continue
if flagStopped then
return
end
hero:update(dt, game.getHeroDirection(), gameX, gameY)
if weaponType == 5 then
local dir = utilities.findDroneTarget(drone:getX() + drone:getWidth()/2, enemies)
drone:update(dt, dir, gameX, gameY)
end
local remPowerup = {}
local remEnemy = {}
local remShotObject = {}
-- powerups
for ii,powerup in ipairs(powerups) do
if powerup:update(dt, groundHeight) then
table.insert(remPowerup, ii)
end
-- hero and powerup
if utilities.checkBoxCollision(hero, powerup) then
table.insert(remPowerup, ii)
game.chooseWeaponType(powerup:getType())
end
end
-- update the shots
for i,shot in ipairs(shotObjects) do
if shot:update(dt, gameX, gameY, enemies) then
table.insert(remShotObject, i)
end
end
-- check for collision with enemies
for ii,enemy in ipairs(enemies) do
for jj,shot in ipairs(shotObjects) do
local hit,kill = enemy:checkCollision(shot, enemies)
if hit then
shot:hit() -- ensure that it won't do damage for another short period
-- mark the shot to be removed
if shot:getRemoveOnImpact() then
table.insert(remShotObject, jj)
end
end
if kill then
-- mark that enemy for removal
table.insert(remEnemy, ii)
if enemy:is(EnemySubBoss) or enemy:is(EnemyCaterpillarBoss) then
for kk=0,10,1
do
local rotation = love.math.random()*360
local scale = love.math.random() + 2.5
local randomX = (love.math.random() * 40) - 20
local randomy = (love.math.random() * 40) - 20
local randomTTL = (love.math.random() * 3) + 0.3
table.insert(effects, Effect(shot:getX() - randomX, shot:getY() - randomy, rotation, "effect_explosion", scale, randomTTL, randomTTL))
end
else
local rotation = love.math.random()*360
local scale = love.math.random() + 2.5
table.insert(effects, Effect(shot:getX(), shot:getY(), rotation, "effect_explosion", scale))
end
score = score + enemy:getScore()
game.spawnPowerup(enemy)
end
end
end
-- remove the marked enemies and shots. work backwards to avoid removing the wrong ones on multiple removal
-- there are more efficient algos for this that aren't O(n^2) from repeated calls to table.remove()
for i,enemy in utilities.ripairs(remEnemy) do
i = i
table.remove(enemies, enemy)
totalEnemiesKilledThisLevel = totalEnemiesKilledThisLevel + 1
end
for i,shot in utilities.ripairs(remShotObject) do
i = i
table.remove(shotObjects, shot)
end
for i,powerup in utilities.ripairs(remPowerup) do
i = i
table.remove(powerups, powerup)
end
-- update the enemies' positions
for i,enemy in ipairs(enemies) do
i = i
enemy:update(dt)
-- check for collision between enemy and hero
if utilities.checkBoxCollision(hero,enemy) then
flagStopped = true
flagGameover = true
end
-- check for collision with ground
if enemy:getY() > groundHeight then
flagStopped = true
flagGameover = true
end
end
-- spawn more enemies
if (totalEnemiesKilledThisLevel >= enemyKillTrigger) then
for i,enemy in ipairs(enemiesNextWave) do
i = i
table.insert(enemies, enemy)
end
enemiesNextWave = {}
end
-- secret spawn
local rare = love.math.random(1,500/dt)
if rare == 1 then
for i=0,6 do
local enemy = EnemyBlue(i*90 + 100, 180)
table.insert(enemies, enemy)
end
end
for ii,effect in utilities.ripairs(effects) do
if effect:update(dt) then
table.remove(effects, ii)
end
end
-- check for win condition
if #enemies == 0 then
level = level + 1
game.spawnEnemies(gameX, gameY)
end
end
function game.getHeroDirection()
local dir = 0
-- sticks
local joysticks = love.joystick.getJoysticks()
for i,joystick in ipairs(joysticks) do
i = i
if joystick:isGamepad() then
local value = joystick:getGamepadAxis('leftx')
if math.abs(value) > joystickDeadzone then
--dir = math.ceil(value)
dir = value
end
end
end
-- keyboard actions for our hero
if love.keyboard.isDown("left") then
dir = -1
elseif love.keyboard.isDown("right") then
dir = 1
end
return dir
end
function game.spawnPowerup(enemy)
local max = #shotStrings * math.ceil(1.0/powerupChance)
local powerupType = love.math.random(1,max)
if powerupType <= #shotStrings then
local powerup = Powerup(enemy:getX() + enemy:getWidth()/2, enemy:getY(), 150, powerupType)
table.insert(powerups, powerup)
end
end
function game.draw(gameX, gameY) -- let's draw a background
local hour = tonumber(os.date("%H"))
if hour <=10 and hour >= 7 then
love.graphics.setColor(0.45,0.2,0.07,1)
elseif hour <=6 or hour >= 21 then
love.graphics.setColor(0,0,0,1)
elseif hour <=20 and hour >= 18 then
love.graphics.setColor(0.1,0,0.1,1)
elseif hour <=17 and hour >= 11 then
love.graphics.setColor(0.1,0.05,0.05,1)
end
--local hour = tonumber(os.date("%H"))
love.graphics.setColor(0,0.02,0.1,1)
love.graphics.rectangle("fill", 0, 0, gameX, gameY)
if(flagWin) then
local alpha = (gameTime-winTime)/5
love.graphics.setColor(1,1,1,alpha)
love.graphics.draw(resource_manager.getGradient(), 0, 0, 0, gameX, gameY)
end
-- draw enemies
for i,enemy in ipairs(enemies) do
i = i
enemy:draw()
end
-- draw some ground _over_ the enemies
love.graphics.setColor(0,0.6,0,1.0)
love.graphics.rectangle("fill", 0, groundHeight, gameX, gameY-groundHeight)
-- draw hero
if weaponType == 5 then
drone:draw()
end
hero:draw()
-- draw powerups
for i,powerup in ipairs(powerups) do
i = i
powerup:draw()
end
-- draw effects
for i,effect in ipairs(effects) do
i = i
effect:draw()
end
-- draw shots
for i,shot in ipairs(shotObjects) do
i = i
shot:draw()
end
-- draw overlay
if(not flagStopped) then
love.graphics.setColor(1,1,1,1)
local border = 10
love.graphics.printf( "Level: " .. level, gameX-250-border, 10, 400/1.8, "right")
love.graphics.printf( "Shot: " .. game.shotString(weaponType), border, 50, 400/1.8, "left", -0.1, 1.8, 1.6)
love.graphics.printf( "Score: " .. score, gameX-400-border, 10, 400/1.8, "right", 0.1, 1.8, 1.6)
love.graphics.print("FPS: " .. tostring(love.timer.getFPS( )), 10, 10)
game.drawGauge(gameX, gameY)
end
if flagPaused then
love.graphics.setColor(1,1,1,1)
love.graphics.printf( 'Paused!', (gameX - 3*200)/2, gameY/3, 200, "center", 0, 3, 3)
love.graphics.printf( 'Score: '.. score .. '\n\nPress \'P\' to Resume', (gameX - 2*250)/2, gameY/3 + 90, 250, "center", 0, 2, 2)
end
if flagGameover then
love.graphics.setColor(1,1,1,1)
love.graphics.printf( 'Game Over!', (gameX - 3*200)/2, gameY/3, 200, "center", 0, 3, 3)
love.graphics.printf( 'Score: '.. score .. '\n\nPress \'R\' to Try Again', (gameX - 2*250)/2, gameY/3 + 90, 250, "center", 0, 2, 2)
end
if flagWin then
love.graphics.setColor(1,1,1,1)
love.graphics.printf( 'You Win!', (gameX - 3*200)/2, gameY/3, 200, "center", 0, 3, 3)
love.graphics.printf( 'Score: '.. score .. '\n\nPress \'R\' to Try Again', (gameX - 2*250)/2, gameY/3 + 90, 250, "center", 0, 2, 2)
end
end
function game.drawGauge(gameX, gameY)
local fillLevel = (#shotObjects / maxShotNumber)
if(fillLevel > 1.0) then
fillLevel = 1.0
end
if(fillLevel > 0.7) then
love.graphics.setColor(0.6,0,0,1)
else
love.graphics.setColor(0,1,0,1)
end
love.graphics.rectangle("fill", 10, (gameY-70) - (50 * fillLevel), 25, 50 * fillLevel)
if(fillLevel > 0.95) then
love.graphics.setColor(1.0,0,0,0.15)
love.graphics.printf( 'Overheat!', (gameX - 3*200)/2, gameY/3, 200, "center", 0, 3, 3)
love.graphics.setColor(1.0,0,0,1)
else
love.graphics.setColor(0.7,0.7,0.7,1)
end
love.graphics.rectangle("line", 10, gameY-120, 25, 50)
end
function game.togglePause()
if not flagGameover and not flagWin then
flagStopped = not flagPaused
flagPaused = not flagPaused
end
end
return game