-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameEngine.lua
More file actions
553 lines (481 loc) · 14.4 KB
/
Copy pathgameEngine.lua
File metadata and controls
553 lines (481 loc) · 14.4 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
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
local gameMap = require("gameMap")
local mapData = require("mapData")
local combatScene = require("combatScene")
local cityScene = require("cityScene")
local textScene = require("textScene")
local input = require "input"
local utility = require("utility")
local resources = require("resources")
local stateMachine = require("stateMachine")
local player = require("player")
local pirate = require("pirate")
local highLevelPirate = require("highLevelPirate")
local keyStarPirate = require("keyStarPirate")
local merchantShip = require("merchantShip")
local boss = require("boss")
local starrySky = resources.images.starrySky
local titleTheme = resources.music.titleTheme
local battleTheme = resources.music.battleTheme
local mainTheme = resources.music.mainTheme
local cityTheme = resources.music.cityTheme
local credits = resources.music.credits
local menuClick = resources.sounds.menuClick
--[[
Game Engine module.
Keeps track of the game state and makes sure that appropriate
objects are updated and drawn.
]]
local gameEngine = {}
gameEngine.running = false
--[[
Local fields and functions.
]]
local function addTypeToRandom(nodes, type, isSpecial)
local selectedIndex
repeat
selectedIndex = math.random(1, table.getn(nodes))
until not isSpecial[selectedIndex]
nodes[selectedIndex].type = type
isSpecial[selectedIndex] = true
end
local function shuffle(tbl)
local size = table.getn(tbl)
for i = size, 1, -1 do
local rand = math.random(size)
tbl[i], tbl[rand] = tbl[rand], tbl[i]
end
return tbl
end
local function getUniqueUpgrade(node)
local upgrades = {}
if not node.upgrades["dodge"] then
table.insert(upgrades, "dodge")
end
if not node.upgrades["crit"] then
table.insert(upgrades, "crit")
end
if not node.upgrades["armor"] then
table.insert(upgrades, "armor")
end
local roll = math.random(1, table.getn(upgrades))
local receivedUpgrade = upgrades[roll]
node.upgrades[receivedUpgrade] = true
end
local function getUniqueGun(weapons)
local specialWeapons = {}
if not weapons["debuff"] then
table.insert(specialWeapons, "debuff")
end
if not weapons["crit"] then
table.insert(specialWeapons, "crit")
end
if not weapons["pierce"] then
table.insert(specialWeapons, "pierce")
end
local roll = math.random(1, table.getn(specialWeapons))
local receivedWeapon = specialWeapons[roll]
table.insert(weapons, receivedWeapon)
end
local function stockStoreInventory(nodes)
local weapons = {}
getUniqueGun(weapons)
getUniqueGun(weapons)
local counter = 0
for i = 1, table.getn(nodes) do
local node = nodes[i]
if node.type == "city" then
counter = counter + 1
node.upgrades = {}
node.weapons = {}
getUniqueUpgrade(node)
getUniqueUpgrade(node)
if counter < 3 then
node.weapons[weapons[counter]] = true
end
end
end
end
local function addNamesToNormal(nodes)
local names = {
"Asinus",
"Magi",
"Sacerdos",
"Imperatrix",
"Caesar",
"Antistes",
"Amantium",
"Curru",
"Viribus",
"Eremita",
"Fortuna",
"Lustitiae",
"Criminalis",
"Mors",
"Temperantia",
"Diaboli",
"Turrim",
"Stella",
"Luna",
"Solis",
"Judicii",
"Mundi"
}
names = shuffle(names)
local nameIndex = 1
for i = 1, table.getn(nodes) do
local node = nodes[i]
if node.type == "normal" or node.type == "key" then
node.name = names[nameIndex]
nameIndex = nameIndex + 1
end
end
end
-- Randomly chooses nodes to get special types
local function randomizeNodes(nodes)
local isSpecial = {}
-- Randomize cities
for _ = 1, 4 do
addTypeToRandom(nodes, "city", isSpecial)
end
-- Randomize danger zones
for _ = 1, 8 do
addTypeToRandom(nodes, "dangerZone", isSpecial)
end
addTypeToRandom(nodes, "key", isSpecial)
addNamesToNormal(nodes)
stockStoreInventory(nodes)
end
local function enterCombat(enemyType, extraLoot)
gameEngine.fsm:setState(gameEngine.combatState)
local enemy = {}
print("type: " .. enemyType)
if enemyType == "pirate" then
enemy = pirate.newPirate()
elseif enemyType == "highLevelPirate" then
enemy = highLevelPirate.newHighLevelPirate()
elseif enemyType == "keyStarPirate" then
enemy = keyStarPirate.newKeyStarPirate()
elseif enemyType == "merchantShip" then
enemy = merchantShip.newMerchantShip()
elseif enemyType == "boss" then
enemy = boss.newBoss()
end
gameEngine.combatScene = combatScene.newCombatScene(gameEngine.player, enemy, extraLoot)
end
local function enterCity(node)
gameEngine.fsm:setState(gameEngine.cityState)
gameEngine.cityScene = cityScene.newCityScene(node, gameEngine.player)
end
local function enterTextScene(type)
gameEngine.fsm:setState(gameEngine.textState)
gameEngine.textScene = textScene.newEncounter(type, gameEngine.player)
end
local function generateMap()
local nodes = mapData.getNodes()
randomizeNodes(nodes)
return gameMap.newGameMap(nodes)
end
local function exitScene(message)
if message == "restart" then
gameEngine.map = generateMap()
gameEngine.player = player.newPlayer()
resources.restartMusic(mainTheme)
elseif message == "foundBoss" then
gameEngine.map:showBoss()
end
gameEngine.fsm:setState(gameEngine.mapState)
end
local function enterMenu()
gameEngine.fsm:setState(gameEngine.menuState)
end
local function scrollSky(dt)
local pos = gameEngine.menuState.skyPosition
if pos > -1280 then
pos = pos - dt * 20
else
pos = 0
end
return pos
end
local function drawSky()
love.graphics.draw(starrySky, gameEngine.menuState.skyPosition, 0)
love.graphics.draw(starrySky, gameEngine.menuState.skyPosition + 1280, 0)
end
local function drawMenu()
for i = 1, table.getn(gameEngine.menuState.Buttons) do
local drawFunction = function()
local option = gameEngine.menuState.Buttons[i]
if option.visible then
love.graphics.print(option.text, option.xPos, option.yPos)
end
end
if i == gameEngine.menuState.selectedIndex then
resources.drawWithColor(
255,
0,
0,
255,
function()
resources.printWithFont("largeFont", drawFunction)
end
)
else
resources.printWithFont("largeFont", drawFunction)
end
end
end
local function MenuUp(menu)
if menu.selectedIndex == 1 then
menu.selectedIndex = table.getn(menu.Buttons)
else
menu.selectedIndex = menu.selectedIndex - 1
end
if not menu.Buttons[menu.selectedIndex].visible then
MenuUp(menu)
end
menu.Buttons[menu.selectedIndex]:focus()
end
local function MenuDown(menu)
if menu.selectedIndex == table.getn(menu.Buttons) then
menu.selectedIndex = 1
else
menu.selectedIndex = menu.selectedIndex + 1
end
if not menu.Buttons[menu.selectedIndex].visible then
MenuDown(menu)
end
menu.Buttons[menu.selectedIndex]:focus()
end
local function MenuSelect(menu)
menu.Buttons[menu.selectedIndex].execute()
end
local function setOptionVisible(option, visible)
option.visible = visible
end
local function resetMenu()
gameEngine.menuState.Buttons[1].visible = gameEngine.running
gameEngine.menuState.Buttons[2].visible = true
gameEngine.menuState.Buttons[3].visible = true
gameEngine.menuState.Buttons[4].visible = true
gameEngine.menuState.Buttons[6].visible = true
gameEngine.menuState.Buttons[5].visible = false
resources.playMusic(titleTheme)
gameEngine.credits = false
gameEngine.tutorial = nil
end
gameEngine.menuState = stateMachine.newState()
gameEngine.menuState.Buttons = {
utility.UI.newButton(
500,
260,
"Resume Game",
false,
true,
function()
gameEngine.fsm:setState(gameEngine.mapState)
end
),
utility.UI.newButton(
500,
300,
"New Game",
true,
true,
function()
gameEngine.running = true
gameEngine.map = generateMap()
gameEngine.player = player.newPlayer()
resources.restartMusic(mainTheme)
gameEngine.fsm:setState(gameEngine.mapState)
setOptionVisible(gameEngine.menuState.Buttons[1], true)
end
),
utility.UI.newButton(
500,
380,
"Credits",
true,
true,
function()
gameEngine.menuState.Buttons[1].visible = false
gameEngine.menuState.Buttons[2].visible = false
gameEngine.menuState.Buttons[3].visible = false
gameEngine.menuState.Buttons[4].visible = false
gameEngine.menuState.Buttons[6].visible = false
gameEngine.menuState.Buttons[5].visible = true
resources.playMusic(credits)
gameEngine.credits = true
end
),
utility.UI.newButton(
500,
420,
"Quit",
true,
true,
function()
love.event.quit()
end
),
utility.UI.newButton(500, 660, "Back", false, true, resetMenu),
utility.UI.newButton(
500,
340,
"Tutorial",
true,
true,
function()
gameEngine.menuState.Buttons[1].visible = gameEngine.running
gameEngine.menuState.Buttons[1].visible = false
gameEngine.menuState.Buttons[2].visible = false
gameEngine.menuState.Buttons[3].visible = false
gameEngine.menuState.Buttons[4].visible = false
gameEngine.menuState.Buttons[6].visible = false
gameEngine.tutorial = 1
end
)
}
function gameEngine.menuState:enter()
resources.playMusic(titleTheme)
if gameEngine.running then
gameEngine.menuState.selectedIndex = 1
else
gameEngine.menuState.selectedIndex = 2
end
end
function gameEngine.menuState:update(dt)
gameEngine.menuState.skyPosition = scrollSky(dt)
local menuInput = input.getMenuInput()
if menuInput == "up" then
MenuUp(gameEngine.menuState)
elseif menuInput == "down" then
MenuDown(gameEngine.menuState)
elseif menuInput == "return" then
MenuSelect(gameEngine.menuState)
end
for i = 1, table.getn(gameEngine.menuState.Buttons) do
local option = gameEngine.menuState.Buttons[i]
if input.mouseOver(option:getRect()) then
if option.visible then
gameEngine.menuState.selectedIndex = i
if not gameEngine.menuState.lastIndex == gameEngine.menuState.selectedIndex then
gameEngine.menuState.Buttons[gameEngine.menuState.selectedIndex]:focus()
end
gameEngine.menuState.lastIndex = gameEngine.menuState.selectedIndex
if input.getLeftClick() then
MenuSelect(gameEngine.menuState)
end
break
end
end
end
if gameEngine.tutorial and input.getLeftClick() then
if gameEngine.tutorial == 4 then
resetMenu()
else
gameEngine.tutorial = gameEngine.tutorial + 1
end
resources.playSound(menuClick)
end
end
function gameEngine.menuState:draw()
drawSky()
if gameEngine.credits then
resources.printWithFont(
"largeFont",
function()
love.graphics.print("Design:\nDiego Ramos", 50, 100)
end
)
resources.printWithFont(
"largeFont",
function()
love.graphics.print("Code:\nJens Genberg", 850, 100)
end
)
resources.printWithFont(
"largeFont",
function()
love.graphics.print("Art:\nGillian Sneve", 50, 300)
end
)
resources.printWithFont(
"largeFont",
function()
love.graphics.print("Sound:\nCoatedpolecat", 850, 300)
end
)
elseif gameEngine.tutorial then
love.graphics.draw(resources.images["tut" .. gameEngine.tutorial], -2, -27)
resources.printWithFont(
"largeFont",
function()
love.graphics.print("Click to advance", 400, 660)
end
)
end
drawMenu(gameEngine.menuState.options)
end
gameEngine.mapState = stateMachine.newState()
function gameEngine.mapState:enter()
--print("Map is " .. gameEngine.map)
print("Enter map state\n")
resources.playMusic(mainTheme)
end
function gameEngine.mapState:update(dt)
gameEngine.map:update(dt)
if input.getEsc() then
gameEngine.fsm:setState(gameEngine.menuState)
end
end
function gameEngine.mapState:draw()
gameEngine.map:draw()
end
gameEngine.combatState = stateMachine.newState()
function gameEngine.combatState:enter()
resources.playMusic(battleTheme)
end
function gameEngine.combatState:update(dt)
gameEngine.combatScene:update(dt)
end
function gameEngine.combatState:draw()
gameEngine.combatScene:draw()
end
gameEngine.cityState = stateMachine.newState()
function gameEngine.cityState.enter()
resources.playMusic(cityTheme)
end
function gameEngine.cityState:update()
gameEngine.cityScene:update(dt)
end
function gameEngine.cityState:draw()
gameEngine.cityScene:draw()
end
gameEngine.textState = stateMachine.newState()
function gameEngine.textState:update()
gameEngine.textScene:update(dt)
end
function gameEngine.textState:draw()
gameEngine.textScene:draw()
end
--[[
Module interface.
]]
function gameEngine.init()
math.randomseed(os.time())
gameMap.init(enterCombat, enterCity, enterMenu, enterTextScene)
combatScene.init(exitScene)
cityScene.init(exitScene)
textScene.init(exitScene, enterCombat)
gameEngine.fsm = stateMachine.newStateMachine()
gameEngine.fsm:setState(gameEngine.menuState)
gameEngine.menuState.skyPosition = 0
resources.playMusic(titleTheme)
end
function gameEngine.update(dt)
gameEngine.fsm.state:update(dt)
end
function gameEngine.draw()
gameEngine.fsm.state:draw()
end
return gameEngine