-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmainmenu.lua
259 lines (247 loc) · 8.61 KB
/
mainmenu.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
local function startplaying(name)
mainmenu.unload()
editor.allowed = false
startgame(name)
end
local function loadGame()
startplaying(mainmenu.options.load[mainmenu.selected])
end
local function prepareload ()
mainmenu.options.load = {}
mainmenu.actions.load = {}
local maps = love.filesystem.enumerate("maps")
for i, v in ipairs(maps) do
if v:sub(-4, -1) == ".lua" and --only load .lua files
v:sub(1, 3) ~= 'map' and --can't load campaign scenarios
v ~= 'newmap.lua' then --don't show newmap
table.insert(mainmenu.options.load, string.sub(v, 1, -5))
table.insert(mainmenu.actions.load, loadGame)
end
end
table.insert(mainmenu.options.load, 'Back')
table.insert(mainmenu.actions.load, function () mainmenu.start'play' end)
mainmenu.start'load'
end
mainmenu = {
options = {
main = {'Play', 'Share', 'Edit', 'Settings', 'Credits', 'Exit'},
play = {'Start game', 'Start campaign', 'Start tutorial', 'Load game', 'Back'},
settings = {'Screensize', 'Fullscreen', 'Update', 'Back', },
update = {'Back', },
resolution = {'Back', },
load = {'?'}
},
actions = {
main = {function () mainmenu.start'play' end, function () mainmenu.unload() ;marketplace.load() end,
function () startplaying('newmap', true);editor.allowed=true;editor.active=true end,
function () mainmenu.start'settings' end,
function () mainmenu.credits.start() end,
function () quitgame() end
},
play = {function () startplaying'testmap' end, function () startplaying("map" .. campaignmap) end,
function () startplaying'map0' end, prepareload, function () mainmenu.start'main' end
},
settings = {function () mainmenu.start'resolution' end, function () love.graphics.toggleFullscreen(); mainmenu.fullscreen = true; mainmenu.start'settings' end,
function () mainmenu.start'update' end, function () mainmenu.start'main' end,
},
update = {function () mainmenu.start'settings' end, },
resolution = {function () mainmenu.start'settings' end, },
load = {loadGame}
},
itemx = 600,
logox = -400,
logodx = 50,
state = 1,
fadetime = .35,
active = false,
start = function (new,first)
mainmenu.current = new
mainmenu.ys = {}
mainmenu.y_tos = {}
mainmenu.lines = 900
local n = #mainmenu.options[new]
local basey = love.graphics.getHeight()/4
for i=1,n do
mainmenu.ys[i] = -10
mainmenu.y_tos[i] = i*20+basey
end
mainmenu.y_tos[n] = n*20+basey+3
mainmenu.selected = 1
mainmenu.state = 1
mainmenu.fadeoutcountdown = nil
if first then
mainmenu.fadeincountdown = nil
else
mainmenu.fadeincountdown = mainmenu.fadetime
end
mainmenu.itemx = love.graphics.getWidth()/2
end,
credits = {
names = {
"Bart van Strien", "bartbes", "Project starter, general coding",
"Robin Wellner", "Robin", "Physics, editor, main menu & testing",
"SnakeFace", "", "General coding & artwork",
"Qubodup", "", "Artwork",
"Linus Sjogren", "thelinx", "Donates host"
},
index = 0,
x = 0,
timeout = 0,
start = function()
mainmenu.credits.index = 0
mainmenu.credits.x = love.graphics.getWidth()+10
mainmenu.credits.timeout = 0
mainmenu.state=3
mainmenu.fadeincountdown = mainmenu.fadetime
mainmenu.fadeoutcountdown = nil
end
},
fullscreen = false
}
function mainmenu.load ()
stopallsounds()
setCamera(cameras.mainmenu)
mainmenu.start('main', true)
love.graphics.setBackgroundColor(255, 255, 255)
love.graphics.setLineWidth(2)
mainmenu.active = true
editor.active = false
love.update = mainmenu.update
love.draw = mainmenu.draw
end
function mainmenu.unload ()
love.graphics.setBackgroundColor(0,0,0)
setCamera(cameras.hud) --> strangely, I have to do this to fix line width
love.graphics.setLineWidth(1)
setCamera(cameras.default)
mainmenu.active = false
love.update = game.update
love.draw = game.draw
end
function mainmenu.update (dt)
if mainmenu.state == 1 then
for i,v in ipairs(mainmenu.ys) do
mainmenu.ys[i] = mainmenu.ys[i] + (mainmenu.y_tos[i]-mainmenu.ys[i])*dt*4
end
mainmenu.lines = mainmenu.lines + (mainmenu.ys[mainmenu.selected]-mainmenu.lines)*dt*4
if mainmenu.fadeoutcountdown then
mainmenu.fadeoutcountdown = mainmenu.fadeoutcountdown - dt
if mainmenu.fadeoutcountdown < 0 then mainmenu.state = 2;mainmenu.actions[mainmenu.current][mainmenu.selected]() end
end
if mainmenu.fadeincountdown then
mainmenu.fadeincountdown = mainmenu.fadeincountdown - dt
if mainmenu.fadeincountdown < 0 then mainmenu.fadeincountdown=nil end
end
mainmenu.logox = mainmenu.logox + mainmenu.logodx*dt
if mainmenu.logox > 180 and mainmenu.logodx > 0 then
mainmenu.logodx = mainmenu.logodx*-.2
if mainmenu.logodx > -30 then mainmenu.logodx = 0 end
elseif mainmenu.logox < 50 then
mainmenu.logodx = mainmenu.logodx + (50-mainmenu.logox)/10
end
elseif mainmenu.state==3 then
--credits
local c = mainmenu.credits
local halfw = love.graphics.getWidth()*.5-50
if c.timeout > 0 then
c.timeout = c.timeout - dt
else
if c.x > halfw then
c.x = c.x - ((c.x-halfw)*4.8+10)*dt
if c.x < halfw then
c.timeout = .15+ 0.01*(#c.names[c.index*3+1]+#c.names[c.index*3+2]+#c.names[c.index*3+3])
end
else
c.x = c.x - ((halfw-c.x)*6.8+7)*dt
if c.x < -100 then
c.index = c.index + 1
if c.index*3+1 > #c.names then
mainmenu.start'main'
end
c.x = halfw*2+110
c.timeout = .1
end
end
end
if mainmenu.fadeoutcountdown then
mainmenu.fadeoutcountdown = mainmenu.fadeoutcountdown - dt
if mainmenu.fadeoutcountdown < 0 then mainmenu.start'main' end
end
if mainmenu.fadeincountdown then
mainmenu.fadeincountdown = mainmenu.fadeincountdown - dt
if mainmenu.fadeincountdown < 0 then mainmenu.fadeincountdown=nil end
end
end
love.timer.sleep(15)
end
function mainmenu.draw ()
love.graphics.setColor(0,0,0)
if mainmenu.state==1 then
for i,v in ipairs(mainmenu.options[mainmenu.current]) do
love.graphics.print(v, mainmenu.itemx, mainmenu.ys[i])
end
if mainmenu.current == 'main' then
love.graphics.print("LovelyBigPlanet", mainmenu.logox, 100)
end
love.graphics.setColor(0,255,0)
love.graphics.line(0, mainmenu.lines-13, love.graphics.getWidth(), mainmenu.lines-13)
love.graphics.line(0, mainmenu.lines+5, love.graphics.getWidth(), mainmenu.lines+5)
love.graphics.setColor(0,255,0,50)
love.graphics.rectangle(love.draw_fill, 0, mainmenu.lines-13, love.graphics.getWidth(), 20)
elseif mainmenu.state==3 then
--credits
local c = mainmenu.credits
local basey = love.graphics.getHeight()/4
love.graphics.print(c.names[c.index*3+1], c.x, basey)
love.graphics.print(c.names[c.index*3+2], c.x, basey+20)
love.graphics.print(c.names[c.index*3+3], c.x, basey+60)
love.graphics.setColor(0,255,0)
local x
if c.x >love.graphics.getWidth()/2-50 then
x = love.graphics.getWidth()-c.x-160
else
x = c.x-60
end
love.graphics.line(x, 0, x, love.graphics.getHeight())
love.graphics.line(x+20, 0, x+20, love.graphics.getHeight())
love.graphics.setColor(0,255,0,50)
love.graphics.rectangle(love.draw_fill, x, 0, 20, love.graphics.getHeight())
else
love.graphics.print("Not yet implemented... press escape...", 100, 100)
end
if mainmenu.fadeoutcountdown then
love.graphics.setColor(255,255,255,255-(mainmenu.fadeoutcountdown/mainmenu.fadetime*255))
love.graphics.rectangle(love.draw_fill, 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
end
if mainmenu.fadeincountdown then
love.graphics.setColor(255,255,255,(mainmenu.fadeincountdown/mainmenu.fadetime*255))
love.graphics.rectangle(love.draw_fill, 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
end
end
function mainmenu.keypressed(key)
if mainmenu.state==1 then
if not mainmenu.fadeoutcountdown and not mainmenu.fadeincountdown then
if key == love.key_up then
mainmenu.selected = mainmenu.selected - 1
if mainmenu.selected < 1 then mainmenu.selected = #mainmenu.options[mainmenu.current] end
elseif key == love.key_down then
mainmenu.selected = mainmenu.selected + 1
if mainmenu.selected > #mainmenu.options[mainmenu.current] then mainmenu.selected = 1 end
elseif key == love.key_return or key == love.key_space then
for i=1,#mainmenu.ys do
mainmenu.y_tos[i] = -i*30
end
mainmenu.y_tos[mainmenu.selected] = mainmenu.ys[mainmenu.selected]
mainmenu.fadeoutcountdown = mainmenu.fadetime
end
end
else
if key == love.key_escape then
mainmenu.start("main")
end
end
end
for i,mode in ipairs(love.graphics.getModes()) do
table.insert(mainmenu.options.resolution, 1, mode.width .. 'x' .. mode.height)
table.insert(mainmenu.actions.resolution, 1, function () setRes(mode.width, mode.height, mainmenu.fullscreen); mainmenu.start "settings" end)
end