Files
YataiDONNijiiro/Scripts/result/result_medal.lua
T

227 lines
10 KiB
Lua

ResultMedal = {}
ResultMedal.__index = ResultMedal
local FRAME_MS = 1000 / 60
local UPDATE_F = 65
local CLOSE_G = 214
local WHITE, BLACK, MAROON = { 255, 255, 255, 255 }, { 0, 0, 0, 255 }, { 97, 11, 28, 255 }
local SEASONS = { "winter", "winter", "spring", "spring", "spring", "summer", "summer", "summer",
"autumn", "autumn", "autumn", "winter" }
local SEASON_INDEX = { spring = 1, summer = 2, autumn = 3, winter = 4 }
local SIZE = {
panel = { 432, 360 }, coin = { 88, 88 }, sparkle = { 40, 40 }, number = { 48, 48 }, bar_glow = { 400, 88 },
add_pill = { 88, 64 }, digit = { 24, 32 },
}
local function keys(f, k)
if f <= k[1][1] then return k[1][2] end
for i = 2, #k do
local f1, v1 = k[i][1], k[i][2]
if f <= f1 then
local f0, v0 = k[i - 1][1], k[i - 1][2]
return v0 + (v1 - v0) * (f - f0) / (f1 - f0)
end
end
return k[#k][2]
end
local function ease_in(f, f0, f1, v0, v1)
local u = math.max(0, math.min(1, (f - f0) / (f1 - f0)))
return v0 + (v1 - v0) * u * u
end
-- the counter opens by stretching vertically from a line, then waits before counting the new medals in
local POP_SY = { { 0, 0.01 }, { 8, 0.981 }, { 9, 1.048 }, { 10, 1.103 }, { 12, 1.176 }, { 14, 1.2 }, { 17, 1.125 },
{ 19, 0.992 }, { 20, 0.9 }, { 22, 0.94 }, { 24, 0.97 }, { 27, 1 } }
-- a second after the count settles it stretches once, then squashes back into a line
local CLOSE_SY = { { 0, 1 }, { 1, 1.072 }, { 5, 1.2 }, { 7, 1.179 }, { 8, 1.152 }, { 15, 0.671 }, { 16, 0.56 },
{ 20, 0.01 } }
-- below, frames are counted from the update (the arcade's `update` label)
local COIN_SY = { { 32, 1 }, { 33, 0.8 }, { 35, 1.2 }, { 37, 0.9 }, { 39, 1 } }
local COIN_DY = { { 32, 0 }, { 33, 6.9 }, { 35, -2 }, { 37, 4 }, { 39, 0 }, { 41, 2 }, { 43, 0 } }
local FLASH_A = { { 32, 0 }, { 33, 1 }, { 43, 1 }, { 74, 0 }, { 75, 0.055 }, { 84, 0.531 }, { 93, 0 } }
local FLASH_S = { { 74, 1 }, { 75, 1.03 }, { 84, 1.3 }, { 93, 1.4 } }
local NUMBER_S = { { 37, 1 }, { 38, 1.108 }, { 42, 1.3 }, { 44, 1.252 }, { 46, 1.108 }, { 47, 1 } }
local DIGIT_POP = { { 0, 1.312 }, { 2, 1.078 }, { 4, 1 } }
local BAR_GLOW_A = { { 77, 0 }, { 87, 1 }, { 88, 0.891 }, { 96, 0 } }
local PILL_S = { { 86, 0.5 }, { 88, 0.806 }, { 89, 1.025 }, { 91, 1.2 }, { 94, 1.128 }, { 96, 1 } }
local PILL_A = { { 86, 0 }, { 88, 0.438 }, { 90, 0.938 }, { 91, 1 } }
local PILL_DIGIT_A = { { 96, 0 }, { 97, 0.2 }, { 101, 1 } }
-- stars thrown up from the coin when it lands: { x, scale, y keys, alpha keys }
local BURST = {
{ 31, 1, { { 32, 14 }, { 45, -66 }, { 46, -67.9 }, { 55, -76 } },
{ { 31, 0 }, { 32, 1 }, { 45, 1 }, { 46, 0.809 }, { 55, 0 } } },
{ -37, 1, { { 36, 14 }, { 49, -66 }, { 50, -67.9 }, { 59, -76 } },
{ { 35, 0 }, { 36, 1 }, { 49, 1 }, { 50, 0.809 }, { 59, 0 } } },
{ -4, 0.75, { { 32, -0.25 }, { 38, -24 }, { 47, -51.1 }, { 50, -57.85 }, { 54, -65.25 } },
{ { 31, 0 }, { 32, 1 }, { 47, 1 }, { 48, 0.848 }, { 50, 0.566 }, { 54, 0.098 }, { 55, 0 } } },
{ 38, 0.75, { { 38, -0.25 }, { 51, -44.25 }, { 52, -45.6 }, { 61, -51.75 } },
{ { 37, 0 }, { 38, 1 }, { 51, 1 }, { 52, 0.809 }, { 61, 0 } } },
{ -49, 0.75, { { 40, -0.25 }, { 52, -42.4 }, { 54, -45.6 }, { 62, -51.6 } },
{ { 39, 0 }, { 40, 1 }, { 53, 1 }, { 54, 0.809 }, { 62, 0 } } },
}
-- later twinkle pairs: { start, x, y }, each blooms at (-102, -65) then at (-92, -55)
local TWINKLES = { { 70, 31, 46 }, { 75, 132, 6 }, { 81, 152, 77 } }
local TWINKLE_S = { { 0, 0.5 }, { 14, 1.2 } }
local TWINKLE_A = { { 8, 1 }, { 14, 0 } }
local function split(s, sep)
local out = {}
for part in (s .. sep):gmatch("(.-)" .. sep:gsub("%p", "%%%0")) do out[#out + 1] = part end
return out
end
function ResultMedal.new(medals, right)
local self = setmetatable({}, ResultMedal)
self.t0 = tex.get_current_ms()
local season = SEASONS[medals.month or 1]
self.panel = tex.get_texture("result/medal/panel_" .. season)
self.coin = tex.get_texture("result/medal/coin_" .. season)
self.coin_glow = tex.get_texture("result/medal/coin_glow")
self.coin_flash = tex.get_texture("result/medal/coin_flash")
self.sparkle = tex.get_texture("result/medal/sparkle")
self.number = tex.get_texture("result/medal/number")
self.bar_glow = tex.get_texture("result/medal/bar_glow")
self.add_pill = tex.get_texture("result/medal/add_pill")
self.plus = tex.get_texture("result/medal/plus")
self.digit = tex.get_texture("result/medal/digit")
self.add = math.max(0, math.min(99, medals.add or 0))
self.after = math.max(0, math.min(999, medals.total or self.add))
self.before = math.max(0, self.after - self.add)
local S = tex.get_screen_scale()
local season_name = split(tex.skin_text("result_medal_seasons"), "|")[SEASON_INDEX[season]] or ""
local title_cfg = tex.get_skin_config("result_medal") or { font_size = 22 }
self.title = text.create_raw_text((tex.skin_text("result_medal"):gsub("{0}", season_name)),
math.floor(title_cfg.font_size), MAROON, WHITE, false, 3 / S)
local body_cfg = tex.get_skin_config("result_medal_body") or { font_size = 22 }
self.body = {}
for _, line in ipairs(split(tex.skin_text("result_medal_body"), "\n")) do
self.body[#self.body + 1] = text.create_raw_text(line, math.floor(body_cfg.font_size), WHITE, BLACK, false,
3 / S)
end
return self
end
function ResultMedal:done()
return (tex.get_current_ms() - self.t0) / FRAME_MS - UPDATE_F >= CLOSE_G + 20
end
-- draws texture t centred on (cx, cy) with separate x / y scale
local function put(t, size, cx, cy, sx, sy, fade, blend, frame)
if not t or fade <= 0 then return end
local w, h = size[1], size[2]
tex.draw_texture(t, { x = cx - w * sx / 2, y = cy - h * sy / 2, x2 = w * (sx - 1), y2 = h * (sy - 1),
fade = fade, blend = blend, frame = frame })
end
function ResultMedal:draw_number(value, cx, cy, s, sy, pop)
local str = tostring(value)
for i = 1, #str do
local slot = 3 - #str + i
local ds = s * pop
put(self.number, SIZE.number, cx + (slot - 2) * 33 * s, cy, ds, ds * sy, 1, nil, tonumber(str:sub(i, i)))
end
end
function ResultMedal:draw_add(ox, oy, g, sy)
sy = sy or 1
local a = keys(g, PILL_A)
if a <= 0 then return end
local s = keys(g, PILL_S)
put(self.add_pill, SIZE.add_pill, ox + 146, oy - 63 * sy, s, s * sy, a)
local da = keys(g, PILL_DIGIT_A)
if da <= 0 then return end
local pop = keys(g - 97, DIGIT_POP)
local str = tostring(self.add)
local plus_x = #str == 1 and 137 or 126
put(self.plus, SIZE.digit, ox + plus_x, oy - 62 * sy, 1, sy, da)
for i = 1, #str do
local x = #str == 1 and 159 or (148 + (i - 1) * 21)
put(self.digit, SIZE.digit, ox + x, oy - 61 * sy, pop, pop * sy, da, nil, tonumber(str:sub(i, i)))
end
end
function ResultMedal:draw_coin(cx, cy, g)
if g >= 5 and g <= 33 then
local y, sx, sy
if g <= 10 then
local u = (g - 5) / 5
y, sx = -83 + (-132.85 + 83) * u, 1 + 0.591 * u
sy = sx
else
y, sx, sy = ease_in(g, 11, 33, -130.95, -1.95), ease_in(g, 11, 33, 1.523, 0.455),
ease_in(g, 11, 33, 1.523, 0.705)
end
put(self.coin, SIZE.coin, cx, cy + y, sx, sy, keys(g, { { 5, 0 }, { 10, 1 } }))
if g <= 15 then
local gs = g <= 10 and sx or keys(g, { { 11, 1.523 }, { 15, 1.25 } })
local gy = g <= 10 and y or keys(g, { { 11, -130.95 }, { 15, -123 } })
put(self.coin_glow, SIZE.coin, cx, cy + gy, gs, gs,
keys(g, { { 5, 0 }, { 10, 0.73 }, { 11, 0.586 }, { 15, 0 } }))
end
end
local sy, dy = keys(g, COIN_SY), keys(g, COIN_DY)
put(self.coin, SIZE.coin, cx, cy + dy, 1, sy, 1)
if g >= 33 then
local fs = keys(g, FLASH_S)
put(self.coin_flash, SIZE.coin, cx, cy + dy, fs, fs * sy, keys(g, FLASH_A), "additive")
end
for _, b in ipairs(BURST) do
put(self.sparkle, SIZE.sparkle, cx + b[1], cy + keys(g, b[3]), b[2], b[2], keys(g, b[4]))
end
for _, t in ipairs(TWINKLES) do
local h = g - t[1]
for k, at in ipairs({ { -102, -65 }, { -92, -55 } }) do
local hk = h - (k - 1) * 24
if hk >= 0 and hk <= 14 then
local s = keys(hk, TWINKLE_S)
put(self.sparkle, SIZE.sparkle, cx + t[2] + at[1], cy + t[3] + at[2], s, s, keys(hk, TWINKLE_A))
end
end
end
end
function ResultMedal:draw(cx, cy)
local f = (tex.get_current_ms() - self.t0) / FRAME_MS
local ox, oy = cx + 201, 783
local g = f - UPDATE_F
if g >= CLOSE_G + 20 then return end
local closing = g >= CLOSE_G
local SY = closing and keys(g - CLOSE_G, CLOSE_SY) or keys(f, POP_SY)
local function y(dy) return oy + dy * SY end
put(self.panel, SIZE.panel, ox + 5, y(4), 1, SY, 1)
local fade = math.min(1, SY)
if self.title then
self.title:draw({ x = math.floor(ox - self.title.width / 2), y = math.floor(y(-128) - self.title.height / 2),
fade = fade })
end
for i, line in ipairs(self.body) do
local ly = 65 + (i - (#self.body + 1) / 2) * 40
line:draw({ x = math.floor(ox - line.width / 2), y = math.floor(y(ly) - line.height / 2), fade = fade })
end
if g < 0 or closing then
put(self.coin, SIZE.coin, ox - 154, y(-64), 1, SY, 1)
self:draw_number(closing and self.after or self.before, ox + 4, y(-62), 1, SY, 1)
if closing then self:draw_add(ox, oy, 154, SY) end
return
end
local ns = keys(g, NUMBER_S)
if g < 42 then
self:draw_number(self.before, ox + 4, oy - 62, ns, 1, 1)
else
self:draw_number(self.after, ox + 4, oy - 62, ns, 1, keys(g - 42, DIGIT_POP))
end
self:draw_add(ox, oy, g)
self:draw_coin(ox - 154, oy - 64, g)
put(self.bar_glow, SIZE.bar_glow, ox, oy - 63, 1, 1, keys(g, BAR_GLOW_A), "additive")
end
return ResultMedal