-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.lua
More file actions
493 lines (440 loc) · 15.5 KB
/
level.lua
File metadata and controls
493 lines (440 loc) · 15.5 KB
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
map = require("map")
enemies = require("enemies")
player = require("player")
projectiles = require("projectiles")
ui = require("ui")
items = require("items")
dialog = require("dialog")
snow = require("snow")
boss = require("boss")
local level = {}
level.currentLevel = 1
level.isPaused = false
level.wonTheGame = false
level.music = {}
level.music["main"] = love.audio.newSource("sounds/651871__davejf__synthwave.mp3", "stream")
level.music["main"]:setLooping(true)
--a temporary feature for changing levels one at a time
local canChangeLevel = true
local canChangeLevelTimer = 0
-- events manager
level.event = {}
---------------------------------------------------------------
function level.Load(index)
print("loading level...")
-- populate map
local nCol = map.tileSheet:getWidth()/ui.tileWidth
local nRow = map.tileSheet:getHeight()/ui.tileHeight
local row, col
local id = 1
for row = 1, nRow do
for col = 1, nCol do
map.tileTexture[id] = love.graphics.newQuad(
(col - 1) * ui.tileWidth,
(row - 1) * ui.tileHeight,
ui.tileWidth,
ui.tileHeight,
map.tileSheet:getWidth(),
map.tileSheet:getHeight()
)
id = id + 1
end
end
print("...textures loaded")
-- add enemies
if index == 2 then
enemies.SpawnTurret(17*32, 4*32, "idle", math.pi/2)
enemies.SpawnTurret(17*32, 16*32, "idle", - math.pi/2)
elseif index == 3 then
enemies.SpawnTurret(13*32, 12*32, "idle", math.pi)
enemies.SpawnTurret(13*32, 2*32, "idle", math.pi)
enemies.SpawnTurret(26*32, 3*32, "idle", math.pi)
elseif index == 4 then
enemies.SpawnTurret(3*32, 3*32, "idle", math.pi/2)
enemies.SpawnTurret(22*32, 2*32, "idle", math.pi)
enemies.SpawnTurret(22*32, 16*32, "idle", math.pi)
enemies.SpawnTurret(21*32, 10*32, "idle", math.pi)
elseif index == 5 then
enemies.SpawnTank(17*32, 5*32, "idle", math.pi)
enemies.SpawnTank(21*32, 11*32, "idle", math.pi)
enemies.SpawnTank(22*32, 15*32, "idle", math.pi)
elseif index == 6 then
enemies.SpawnTank(26*32, 1*32, "idle", math.pi)
enemies.SpawnTank(20*32, 14*32, "idle", math.pi)
enemies.SpawnTank(13*32, 17*32, "idle", math.pi)
elseif index == 7 then
enemies.SpawnTank(12*32, 15*32, "idle", math.pi)
enemies.SpawnTank(23*32, 12*32, "idle", math.pi)
enemies.SpawnTurret(12*32, 2*32, "idle", math.pi/2)
enemies.SpawnTurret(20*32, 2*32, "idle", math.pi)
elseif index == 8 then
enemies.SpawnTank(16*32, 4*32, "idle", math.pi)
enemies.SpawnTank(16*32, 16*32, "idle", math.pi)
enemies.SpawnTurret(24*32, 10*32, "idle", math.pi)
enemies.SpawnTurret(8*32, 1*32, "idle", math.pi/2)
elseif index == 9 then
level.event[1] = false
elseif index == 10 then
level.event[2] = false
enemies.SpawnTank(24*32, 6*32, "idle", math.pi)
enemies.SpawnTank(22*32, 14*32, "idle", math.pi)
enemies.SpawnTank(9*32, 16*32, "warp", -math.pi/2)
elseif index == 11 then
level.event[3] = false
level.event[4] = false
elseif index == 12 then
level.event[5] = false
enemies.SpawnTank(21*32, 2*32, "idle", math.pi/2)
enemies.SpawnTank(26*32, 14*32, "idle", math.pi)
elseif index == 13 then
level.event[6] = false
level.event[7] = false
enemies.SpawnTurret(15*32, 6*32, "idle", math.pi/2)
enemies.SpawnTurret(15*32, 19*32, "idle", - math.pi/2)
elseif index == 14 then
enemies.SpawnMammoth(22*32, 2*32, "idle")
enemies.SpawnMammoth(24*32, 3*32, "idle")
enemies.SpawnMammoth(26*32, 2*32, "idle")
enemies.SpawnMammoth(21*32, 6*32, "idle")
enemies.SpawnMammoth(24*32, 8*32, "idle")
enemies.SpawnMammoth(25*32, 6*32, "idle")
elseif index == 15 then
enemies.SpawnTurret(13*32, 8*32, "idle", math.pi)
--grp 1
enemies.SpawnMammoth(13*32, 14*32, "idle", math.pi/9)
enemies.SpawnMammoth(12*32, 15*32, "idle", math.pi/13*15)
enemies.SpawnMammoth(11*32, 16*32, "idle", math.pi/4*5)
--grp 2
enemies.SpawnMammoth(20*32, 3*32, "idle", -math.pi)
enemies.SpawnMammoth(20*32, 6*32, "idle", -math.pi/9)
enemies.SpawnMammoth(24*32, 8*32, "idle", -math.pi/13*15)
elseif index == 16 then
level.event[8] = false
enemies.SpawnTurret(23*32, 6*32, "idle", math.pi)
--grp 1
enemies.SpawnMammoth(12*32, 5*32, "idle", math.pi/9)
enemies.SpawnMammoth(12*32, 7*32, "idle", math.pi/9)
enemies.SpawnMammoth(13*32, 9*32, "idle", math.pi/13*15)
--grp 2
enemies.SpawnMammoth(12*32, 15*32, "idle", -math.pi)
enemies.SpawnMammoth(13*32, 14*32, "idle", -math.pi/9)
enemies.SpawnMammoth(12*32, 17*32, "idle", -math.pi/13*15)
elseif index == 17 then
level.event[9] = false
level.event[10] = false
enemies.SpawnTurret(13*32, 9*32, "idle", math.pi)
enemies.SpawnTurret(13*32, 11*32, "idle", math.pi)
--enemies.SpawnTank(4*32, 3*32, "warp", math.pi/2)
--enemies.SpawnTank(4*32, 15*32, "warp", -math.pi/2)
--grp 1
enemies.SpawnMammoth(22*32, 6*32, "idle", math.pi/9)
enemies.SpawnMammoth(24*32, 7*32, "idle", math.pi/9)
enemies.SpawnMammoth(22*32, 7*32, "idle", math.pi/13*15)
--grp 2
enemies.SpawnMammoth(23*32, 15*32, "idle", -math.pi)
enemies.SpawnMammoth(20*32, 17*32, "idle", -math.pi/9)
enemies.SpawnMammoth(21*32, 16*32, "idle", -math.pi/13*15)
elseif index == 18 then
boss.Load()
end
print("...the ennemies have spawned")
-- add player
if index == 1 then
player.Load(ui.width / 8, ui.height / 2)
elseif index == 4 then
player.Load(ui.width / 8 - 50, ui.height / 2 + 50)
else
player.Load(ui.width / 8 - 50, ui.height / 2)
end
print("...player 1 has spawned")
-- add medipacks
if index == 3 then
items.SpawnMedipack(22*32, 2*32)
end
if index == 6 then
items.SpawnMedipack(28*32, 5*32)
end
if index == 9 then
items.SpawnMedipack(15*32, 10*32)
end
if index == 12 then
items.SpawnMedipack(26*32, 2*32)
end
if index == 15 then
items.SpawnMedipack(13*32 + 16, 2*32)
end
if index == 18 then
items.SpawnMedipack(26*32, 5*32)
items.SpawnMedipack(3*32, 5*32)
items.SpawnMedipack(26*32, 16*32)
items.SpawnMedipack(3*32, 16*32)
end
print("...medipacks deployed")
-- add snow
if index == 1 then
snow.Add(10)
snow.wind:stop()
snow.wind:play()
snow.wind:setPitch(0.8)
elseif index == 6 then
snow.Remove(10)
snow.Add(20)
snow.wind:setPitch(1.2)
elseif index == 10 then
snow.Remove(20)
snow.Add(30)
snow.wind:setPitch(1.6)
elseif index == 14 then
snow.Remove(30)
snow.Add(50)
snow.wind:setPitch(2.0)
elseif index == 18 then
snow.Remove(25)
snow.wind:setPitch(0.8)
end
print("...a snowstorm has arrived")
-- launch dialog
dialog.index = 1
dialog.isDialog = true
-- add music
if index == 1 then
level.music["main"]:stop()
level.music["main"]:play()
level.music["main"]:setVolume(0.9)
elseif index == 18 then
level.music["main"]:stop()
end
end
---------------------------------------------------------------
function level.Update(dt)
-- change level
if player.touchRightBorder == true then
if ui.currentMob == 0 and canChangeLevel == true then
-- update counters
local nextLevel = level.currentLevel + 1
level.currentLevel = nextLevel
ui.maxMob = 0
-- remove items
for i = #items.medipacks, 1, -1 do
table.remove(items.medipacks, i)
end
-- remove bullets
for i = #projectiles.friendly, 1, -1 do
table.remove(projectiles.friendly, i)
end
for i = #projectiles.foe, 1, -1 do
table.remove(projectiles.foe, i)
end
-- load level
level.Load(level.currentLevel)
end
end
if level.isPaused == false then
enemies.Update(map.tileMaps[level.currentLevel], map.tileType, dt)
if level.currentLevel == 18 then
boss.Update(dt)
end
player.Update(map.tileMaps[level.currentLevel], map.tileType, dt)
projectiles.Update(map.tileMaps[level.currentLevel], map.tileType, dt)
items.Update(dt)
ui.Update(dt)
snow.Update(level.currentLevel, dt)
end
if dialog.isDialog == true and level.isPaused == false and ui.pauseMenu == false and boss.state == "hidden" then
dialog.Update(level.currentLevel, dt)
elseif boss.anger == 2 and boss.isAlive == true then
dialog.isDialog = true
dialog.Update(19, dt)
elseif boss.isAlive == false and boss.state == "dead" then
dialog.isDialog = true
dialog.Update(20, dt)
end
-- collision of projectiles with...
-- ...enemy tanks
for i = 1, #projectiles.friendly do
for j = 1, #enemies.tanks do
local distance = player.Distance(projectiles.friendly[i], enemies.tanks[j])
if distance <= 30 then
projectiles.friendly[i].isCollided = true
if projectiles.friendly[i].vx ~= 0 and projectiles.friendly[i].vy ~= 0 then
enemies.tanks[j].hp = enemies.tanks[j].hp - 1
end
end
end
end
-- ...enemy turrets
for i = 1, #projectiles.friendly do
for j = 1, #enemies.turrets do
local distance = player.Distance(projectiles.friendly[i], enemies.turrets[j])
if distance <= 30 then
projectiles.friendly[i].isCollided = true
if projectiles.friendly[i].vx ~= 0 and projectiles.friendly[i].vy ~= 0 then
enemies.turrets[j].hp = enemies.turrets[j].hp - 1
end
end
end
end
-- ...enemy mammoths
for i = 1, #projectiles.friendly do
for j = 1, #enemies.mammoths do
local distance = player.Distance(projectiles.friendly[i], enemies.mammoths[j])
if distance <= 30 then
projectiles.friendly[i].isCollided = true
if projectiles.friendly[i].vx ~= 0 and projectiles.friendly[i].vy ~= 0 then
enemies.mammoths[j].hp = enemies.mammoths[j].hp - 1
end
end
end
end
-- ...boss
if boss.isAlive == true then
for i = 1, #projectiles.friendly do
local distance = player.Distance(projectiles.friendly[i], boss)
if distance <= 64 then
projectiles.friendly[i].isCollided = true
if projectiles.friendly[i].vx ~= 0 and projectiles.friendly[i].vy ~= 0 then
boss.hp = boss.hp - 1
end
end
end
end
-- ...player
for i = 1, #projectiles.foe do
local distance = player.Distance(projectiles.foe[i], player)
if distance <= 40 then
projectiles.foe[i].isCollided = true
if projectiles.foe[i].vx ~= 0 and projectiles.foe[i].vy ~= 0 then
ui.lifeManager.currentLife = LoseLife(ui.lifes, ui.lifeManager, dt)
end
end
end
--collision of mammoths with player
for i = 1, #enemies.mammoths do
local distance = player.Distance(enemies.mammoths[i].bBox[1], player)
if distance <= enemies.pngMammoth1:getWidth() / 2 + player.ox + 5 then
ui.lifeManager.currentLife = LoseLife(ui.lifes, ui.lifeManager, dt)
end
end
-- events
if level.currentLevel == 9 and player.x > 10*32 and level.event[1] == false then
enemies.SpawnTank(8*32, 7*32, "warp", math.pi/4)
enemies.SpawnTank(9*32, 14*32, "warp", -math.pi/4)
enemies.SpawnTank(17*32, 3*32, "warp", math.pi/4*5)
enemies.SpawnTank(23*32, 8*32, "warp", -math.pi/4*5)
level.event[1] = true
elseif level.currentLevel == 10 and player.x > 12*32 and level.event[2] == false then
enemies.SpawnTank(9*32, 6*32, "warp")
level.event[2] = true
elseif level.currentLevel == 11 and player.x > 10*32 and level.event[3] == false then
enemies.SpawnTank(6*32, 17*32, "warp", math.pi)
enemies.SpawnTank(18*32, 6*32, "warp", math.pi)
level.event[3] = true
elseif level.currentLevel == 11 and player.x > 21*32 and level.event[4] == false then
enemies.SpawnTank(18*32, 3*32, "warp")
enemies.SpawnTank(25*32, 8*32, "warp", math.pi)
enemies.SpawnTank(25*32, 14*32, "warp", math.pi)
level.event[4] = true
elseif level.currentLevel == 12 and player.x > 17*32 and level.event[5] == false then
enemies.SpawnTank(12*32, 6*32, "warp")
enemies.SpawnTank(12*32, 11*32, "warp")
enemies.SpawnTank(12*32, 15*32, "warp")
level.event[5] = true
elseif level.currentLevel == 13 and player.x > 13*32 and level.event[6] == false then
enemies.SpawnTank(17*32, 13*32, "warp")
level.event[6] = true
elseif level.currentLevel == 13 and player.x > 23*32 and level.event[7] == false then
enemies.SpawnTank(17*32, 12*32, "warp")
enemies.SpawnTank(28*32, 5*32, "warp", math.pi)
enemies.SpawnTank(28*32, 12*32, "warp", math.pi)
level.event[7] = true
elseif level.currentLevel == 16 and player.x > 17*32 and level.event[8] == false then
enemies.SpawnTank(12*32, 11*32, "warp")
enemies.SpawnTank(26*32, 11*32, "warp", math.pi)
level.event[8] = true
elseif level.currentLevel == 17 and player.x > 17*32 and level.event[9] == false then
enemies.SpawnTank(13*32, 1*32, "warp")
enemies.SpawnTank(13*32, 16*32, "warp")
level.event[9] = true
elseif level.currentLevel == 17 and player.x > 24 and level.event[10] == false then
enemies.SpawnTank(28*32, 9*32, "warp", math.pi)
enemies.SpawnTank(28*32, 10*32, "warp", math.pi)
level.event[10] = true
end
--Victory!
if boss.isAlive == false and boss.state == "dead" and dialog.isDialog == false then
level.wonTheGame = true
ui.alarm:stop()
end
end
---------------------------------------------------------------
function level.Draw()
-- boucle de dessin de la map en fonction des textures
love.graphics.setColor(0.8, 0.8, 1, 1)
local row, col
for row = 1, ui.mapHeight do
for col = 1, ui.mapWidth do
local ground = map.tileMaps[level.currentLevel].ground[row][col]
local quadGround = map.tileTexture[ground]
if quadGround ~= nil then
love.graphics.draw(
map.tileSheet,
quadGround,
(col - 1) * ui.tileWidth,
(row - 1) * ui.tileHeight
)
end
end
end
love.graphics.setColor(1, 1, 1, 1)
for row = 1, ui.mapHeight do
for col = 1, ui.mapWidth do
local grid = map.tileMaps[level.currentLevel].top[row][col]
local quadGrid = map.tileTexture[grid]
if quadGrid ~= nil then
love.graphics.draw(
map.tileSheet,
quadGrid,
(col - 1) * ui.tileWidth,
(row - 1) * ui.tileHeight
)
end
end
end
items.Draw()
if player.isVisible == true then
player.Draw()
end
enemies.Draw()
if level.currentLevel == 18 then
boss.Draw()
end
projectiles.Draw()
snow.Draw()
ui.Draw()
dialog.Draw()
-- pause menu
if level.isPaused == true and ui.pauseMenu == true then
--draw pause menu
love.graphics.setColor(1, 1, 1, 0.7)
love.graphics.rectangle(
"fill",
ui.width / 2 - ui.fontTitle2:getWidth("Continuer ? Y/N") / 2 - 20, ui.height / 2 - 20,
ui.fontTitle2:getWidth("Continuer ? Y/N") + 20 + 20, ui.fontTitle2:getHeight("Continuer ? Y/N") + 40 + 20 + 20)
love.graphics.setColor(0.5, 0, 0, 1)
love.graphics.setFont(ui.fontTitle1)
love.graphics.print("- PAUSE- ", ui.width / 2, ui.height / 2, 0, 1, 1, ui.fontTitle1:getWidth("- PAUSE -")/2)
love.graphics.setFont(ui.fontTitle2)
love.graphics.print("Continuer ? Y/N", ui.width / 2, ui.height / 2 + 40, 0, 1, 1, ui.fontTitle2:getWidth("Continuer ? Y/N")/2)
love.graphics.setColor(1, 1, 1, 1)
end
end
---------------------------------------------------------------
function level.Keypressed(button)
if level.isPaused == false then
player.Keypressed(button)
end
if ui.pauseMenu == false and dialog.isDialog == true then
dialog.Keypressed(button)
end
end
return level