-
Notifications
You must be signed in to change notification settings - Fork 2
/
menu.lua
248 lines (217 loc) · 5.31 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
local G = love.graphics
local isDown = love.keyboard.isDown
local stats = {
record = false,
highscore = {
{ "TWOBIT", 1000 },
{ "TWOBIT", 900 },
{ "TWOBIT", 800 },
{ "TWOBIT", 700 },
{ "TWOBIT", 600 },
{ "TWOBIT", 500 },
{ "TWOBIT", 400 },
{ "TWOBIT", 300 },
{ "TWOBIT", 200 },
{ "TWOBIT", 100 },
}
}
if love.filesystem.isFile("stats") then
stats = loadstring("return " .. love.filesystem.read("stats"))()
end
local function saveStats()
local f = love.filesystem.newFile("stats", "w")
local function w(t)
if type(t) == "table" then
f:write("{")
local j = 1
for i, a in pairs(t) do
if i == j then
j = j + 1
else
if type(i) == "number" then
f:write("[" .. i .. "]=")
else
f:write(i .. "=")
end
end
w(a)
f:write(",")
end
f:write("}")
elseif type(t) == "string" then
f:write(("%q"):format(t))
else
f:write(tostring(t))
end
end
w(stats)
f:close()
end
Menu = Object()
Menu.img = G.newImage("media/title.png")
Menu.options = {
main = { "START GAME", "HIGHSCORE", "CREDITS", "EXIT" },
credits = { "BACK" },
highscore = { "BACK" }
}
function Menu:init()
self.stars = Stars()
self.stars:reset(makeRandomGenerator(32))
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
Particle.list = {}
end
function Menu:gameOver(game)
local entry = {"", 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)
if not stats.record or i == 1 then
stats.record = game.record
end
self:swapState("highscore")
self.entry = entry
return
end
end
self:swapState("main")
end
function Menu:update()
self.tick = self.tick + 1
self.stars:update(1)
updateList(Particle.list)
if self.state == "main" then
if math.random() < 0.3 then
SparkleParticle(math.random(200, 600), math.random(136, 230))
end
end
if not self.action then
if self.blend > 0 then
self.blend = self.blend - 0.1
end
-- start demo mode
if self.state == "main" and self.tick > 60 * 10 and stats.record then
self.action = "DEMO"
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:reset()
elseif self.action == "HIGHSCORE" then
self:swapState("highscore")
elseif self.action == "CREDITS" then
self:swapState("credits")
elseif self.action == "DEMO" then
state = game
game:playBack(stats.record)
end
end
end
end
function Menu:keypressed(key, isrepeat)
if self.state == "highscore" and self.entry then
if key == "backspace" then
self.entry[1] = self.entry[1]:sub(1, -2)
self.tick = 0
elseif key == "return" or key == "escape" then
self.entry = false
self.stats_changed = true
return
end
if #self.entry[1] >= 12 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" then
self.entry[1] = self.entry[1] .. " "
self.tick = 0
end
return
end
if self.action then return end
if self.options[self.state] then
local dy = bool[key == "down"] - bool[key == "up"]
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, #self.options[self.state])
if self.select ~= s then self.tick = 0 end
end
if key == "return" or key == "space" or key == "x" then
self.action = self.options[self.state][self.select]
end
end
if key == "escape" then
if self.state == "main" then
self.action = "EXIT"
else
self.action = "BACK"
end
end
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(255, 255, 255)
G.draw(self.img, 400, 140, 0, 4, 4, self.img:getWidth() / 2)
drawList(Particle.list)
end
-- draw highscore
if self.state == "main" then
for i, m in ipairs(self.options[self.state]) do
font:print(m, 280, 320 + 40 * (i - 1), 4)
end
if self.tick % 32 < 24 then
font:print(">", 248, 320 + 40 * (self.select - 1), 4)
end
elseif self.state == "highscore" then
G.setColor(255, 255, 0)
font:print("HIGHSCORE", 184, 108, 4)
G.setColor(255, 255, 255)
for i, e in ipairs(stats.highscore) do
font:print(
("%2d. %-12s %08d"):format(i, e[1], e[2]),
400 - 24 * 13,
140 + 32 * i, 4)
if e == self.entry and self.tick % 32 < 24 then
font:print("\x7f",
400 + 24 * (#self.entry[1] - 9),
140 + 32 * i, 4)
end
end
elseif self.state == "credits" then
G.setColor(255, 255, 0)
font:print("CREDITS", 184, 108, 4)
G.setColor(255, 255, 255)
font:print("TODO", 184, 172, 4)
end
G.setColor(40, 40, 40)
font:printCentered("\0 2015 DANIEL LANGNER", 400, 360 + 40 * 5, 4)
if self.blend > 0 then
G.setColor(0, 0, 0, 255 * self.blend)
G.rectangle("fill", 0, 0, 800, 600)
end
end