145 lines
5.3 KiB
Lua
145 lines
5.3 KiB
Lua
PlayerBoard = {}
|
|
|
|
local S = 0.95
|
|
local OX, OY = 28.8, 313.0 -- screen position of the top texture's top-left
|
|
local DIGIT_O = 40 -- digit textures are 80x80 around the sprite's origin
|
|
local COUNTERS = { -- {x, y} in top-texture coords; ranks 1..7 then crowns 1..3
|
|
rank = { { 89, 113 }, { 181, 113 }, { 279, 113 }, { 89, 75 }, { 181, 75 }, { 279, 75 }, { 279, 36 } },
|
|
crown = { { 89, 156 }, { 181, 156 }, { 279, 156 } },
|
|
}
|
|
|
|
-- main_instance "defalut_open": the record board flips open around its centre, scale x per frame
|
|
local OPEN = { 0.02, 0.05, 0.13, 0.27, 0.46, 0.7, 1 }
|
|
local TOP_CX = 166
|
|
|
|
local T = {}
|
|
local function tx(name)
|
|
local t = T[name]
|
|
if t == nil then
|
|
local id = tex.get_texture("player_board/" .. name)
|
|
local info = id and tex.get_texture_info("player_board", name)
|
|
t = id and { t = id, w = info and info.width or 0, h = info and info.height or 0 } or false
|
|
T[name] = t
|
|
end
|
|
return t or nil
|
|
end
|
|
|
|
-- board picked on the site: 0-4 = kantan/futsuu/muzukashii/onii/ura, 5 = the old oni+ura plate ("top")
|
|
local BOARD_NAMES = { "kantan", "futsuu", "muzukashii", "onii", "ura" }
|
|
local function board_index()
|
|
local idx = tex.player_board and tex.player_board(1) or 3
|
|
return (idx < 0 or idx > 5) and 3 or idx
|
|
end
|
|
local function board_texture()
|
|
local idx = board_index()
|
|
if idx == 5 then return tx("top") end
|
|
return tx("top_" .. BOARD_NAMES[idx + 1]) or tx("top")
|
|
end
|
|
|
|
local function enabled()
|
|
if T.on == nil then T.on = tex.get_skin_config("player_board_on") ~= nil end
|
|
return T.on
|
|
end
|
|
|
|
local open = { last = -1e9 }
|
|
local rec = { ms = -1e9 }
|
|
|
|
local function record_counts()
|
|
local now = tex.get_current_ms()
|
|
if not tex.record_counts then return nil end
|
|
local idx = board_index()
|
|
local lo, hi = idx, idx
|
|
if idx == 5 then lo, hi = 3, 4 end -- the oni+ura plate counts both
|
|
if now - rec.ms > 5000 or now < rec.ms or rec.idx ~= idx then -- a play or a board change: refresh
|
|
rec.v = tex.record_counts(1, lo, hi)
|
|
rec.ms = now
|
|
rec.idx = idx
|
|
end
|
|
return rec.v
|
|
end
|
|
|
|
-- Don medal chip under the board (the cabinet draws it right under the board's bottom edge, at 0.85 of the
|
|
-- board; the digits sit in the chip's right half). Values measured off the pack's own render (frame 25).
|
|
local MEDAL = {
|
|
x = 0, y = 192, -- board-local top-left of the chip
|
|
scale = 0.85, -- chip size relative to the board
|
|
right = 152, cy = 44, -- digit area inside the chip texture (168x72)
|
|
digit = 0.38, -- of the 80x80 digit art
|
|
}
|
|
local medal_text
|
|
|
|
local function medal_chip(medals, max)
|
|
local complete = max > 0 and medals >= max
|
|
local tier = 1
|
|
if max > 0 then
|
|
tier = math.min(4, math.max(1, math.floor(medals * 4 / max) + 1))
|
|
end
|
|
return tx("medal_" .. tier .. (complete and "_complete" or "")), complete
|
|
end
|
|
|
|
local function draw_medals()
|
|
if not tex.medals then return end
|
|
local medals = tex.medals(1)
|
|
local max = tex.medals_max and tex.medals_max() or 0
|
|
local chip, complete = medal_chip(medals, max)
|
|
if not chip then return end
|
|
local cs = S * MEDAL.scale
|
|
local x0, y0 = OX + MEDAL.x * S, OY + MEDAL.y * S
|
|
tex.draw_texture(chip.t, { x = x0, y = y0, scale = cs })
|
|
|
|
if complete then
|
|
if not medal_text then
|
|
medal_text = text.create_raw_text(tex.skin_text("medal_complete"), 34,
|
|
{ 255, 255, 255, 255 }, { 60, 40, 10, 255 }, false, 4, 2.0)
|
|
end
|
|
medal_text:draw({ x = x0 + (MEDAL.right - medal_text.width) * cs,
|
|
y = y0 + (MEDAL.cy - medal_text.height / 2) * cs, scale = cs })
|
|
return
|
|
end
|
|
local t = tx("medal_digit")
|
|
if not t then return end
|
|
local str = tostring(math.min(9999, math.max(0, medals)))
|
|
local dw = 80 * MEDAL.digit
|
|
for i = 1, #str do
|
|
local dx = MEDAL.right - dw * (#str - i) - dw
|
|
tex.draw_texture(t.t, { x = x0 + dx * cs, y = y0 + (MEDAL.cy - dw / 2) * cs,
|
|
scale = cs * MEDAL.digit, frame = str:byte(i) - 48 })
|
|
end
|
|
end
|
|
|
|
local function draw_counter(v, cx, cy)
|
|
local s = tostring(math.max(0, math.min(9999, math.floor(v or 0))))
|
|
local k = #s
|
|
local t = tx("digit")
|
|
if not t then return end
|
|
for i = 1, k do
|
|
local x = cx + 7 * (k - 1) - 14 * (i - 1)
|
|
tex.draw_texture(t.t, { x = OX + S * x - DIGIT_O, y = OY + S * cy - DIGIT_O, center = true, scale = S,
|
|
frame = s:byte(k - i + 1) - 48 })
|
|
end
|
|
end
|
|
|
|
function PlayerBoard.draw()
|
|
if not enabled() then return end
|
|
local top = board_texture()
|
|
if not top then return end
|
|
local now = tex.get_current_ms()
|
|
if now - open.last > 1000 then open.t0 = now end
|
|
open.last = now
|
|
local of = math.floor((now - open.t0) / (1000 / 60)) + 1
|
|
if of <= #OPEN then
|
|
local sx = OPEN[of]
|
|
tex.draw_texture(top.t, { x = OX + S * TOP_CX * (1 - sx), y = OY, scale = S, x2 = (top.w * sx - top.w) * S })
|
|
return
|
|
end
|
|
tex.draw_texture(top.t, { x = OX, y = OY, scale = S })
|
|
draw_medals()
|
|
local c = record_counts()
|
|
if c then
|
|
for i, p in ipairs(COUNTERS.rank) do draw_counter(c.ranks[i], p[1], p[2]) end
|
|
for i, p in ipairs(COUNTERS.crown) do draw_counter(c.crowns[i], p[1], p[2]) end
|
|
end
|
|
end
|
|
|
|
return PlayerBoard
|