-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhud_voting.lua
252 lines (207 loc) · 9.39 KB
/
hud_voting.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
-- would not recommend taking this code, it's not the best in the world....
TEXTURE_RANDOM_PAINTING = get_texture_info("random_painting")
voteRandomLevels = {}
local fade = 0
local screenWidth = djui_hud_get_screen_width()
local screenHeight = djui_hud_get_screen_height()
local selectedLevel = 1
local joystickCooldown = 0
local justEnabled = true
local function hud_black_bg()
local theme = get_selected_theme()
djui_hud_set_color(theme.background.r, theme.background.g, theme.background.b, fade)
djui_hud_render_rect(0, 0, screenWidth, screenHeight)
end
local function hud_map_vote()
local theme = get_selected_theme()
if voteRandomLevels[1] == nil then
-- render loading
local text = "Loading.."
local x = (screenWidth - djui_hud_measure_text("Loading...")) / 2
local y = screenHeight / 2
djui_hud_set_color(theme.text.r, theme.text.g, theme.text.b, fade)
djui_hud_print_text(text, x, y, 1)
return
end
-- render top text
local text = "Vote for a Map!"
djui_hud_set_color(theme.text.r, theme.text.g, theme.text.b, fade)
djui_hud_print_text(text, (screenWidth - djui_hud_measure_text(text)) / 2, 50, 1)
local currentMapWinner = -1
local currentMapHighestVotes = -1
-- render 4 paintings
for i = 1, 4 do
-- get number of votes
local votes = 0
for v = 0, MAX_PLAYERS - 1 do
if gNetworkPlayers[v].connected then
if gPlayerSyncTable[v].votingNumber == i then
votes = votes + 1
end
end
end
if votes > currentMapHighestVotes then
currentMapWinner = i
currentMapHighestVotes = votes
end
-- get positions
local x = (screenWidth - (256 * 4 + 100 * 3)) / 2 + (256 + 100) * (i - 1) -- don't ask
local y = (screenHeight - 400) / 2
-- render question mark if there's no painting assigned
djui_hud_set_color(theme.rect.r, theme.rect.g, theme.rect.b, fade)
djui_hud_render_rect(x, y, 256, 256)
djui_hud_set_color(theme.text.r, theme.text.g, theme.text.b, fade)
djui_hud_print_text("?", x + ((128 - djui_hud_measure_text("?"))) - 16, y + 32, 5) -- don't ask #2
djui_hud_set_color(255, 255, 255, fade)
if i ~= 4 and levels[voteRandomLevels[i]].painting ~= nil then
local dimensions = levels[voteRandomLevels[i]].painting.width
djui_hud_render_texture(levels[voteRandomLevels[i]].painting, x, y, 256 / dimensions, 256 / dimensions)
elseif i == 4 then
djui_hud_render_texture(TEXTURE_RANDOM_PAINTING, x, y, 1, 1)
end
y = (screenHeight + 200) / 2
local outlineColor = nil
if gPlayerSyncTable[0].votingNumber == i then
djui_hud_set_color(theme.confirmedRect.r, theme.confirmedRect.g, theme.confirmedRect.b, fade)
outlineColor = theme.confirmedRectOutline
elseif selectedLevel == i then
djui_hud_set_color(theme.hoverRect.r, theme.hoverRect.g, theme.hoverRect.b, fade)
outlineColor = theme.hoverRectOutline
else
djui_hud_set_color(theme.rect.r, theme.rect.g, theme.rect.b, fade)
outlineColor = theme.rectOutline
end
x = (screenWidth - (256 * 4 + 100 * 3)) / 2 + (256 + 100) * (i - 1) - 18.125 -- help me
djui_hud_render_rect_rounded_outlined(x, y, 290, 50, outlineColor.r, outlineColor.g, outlineColor.b, 3, fade)
djui_hud_set_color(theme.text.r, theme.text.g, theme.text.b, fade)
if i ~= 4 then
text = tostring(name_of_level(levels[voteRandomLevels[i]].level, levels[voteRandomLevels[i]].area, levels[voteRandomLevels[i]])) .. ": " .. tostring(votes)
else
text = "Random: " .. tostring(votes)
end
djui_hud_print_text(text, x + 145 - (djui_hud_measure_text(text) / 2), y + 50 / 8, 1) -- why 50 / 8? idk it works (I hate hud math)
end
-- render bottom text
if math.floor(gGlobalSyncTable.displayTimer / 30) > 7 then
text = "You may begin voting in " .. math.floor(gGlobalSyncTable.displayTimer / 30) - 7
elseif math.floor(gGlobalSyncTable.displayTimer / 30) > 2 then
text = "You have " .. tostring(math.floor(gGlobalSyncTable.displayTimer / 30) - 2) .. " seconds remaining"
else
if currentMapWinner == 4 then
text = "A Random Level has been selected!"
else
local level = levels[voteRandomLevels[currentMapWinner]].level
local area = levels[voteRandomLevels[currentMapWinner]].area
text = name_of_level(level, area, levels[voteRandomLevels[currentMapWinner]]) .. " has been selected!"
end
end
djui_hud_set_color(theme.text.r, theme.text.g, theme.text.b, fade)
djui_hud_print_text(text, (screenWidth - djui_hud_measure_text(text)) / 2, screenHeight - 50, 1)
end
local function hud_gamemode()
local theme = get_selected_theme()
local text = "Gamemode is set to " .. get_gamemode_including_random(gGlobalSyncTable.gamemode)
local x = 40
local y = 20
djui_hud_set_color(theme.text.r, theme.text.g, theme.text.b, fade)
djui_hud_print_colored_text(text, x, y, 1, fade)
end
local function hud_modifier()
local theme = get_selected_theme()
local text = "Modifier is set to " .. get_modifier_including_random()
local width = djui_hud_measure_text(strip_hex(text))
local x = screenWidth - width - 40
local y = 20
djui_hud_set_color(theme.text.r, theme.text.g, theme.text.b, fade)
djui_hud_print_colored_text(text, x, y, 1, fade)
end
local function hud_render()
if gGlobalSyncTable.roundState ~= ROUND_VOTING then
justEnabled = true
selectedLevel = 1
gPlayerSyncTable[0].votingNumber = 0
fade = 0
voteRandomLevels = {}
return
end
if justEnabled then
if network_is_server() then
while voteRandomLevels[3] == nil do
local randomLevel = 0
randomLevel = math.random(1, #levels)
while table.contains(voteRandomLevels, randomLevel) or gGlobalSyncTable.blacklistedCourses[randomLevel] == true or randomLevel == gGlobalSyncTable.selectedLevel do
randomLevel = math.random(1, #levels)
end
table.insert(voteRandomLevels, randomLevel)
end
-- send over our levels
for i = 1, MAX_PLAYERS - 1 do
if gNetworkPlayers[i].connected then
-- create packet from scratch instead of using generic method for more slots
local p = {packetType = PACKET_TYPE_SEND_LEVELS, level1 = voteRandomLevels[1], level2 = voteRandomLevels[2], level3 = voteRandomLevels[3]}
-- send to players
send_packet(network_global_index_from_local(i), p)
end
end
end
justEnabled = false
end
if fade < 255 and gGlobalSyncTable.displayTimer > 0 then
fade = fade + 20
if fade >= 255 then fade = 255 end
elseif fade > 0 and gGlobalSyncTable.displayTimer <= 0 then
fade = fade - 20
if fade < 0 then fade = 0 end
end
screenWidth = djui_hud_get_screen_width()
screenHeight = djui_hud_get_screen_height()
djui_hud_set_resolution(RESOLUTION_DJUI)
djui_hud_set_font(djui_menu_get_font())
hud_black_bg()
hud_map_vote()
hud_modifier()
hud_gamemode()
end
---@param m MarioState
local function mario_update(m)
if m.playerIndex ~= 0 or gGlobalSyncTable.roundState ~= ROUND_VOTING then return end
joystickCooldown = joystickCooldown - 1
-- if our stick is at 0, then set joystickCooldown to 0
if m.controller.stickX == 0 then joystickCooldown = 0 end
if showSettings then return end
if isPaused then return end
m.freeze = 1
if joystickCooldown <= 0 and gPlayerSyncTable[0].votingNumber == 0 then
-- check where our stick is
if m.controller.stickX > 0.5
or m.controller.buttonPressed & R_JPAD ~= 0 then
-- moving right, move our selection right
selectedLevel = selectedLevel + 1
if selectedLevel > 4 then selectedLevel = 4 end
joystickCooldown = 0.2 * 30
play_sound(SOUND_MENU_MESSAGE_DISAPPEAR, gGlobalSoundSource)
end
if m.controller.stickX < -0.5
or m.controller.buttonPressed & L_JPAD ~= 0 then
-- moving left, move our selection left
selectedLevel = selectedLevel - 1
if selectedLevel < 1 then selectedLevel = 1 end
joystickCooldown = 0.2 * 30
play_sound(SOUND_MENU_MESSAGE_DISAPPEAR, gGlobalSoundSource)
end
end
if m.controller.buttonPressed & A_BUTTON ~= 0
and math.floor(gGlobalSyncTable.displayTimer / 30) > 2
and math.floor(gGlobalSyncTable.displayTimer / 30) <= 7
and gPlayerSyncTable[0].votingNumber == 0 then
gPlayerSyncTable[0].votingNumber = selectedLevel
play_sound(SOUND_MENU_CLICK_FILE_SELECT, gGlobalSoundSource)
elseif m.controller.buttonPressed & B_BUTTON ~= 0
and math.floor(gGlobalSyncTable.displayTimer / 30) > 2
and math.floor(gGlobalSyncTable.displayTimer / 30) <= 7 then
gPlayerSyncTable[0].votingNumber = 0
play_sound(SOUND_MENU_CLICK_FILE_SELECT, gGlobalSoundSource)
end
end
hook_event(HOOK_ON_HUD_RENDER, hud_render)
hook_event(HOOK_MARIO_UPDATE, mario_update)