-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmenu.lua
402 lines (342 loc) · 9.32 KB
/
menu.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
local http = require("socket.http")
function submitOnlineHighscore(highscore)
local url = "http://www.langnerd.de/cgi-bin/gorge"
local msg = ""
for _, entry in ipairs(highscore) do
msg = msg .. entry[2] .. " " .. entry[1] .. "\n"
end
local ret, err = http.request(url, msg)
if err == 200 then return tonumber(ret) end
print("Submitting online highscore failed: " .. err)
print(ret, err)
return 0
end
local G = love.graphics
local stats = {
version = 4,
highscore = {
{ "", 10000 },
{ "", 9000 },
{ "", 8000 },
{ "", 7000 },
{ "", 6000 },
{ "", 5000 },
{ "", 4000 },
{ "", 3000 },
{ "", 2000 },
{ "", 1000 },
},
name = "",
music_vol = 7,
sound_vol = 7,
fullscreen = false,
demo = nil,
}
function loadStats()
if not love.filesystem.getInfo(VERSION) then return end
local zipped = love.filesystem.read(VERSION)
local unzipped
pcall(function() unzipped = love.data.decompress("string", "zlib", zipped) end)
if not unzipped then return end
local s = loadstring("return " .. unzipped)()
-- patch
if s.version ~= stats.version then
s.version = stats.version
-- delete demo as it won't play correctly any more
s.demo = nil
end
stats = s
sound.setVolume(stats.sound_vol / 10)
love.window.setFullscreen(stats.fullscreen, "desktop")
end
loadStats()
love.window.setMode(G.getWidth(), G.getHeight(), { vsync=true })
local function saveStats()
local buf = {}
local function w(o)
local t = type(o)
if t == "table" then
buf[#buf+1] = "{"
if o[1] then
for i, a in ipairs(o) do
if i > 1 then buf[#buf+1] = "," end
w(a)
end
else
for k, a in pairs(o) do
buf[#buf+1] = k .. "="
w(a)
buf[#buf+1] = ","
end
end
buf[#buf+1] = "}"
elseif t == "string" then
buf[#buf+1] = ("%q"):format(o)
else
buf[#buf+1] = tostring(o)
end
end
w(stats)
local zipped = love.data.compress("string", "zlib", table.concat(buf))
local f = love.filesystem.newFile(VERSION, "w")
f:write(zipped)
f:close()
end
Menu = Object()
Menu.img = G.newImage("media/title.png")
Menu.options = {
main = { "START GAME", "HIGHSCORE", "OPTIONS", "EXIT" },
highscore = { "BACK" },
options = { "SOUND VOLUME", "MUSIC VOLUME", "TOGGLE FULLSCREEN", "BACK" },
}
function Menu:init()
self.stars = Stars()
self.stars:reset(makeRandomGenerator(4))
self:swapState("main")
end
function Menu:swapState(state)
self.state = state
self.action = false
self.select = 1
self.tick = 0
self.blend = 1
self.entry = false
self.stats_changed = false
end
function Menu:gameOver(game)
-- save demo
if not stats.demo or stats.demo.score < game.player.score then
stats.demo = {
record = game.record,
seed = game.seed,
score = game.player.score,
}
self.stats_changed = true
end
-- check top ten
local entry = { stats.name or "", game.player.score }
for i, e in ipairs(stats.highscore) do
if e[2] < entry[2] then
table.insert(stats.highscore, i, entry)
table.remove(stats.highscore)
self:swapState("highscore")
self.entry = entry
self.stats_changed = true
return
end
end
self:swapState("main")
end
function Menu:update()
self.tick = self.tick + 1
self.stars:update(1)
updateList(Particle.list)
if math.random() < 0.3 then
SparkleParticle(math.random(200, 600), math.random(136, 230))
end
-- buttons
if not self.action then
-- start demo mode
if self.state == "main" and self.tick > 60 * 10 and stats.demo then
self.action = "DEMO"
elseif self.state == "highscore" and self.entry then
-- done writing name?
if Input:gotAnyPressed("enter") then
sound.play("select")
self:submitHighscore(true)
elseif Input:gotAnyPressed("back") then
sound.play("select")
self:submitHighscore(false)
end
else
-- select option
local option = self.options[self.state]
if option then
local o = option[self.select]
local dy = bool[Input:gotAnyPressed("down") and true or false]
- bool[Input:gotAnyPressed("up") and true or false]
if dy ~= 0 then
local s = self.select
self.select = self.select + dy
self.select = math.max(self.select, 1)
self.select = math.min(self.select, #option)
if self.select ~= s then
self.tick = 0
end
end
local dx = bool[Input:gotAnyPressed("right") and true or false]
- bool[Input:gotAnyPressed("left") and true or false]
if dx ~= 0 then
if o == "SOUND VOLUME" then
self.tick = 0
self.stats_changed = true
stats.sound_vol = math.max(0, math.min(stats.sound_vol + dx, 10))
sound.setVolume(stats.sound_vol / 10)
sound.play("laser")
end
if o == "MUSIC VOLUME" then
self.tick = 0
self.stats_changed = true
stats.music_vol = math.max(0, math.min(stats.music_vol + dx, 10))
-- TODO
end
end
if o ~= "MUSIC VOLUME"
and o ~= "SOUND VOLUME" then
-- remember who started the game
self.input = Input:gotAnyPressed("start") or Input:gotAnyPressed("a")
if self.input then
self.action = option[self.select]
if self.action == "BACK" then
sound.play("back")
elseif self.action ~= "EXIT" then
sound.play("select")
end
end
end
end
-- back
if Input:gotAnyPressed("back") or Input:gotAnyPressed("b") then
if self.state == "main" then
self.action = "EXIT"
else
sound.play("back")
self.action = "BACK"
end
end
end
if self.blend > 0 then
self.blend = self.blend - 0.1
end
else
if self.blend < 1 then
self.blend = self.blend + 0.1
end
if self.blend >= 1 then
if self.action == "EXIT" then
love.event.quit()
elseif self.action == "BACK" then
if self.stats_changed then saveStats() end
self:swapState("main")
elseif self.action == "START GAME" then
state = game
game:start(love.math.random(0xfffffff), self.input)
elseif self.action == "HIGHSCORE" then
-- love.keyboard.setTextInput(true)
self:swapState("highscore")
elseif self.action == "OPTIONS" then
self:swapState("options")
elseif self.action == "TOGGLE FULLSCREEN" then
self:swapState("options")
stats.fullscreen = not stats.fullscreen
self.stats_changed = true
love.window.setFullscreen(stats.fullscreen, "desktop")
elseif self.action == "DEMO" then
state = game
game:playBack(stats.demo)
end
end
end
end
function Menu:submitHighscore(online)
if online and self.entry[1] > "" then
local nr = tonumber(submitOnlineHighscore(stats.highscore))
if nr > 0 then
print("Your online rank: " .. nr)
if nr <= 10 then print("Not bad!") end
end
end
stats.name = self.entry[1]
self.entry = false
end
function Menu:keypressed(key)
if self.state == "highscore" and self.entry then
if key == "backspace" then
self.entry[1] = self.entry[1]:sub(1, -2)
self.tick = 0
return
end
if #self.entry[1] >= 13 then return end
if #key == 1 and key:match("[%a%d.-]") then
self.entry[1] = self.entry[1] .. key:upper()
self.tick = 0
elseif key == "space" or key == " " then
if #self.entry[1] > 0 then
self.entry[1] = self.entry[1] .. " "
self.tick = 0
end
end
end
end
function drawFrame(x, y, w, h)
r, g, b, a = love.graphics.getColor()
love.graphics.setColor(r/4, g/4, b/4, a)
y = y + 4
G.rectangle("fill", x, y, w, 4)
G.rectangle("fill", x, y + h - 4, w, 4)
love.graphics.setColor(r, g, b, a)
y = y - 4
G.rectangle("fill", x, y, w, 4)
G.rectangle("fill", x, y + h - 4, w, 4)
G.rectangle("fill", x, y, 4, h)
G.rectangle("fill", x + w - 4, y, 4, h)
end
function Menu:draw()
G.scale(G.getWidth() / 800, G.getHeight() / 600)
G.translate(400, 300)
self.stars:draw()
G.origin()
G.scale(G.getWidth() / 800, G.getHeight() / 600)
if self.state == "main" then
G.setColor(1, 1, 1)
G.draw(self.img, 400, 140, 0, 4, 4, self.img:getWidth() / 2)
Particle:DrawBatch()
G.setColor(1, 1, 1)
for i, m in ipairs(self.options.main) do
font:print(m, 280, 320 + 40 * (i - 1))
end
if self.tick % 32 < 24 then
font:print(">", 248, 320 + 40 * (self.select - 1))
end
G.setColor(0.314, 0.314, 0.314)
font:printCentered("\0 2016 DANIEL LANGNER", 400, 360 + 40 * 5)
elseif self.state == "options" then
G.setColor(1, 1, 1)
G.draw(self.img, 400, 140, 0, 4, 4, self.img:getWidth() / 2)
Particle:DrawBatch()
local x = 184
local y = 320
G.setColor(1, 1, 1)
for i, m in ipairs(self.options.options) do
font:print(m, x, y + 40 * (i - 1))
end
if self.tick % 32 < 24 then
font:print(">", x - 32, y + 40 * (self.select - 1))
end
drawFrame(x + 4 + 6 * 4 * 13, y+8 + 40 * 0, 8 + 12*10, 20)
drawFrame(x + 4 + 6 * 4 * 13, y+8 + 40 * 1, 8 + 12*10, 20)
G.setColor(0.749, 0.749, 0)
G.rectangle("fill", x + 8 + 6 * 4 * 13, y+12 + 40 * 0, 12 * stats.sound_vol, 12)
G.rectangle("fill", x + 8 + 6 * 4 * 13, y+12 + 40 * 1, 12 * stats.music_vol, 12)
G.setColor(0.314, 0.314, 0.314)
font:printCentered("\0 2016 DANIEL LANGNER", 400, 360 + 40 * 5)
elseif self.state == "highscore" then
G.setColor(1, 1, 0)
font:print("HIGHSCORE", 184, 72)
G.setColor(1, 1, 1)
for i, e in ipairs(stats.highscore) do
font:print(("%2d. %-13s %07d"):format(i, e[1], e[2]),
400 - 24 * 13,
96 + 40 * i)
if e == self.entry and self.tick % 32 < 24 then
font:print("\x7f",
400 + 24 * (#self.entry[1] - 9),
96 + 40 * i)
end
end
end
if self.blend > 0 then
G.setColor(0, 0, 0, self.blend)
G.rectangle("fill", 0, 0, 800, 600)
end
end