/
main.lua
612 lines (509 loc) · 16.2 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
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
vector = require "libs.hump.vector"
Gamestate = require "libs.hump.gamestate"
local nosound = false
local blobs = {}
local maxblobs = 100
local blobspdmin = 40
local blobspdmax = 90
local blobradiusmin = 2
local blobradiusmax = 30
local playerradius_start = 5
local playerradius = playerradius_start
local playerhit = false
local playername = "Type Your Name"
local highscores = nil
local highscoresfilename = "highscores.txt"
local escapearrowfilename = "assets/textures/arrowup.png"
local escapearrow = nil
local playerColor = {255, 0, 0, 255}
local playerMutedColor = {100, 100, 100, 255}
local enemyColor = {0, 255, 0, 255}
local hitColor = {255, 0, 255}
local negColor = {255, 255, 0}
local hsColor = {0, 128, 255, 255}
local hsTitleColor = {255, 128, 255, 255}
local escapeColor = {77, 77, 77, 255}
local growthAmount = 1
local shrinkAmount = 1
local decayAmount = 1
local decayRate = 2
local curDecay = 0
local starttime = nil
local endtime = nil
local gameover_state = Gamestate.new()
local play_state = Gamestate.new()
local mainmenu_state = Gamestate.new()
local font = nil
local fontwidth = 16
local fontheight = 28
local curos = "windows"
function resetBlob(blob, minx, maxx)
minx = minx or 830
maxx = maxx or 900
blob.x = math.random(minx, maxx)
blob.y = math.random(1, 600)
blob.r = math.random(blobradiusmin, blobradiusmax)
blob.spd = math.random(blobspdmin, blobspdmax)
blob.hit = false
blob.isneg = math.random(1, 4)
end
function reset()
playerradius = playerradius_start
playerhit = false
for i = 1, maxblobs do
resetBlob(blobs[i], 630, 1430)
end
starttime = love.timer.getTime()
endtime = nil
end
function loadHighScores()
highscores = {}
if not love.filesystem.exists(highscoresfilename) then
return
end
local scores = love.filesystem.lines(highscoresfilename)
for score in scores do
for k, v in string.gmatch(score, "([a-zA-Z0-9 ]+)=([%d\.]+)") do
table.insert(highscores, {name=k, score=tonumber(v)})
end
end
end
function saveHighScores()
local scores = ""
local cnt = 1
for i = 1, #highscores do
scores = scores .. highscores[i].name .. "=" .. highscores[i].score .. "\n"
end
love.filesystem.write(highscoresfilename, scores)
end
function addHighScore(name, score)
if #highscores <= 0 then
table.insert(highscores, {name=name, score=score})
return
end
for i = 1, #highscores do
if score < highscores[i].score then
table.insert(highscores, i, {name=name, score=score})
return
end
end
table.insert(highscores, {name=name, score=score})
end
function centerX(text, scale)
return 400 - ((#text * fontwidth * scale) / 2)
end
function findOS()
-- very primitive (read, error prone) OS detection
curos = "windows"
if love.filesystem.getUserDirectory():sub(1, 6) == "/Users" then
curos = "mac"
elseif love.filesystem.exists("/etc/") then
if love.filesystem.exists("/etc/lsb-release") then
local lsb = love.filesystem.read("/etc/lsb-release")
if string.find(lsb, 'Ubuntu') then
curos = "ubuntu"
end
else
curos = "nix"
end
end
end
function drawHighScores(hoffset)
hoffset = hoffset or 0
if highscores == nil or #highscores <= 0 then
return
end
love.graphics.setColor(hsTitleColor)
local text = "DEATHS"
love.graphics.print(text, centerX(text, .5) + hoffset, 160, 0, .5)
love.graphics.setColor(hsColor)
local cnt = 1
local namestr = ""
local vpos = nil
for i = #highscores, 1, -1 do
if cnt > 10 then
break
end
vpos = 160+(cnt*24)
namestr = "at "..string.format("%.3f", highscores[i].score).."s old"
love.graphics.print(highscores[i].name, (400 - ((#highscores[i].name+1)*fontwidth*.5)) + hoffset, vpos, 0, .5)
love.graphics.print(namestr, 400 + hoffset, vpos, 0, .5)
cnt = cnt + 1
end
end
function drawInstructions(hoffset)
hoffset = hoffset or 0
--[[
And thus She spake:
"EAT! For you shall starve without food."
"GROW! For the big eat the small."
"BEWARE! For the golden children are death."
"LIVE! For life is precious."
"KITTEN! For cuteness."
-- excerpt from the book of prophecy
]]
local leftalign = 400 + hoffset
local spacing = fontheight
local vstart = 210
local scale = .5
love.graphics.setColor({128, 190, 128, 255})
love.graphics.print("And thus She spake:", leftalign, vstart, 0, scale)
love.graphics.setColor({255, 255, 128, 255})
love.graphics.print("\"EAT! For you shall starve without food.\"", leftalign, vstart+(spacing*1), 0, scale)
love.graphics.print("\"GROW! For the big eat the small.\"", leftalign, vstart+(spacing*2), 0, scale)
love.graphics.print("\"BEWARE! For the golden children are death.\"", leftalign, vstart+(spacing*3), 0, scale)
love.graphics.print("\"LIVE! For life is precious.\"", leftalign, vstart+(spacing*4), 0, scale)
love.graphics.print("\"KITTEN! For cuteness.\"", leftalign, vstart+(spacing*5), 0, scale)
love.graphics.setColor({128, 190, 128, 255})
love.graphics.print(" -- excerpt from the book of prophecy", leftalign, vstart+(spacing*6), 0, scale)
end
function drawEscape()
local text = "To Quit, or press ESC"
local arrowx = 6
local textx = 30
if curos ~= "ubuntu" and curos ~= "mac" then
arrowx = 778
textx = 770 - (#text * fontwidth * .25)
end
love.graphics.setColorMode("replace")
love.graphics.draw(escapearrow, arrowx, 3)
love.graphics.setColorMode("modulate")
love.graphics.setColor(escapeColor)
love.graphics.print(text, textx, 8, 0, .25)
end
function loadMusic(filename)
if nosound then
return
end
song = love.audio.newSource(filename, "stream")
song:setLooping(true)
love.audio.play(song)
return song
end
function playMusic(song)
if nosound then
return
end
if song ~= nil and (song:isPaused() or song:isStopped()) then
love.audio.play(song)
end
end
function stopMusic(song)
if nosound then
return
end
if song ~= nil and not song:isPaused() and not song:isStopped() then
love.audio.stop(song)
end
end
function loadSound(filename)
if nosound then
return
end
sound = love.audio.newSource(filename, "static")
song:setLooping(false)
return sound
end
function playSound(sound)
if nosound then
return
end
if sound ~= nil and (sound:isPaused() or sound:isStopped()) then
love.audio.play(sound)
end
end
function stopSound(sound)
if nosound then
return
end
if sound ~= nil and not sound:isPaused() and not sound:isStopped() then
love.audio.stop(sound)
end
end
---------------------------------------------------------------------------
---------------------------------------------------------------------------
-- menu state
--
local mmalphaincamnt = 5
local mmalphadecamnt = -5
local mmalphainc = mmalphadecamnt
local mmalpharate = .01
local mmalphatotal = 0
local mmtextcolor = {0, 255, 255, 255}
local mmhastypedsomething = false
local mmshowneedname = false
local mmmusicfilename = "assets/music/bu-the-poor-puppies.ogg"
local mmmusic = nil
function mainmenu_state:enter(previous)
loadHighScores()
playMusic(mmmusic)
end
function mainmenu_state:leave()
stopMusic(mmmusic)
end
function mainmenu_state:init()
mmmusic = loadMusic(mmmusicfilename)
end
function mainmenu_state:update(dt)
mmalphatotal = mmalphatotal + dt
if mmalphatotal > mmalpharate then
mmalphatotal = 0
mmtextcolor[4] = mmtextcolor[4] + mmalphainc
if mmtextcolor[4] < 50 then
mmalphainc = mmalphaincamnt
mmtextcolor[4] = 50
elseif mmtextcolor[4] > 255 then
mmalphainc = mmalphadecamnt
mmtextcolor[4] = 255
end
end
end
function mainmenu_state:draw()
-- draw a 'muted' player blob
love.graphics.setColor(playerMutedColor)
mousex = love.mouse.getX()
mousey = love.mouse.getY()
love.graphics.circle("fill", mousex, mousey, playerradius_start)
-- the escape instruction
drawEscape()
-- draw the players name, or ask them to enter a name
if not mmhastypedsomething then
love.graphics.setColor({128, 128, 128, 255})
else
love.graphics.setColor({255, 255, 255, 255})
end
love.graphics.print(playername, centerX(playername, 2), 22, 0, 2)
-- tell the player how to start the game
love.graphics.setColor(mmtextcolor)
local text = "To be born, type your name then click your mouse!"
love.graphics.print(text, centerX(text, .5), 550, 0, .5)
-- error message -> a name needs to be entered
if mmshowneedname then
love.graphics.setColor({255, 0, 0, 128})
text = "You need to type something for a name!"
love.graphics.print(text, centerX(text, 1), 100)
end
-- draw the highscores table
drawHighScores(-200)
-- draw the instructions
if highscores == nil or #highscores <= 0 then
drawInstructions((-43*fontwidth*.5)/2)
else
drawInstructions()
end
end
function mainmenu_state:mousereleased(x, y, button)
if not mmhastypedsomething then
mmshowneedname = true
else
Gamestate.switch(play_state)
end
end
function mainmenu_state:keyreleased(key)
if key == "backspace" or key == "delete" then
if not mmhastypedsomething then
playername = ""
elseif #playername <= 0 then
playername = ""
else
playername = playername:sub(1, (#playername-1))
end
end
if love.keyboard.isDown("lshift") or love.keyboard.isDown("rshift") or love.keyboard.isDown("capslock") then
key = key:upper()
end
if #key == 1 and string.find(key, '[a-zA-Z0-9 ]') then
if not mmhastypedsomething then
playername = key
mmhastypedsomething = true
elseif #playername <= 10 then
playername = playername .. key
end
end
end
---------------------------------------------------------------------------
---------------------------------------------------------------------------
-- Gameover state
--
local goalphaincamnt = 5
local goalphadecamnt = -5
local goalphainc = goalphadecamnt
local goalpharate = .01
local goalphatotal = 0
local gotextcolor = {0, 255, 255, 255}
local reborntextcolor = {255, 255, 255, 255}
local gomusicfilename = "assets/music/bu-feet-and-bears.ogg"
local gomusic = nil
function gameover_state:enter(previous)
loadHighScores()
addHighScore(playername, (endtime-starttime))
saveHighScores()
playMusic(gomusic)
end
function gameover_state:leave()
stopMusic(gomusic)
end
function gameover_state:init()
gomusic = loadMusic(gomusicfilename)
end
function gameover_state:update(dt)
goalphatotal = goalphatotal + dt
if goalphatotal > goalpharate then
goalphatotal = 0
reborntextcolor[4] = reborntextcolor[4] + goalphainc
if reborntextcolor[4] < 50 then
goalphainc = goalphaincamnt
reborntextcolor[4] = 50
elseif reborntextcolor[4] > 255 then
goalphainc = goalphadecamnt
reborntextcolor[4] = 255
end
end
end
function gameover_state:draw()
local text = ""
love.graphics.setColor(playerMutedColor)
mousex = love.mouse.getX()
mousey = love.mouse.getY()
love.graphics.circle("fill", mousex, mousey, playerradius_start)
love.graphics.setColor(gotextcolor)
text = "Shit, you died!"
love.graphics.print(text, centerX(text, 1), 22)
text = "But you did manage to live for " .. string.format("%.3f", (endtime-starttime)) .. "s!"
love.graphics.print(text, centerX(text, 1), 54)
love.graphics.setColor(reborntextcolor)
text = "Click your mouse to get reborn!"
love.graphics.print(text, centerX(text, .5), 550, 0, .5)
drawHighScores(-200)
drawInstructions()
drawEscape()
end
function gameover_state:mousereleased(x, y, button)
Gamestate.switch(play_state)
end
---------------------------------------------------------------------------
---------------------------------------------------------------------------
-- Play state
--
local kittenimg = nil
local pmusicfilename = "assets/music/bu-the-tense-foot.ogg"
local pmusic = nil
local psuck2filename = "assets/sound/sucking2.ogg"
local psuck2ref = nil
local pdeathfilename = "assets/sound/death.ogg"
local pdeathref = nil
function play_state:enter(previous)
reset()
playMusic(pmusic)
end
function play_state:leave()
stopMusic(pmusic)
end
function play_state:init()
for i = 1, maxblobs do
blob = {x=nil, y=nil, r=nil, spd=nil, hit=false, isneg=nil}
table.insert(blobs, blob)
end
starttime = love.timer.getTime()
if playername == "KITTEN" then
kittenimg = love.graphics.newImage("assets/textures/kitty.png")
end
pmusic = loadMusic(pmusicfilename)
psuck2 = loadSound(psuck2filename)
pdeath = loadSound(pdeathfilename)
end
function play_state:update(dt)
playerhit = false
local blobv
local playerv = vector(love.mouse.getX(), love.mouse.getY())
for i = 1, maxblobs do
blobs[i].hit = false
-- update blob position
blobs[i].x = blobs[i].x - blobs[i].spd * dt
-- move blob to beginning of flow if off screen or effectively dead
if blobs[i].x < -35 or blobs[i].r < 1 then
resetBlob(blobs[i])
end
-- check for collision
blobv = vector(blobs[i].x, blobs[i].y)
if playerv:dist(blobv) <= blobs[i].r + playerradius then
blobs[i].hit = true
if blobs[i].r > playerradius or blobs[i].isneg == 1 then
-- shrink player
blobs[i].r = blobs[i].r + growthAmount
playerradius = playerradius - shrinkAmount
else
-- grow player
blobs[i].r = blobs[i].r - shrinkAmount
playerradius = playerradius + growthAmount
end
playSound(psuck2)
end
end
curDecay = curDecay + dt
if curDecay > decayRate then
curDecay = 0
playerradius = playerradius - decayAmount
end
if playerradius < 1 and endtime == nil then
playSound(pdeath)
endtime = love.timer.getTime()
Gamestate.switch(gameover_state)
end
end
function play_state:draw()
if kittenimg ~= nil then
love.graphics.setColorMode("replace")
love.graphics.draw(kittenimg, 1, 580)
love.graphics.setColorMode("modulate")
end
drawEscape()
mousex = love.mouse.getX()
mousey = love.mouse.getY()
if not playerhit then
love.graphics.setColor(playerColor)
else
love.graphics.setColor(hitColor)
end
love.graphics.circle("fill", mousex, mousey, playerradius)
for i = 1, maxblobs do
if blobs[i].r >= 1 then
if blobs[i].hit then
love.graphics.setColor(hitColor)
elseif blobs[i].isneg == 1 then
love.graphics.setColor(negColor)
else
love.graphics.setColor(enemyColor)
end
love.graphics.circle("fill", blobs[i].x, blobs[i].y, blobs[i].r)
end
end
end
function play_state:keyreleased(key)
if key == "r" then
Gamestate.switch(play_state)
end
end
---------------------------------------------------------------------------
---------------------------------------------------------------------------
-- LOVE functions
--
function love.load()
for i = 1, #arg do
if arg[i]:lower() == "nosound" then
nosound = true
end
end
love.mouse.setVisible(false)
font = love.graphics.newImageFont("assets/fonts/herkld-28.png", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.!:;,/\\%?'\"[] +-")
love.graphics.setFont(font)
findOS()
escapearrow = love.graphics.newImage(escapearrowfilename)
Gamestate.registerEvents()
Gamestate.switch(mainmenu_state)
end
function love.keyreleased(key)
if key == "escape" then
love.event.quit()
end
end