-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.lua
459 lines (433 loc) · 15.1 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
love.filesystem.load("menus.lua")()
love.filesystem.load("gameplay.lua")()
love.filesystem.load("functions.lua")()
anim8 = require 'anim8/anim8'
love.filesystem.setIdentity("gravitonik")
function love.load()
files = {}
filematch = {}
mm_option = {}
mm_opt_val = {}
levmen_min = 0
levmen_max = 0
usrmen_min = 0
usrmen_max = 0
menufont = love.graphics.newFont("gfx/Prototype.ttf", 42)
textfont = love.graphics.newFont("gfx/Prototype.ttf", 26)
td = ""
level_name = {}
level_name[1] = "Space Walk"
level_name[2] = "A Little Further"
level_name[3] = "Asteroid Belt"
level_name[4] = "Leap of Faith"
level_name[5] = "Deep Space"
level_name[6] = "Orbituary"
level_name[7] = "[Placeholder]"
level_name[8] = "Planetary System"
level_name[9] = "[Placeholder]"
level_name[10] = "Binary System"
level_name[11] = "Spike System"
level_name[12] = "Ultragravity"
level_name[13] = "Spikes of Doom"
level_name[14] = "Platformer"
level_name[15] = "Antigravity"
math.randomseed(os.time())
sm_mass = 1
cam_x = 0
cam_y = 0
cam_o_x = 0
cam_o_y = 0
cam_n_x = 0
cam_n_y = 0
force = 0
force_x = 0
force_y = 0
dist = 0
dir_x = 0
dir_y = 0
G = 0.2
lat = 0
hitangle = 0
vangle1 = 0
relvangle1 = 0
vangle2 = 0
speed = 0
bounce = false
gcross = 0
relgcross = 0
blood_v_x = 0
blood_v_y = 0
runangle = 0
runtime = 0
i = 0
j = 0
rocket = false
rocket_on = false
fuel = 100
level_loaded = true
level_finished = false
paused = false
dead = false
lev_finish_time = 0
menu = true
name_table = {}
name = ""
name_error = ""
can_jump = true
firsthalf = true
change_dist = 0
fuel_planet = {}
fuel_rel_lat = {}
mt1 = love.timer.getTime()
mt2 = mt1
love.graphics.setBackgroundColor(0,0,0)
numlevels = 15
lev_option = {}
lev_opt_val = {}
lev_options = 0
lev_selected = 1
uptolevel = 1
leveltime = 0
newpb = false
besttime = {}
for i = 1, numlevels, 1 do
besttime[i] = 0
end
title = love.graphics.newImage("gfx/gravitonik-title.png")
cc = love.graphics.newImage("gfx/cc-by-nc-sa80x15.png")
lovelogo = love.graphics.newImage("gfx/lovelogo.png")
bg = love.graphics.newImage("gfx/starbg.png")
flame = love.graphics.newImage("gfx/flame.png")
particon = love.graphics.newImage("gfx/particon.png")
bottle = love.graphics.newImage("gfx/fuelbottle.png")
arrow = love.graphics.newImage("gfx/arrow.png")
glow = love.graphics.newImage("gfx/glow.png")
windowicon = love.image.newImageData("gfx/window-icon.png")
love.window.setIcon(windowicon)
sms = love.graphics.newImage("gfx/spaceman-stand.png")
smj = love.graphics.newImage("gfx/spaceman-jump.png")
smr_img = love.graphics.newImage("gfx/spaceman-run.png")
smr_g = anim8.newGrid(512, 512, smr_img:getWidth(), smr_img:getHeight())
smr = anim8.newAnimation(smr_g('1-2', '1-2'), {0.15, 0.05, 0.15, 0.05})
planet = {}
for i = 1, 24, 1 do
planet[i] = love.graphics.newImage("gfx/planet" .. pad_zeros(i, 4) .. ".png")
end
part = {}
part[1] = love.graphics.newImage("gfx/part1.png")
part[2] = love.graphics.newImage("gfx/part2.png")
part[3] = love.graphics.newImage("gfx/part3.png")
p_pos_x = {}
p_pos_y = {}
p_oldpos_x = {}
p_oldpos_y = {}
p_v_x = {}
p_v_y = {}
p_dir = {}
p_speed = {}
p_rad = {}
p_density = {}
p_mass = {}
p_ang = {}
p_ang[0] = 0
p_angv = {}
p_style = {}
part_planet = {}
part_rel_lat = {}
part_pos_x = {}
part_pos_y = {}
part_style = {}
part_ang = {}
part_visible = {}
fuel_remaining = {}
fuel_pos_x = {}
fuel_pos_y = {}
fuel_ang = {}
init_menu()
refresh_levels()
part1 = love.graphics.newImage("gfx/particle.png")
jet = love.graphics.newParticleSystem(part1, 1000)
jet:setEmissionRate(500)
jet:setEmitterLifetime(-1)
jet:setParticleLifetime(0.1, 0.2)
jet:setSpeed(200, 700)
jet:setSpread(0.2)
jet:setRadialAcceleration(-20, -10)
jet:setSizes(0.3, 0.5)
jet:setSizeVariation(1)
jet:setColors(255, 255, 255, 255, 127, 127, 255, 0)
jet:stop()
smoke = love.graphics.newParticleSystem(part1, 2000)
smoke:setEmissionRate(500)
smoke:setEmitterLifetime(-1)
smoke:setParticleLifetime(1, 3)
smoke:setSpeed(50, 100)
smoke:setSpread(0.5)
smoke:setRadialAcceleration(-20, -10)
smoke:setSizes(0.4, 0.7)
smoke:setSizeVariation(1)
smoke:setColors(191, 191, 191, 255, 191, 191, 191, 0)
smoke:stop()
blood = love.graphics.newParticleSystem(part1, 2000)
blood:setEmissionRate(1000)
blood:setEmitterLifetime(0.1)
blood:setParticleLifetime(0.5, 1)
blood:setRadialAcceleration(-20, -10)
blood:setSizes(0.1, 0.7)
blood:setSizeVariation(1)
blood:setColors(255, 0, 0, 255, 255, 0, 0, 0)
blood:stop()
explosion = love.graphics.newParticleSystem(part1, 2000)
explosion:setEmissionRate(2000)
explosion:setEmitterLifetime(0.2)
explosion:setParticleLifetime(0.5, 1.5)
explosion:setSpeed(50, 200)
explosion:setSpread(math.pi)
explosion:setRadialAcceleration(-20, -10)
explosion:setSizes(0.1, 0.7)
explosion:setSizeVariation(1)
explosion:setColors(255, 255, 0, 255, 255, 127, 0, 0)
explosion:stop()
expsmoke = love.graphics.newParticleSystem(part1, 2000)
expsmoke:setEmissionRate(500)
expsmoke:setEmitterLifetime(1)
expsmoke:setParticleLifetime(1.5, 3)
expsmoke:setSpeed(50, 100)
expsmoke:setSpread(math.pi)
expsmoke:setRadialAcceleration(-20, -10)
expsmoke:setSizes(0.4, 0.7)
expsmoke:setSizeVariation(1)
expsmoke:setColors(191, 191, 191, 255, 191, 191, 191, 0)
expsmoke:stop()
end
function love.update(dt)
if menu then update_menus(dt)
else update_level(dt)
end
if paused == false then update_gameplay(dt)
end
end
function love.draw()
love.graphics.setColor(255, 255, 255)
drawbg()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(smoke, -cam_x, -cam_y)
love.graphics.draw(jet, -cam_x, -cam_y)
love.graphics.setColor(255, 255, 255)
for j = 1, numplanets, 1 do
if p_style ~= 0 then love.graphics.draw(planet[p_style[j]], p_pos_x[j] - cam_x, p_pos_y[j] - cam_y, p_ang[j], p_rad[j] / 256, p_rad[j] / 256, 512, 512) end
if thisplanet == j and dead == false and runspeed ~= 0 then
smr:draw(smr_img, sm_pos_x - cam_x, sm_pos_y - cam_y, sm_ang, 0.125 * sm_orient, 0.125, 256, 256)
elseif thisplanet == j and dead == false then
love.graphics.draw(sms, sm_pos_x - cam_x, sm_pos_y - cam_y, sm_ang, 0.125 * sm_orient, 0.125, 256, 256)
end
end
if thisplanet == 0 and rocket_on then
love.graphics.draw(sms, sm_pos_x - cam_x, sm_pos_y - cam_y, sm_ang, 0.125 * sm_orient, 0.125, 256, 256)
elseif thisplanet == 0 then
love.graphics.draw(smj, sm_pos_x - cam_x, sm_pos_y - cam_y, sm_ang, 0.125 * sm_orient, 0.125, 256, 256)
end
if menu then -- Menu screens
draw_menus()
else
if thispart > 0 and thispart <= numparts and part_visible[thispart] then
love.graphics.setColor(255, 255, 255)
love.graphics.draw(glow, part_pos_x[thispart] - cam_x, part_pos_y[thispart] - cam_y, 0, 0.18 + 0.02 * math.sin(10 * love.timer.getTime()), 0.18 + 0.02 * math.sin(10 * love.timer.getTime()), 128, 128)
love.graphics.draw(part[part_style[thispart]], part_pos_x[thispart] - cam_x, part_pos_y[thispart] - cam_y, part_ang[thispart] + p_ang[part_planet[thispart]], 0.125, 0.125, 128, 128)
if dead == false and part_visible[thispart] then draw_arrow(part_pos_x[thispart], part_pos_y[thispart]) end
end
for i = 1, fuel_bottles, 1 do
if fuel_remaining[i] then
love.graphics.setColor(255, 255, 255)
love.graphics.draw(glow, fuel_pos_x[i] - cam_x, fuel_pos_y[i] - cam_y, 0, 0.18 + 0.02 * math.sin(10 * love.timer.getTime()), 0.18 + 0.02 * math.sin(10 * love.timer.getTime()), 128, 128)
love.graphics.draw(bottle, fuel_pos_x[i] - cam_x, fuel_pos_y[i] - cam_y, fuel_ang[i], 0.125, 0.125, 128, 128)
end
end
-- OSD
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(flame, 30, love.graphics.getHeight() - 35, 0, 1, 1, 16, 16)
love.graphics.line(50, love.graphics.getHeight() - 35, 50, love.graphics.getHeight() - 25)
love.graphics.line(50, love.graphics.getHeight() - 30, 50 + fuel, love.graphics.getHeight() - 30)
love.graphics.line(150, love.graphics.getHeight() - 35, 150, love.graphics.getHeight() - 25)
love.graphics.draw(particon, love.graphics.getWidth() - 80, love.graphics.getHeight() - 35, 0, 1, 1, 16, 16)
love.graphics.setFont(textfont)
love.graphics.print(numparts - thispart + 1, love.graphics.getWidth() - 60, love.graphics.getHeight() - 50)
love.graphics.draw(blood, -cam_x, -cam_y)
love.graphics.draw(expsmoke, -cam_x, -cam_y)
love.graphics.draw(explosion, -cam_x, -cam_y)
if paused then
love.graphics.setColor(0, 0, 0, 127)
love.graphics.rectangle("fill", -5, -5, love.graphics.getWidth() + 10, love.graphics.getHeight() + 10)
love.graphics.setColor(255, 255, 255)
love.graphics.setFont(menufont)
love.graphics.print("Paused", love.graphics.getWidth() / 2 - 70, 200)
elseif level_finished then
love.graphics.setColor(0, 0, 0, 127)
love.graphics.rectangle("fill", -5, -5, love.graphics.getWidth() + 10, love.graphics.getHeight() + 10)
love.graphics.setColor(255, 255, 255)
if love.timer.getTime() - lev_finish_time > 1 then
love.graphics.setFont(menufont)
love.graphics.print("Level finished", love.graphics.getWidth() / 2 - 130, 200)
love.graphics.print("Time: " .. display_time(leveltime), love.graphics.getWidth() / 2 - 125, 400)
if newpb then
love.graphics.setFont(textfont)
love.graphics.print("New personal best!", love.graphics.getWidth() / 2 - 110, 450)
end
end
elseif dead then
love.graphics.setColor(0, 0, 0, 127)
love.graphics.rectangle("fill", -5, -5, love.graphics.getWidth() + 10, love.graphics.getHeight() + 10)
if love.timer.getTime() - lev_finish_time > 1 then
love.graphics.setColor(255, 255, 255)
love.graphics.setFont(menufont)
love.graphics.print("Dead", love.graphics.getWidth() / 2 - 40, 200)
love.graphics.print("Time: " .. display_time(leveltime), 400, 400)
end
end
end
-- Debug stuff
--love.graphics.setColor(255, 255, 255, 255)
--love.graphics.setFont(textfont)
--love.graphics.print("visible", 40, 40)
end
function love.keypressed(k, u)
if menu and changescreen == false then -- Process input at menus
if menu_screen == 1 then -- Main menu
if k == "up" then
mm_selected = mm_selected - 1
if mm_selected == 0 then mm_selected = mm_options end
elseif k == "down" then
mm_selected = mm_selected + 1
if mm_selected == mm_options + 1 then mm_selected = 1 end
elseif k == " " or k == "return" then
change_menu(mm_opt_val[mm_selected])
elseif k == "escape" then
love.event.push("quit")
end
elseif menu_screen == 2 then -- New game screen
if string.match(k, '^[rl]shift$') then
return
elseif table.maxn(name_table) and string.match(k, '^[a-z0-9%-_]$') then
if ((love.keyboard.isDown('lshift') or love.keyboard.isDown('lshift')) and string.match(k, '^[a-z]$')) then
k = string.upper(k)
end
table.insert(name_table, k)
elseif k == "backspace" then
table.remove(name_table)
elseif k == "return" then
if table.maxn(name_table) == 0 then
name_error = "Name must contain at least one character"
elseif love.filesystem.exists("save/" .. name .. ".grvsv.lua") then
name_error = "Name already taken"
elseif new_savefile(name) then
refresh_saves()
change_menu(4)
else
name_error = "Error: Could not write save file"
end
elseif k == "escape" then
change_menu(1)
end
name = table.concat(name_table)
elseif menu_screen == 3 then -- Load game screen
if k == "up" then
lm_selected = lm_selected - 1
if lm_selected == 0 then lm_selected = lm_options end
elseif k == "down" then
lm_selected = lm_selected + 1
if lm_selected == lm_options + 1 then lm_selected = 1 end
elseif k == " " or k == "return" then
load_savefile(lm_option[lm_selected])
change_menu(4)
elseif k == "escape" then
change_menu(1)
end
elseif menu_screen == 4 then -- Select level screen
if k == "up" then
lev_selected = lev_selected - 1
if lev_selected == 0 then lev_selected = uptolevel end
elseif k == "down" then
lev_selected = lev_selected + 1
if lev_selected == uptolevel + 1 then lev_selected = 1 end
elseif k == " " or k == "return" then
if lev_opt_val[lev_selected] == 0 then
else
load_level(lev_opt_val[lev_selected])
end
elseif k == "escape" then
change_menu(1)
end
elseif menu_screen == 5 then -- How to play screen
if k == " " or k == "return" then
change_menu(1)
elseif k == "escape" then
change_menu(1)
end
end
elseif level_finished then -- Level finished
if love.timer.getTime() - lev_finish_time > 1 and (k == " " or k == "return" or k == "escape") then
init_menu()
write_savefile(name)
menu = true
level_finished = false
dead = false
cam_x = cam_n_x[4]
cam_y = cam_n_y[4]
change_menu(4)
end
elseif dead then -- Player dead
if love.timer.getTime() - lev_finish_time > 1 and (k == " " or k == "return" or k == "escape") then
init_menu()
menu = true
level_finished = false
dead = false
cam_x = cam_n_x[4]
cam_y = cam_n_y[4]
change_menu(4)
end
elseif menu == false then -- Gameplay
if paused then
if k == "return" or k == " " or k == "escape" or k == "kpenter" or k == "p" then
paused = false
end
elseif k == "p" then
paused = true
elseif k == "f5" then
load_level(thislevel)
elseif k == "up" then
if thisplanet ~= 0 then -- Jump off planet
can_jump = true
for i = 1, numplanets, 1 do
dist = math.sqrt((p_pos_x[i] - sm_pos_x)^2 + (p_pos_y[i] - sm_pos_y)^2)
if i ~= thisplanet and dist <= p_rad[i] + 25 then can_jump = false end
end
if can_jump then
sm_pos_x = p_pos_x[thisplanet] + (p_rad[thisplanet] + 35) * math.cos(lat)
sm_pos_y = p_pos_y[thisplanet] + (p_rad[thisplanet] + 35) * math.sin(lat)
sm_v_x = (runspeed + p_angv[thisplanet] * p_rad[thisplanet]) * math.cos(runangle) + 200 * math.cos(lat) + p_v_x[thisplanet]
sm_v_y = (runspeed + p_angv[thisplanet] * p_rad[thisplanet]) * math.sin(runangle) + 200 * math.sin(lat) + p_v_y[thisplanet]
sm_angv = runspeed / p_rad[thisplanet] + p_angv[thisplanet]
thisplanet = 0
end
elseif thisplanet == 0 and fuel > 0 then
jet:start()
smoke:start()
rocket = true
end
elseif k == "escape" then
init_menu()
menu = true
cam_x = cam_n_x[4]
cam_y = cam_n_y[4]
change_menu(4)
end
end
end
function love.keyreleased(k)
if k == "up" and paused == false then
jet:stop()
smoke:stop()
end
end
function update_level(dt)
end