-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.lua
213 lines (182 loc) · 6.22 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
function Button(x, y, tox, toy, text, fn, fnargs)
return {
x = x,
y = y,
tox = tox,
toy = toy,
text = text,
width = 400,
height = 75,
selcolor = {1.0, 0.5, 0.5},
bgcolor = {0.7, 0.7, 0.7},
fgcolor = {0, 0, 0},
font = fonts.large,
selected = false,
fn = fn,
fn_args = fn_args,
draw = function(self)
local bgcolor = self.bgcolor
if self.selected then
bgcolor = self.selcolor
end
love.graphics.setColor(bgcolor[1], bgcolor[2], bgcolor[3])
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
love.graphics.setColor(self.fgcolor[1], self.fgcolor[2], self.fgcolor[3])
text = love.graphics.newText(self.font, self.text)
love.graphics.draw(text, self.x + self.tox, self.y + self.toy)
end,
update = function(self, dt)
end,
}
end
local menuHide = function()
_G.menu.show = false
end
local menuOptions = function()
_G.menu.screen = "options"
end
local menuOptionsBack = function()
_G.menu.screen = "main"
end
local spaceVertically = function(buttons, spacing)
local nextY = buttons[1].y
for num, btn in pairs(buttons) do
btn.x = buttons[1].x
btn.y = nextY
btn.width = buttons[1].width
btn.height = buttons[1].height
nextY = nextY + btn.height + spacing
end
end
local label = function(x, y, text)
--
-- Hacky: labels are just buttons that we never select
--
local lbl = Button(x, y, 10, 15, text)
lbl.width = 600
lbl.fgcolor = {1, 1, 1}
lbl.bgcolor = {0.2, 0.2, 0.2}
return lbl
end
local wrap = function(value, max)
if value > max then
return 1
elseif value < 1 then
return max
end
return value
end
function Menu()
local planes = {}
for key, val in pairs(_G.planes) do
table.insert(planes, key)
end
local currentPlane = 1
for key, val in pairs(planes) do
if val == "Су-27" then
currentPlane = key
break
end
end
local maps = {}
for key, val in pairs(_G.terrain) do
table.insert(maps, key)
end
local currentMap = 1
local lastEvent = love.timer.getTime()
local buttons = {}
local spacing = 20
buttons["main"] = {}
buttons["options"] = {}
local btnGo = Button(100, 100, 10, 15, "Полетели!", menuHide)
local btnOptions = Button(0, 0, 10, 15, "Настройки", menuOptions)
local btnQuit = Button(0, 0, 10, 15, "Выйти", love.event.quit)
table.insert(buttons["main"], btnGo)
table.insert(buttons["main"], btnOptions)
table.insert(buttons["main"], btnQuit)
spaceVertically(buttons["main"], spacing)
local changePlane = function() currentPlane = wrap(currentPlane + 1, #planes) end
local changeMap = function() currentMap = wrap(currentMap + 1, #maps) end
local toggleSound = function() sfx.enabled = not sfx.enabled end
local btnChangePlane = Button(btnGo.x, btnGo.y, 10, 15, "Сменить самолёт", changePlane)
local btnChangeMap = Button(0, 0, 10, 15, "Сменить карту", changeMap)
local btnToggleSound = Button(0, 0, 10, 15, "Звук", toggleSound)
local btnOptionsBack = Button(0, 0, 10, 15, "Назад", menuOptionsBack)
table.insert(buttons["options"], btnChangePlane)
table.insert(buttons["options"], btnChangeMap)
table.insert(buttons["options"], btnToggleSound)
table.insert(buttons["options"], btnOptionsBack)
spaceVertically(buttons["options"], spacing)
local sel = {}
sel["main"] = 1
sel["options"] = 1
local labels = {}
labels["main"] = {}
labels["options"] = {}
local lblCurrentPlane = label(btnGo.x + btnGo.width + spacing, btnGo.y, planes[currentPlane])
local lblCurrentMap = label(0, 0, maps[currentMap])
local lblSound = label(0, 0, "")
table.insert(labels["options"], lblCurrentPlane)
table.insert(labels["options"], lblCurrentMap)
table.insert(labels["options"], lblSound)
spaceVertically(labels["options"], spacing)
return {
show = true,
screen = "main",
buttons = buttons,
draw = function(self)
for key, btn in pairs(buttons[self.screen]) do
btn.selected = key == sel[self.screen]
btn:draw()
end
for key, lbl in pairs(labels[self.screen]) do
lbl:draw()
end
end,
update = function(self, dt)
lblCurrentPlane.text = planes[currentPlane]
lblCurrentMap.text = maps[currentMap]
if sfx.enabled then
lblSound.text = "Включить"
else
lblSound.text = "Выключить"
end
end,
keypressed = function(self, key, scancode, isrepeat)
--
-- these keys get handled uniformly across all screens
--
if key == "down" then
sel[self.screen] = wrap(sel[self.screen] + 1, #buttons[self.screen])
sfx:playEffect("ping")
elseif key == "up" then
sel[self.screen] = wrap(sel[self.screen] - 1, #buttons[self.screen])
sfx:playEffect("ping")
elseif key == "space" or key == "return" then
local idx = sel[self.screen]
local btn = buttons[self.screen][idx]
if btn.fn then
btn.fn(btn.fn_args)
sfx:playEffect("ping")
end
end
--
-- screen-specific shortcuts
--
if self.screen == "main" then
if key == "escape" then
menuHide()
sfx:playEffect("ping")
end
elseif self.screen == "options" then
if key == "escape" then
menuOptionsBack()
sfx:playEffect("ping")
end
end
end,
getCurrentPlane = function(self) return planes[currentPlane] end,
getCurrentMap = function(self) return maps[currentMap] end,
}
end
return Menu