-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
525 lines (451 loc) · 17.4 KB
/
main.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
local wf = require 'libs.windfield'
local json = require 'libs.json'
local cpml = require 'libs.cpml'
require 'src.player'
require 'src.rotatingplanet'
require 'src.turret'
require 'src.background'
require 'src.bullet'
require 'src.missile'
require 'src.explosion'
require 'src.wall'
require 'src.lasergate'
require 'src.key'
require 'src.lockedgate'
local lastJumped = 0
local jumpLimit = 0.6 --how often can the player jump... lower numbers are faster
local bulletLifetime = 10 --how long a bullet lives before being destroyed (if it doesnt collide with something first)
local missileLifetime = 20
local gravityMultiplier = 2800
local jumpMultiplier = 550
local walkMultiplier = 180
local paused = false
local debug = false --make sure this is false for real deployment
local music = true --make sure this is true for real deployment
local currentLevelName = nil
local currentLevel = 1
local levels = nil
local planets = {}
local levelStartTime = nil
local timeSinceLevelStart = 0
local lastPlanetTouched = nil
local jumpState = 'jumped'
entities = {}
bullets = {}
missiles = {}
explosions = {}
keys = {}
lockedGates = {}
player = nil
function love.load()
love.math.setRandomSeed(love.timer.getTime())
local major, minor, revision, codename = love.getVersion()
print("running with LÖVE version: " .. major .. "." .. minor .. "." .. revision .. " " .. codename)
love.graphics.setDefaultFilter('nearest', 'nearest', 8)
levels = json.decode(readFile("./levels/levels.json"))
world = wf.newWorld(0, 0, true)
world:setGravity(0, 0)
world:addCollisionClass('Missile', { ignores = { 'Missile' } })
world:addCollisionClass('Bullet', { ignores = { 'Bullet', 'Missile' } })
world:addCollisionClass('Planet')
world:addCollisionClass('Player')
world:addCollisionClass('Wall')
world:addCollisionClass('Laser')
world:addCollisionClass('LockedGate')
world:addCollisionClass('Meteor', { ignores = { 'All' } })
world:addCollisionClass('Key', { ignores = { 'Bullet', 'Missile' } })
mainFont = love.graphics.newFont("image/font/Amuro.otf", 12, 'mono')
love.graphics.setFont(mainFont)
titleImage = love.graphics.newImage("image/title.png")
woodenControlsImage = love.graphics.newImage("image/WoodenControls.png")
rightArrowQuad = love.graphics.newQuad(137, 0, 12, 15, woodenControlsImage:getWidth(), woodenControlsImage:getHeight())
leftArrowQuad = love.graphics.newQuad(120, 0, 12, 15, woodenControlsImage:getWidth(), woodenControlsImage:getHeight())
upArrowQuad = love.graphics.newQuad(150, 0, 15, 15, woodenControlsImage:getWidth(), woodenControlsImage:getHeight())
escQuad = love.graphics.newQuad(180, 60, 35, 16, woodenControlsImage:getWidth(), woodenControlsImage:getHeight())
initAudio()
background = Background:new()
loadLevel(currentLevel)
end
function love.draw()
background:draw()
love.graphics.setColor(1, 1, 1, 1)
for i in ipairs(entities) do
entities[i]:draw()
end
for i in ipairs(planets) do
planets[i]:draw()
end
for i in ipairs(bullets) do
bullets[i]:draw()
end
for i in ipairs(missiles) do
missiles[i]:draw()
end
for i in ipairs(explosions) do
explosions[i]:draw()
end
for i in ipairs(keys) do
keys[i]:draw()
end
for i in ipairs(lockedGates) do
lockedGates[i]:draw()
end
player:draw()
if debug then
world:draw()
end
love.graphics.setColor(1, 1, 1, 1)
if currentLevel == 1 and not debug then
love.graphics.draw(titleImage, 640, 150, 0, 1, 1, titleImage:getWidth() / 2, titleImage:getHeight() / 2)
drawControls(50, 569)
elseif isLastLevel() then
love.graphics.draw(titleImage, 640, 150, 0, 1, 1, titleImage:getWidth() / 2, titleImage:getHeight() / 2)
love.graphics.print("YOU GOT TO THE MOON!!!", love.graphics.getFont(), 25, 675, 0, 2, 2)
love.graphics.print("Thanks for playing!", love.graphics.getFont(), 975, 675, 0, 2, 2)
else
local levelText = "Level " .. currentLevel .. " - " .. currentLevelName
love.graphics.print(levelText, love.graphics.getFont(), 25, 675, 0, 2, 2)
local timeString = string.format("Time: %.2f", timeSinceLevelStart)
love.graphics.print(timeString, 25, 25, 0, 2, 2)
end
if player:getNumOfKeysInInventory() > 0 then
love.graphics.print("Keys: " .. player:getNumOfKeysInInventory(), 1150, 30, 0, 2, 2)
end
if paused then
drawPausePopup()
end
end
function love.keypressed(key, scancode, isrepeat)
if key == "r" and paused and debug then
restartLevel()
end
if key == "q" and paused and debug then
love.event.quit()
end
if key == "v" and paused and debug then
love.audio.setVolume(0)
end
if key == "n" and paused and debug then
loadNextLevel()
end
if key == "l" and paused and debug then
if pcall(function()
local textFromClipoboard = tostring(love.system.getClipboardText())
local jsonFromClipboard = json.decode(textFromClipoboard)
levels = jsonFromClipboard
clearLevel()
loadLevel(1)
end) then
print("loaded level from clipboard")
else
print("error loading level from clipboard")
end
end
if (key == "up" or key == "space") and lastJumped > jumpLimit and timeSinceLevelStart > .2 then
local closestPlanet = getClosestPlanet()
local normalizedVectorTowardsClosestPlanet = getNormalizedVectorTowardsPlanet(closestPlanet)
player:getBox():setLinearVelocity(
-normalizedVectorTowardsClosestPlanet.x * jumpMultiplier,
-normalizedVectorTowardsClosestPlanet.y * jumpMultiplier)
jumpSound:clone():play()
lastJumped = 0
jumpState = 'jumped'
end
if key == "escape" then
paused = not paused
end
end
function love.update(dt)
timeSinceLevelStart = love.timer.getTime() - levelStartTime
lastJumped = lastJumped + dt
--if game is paused then don't update anything's state
if paused then
return
end
local closestPlanet = getClosestPlanet()
local normalizedVectorTowardsClosestPlanet = getNormalizedVectorTowardsPlanet(closestPlanet)
--apply gravity towards the closest planet or handle landing on a planet
if not player:getBox():enter('Planet') then
player:getBox():applyForce(
normalizedVectorTowardsClosestPlanet.x * gravityMultiplier,
normalizedVectorTowardsClosestPlanet.y * gravityMultiplier)
else
if lastJumped > .1 and lastPlanetTouched ~= closestPlanet then
player:getBox():setLinearVelocity(0, 0)
player:getBox():setAngularVelocity(0)
end
if closestPlanet:getType() == 'moon' and not isLastLevel() then
successSound:clone():play()
loadNextLevel()
return
end
lastPlanetTouched = closestPlanet
jumpState = 'landed'
end
--handle missiles or bullets hitting the player
if player:getBox():enter('Bullet') or player:getBox():enter('Missile') then
deathSound:clone():play()
restartLevel()
end
--handle left/right input
isMovingLeft = false
isMovingRight = false
if love.keyboard.isDown("right") and lastJumped > jumpLimit and jumpState == 'landed' then
--clockwise
isMovingRight = true
player:getBox():setLinearVelocity(
normalizedVectorTowardsClosestPlanet.y * walkMultiplier,
-normalizedVectorTowardsClosestPlanet.x * walkMultiplier)
end
if love.keyboard.isDown("left") and lastJumped > jumpLimit and jumpState == 'landed' then
--counterclockwise
isMovingLeft = true
player:getBox():setLinearVelocity(
-normalizedVectorTowardsClosestPlanet.y * walkMultiplier,
normalizedVectorTowardsClosestPlanet.x * walkMultiplier)
end
--update all entities in the world
world:update(dt)
background:update(dt)
for i in ipairs(entities) do
entities[i]:update(dt)
end
for i in ipairs(keys) do
keys[i]:update(dt)
end
for i in ipairs(lockedGates) do
lockedGates[i]:update(dt)
end
for i in ipairs(planets) do
planets[i]:update(dt)
end
for i, bullet in ipairs(bullets) do
if bullet:getBox():enter('Planet') or bullet:getTimeAlive() > bulletLifetime then
local explosion = Explosion:new(bullet:getX(), bullet:getY(), .5, 1, false)
table.insert(explosions, explosion)
bullet:destroy(i)
else
bullet:update(dt)
end
end
for i, missile in ipairs(missiles) do
if missile:getBox():enter('Planet')
or missile:getBox():enter('Wall')
or missile:getBox():enter('Bullet')
or missile:getBox():enter('Laser')
or missile:getTimeAlive() > missileLifetime then
local explosion = Explosion:new(missile:getX(), missile:getY(), 1, 2.5, true)
table.insert(explosions, explosion)
missile:destroy(i)
else
missile:update(dt)
end
end
for i, explosion in ipairs(explosions) do
if explosion:getTimeAlive() > explosion:getLifetime() then
table.remove(explosions, i)
else
explosion:update(dt)
end
end
--angle the player feet-down towards the closest planet
local angleTo = math.atan2(normalizedVectorTowardsClosestPlanet.y, normalizedVectorTowardsClosestPlanet.x)
player:update(dt, angleTo)
end
function distanceBetweenEntities(entity1, entity2)
return distanceFrom(entity1:getX(), entity1:getY(), entity2:getX(), entity2:getY())
end
function distanceFrom(x1, y1, x2, y2)
return math.sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)
end
function readFile(file)
local f = assert(io.open(file, "rb"))
local content = f:read("*all")
f:close()
return content
end
function clearLevel()
for i in ipairs(entities) do
entities[i]:destroy()
end
for i in ipairs(planets) do
planets[i]:getBox():destroy()
end
for i in ipairs(bullets) do
bullets[i]:getBox():destroy()
end
for i in ipairs(missiles) do
missiles[i]:getBox():destroy()
end
for i in ipairs(keys) do
keys[i]:getBox():destroy()
end
for i in ipairs(lockedGates) do
lockedGates[i]:getBox():destroy()
end
if player ~= nil then
player:destroy()
end
entities = {}
planets = {}
bullets = {}
missiles = {}
explosions = {}
keys = {}
lockedGates = {}
player = nil
end
function restartLevel()
loadLevel(currentLevel)
if paused then
paused = false
end
end
function loadLevel(level)
clearLevel()
local levelToLoad = levels[level]
local playerToLoad = levelToLoad.entities.player
player = Player:new(playerToLoad.startPosition.x, playerToLoad.startPosition.y)
local planetsToLoad = levelToLoad.entities.planets
for i in ipairs(planetsToLoad) do
table.insert(planets, RotatingPlanet:new({ x = planetsToLoad[i].position.x,
y = planetsToLoad[i].position.y,
size = planetsToLoad[i].size,
type = planetsToLoad[i].type }))
end
local turretsToLoad = levelToLoad.entities.turrets
if turretsToLoad ~= nil then
for i in ipairs(turretsToLoad) do
table.insert(entities, Turret:new({ x = turretsToLoad[i].position.x,
y = turretsToLoad[i].position.y,
type = turretsToLoad[i].type,
startAngle = turretsToLoad[i].startAngle,
firingSpeed = turretsToLoad[i].firingSpeed,
projectileSpeed = turretsToLoad[i].projectileSpeed,
shouldTrackPlayer = turretsToLoad[i].tracksPlayer }))
end
end
local wallsToLoad = levelToLoad.entities.walls
if wallsToLoad ~= nil then
for i in ipairs(wallsToLoad) do
table.insert(entities, Wall:new({ x = wallsToLoad[i].position.x,
y = wallsToLoad[i].position.y,
w = wallsToLoad[i].size.w,
h = wallsToLoad[i].size.h }))
end
end
local laserGatesToLoad = levelToLoad.entities.laserGates
if laserGatesToLoad ~= nil then
for i in ipairs(laserGatesToLoad) do
table.insert(entities, LaserGate:new({ x = laserGatesToLoad[i].position.x,
y = laserGatesToLoad[i].position.y,
direction = laserGatesToLoad[i].direction,
length = laserGatesToLoad[i].length,
switchRate = laserGatesToLoad[i].switchRate }))
end
end
local keysToLoad = levelToLoad.entities.keys
if keysToLoad ~= nil then
for i in ipairs(keysToLoad) do
table.insert(keys, Key:new({ x = keysToLoad[i].position.x,
y = keysToLoad[i].position.y }))
end
end
local lockedGatesToLoad = levelToLoad.entities.lockedGates
if lockedGatesToLoad ~= nil then
for i in ipairs(lockedGatesToLoad) do
table.insert(lockedGates, LockedGate:new({ x = lockedGatesToLoad[i].position.x,
y = lockedGatesToLoad[i].position.y,
direction = lockedGatesToLoad[i].direction }))
end
end
currentLevelName = levelToLoad.name
levelStartTime = love.timer.getTime()
if isLastLevel() then
background:setMeteorsEnabled(true)
end
end
function initAudio()
jumpSound = love.audio.newSource("audio/jump.wav", "static")
jumpSound:setVolume(0.25)
explSound = love.audio.newSource("audio/explosion.mp3", "static")
explSound:setVolume(0.3)
laserSound = love.audio.newSource("audio/laserbuzz.wav", "static")
laserSound:setVolume(0.3)
missileSound = love.audio.newSource("audio/missile.wav", "static")
missileSound:setVolume(0.2)
gunSound = love.audio.newSource("audio/gunshot.wav", "static")
gunSound:setVolume(0.1)
keyPickupSound = love.audio.newSource("audio/keypickup.ogg", "static")
keyPickupSound:setVolume(1.0)
unlockSound = love.audio.newSource("audio/unlock.wav", "static")
unlockSound:setVolume(1.0)
successSound = love.audio.newSource("audio/success.wav", "static")
successSound:setVolume(1.0)
deathSound = love.audio.newSource("audio/death.wav", "static")
deathSound:setVolume(1.0)
local musicSound = love.audio.newSource("audio/music/manystars.ogg", 'static')
musicSound:setLooping(true)
musicSound:setVolume(1.3)
if music then
musicSound:play()
end
end
function drawControls(x, y)
love.graphics.draw(woodenControlsImage, rightArrowQuad, x, y, 0, 2, 2)
love.graphics.draw(woodenControlsImage, leftArrowQuad, x, y + 30, 0, 2, 2)
love.graphics.draw(woodenControlsImage, upArrowQuad, x, y + 60, 0, 2, 2)
love.graphics.draw(woodenControlsImage, escQuad, x - 5, y + 103, 0, 1, 1)
love.graphics.print(" - Move clockwise", x, y + 5, 0, 1.5)
love.graphics.print(" - Move counterclockwise", x, y + 35, 0, 1.5)
love.graphics.print(" - Jump", x, y + 68, 0, 1.5)
love.graphics.print(" - Pause", x, y + 98, 0, 1.5)
end
function drawPausePopup()
local topLeftX = 440
local topLeftY = 260
local width = 375
local height = 240
love.graphics.setColor(1, 1, 1, 1)
love.graphics.rectangle('fill', topLeftX, topLeftY, width, height, 5, 5)
love.graphics.setColor(0, 0, 0, 1)
love.graphics.rectangle('fill', topLeftX + 5, topLeftY + 5, width - 10, height - 10, 5, 5)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.print("PAUSED", topLeftX + 142, topLeftY + 30, 0, 1.5)
drawControls(topLeftX + 30, topLeftY + 80)
end
function isLastLevel()
return table.getn(levels) == currentLevel
end
function loadNextLevel()
currentLevel = currentLevel + 1
loadLevel(currentLevel)
end
function getClosestPlanet()
local closestPlanet = nil
if table.getn(planets) > 0 then
closestPlanet = planets[1]
for i in ipairs(planets) do
local distanceBetweenPlayerAndCurrentClosest = distanceBetweenEntities(player, closestPlanet)
local distanceBetweenPlayerAndThisPlanet = distanceBetweenEntities(player, planets[i])
if distanceBetweenPlayerAndCurrentClosest > distanceBetweenPlayerAndThisPlanet then
closestPlanet = planets[i]
end
end
end
return closestPlanet
end
function getNormalizedVectorTowardsPlanet(planet)
local vectorXTowardPlanet = planet:getX() - player:getX();
local vectorYTowardPlanet = planet:getY() - player:getY();
return cpml.vec2(vectorXTowardPlanet, vectorYTowardPlanet):normalize()
end
function clampVector(v, d)
local x, y = v.x, v.y
local d2 = math.sqrt(x * x + y * y)
if d2 > d then
v.x = x / d2 * d
v.y = y / d2 * d
end
return d2
end