169 lines
6.4 KiB
Lua
169 lines
6.4 KiB
Lua
RankingScene = {}
|
|
RankingScene.__index = RankingScene
|
|
|
|
-- Taiko 14 ranking.swg (総合ランキング, 1人プレイ) with Hiroba's player leaderboard: rows dash in from the right
|
|
-- counting down to 1位, the list scrolls once it is full. Rating (as on the site) stands in for the score, the
|
|
-- title plate is the rating band. Positions are the swg's 640x480 px (K scales them), 60 fps frames.
|
|
local PATH = "title/ranking/"
|
|
local FRAME_MS = 1000 / 60
|
|
local WHITE, BLACK = { 255, 255, 255, 255 }, { 0, 0, 0, 255 }
|
|
|
|
local ROW_X, ROW_TOP, ROW_GAP, VISIBLE = 8, 66, 80, 5
|
|
local START_AT, ROW_EVERY, SLIDE_LEN, SCROLL_LEN = 40, 40, 12, 14
|
|
local HOLD, FADE_LEN = 240, 20
|
|
local TITLE_STEP = 1.2
|
|
local WAIT_FOR_DATA = 120
|
|
|
|
local function clamp(v, lo, hi) return math.max(lo, math.min(hi, v)) end
|
|
local function lerp(a, b, p) return a + (b - a) * p end
|
|
local function ease_out(p) return 1 - (1 - p) * (1 - p) end
|
|
local function seg(t, from, len) return clamp((t - from) / len, 0, 1) end
|
|
|
|
local function snd(name, kind)
|
|
if audio.has_sound(name) then audio.play_sound(name, kind or "sound") end
|
|
end
|
|
|
|
function RankingScene.new()
|
|
local self = setmetatable({}, RankingScene)
|
|
self.K = tex.get_screen_width() / 640
|
|
self.tex, self.info = {}, {}
|
|
for _, name in ipairs({ "back", "header_bar", "header", "solo", "plate", "dash", "sparkle", "place",
|
|
"rank_title" }) do
|
|
self.tex[name] = tex.get_texture(PATH .. name)
|
|
local info = tex.get_texture_info("ranking", name)
|
|
self.info[name] = info and { info.x[1] or 0, info.y[1] or 0, info.x2[1] or 0, info.y2[1] or 0 }
|
|
or { 0, 0, 0, 0 }
|
|
end
|
|
self.t0 = nil
|
|
self.f = 0
|
|
self.rows = nil
|
|
self.waited = 0
|
|
self.fired = {}
|
|
self.finished = false
|
|
self:fetch()
|
|
return self
|
|
end
|
|
|
|
function RankingScene:fetch()
|
|
local list = network.top_players()
|
|
if not list or #list == 0 then return end
|
|
self.rows = {}
|
|
local size = math.floor(26 * self.K)
|
|
for i, player in ipairs(list) do
|
|
if i > 20 then break end
|
|
self.rows[i] = {
|
|
place = i,
|
|
name = text.create_raw_text(player.name, size, WHITE, BLACK, false, 7),
|
|
rating = text.create_raw_text(string.format("%.2f", player.rating), size, WHITE, BLACK, false, 7),
|
|
title = clamp(math.floor(player.rating / TITLE_STEP), 0, 9),
|
|
}
|
|
end
|
|
-- counted down: the last place in comes first
|
|
self.order = {}
|
|
for i = #self.rows, 1, -1 do self.order[#self.order + 1] = self.rows[i] end
|
|
self.end_at = START_AT + (#self.order - 1) * ROW_EVERY + SLIDE_LEN + HOLD
|
|
end
|
|
|
|
function RankingScene:tl(name, x, y, o)
|
|
o = o or {}
|
|
local t = self.tex[name]
|
|
if not t then return end
|
|
local i = self.info[name]
|
|
tex.draw_texture(t, { frame = o.frame or 0, x = x * self.K - i[1], y = y * self.K - i[2], fade = o.fade })
|
|
end
|
|
|
|
-- text at (x, cy) swg px, left- or right-aligned, shrunk to `width`
|
|
function RankingScene:text(t, x, cy, width, right, fade)
|
|
local s = math.min(1, width * self.K / math.max(1, t.width))
|
|
local w = t.width * s
|
|
local dx = right and x * self.K - w or x * self.K
|
|
t:draw({ x = dx, y = cy * self.K - t.height * s / 2, scale = s, fade = fade })
|
|
end
|
|
|
|
function RankingScene:once(key, cond, fn)
|
|
if cond and not self.fired[key] then
|
|
self.fired[key] = true
|
|
fn()
|
|
end
|
|
end
|
|
|
|
function RankingScene:update(current_ms)
|
|
if not self.rows then
|
|
self:fetch()
|
|
if not self.rows then
|
|
self.waited = self.waited + 1
|
|
if self.waited > WAIT_FOR_DATA then self.finished = true end
|
|
return
|
|
end
|
|
end
|
|
if not self.t0 then self.t0 = current_ms end
|
|
self.f = (current_ms - self.t0) / FRAME_MS
|
|
local f = self.f
|
|
self:once("bgm", true, function() snd("rk_bgm", "music") end)
|
|
self:once("title", f >= 10, function() snd("rk_title", "voice") end)
|
|
for k = 1, #self.order do
|
|
local at = START_AT + (k - 1) * ROW_EVERY
|
|
self:once("in" .. k, f >= at, function() snd("rk_rank_in") end)
|
|
if k > VISIBLE then self:once("slide" .. k, f >= at, function() snd("rk_slide") end) end
|
|
end
|
|
self:once("first", f >= START_AT + (#self.order - 1) * ROW_EVERY + SLIDE_LEN,
|
|
function() snd("rk_first", "voice") end)
|
|
if f >= self.end_at + FADE_LEN then
|
|
if audio.is_sound_playing("rk_bgm") then audio.stop_sound("rk_bgm") end
|
|
self.finished = true
|
|
end
|
|
end
|
|
|
|
function RankingScene:is_finished() return self.finished end
|
|
function RankingScene:close()
|
|
if audio.is_sound_playing("rk_bgm") then audio.stop_sound("rk_bgm") end
|
|
end
|
|
|
|
-- rows shown so far, and how far the list has scrolled (rows)
|
|
function RankingScene:progress()
|
|
local f = self.f - START_AT
|
|
if f < 0 then return 0, 0 end
|
|
local shown = math.min(#self.order, math.floor(f / ROW_EVERY) + 1)
|
|
local scroll = math.max(0, shown - VISIBLE)
|
|
if shown > VISIBLE then
|
|
local since = f - (shown - 1) * ROW_EVERY
|
|
scroll = scroll - 1 + ease_out(seg(since, 0, SCROLL_LEN))
|
|
end
|
|
return shown, scroll
|
|
end
|
|
|
|
function RankingScene:draw_row(row, k, y)
|
|
local e = self.f - (START_AT + (k - 1) * ROW_EVERY)
|
|
local x = ROW_X + lerp(640, 0, ease_out(seg(e, 0, SLIDE_LEN)))
|
|
local placed = e >= SLIDE_LEN
|
|
local plate = not placed and 0 or (row.place == 1 and 2 or 1)
|
|
self:tl("plate", x, y, { frame = plate })
|
|
self:tl("dash", x - 38, y - 15, { frame = math.min(4, math.floor(math.max(0, e) / 4)) })
|
|
self:tl("place", x + 18, y + 24, { frame = row.place - 1 })
|
|
self:text(row.name, x + 150, y + 40, 220, false)
|
|
self:tl("rank_title", x + 382, y + 24, { frame = row.title })
|
|
self:text(row.rating, x + 600, y + 40, 100, true)
|
|
if e >= SLIDE_LEN and e < SLIDE_LEN + 24 then
|
|
local p = (e - SLIDE_LEN) / 24
|
|
self:tl("sparkle", x + 120 + 380 * p, y + 4, { frame = 0, fade = 1 - p })
|
|
self:tl("sparkle", x + 460 - 300 * p, y + 20, { frame = 1, fade = 1 - p })
|
|
end
|
|
end
|
|
|
|
function RankingScene:draw()
|
|
if not self.rows then return end
|
|
self:tl("back", 0, 0)
|
|
local shown, scroll = self:progress()
|
|
for k = 1, shown do
|
|
local y = ROW_TOP + (k - 1 - scroll) * ROW_GAP
|
|
if y > ROW_TOP - ROW_GAP and y < 480 then self:draw_row(self.order[k], k, y) end
|
|
end
|
|
self:tl("header_bar", 0, 0)
|
|
self:tl("header", 10, 4)
|
|
self:tl("solo", 504, 8)
|
|
local fade = seg(self.f, self.end_at or 1e9, FADE_LEN)
|
|
if fade > 0 then
|
|
tex.draw_rect(0, 0, tex.get_screen_width(), tex.get_screen_height(), 0, 0, 0, math.floor(fade * 255))
|
|
end
|
|
end
|