-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
360 lines (308 loc) · 11.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
require("src.math")
love.graphics.setDefaultFilter("nearest")
local gs = require("src.gamestates")
local gui = require("src.gui")
local mm = require("src.musicManager")
local planets = require("src.planets")
local explosions = require("src.explosions")
local asteroids = require("src.asteroids")
local land = require("src.land")
local player = require("src.player")
local playerLand = require("src.playerLand")
local camera = require("src.camera")
local asteroidTimer
local reason = "startmenu"
local gameoverScreens = {}
gameoverScreens["crashed"] = love.graphics.newImage("res/img/RanOutOfSpaceBetweenUAndThePlanet.png")
gameoverScreens["destroyed"] = love.graphics.newImage("res/img/RanOutOfSpaceBtwUAndTheAsteroid.png")
gameoverScreens["fuel"] = love.graphics.newImage("res/img/RanOutOfFuel.png")
gameoverScreens["food"] = love.graphics.newImage("res/img/RanOutOfFood.png")
gameoverScreens["pop"] = love.graphics.newImage("res/img/RanOutOfSpace.png")
gameoverScreens["0pop"] = love.graphics.newImage("res/img/RanOutOfPpl.png")
gameoverScreens["trapped"] = love.graphics.newImage("res/img/RanOutOfGround.png")
gameoverScreens["victory"] = love.graphics.newImage("res/img/VICTORY.png")
gameoverScreens["startmenu"] = love.graphics.newImage("res/img/startmenu.png")
local scores = {}
local bgImg = love.graphics.newImage("res/img/space/bg_star.png")
local a = 0
local fadein = true
function fadeOut(time, dt, cb)
if a < 1 then
a = a+(1/time*dt)
else
a = 1
if cb then
cb()
end
end
end
function fadeIn(time, dt, cb)
if fadein then
if a > 0 then
a = a-(1/time*dt)
else
a = 0
if cb then
cb()
end
end
end
end
function love.load()
love.graphics.setBackgroundColor(22/255, 36/255, 48/255)
mm.addMusic(love.audio.newSource("res/music/main.mp3", "stream"))
mm.addMusic(love.audio.newSource("res/music/mainMenu.mp3", "stream"))
mm.addMusic(love.audio.newSource("res/music/earth.mp3", "stream"))
mm.addMusic(love.audio.newSource("res/music/desert.mp3", "stream"))
mm.addMusic(love.audio.newSource("res/music/lava.mp3", "stream"))
mm.addMusic(love.audio.newSource("res/music/ice.mp3", "stream"))
mm.addMusic(love.audio.newSource("res/music/gameover.mp3", "stream"))
mm.addMusic(love.audio.newSource("res/music/victory.mp3", "stream"))
w = love.graphics.getWidth()
h = love.graphics.getHeight()
initGameState("GameOver")
mm.playMusic(2)
end
function initGameState(pGs)
if pGs == "SpaceFlight" then
gs.set("SpaceFlight")
scores.fuel = 100
scores.food = 500
scores.pop = 1000
scores.plan = 10
mm.playMusic(1)
asteroidTimer = math.floor(math.prandom(2,5))
planets.init()
player.init(w/2, h-450/3, "res/img/space/ship.png")
camera.init(player.x-w/2, player.y-h/2, 0.5)
planets.add("colonized", w/2, h+2*planets.rads["medium"]/3, "medium", false)
for i=1,35 do
local rnd = math.floor(math.prandom(1, 5))
local size = math.floor(math.prandom(1, 4))
if rnd == 1 then rnd = "lava"
elseif rnd == 2 then rnd = "ice"
elseif rnd == 3 then rnd = "desert"
else rnd = "earth" end
if size == 1 then size = "large"
elseif size == 2 then size = "medium"
else size = "small" end
planets.add(rnd, math.prandom(-2*w, 4*w), math.prandom(-65000, -800), size)
end
elseif pGs == "OnLand" then
gs.set("OnLand")
land.generate(player.refPlanet.size, player.refPlanet.type)
if player.refPlanet.type == "earth" then
mm.playMusic(3)
elseif player.refPlanet.type == "desert" then
mm.playMusic(4)
elseif player.refPlanet.type == "lava" then
mm.playMusic(5)
elseif player.refPlanet.type == "ice" then
mm.playMusic(6)
end
player.landed = false
player.refPlanet.landable = false
player.refPlanet.type = "colonized"
local x,y
repeat
x,y = math.floor(math.prandom(2, land.map.w-1)), math.floor(math.prandom(2, land.map.h-1))
until land.map.terrain[x][y] ~= 3
and (land.map.terrain[x+1][y] ~= 3
or land.map.terrain[x-1][y] ~= 3
or land.map.terrain[x][y-1] ~= 3
or land.map.terrain[x][y+1] ~= 3)
land.setShipPos(x, y)
local ok = false
while not ok do
local rndPos = math.floor(math.prandom(1, 5))
if rndPos == 1 and land.map.terrain[x][y-1] ~= 3 then
playerLand.init(x, y-1, "res/img/land/francis.png")
ok = true
elseif rndPos == 2 and land.map.terrain[x][y+1] ~= 3 then
playerLand.init(x, y+1, "res/img/land/francis.png")
ok = true
elseif rndPos == 3 and land.map.terrain[x+1][y] ~= 3 then
playerLand.init(x+1, y, "res/img/land/francis.png")
ok = true
elseif rndPos == 4 and land.map.terrain[x-1][y] ~= 3 then
playerLand.init(x-1, y, "res/img/land/francis.png")
ok = true
end
end
land.map.objects[playerLand.x][playerLand.y] = 0
elseif pGs == "GameOver" then
gs.set("GameOver")
end
end
function love.update(dt)
mm.update()
if gs.get() == "SpaceFlight" then
-- Update the space part
fadeIn(3, dt)
player.update(dt, planets, scores, asteroids, explosions)
gui.update(dt, player)
asteroids.update(player, planets, explosions, dt)
explosions.update(dt)
updateBullets(dt, asteroids)
camera.moveTo(player.x, player.y)
camera.zoomTo(player.getSpeed()/150)
asteroidTimer = asteroidTimer - dt
if asteroidTimer <= 0 then
asteroidTimer = math.floor(math.prandom(0.5,2))
local x,y = math.prandom(player.x-w, player.x+w), player.y-h
asteroids.add(x, y, math.floor(math.prandom(80, 70)))
end
if player.landed then
fadein = false
fadeOut(1.5, dt, function()
initGameState("OnLand")
fadein = true
end)
end
if player.crashed then
fadein = false
fadeOut(3, dt, function()
reason = "crashed"
initGameState("GameOver")
mm.playMusic(7)
fadein = true
end)
end
if player.destroyed then
fadein = false
fadeOut(3, dt, function()
reason = "destroyed"
initGameState("GameOver")
mm.playMusic(7)
fadein = true
end)
end
if scores.fuel <= 0 then
fadein = false
fadeOut(3, dt, function()
reason = "fuel"
initGameState("GameOver")
mm.playMusic(7)
fadein = true
end)
end
if scores.pop >= 5000 then
fadein = false
fadeOut(3, dt, function()
reason = "pop"
initGameState("GameOver")
mm.playMusic(7)
fadein = true
end)
end
if scores.plan <= 0 then
fadein = false
fadeOut(3, dt, function()
reason = "victory"
initGameState("GameOver")
mm.playMusic(8)
fadein = true
end)
end
elseif gs.get() == "OnLand" then
-- Update the land part
fadeIn(3, dt)
playerLand.update(land.map, land.ship.x, land.ship.y, scores, dt)
land.update(dt)
if playerLand.takeOff then
fadein = false
fadeOut(1.5, dt, function()
gs.set("SpaceFlight")
mm.playMusic(1)
scores.plan = scores.plan - 1
fadein = true
end)
end
if scores.food <= 0 then
fadein = false
fadeOut(3, dt, function()
reason = "food"
initGameState("GameOver")
mm.playMusic(7)
fadein = true
end)
end
if scores.pop <= 0 then
fadein = false
fadeOut(3, dt, function()
reason = "0pop"
initGameState("GameOver")
mm.playMusic(7)
fadein = true
end)
end
if playerLand.trapped then
fadein = false
fadeOut(3, dt, function()
reason = "trapped"
initGameState("GameOver")
mm.playMusic(7)
fadein = true
end)
end
elseif gs.get() == "GameOver" then
fadeIn(3, dt)
end
end
function love.draw()
for x=1,math.ceil(w/128) do
for y=1,math.ceil(h/128) do
love.graphics.draw(bgImg, (x-1)*128, (y-1)*128, 0, 2, 2)
end
end
if gs.get() == "SpaceFlight" then
camera.set()
-- Draw the space part
for i=1,#planets do
local planet = planets[i]
local dist = math.dist(player.x, player.y, planet.x, planet.y)
local angle = math.angle(player.x, player.y, planet.x, planet.y)
if dist > planet.r+50 and dist < 7000 then
love.graphics.setColor(0,1,0, math.normalize(dist, 7000, planet.r+50))
love.graphics.circle("fill", player.x + math.cos(angle) * 75, player.y + math.sin(angle) * 75, 10*math.normalize(dist, 7000, planet.r+50))
love.graphics.setColor(1,1,1,1)
end
end
planets.draw()
drawBullets()
asteroids.draw()
explosions.draw()
player.draw()
planets.drawAtmos()
camera.unset()
elseif gs.get() == "OnLand" then
love.graphics.push()
love.graphics.translate(w/2-land.map.w/2*land.TILESIZE, h/2-land.map.h/2*land.TILESIZE)
land.draw()
playerLand.draw(land.TILESIZE)
love.graphics.pop()
elseif gs.get() == "GameOver" then
love.graphics.draw(gameoverScreens[reason], 0, 0)
if reason ~= "startmenu" then
love.graphics.setColor(25/255, 25/255, 25/255)
love.graphics.rectangle("fill", w/2-180, 21, 320, 30)
love.graphics.setColor(1,1,1,1)
love.graphics.print("Press <space> to restart", w/2-170, 20, 0, 2, 2)
end
end
if gs.get() == "SpaceFlight" or gs.get() == "OnLand" then
gui.draw(scores, w, h)
end
love.graphics.setColor(0,0,0,a)
love.graphics.rectangle("fill", 0, 0, w, h)
love.graphics.setColor(1,1,1,1)
end
function love.keypressed(key)
if gs.get() == "SpaceFlight" then
player.keypressed(key)
elseif gs.get() == "OnLand" then
playerLand.keypressed(key)
elseif gs.get() == "GameOver" and key == "space" then
initGameState("SpaceFlight")
end
end