Add level selector / leaderboard browser #7

Merged
Not merged 23 commits from browser into master 2022-09-06 10:08:42 +02:00
2 changed files with 42 additions and 28 deletions
Showing only changes of commit 127161fe9c - Show all commits

View File

@ -1,26 +1,46 @@
local leaderboard = nil
local maps local maps
local mapIndex = 1 local mapIndex = 1
local scrollPos = 1 local scrollPos = 1
local modes = nil local modes = nil
local mode = 1 local mode = 1
local prefMode = nil
local function mapIndexOffset(n) local function mapIndexOffset(n)
return (mapIndex + n + #maps - 1) % #maps + 1 return (mapIndex + n + #maps - 1) % #maps + 1
end end
local function updateMapIndex(n)
mapIndex = mapIndexOffset(n)
-- TODO: keep mode while scrolling through levels
mode = 1
modes = nil
scrollPos = 1
end
local function getMap(offset) local function getMap(offset)
return maps[mapIndexOffset(offset or 0)] return maps[mapIndexOffset(offset or 0)]
end end
local function updateModes()
-- set available modes for this map
modes = {}
for mode, scoreTable in pairs(leaderboard) do
if scoreTable[getMap()] then
table.insert(modes, mode)
end
end
table.sort(modes)
mode = 1
-- select pref mode
for i, m in ipairs(modes) do
if m == prefMode then
mode = i
break
end
end
end
local function updateMapIndex(n)
mapIndex = mapIndexOffset(n)
scrollPos = 1
updateModes()
end
local scalar = 2 local scalar = 2
local hlfScrnWdth = 320 / 2 local hlfScrnWdth = 320 / 2
local mappY = 26 local mappY = 26
@ -280,18 +300,8 @@ local function drawScore(v, i, pos, score)
drawFlags(v, column[4], y, score["flags"]) drawFlags(v, column[4], y, score["flags"])
end end
local function drawBrowser(v, leaderboard) local function drawBrowser(v)
-- set available modes for this map if not leaderboard then return end
-- TODO: set this elsewhere
if modes == nil then
modes = {}
for mode, scoreTable in pairs(leaderboard) do
if scoreTable[getMap()] then
table.insert(modes, mode)
end
end
table.sort(modes)
end
v.fadeScreen(0xFF00, 16) v.fadeScreen(0xFF00, 16)
@ -326,7 +336,9 @@ local function drawBrowser(v, leaderboard)
end end
rawset(_G, "DrawBrowser", drawBrowser) rawset(_G, "DrawBrowser", drawBrowser)
local function initBrowser(leaderboard) local function initBrowser(lb)
leaderboard = lb
-- set mapIndex to current map -- set mapIndex to current map
for i, m in ipairs(maps) do for i, m in ipairs(maps) do
if m == gamemap then if m == gamemap then
@ -335,8 +347,7 @@ local function initBrowser(leaderboard)
end end
end end
modes = nil updateModes()
mode = 1
end end
rawset(_G, "InitBrowser", initBrowser) rawset(_G, "InitBrowser", initBrowser)
@ -393,10 +404,11 @@ local function controller(player)
scrollPos = $ + 1 scrollPos = $ + 1
updateKeyRepeat() updateKeyRepeat()
elseif cmd.buttons & BT_DRIFT then elseif cmd.buttons & BT_DRIFT then
if #modes then
mode = $ % #modes + 1
end
scrollPos = 1 scrollPos = 1
if modes then
mode = $ % #modes + 1
prefMode = modes[mode]
end
updateKeyRepeat() updateKeyRepeat()
elseif cmd.buttons & BT_BRAKE then elseif cmd.buttons & BT_BRAKE then
S_StartSound(nil, 115) S_StartSound(nil, 115)
@ -416,6 +428,8 @@ local function netvars(net)
mapIndex = net($) mapIndex = net($)
modes = net($) modes = net($)
mode = net($) mode = net($)
prefMode = net($)
scrollPos = net($) scrollPos = net($)
leaderboard = net($)
end end
addHook("NetVars", netvars) addHook("NetVars", netvars)

View File

@ -404,7 +404,7 @@ local function browser(player)
print("Browser is not loaded") print("Browser is not loaded")
return return
end end
rawget(_G, "InitBrowser")() rawget(_G, "InitBrowser")(lb)
drawState = DS_BROWSER drawState = DS_BROWSER
end end
COM_AddCommand("levelselect", browser) COM_AddCommand("levelselect", browser)