-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.lua
190 lines (150 loc) · 4 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
--[[
-- A menu class, for holding Buttons and the like.
--]]
Menu = {}
Menu.__index = Menu
function Menu.new()
local self = setmetatable({}, Menu)
self.buttons = {}
self.width = 250
self.pos = {
x = (love.window.getWidth() / 2) - (self.width / 2),
y = love.window.getHeight() / 2
}
return self
end
function Menu:addButton(button)
local prevButton = self.buttons[#self.buttons]
if prevButton == nil then
button.selected = true
button.pos.y = self.pos.y
else
button:align(prevButton)
end
table.insert(self.buttons, button)
end
function Menu:selectFirst()
for k, v in ipairs(self.buttons) do
v.selected = false
end
self.buttons[1].selected = true
end
--[[
-- Dumb circular list kind of implementation. If the last item is selected and
-- then the next one is selected, we rotate towards the beginning. Same thing
-- applies when we navigate before the first item.
--]]
function Menu:focus(diff)
local first = self.buttons[1]
local last = self.buttons[#self.buttons]
for i = 1, #self.buttons do
local btn = self.buttons[i]
if btn.selected then
btn.selected = false
local nextbtn = self.buttons[i+diff]
if nextbtn then
nextbtn.selected = true
else
if diff <= -1 then last.selected = true end
if diff >= 1 then first.selected = true end
end
return
end
end
end
function Menu:fireEvent()
for k, v in ipairs(self.buttons) do
if v.selected and v.action then
v.action()
end
end
end
function Menu:update(dt)
self.pos = {
x = (love.window.getWidth() / 2) - (self.width / 2),
y = love.window.getHeight() / 2
}
for k, v in ipairs(self.buttons) do
v:update(dt)
end
end
function Menu:draw()
-- draw background under buttons
love.graphics.setColor(50, 50, 50)
love.graphics.rectangle("fill", self.pos.x - 30, 0, self.width + 60, love.window.getHeight())
for k, v in ipairs(self.buttons) do
v:draw()
end
end
--[[=========================================================================]]--
--
Button = {}
Button.__index = Button
function Button.new(text)
local self = setmetatable({}, Button)
self.text = text
self.selected = false
self.pos = {
x = 0,
y = 0
}
self.width = 250
self.height = 25
self.action = nil
return self
end
function Button:align(button)
self.pos.y = button.pos.y + button.height + 10
end
function Button:update(dt)
end
function Button:draw()
local x = love.window.getWidth() / 2 - (self.width / 2)
love.graphics.setFont(globals.gameFontLarge)
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("fill", x, self.pos.y, self.width, self.height)
if self.selected then
love.graphics.setColor(255, 0, 0)
else
love.graphics.setColor(255, 255, 255)
end
love.graphics.rectangle("line", x, self.pos.y, self.width, self.height)
love.graphics.print(self.text, x + 5, self.pos.y + 3)
end
--[[=========================================================================]]--
Scoreboard = {}
Scoreboard.__index = Scoreboard
function Scoreboard.new()
local self = setmetatable({}, Scoreboard)
self.height = 100
self.players = nil
return self
end
function Scoreboard:draw()
local w = globals.playableArea.width
local h = globals.playableArea.height
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("fill", 0, h, w, h)
love.graphics.setLineWidth(1)
love.graphics.setColor(255, 255, 255)
love.graphics.line(0, h, love.window.getWidth(), h)
love.graphics.setColor(255, 255, 255)
love.graphics.line(0, h+3, love.window.getWidth(), h+3)
love.graphics.setFont(globals.gameFontLarge)
-- For each player, draw the score.
for k, v in ipairs(self.players) do
local posx = w / 4 * (k - 1)
local w2 = w / 4
local color = { v.color[1] / 4, v.color[2] / 4, v.color[3] / 4 }
love.graphics.setColor(color)
love.graphics.rectangle("fill", posx, h, w2, h)
love.graphics.setColor(0, 0, 0)
love.graphics.print(v.name .. ": " .. v.score, posx + 10 + 1, h+16 + 1)
love.graphics.setColor(v.color)
love.graphics.print(v.name .. ": " .. v.score, posx + 10, h+16)
if v.dead then
end
end
end
function Scoreboard:update(dt)
end