forked from GanYiZhong/YataiDON14
448 lines
17 KiB
Lua
448 lines
17 KiB
Lua
local M = {}
|
|
M.__index = M
|
|
|
|
-- 640x480 swg px x K (the art is baked x1.5, so K = 1.5 at 960 wide)
|
|
local K = tex.get_screen_width() / 640
|
|
local SCR = "game"
|
|
local FPS = 60
|
|
|
|
local function loader(sub)
|
|
local cache = {}
|
|
return function(name)
|
|
local c = cache[name]
|
|
if c ~= nil then return c or nil end
|
|
local id = tex.get_texture(SCR .. "/" .. sub .. "/" .. name)
|
|
local info = id and tex.get_texture_info(sub, name)
|
|
if not (id and info) then cache[name] = false; return nil end
|
|
c = { id = id, w = info.width, h = info.height, x = info.x and info.x[1] or 0, y = info.y and info.y[1] or 0 }
|
|
cache[name] = c
|
|
return c
|
|
end, function() for k in pairs(cache) do cache[k] = nil end end
|
|
end
|
|
local GSUB, HSUB = "t14_gauge", "t14_hud"
|
|
local G, G_reset = loader(GSUB)
|
|
local H, H_reset = loader(HSUB)
|
|
|
|
local MISS_BUBBLE_COMBO = 50
|
|
local ROLL_SHOW_MS = 1550
|
|
|
|
local function now_ms(self)
|
|
local ok, v = pcall(tex.get_current_ms)
|
|
if ok and type(v) == "number" then return v end
|
|
return self.last_update or 0
|
|
end
|
|
|
|
local function frames_since(n, t) return t and (n - t) * FPS / 1000 or math.huge end
|
|
|
|
-- the 14's digit pop: stretched up from the bottom edge to 1.2 over 2 frames, back over 6
|
|
local function pop(f)
|
|
if f < 0 or f >= 8 then return 1 end
|
|
if f < 2 then return 1 + .1 * f end
|
|
return 1.2 - .2 * (f - 2) / 6
|
|
end
|
|
|
|
-- texture t (rest position from texture.json) drawn moved by (dx, dy), scaled (sx, sy) about its top-left
|
|
local function put(t, dx, dy, sx, sy, frame, fade)
|
|
if not t or (fade and fade <= 0) then return end
|
|
sx, sy = sx or 1, sy or 1
|
|
local p = { x = dx or 0, y = dy or 0, frame = frame, fade = (fade and fade < 1) and fade or nil }
|
|
if sx ~= 1 then p.x2 = t.w * (sx - 1) end
|
|
if sy ~= 1 then p.y2 = t.h * (sy - 1) end
|
|
tex.draw_texture(t.id, p)
|
|
end
|
|
|
|
-- the same, scaled by s about the point (ax, ay) (screen px)
|
|
local function put_about(t, s, ax, ay, fade, frame)
|
|
if not t then return end
|
|
put(t, (t.x - ax) * (s - 1), (t.y - ay) * (s - 1), s, s, frame, fade)
|
|
end
|
|
|
|
function M.new(player_num)
|
|
local self = setmetatable({}, M)
|
|
self.two = (player_num == 3)
|
|
self.solo2 = (player_num == 2) -- 2P playing alone, on the top lane
|
|
self.p = {}
|
|
for i = 1, 2 do self.p[i] = M.fresh() end
|
|
tex.load_folder(SCR, GSUB)
|
|
tex.load_folder(SCR, HSUB)
|
|
return self
|
|
end
|
|
|
|
function M.fresh()
|
|
return { combo = 0, roll = 0, roll_last = nil, roll_break = true, roll_pops = {}, gauge = nil }
|
|
end
|
|
|
|
function M:st(pn) return self.p[(pn == 2) and 2 or 1] end
|
|
function M:is_p2(pn) return self.two and pn == 2 end
|
|
function M:side(pn) return self:is_p2(pn) and "katsu_2p" or (self.solo2 and "katsu" or "don") end
|
|
|
|
function M:update(current_ms)
|
|
self.last_update = current_ms
|
|
self.restart_st = self.restart_st or {}
|
|
if NK_RESTARTED and NK_RESTARTED(self.restart_st) then
|
|
for i = 1, 2 do self.p[i] = M.fresh() end
|
|
end
|
|
end
|
|
|
|
-- per-place digit pops: a place pops when its digit changes
|
|
local function track(pops, old, new, n)
|
|
for i = 0, 5 do
|
|
local p = 10 ^ i
|
|
if (old // p) % 10 ~= (new // p) % 10 or (old < p) ~= (new < p) then pops[i] = n end
|
|
end
|
|
end
|
|
|
|
local function judged(self, pn, good)
|
|
local s = self:st(pn)
|
|
local n = now_ms(self)
|
|
s.roll_break = true
|
|
if not good then
|
|
if s.combo >= MISS_BUBBLE_COMBO then s.t_miss = n end
|
|
s.combo = 0; s.t10 = nil; s.t100 = nil
|
|
return
|
|
end
|
|
s.combo = s.combo + 1
|
|
s.t_combo = n
|
|
if s.combo % 10 == 0 then s.t10 = n; s.t_miss = nil end
|
|
if s.combo % 100 == 0 then s.t100 = n end
|
|
end
|
|
|
|
function M:handle_good(pn) judged(self, pn, true) end
|
|
function M:handle_ok(pn) judged(self, pn, true) end
|
|
function M:handle_bad(pn) judged(self, pn, false) end
|
|
|
|
function M:handle_drumroll(pn)
|
|
local s = self:st(pn)
|
|
local n = now_ms(self)
|
|
if s.roll_break or not s.roll_last or n - s.roll_last >= ROLL_SHOW_MS then s.roll = 0; s.roll_pops = {} end
|
|
track(s.roll_pops, s.roll, s.roll + 1, n)
|
|
s.roll = s.roll + 1
|
|
s.roll_last = n
|
|
s.roll_break = false
|
|
end
|
|
|
|
function M:handle_gauge(pn, progress, is_clear, is_rainbow)
|
|
local s = self:st(pn)
|
|
s.gauge = { g = math.max(0, math.min(1, progress or 0)) * 100,
|
|
clear = is_clear and true or false, rainbow = is_rainbow and true or false }
|
|
end
|
|
|
|
---------------------------------------------------------------------------------------------
|
|
-- Taiko 14 soul gauge (Graphics/game/t14_gauge, rest positions in its texture.json).
|
|
-- 50 cells; the e / n / h art puts the norma at 30 / 35 / 40 cells. The engine's norma is 60 % on easy,
|
|
-- 70 % on normal / hard, 80 % on oni / ura; a dan's soul exam (NK_DAN_RED) takes the nearest art.
|
|
-- Sides: don (1P), katsu (2P alone on the top lane), katsu_2p (the bottom lane).
|
|
local NORMA_CELLS = { e = 30, n = 35, h = 40 }
|
|
local VARIANT_BY_COURSE = { [0] = "e", [1] = "n", [2] = "n", [3] = "h", [4] = "h" }
|
|
local FILL_X0, CELL_W, CAP_STEP = 3.9 * K, 176 / 29 * K, 6 * K -- fill start (gauge-relative), cell widths
|
|
local GAUGE_X = 260 * K
|
|
local SOUL_FPS = 60
|
|
|
|
function M:gauge_key(pn)
|
|
local v
|
|
if NK_DAN_RED then
|
|
v = NK_DAN_RED < 65 and "e" or (NK_DAN_RED < 75 and "n" or "h")
|
|
elseif type(NK_COURSE_BY_PLAYER) == "table" then
|
|
v = VARIANT_BY_COURSE[NK_COURSE_BY_PLAYER[pn]]
|
|
end
|
|
local side = self:is_p2(pn) and "katsu_2p" or (self.solo2 and "katsu" or "don")
|
|
return side .. "_" .. (v or "h") .. "_", v or "h"
|
|
end
|
|
|
|
local function gdraw(t, dx, w, fade, scale)
|
|
if not t or (fade and fade <= 0) then return end
|
|
local p = { x = dx or 0, y = 0 }
|
|
if w then p.x2 = w - t.w end
|
|
if fade and fade < 1 then p.fade = fade end
|
|
if scale and scale ~= 1 then p.center = true; p.scale = scale end
|
|
tex.draw_texture(t.id, p)
|
|
end
|
|
|
|
-- edge cap flash on each new cell: in over 4 swg frames, out by frame 29
|
|
local function cap_alpha(f)
|
|
if f < 0 or f >= 29 then return 0 end
|
|
if f < 4 then return f / 4 end
|
|
return math.max(0, 1 - ((f - 5) / 24) ^ 2)
|
|
end
|
|
|
|
-- soul flame (movie 26): rises out of the gauge and settles over 10 frames, flickers through its 8 tinted
|
|
-- flames (3 frames each, 24-frame loop with the glow dimmed 2 frames in 4), and sinks back over 12 frames
|
|
local SOUL_FROM = { -22.5 * K, 32.4 * K }
|
|
local SOUL_PEAK, SOUL_TOP = { 2.5 * K, -3.5 * K }, { 5 * K, -7.2 * K }
|
|
local FLAME_ORDER = { 1, 2, 3, 4, 5, 6, 7, 8 }
|
|
local function lerp(a, b, t) return a + (b - a) * t end
|
|
|
|
function M:draw_soul(k, s, n)
|
|
local soul, glow = G(k .. "soul"), G(k .. "soul_glow")
|
|
if not soul then return end
|
|
local f_on = s.fever_on and (n - s.fever_on) * SOUL_FPS / 1000
|
|
local f_off = s.fever_off and (n - s.fever_off) * SOUL_FPS / 1000
|
|
local dx, dy, sc, ga, flame = 0, 0, 1, 1, 1
|
|
if f_off then
|
|
if f_off >= 12 then s.fever_off = nil; return end
|
|
if f_off < 6 then
|
|
local t = f_off / 6
|
|
dx, dy, sc = lerp(0, SOUL_TOP[1], t), lerp(0, SOUL_TOP[2], t), lerp(1, 1.2, t)
|
|
else
|
|
local t = (f_off - 6) / 6
|
|
dx, dy, sc = lerp(SOUL_TOP[1], SOUL_FROM[1], t), lerp(SOUL_TOP[2], SOUL_FROM[2], t), lerp(1.2, .1, t)
|
|
ga = lerp(1, .12, t)
|
|
end
|
|
elseif f_on < 10 then
|
|
if f_on < 5 then
|
|
local t = f_on / 5
|
|
dx, dy, sc = lerp(SOUL_FROM[1], SOUL_PEAK[1], t), lerp(SOUL_FROM[2], SOUL_PEAK[2], t), lerp(.1, 1.1, t)
|
|
ga = lerp(.12, 1, t)
|
|
else
|
|
local t = (f_on - 5) / 5
|
|
dx, dy, sc = lerp(SOUL_PEAK[1], 0, t), lerp(SOUL_PEAK[2], 0, t), lerp(1.1, 1, t)
|
|
end
|
|
else
|
|
local loop = math.floor(f_on - 10) % 24
|
|
flame = FLAME_ORDER[loop // 3 + 1]
|
|
if loop % 4 >= 2 then ga = .12 end
|
|
end
|
|
local fire = G(k .. "fire_" .. flame)
|
|
if fire then
|
|
tex.draw_texture(fire.id, { x = dx, y = dy, center = true, scale = sc })
|
|
end
|
|
gdraw(soul)
|
|
gdraw(glow, nil, nil, ga)
|
|
end
|
|
|
|
function M:draw_gauge(pn)
|
|
local s = self:st(pn)
|
|
if not s.gauge then return end
|
|
local n = now_ms(self)
|
|
local k, v = self:gauge_key(pn)
|
|
local cells = math.floor(s.gauge.g / 2 + 1e-6)
|
|
local N = NORMA_CELLS[v]
|
|
if s.cells and cells > s.cells then s.cap_ms = n end
|
|
s.cells = cells
|
|
local full = cells >= 50
|
|
if full and not s.fever_on then s.fever_on, s.fever_off = n, nil end
|
|
if not full and s.fever_on then s.fever_on, s.fever_off = nil, n end
|
|
|
|
gdraw(G(k .. "frame"))
|
|
local red, yellow = G(k .. "red"), G(k .. "yellow")
|
|
if cells > 0 and red then
|
|
gdraw(red, 0, math.min(cells, N - 1) * CELL_W)
|
|
end
|
|
if cells >= N and yellow then
|
|
local w = GAUGE_X + FILL_X0 + cells * CELL_W - yellow.x
|
|
if w > 0 then gdraw(yellow, 0, w) end
|
|
end
|
|
if full then
|
|
local f = math.floor((n - s.fever_on) * SOUL_FPS / 1000) % 40
|
|
local i = f // 5
|
|
gdraw(G(k .. "rainbow_" .. (i + 1)))
|
|
gdraw(G(k .. "rainbow_" .. ((i + 1) % 8 + 1)), nil, nil, (f % 5) / 5)
|
|
end
|
|
gdraw(G(k .. "grid"))
|
|
if s.cap_ms and cells > 0 and not full then
|
|
local a = cap_alpha((n - s.cap_ms) * SOUL_FPS / 1000)
|
|
if cells < N then
|
|
gdraw(G(k .. "red_cap"), (cells - 1) * CAP_STEP, nil, a)
|
|
else
|
|
gdraw(G(k .. "yellow_cap"), (cells - 50) * CAP_STEP, nil, a)
|
|
end
|
|
end
|
|
if s.fever_on or s.fever_off then self:draw_soul(k, s, n) end
|
|
end
|
|
|
|
|
|
---------------------------------------------------------------------------------------------
|
|
-- Combo on the drum (enso/1PDON/enso_number, part at (-11, 108) like the drum, +2P_DY on the bottom lane).
|
|
-- White digits up to 99, orange (with twinkles) from 100; every digit bounces on each hit. Slot x / y are the
|
|
-- digits' top-left in swg px, sx / sy their fixed scale. The flower and コンボ bloom on every 100 combo.
|
|
local COMBO_X0, COMBO_Y0 = -11 * K + 6, 108 * K + 2
|
|
local P2_DY = 139 * K
|
|
local COMBO = {
|
|
white = { { x = { 59.0, 37.9 }, y = 35 } },
|
|
orange = { { x = { 70.8, 38.8, 7.0 }, y = 27.4, sx = 1.2, sy = 1.2 },
|
|
{ x = { 81.8, 57.8, 34.0, 9.8 }, y = 27.4, sx = .899, sy = 1.2 } },
|
|
}
|
|
local SPARKLES = { { 0, 21, 3 }, { 4, 33, 27 } } -- start frame, centre from the ones digit's top-left
|
|
local FLOWER_X, FLOWER_Y, WORD_X, WORD_Y = 6.8, -14.7, 40.7, 66.2
|
|
local WORD_FRAMES, FLOWER_FRAMES, FLOWER_FADE = 117, 278, 5
|
|
|
|
local function bloom(f)
|
|
if f < 7 then return .2 + .85 * f / 7 end
|
|
if f < 9 then return 1.05 - .05 * (f - 7) / 2 end
|
|
return 1
|
|
end
|
|
|
|
function M:draw_combo(s, n, dy)
|
|
local c = s.combo
|
|
local X0, Y0 = COMBO_X0, COMBO_Y0 + dy
|
|
local fb = frames_since(n, s.t100)
|
|
if fb < FLOWER_FRAMES + FLOWER_FADE then
|
|
local sc = bloom(fb)
|
|
local a = fb < FLOWER_FRAMES and 1 or 1 - (fb - FLOWER_FRAMES) / FLOWER_FADE
|
|
local fl, wd = H("combo_flower"), H("combo_word")
|
|
if fl then
|
|
local cx, cy = X0 + (FLOWER_X * K) + fl.w / 2, Y0 + FLOWER_Y * K + fl.h / 2
|
|
tex.draw_texture(fl.id, { x = cx - fl.w / 2, y = cy - fl.h / 2, center = true, scale = sc, fade = a < 1 and a or nil })
|
|
end
|
|
if wd and fb < WORD_FRAMES then
|
|
tex.draw_texture(wd.id, { x = X0 + WORD_X * K, y = Y0 + WORD_Y * K, center = true, scale = sc })
|
|
end
|
|
end
|
|
if c < 10 then return end
|
|
local set = c < 100 and "white" or "orange"
|
|
local layouts = COMBO[set]
|
|
local digits = #tostring(c)
|
|
local L = layouts[math.min(#layouts, digits - (set == "white" and 1 or 2))]
|
|
local t = H("combo_" .. set)
|
|
if not (t and L) then return end
|
|
local sx, sy = L.sx or 1, L.sy or 1
|
|
local step = #L.x > 1 and (L.x[1] - L.x[2]) or 0
|
|
for i = 1, digits do
|
|
local x = L.x[i] or (L.x[#L.x] - step * (i - #L.x))
|
|
local p = pop(frames_since(n, s.t_combo))
|
|
local h = t.h * sy
|
|
put(t, X0 + x * K, Y0 + L.y * K + h * (1 - p), sx, sy * p, (c // 10 ^ (i - 1)) % 10)
|
|
end
|
|
if set == "orange" then
|
|
local sp = H("combo_sparkle")
|
|
local f = frames_since(n, s.t_combo)
|
|
for _, k in ipairs(SPARKLES) do
|
|
local g = f - k[1]
|
|
if sp and g >= 0 and g < 9 then
|
|
local cx, cy = X0 + (L.x[1] + k[2]) * K, Y0 + (L.y + k[3]) * K
|
|
tex.draw_texture(sp.id, { x = cx - sp.w / 2, y = cy - sp.h / 2, center = true, scale = 2 - .0714 * g,
|
|
fade = 1 - .0714 * g })
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
---------------------------------------------------------------------------------------------
|
|
-- Combo bubble (fukidashi) every 10 combo: fades in over 3 frames, swells to 1.2 and back (about a point near
|
|
-- its tail edge), holds, fades over the last 5 of 49 frames; its digits stay still. A combo of MISS_BUBBLE_COMBO
|
|
-- or more broken by a miss shows the scribble bubble instead.
|
|
local BUBBLE_FRAMES, BUBBLE_ANCHOR = 49, .87
|
|
local MISS_FRAMES = 70
|
|
function M:bubble_alpha(f, total)
|
|
if f < 3 then return f / 3 end
|
|
if f >= total - 5 then return math.max(0, (total - f) / 5) end
|
|
return 1
|
|
end
|
|
|
|
function M:draw_callout(s, n, pn)
|
|
local k = self:side(pn)
|
|
local fm = frames_since(n, s.t_miss)
|
|
if fm < MISS_FRAMES then
|
|
put(H(k .. "_bubble_miss"), 0, 0, 1, 1, nil, self:bubble_alpha(fm, MISS_FRAMES))
|
|
end
|
|
local f = frames_since(n, s.t10)
|
|
if s.combo < 10 or f >= BUBBLE_FRAMES then return end
|
|
local a = self:bubble_alpha(f, BUBBLE_FRAMES)
|
|
local sc = 1
|
|
if f >= 3 and f < 8 then sc = 1 + .04 * (f - 3)
|
|
elseif f >= 8 and f < 13 then sc = 1.2 - .04 * (f - 8) end
|
|
local b = H(k .. "_bubble")
|
|
if not b then return end
|
|
local anchor = self:is_p2(pn) and 1 - BUBBLE_ANCHOR or BUBBLE_ANCHOR
|
|
local ax, ay = b.x + b.w / 2, b.y + b.h * anchor
|
|
put_about(b, sc, ax, ay, a)
|
|
put_about(H(k .. "_bubble_word"), sc, ax, ay, a)
|
|
local v = 10 * (s.combo // 10)
|
|
local num = H(k .. (v >= 1000 and "_bubble_num1000" or "_bubble_num"))
|
|
if not num then return end
|
|
local step = (v >= 1000 and 26 or 32) * K
|
|
for i = 0, #tostring(v) - 1 do
|
|
put(num, -step * i, 0, 1, 1, (v // 10 ^ i) % 10, a)
|
|
end
|
|
end
|
|
|
|
---------------------------------------------------------------------------------------------
|
|
-- Roll counter (renda_num, part at (-36, -62); the bottom lane's at (-36, 356)): the burst and 連打!! stay put, each
|
|
-- digit pops when it changes; one digit sits at x 239.4, more run left from 248.2 in 25.1 px steps.
|
|
local ROLL_FADE_MS = 150
|
|
local ROLL_X0, ROLL_Y0, ROLL_P2_DY = -36 * K, -62 * K, 418 * K
|
|
function M:draw_roll(s, n, pn)
|
|
if not s.roll_last or s.roll <= 0 then return end
|
|
local t = n - s.roll_last
|
|
if t >= ROLL_SHOW_MS then return end
|
|
local a = (t >= ROLL_SHOW_MS - ROLL_FADE_MS) and (ROLL_SHOW_MS - t) / ROLL_FADE_MS or 1
|
|
local dy = self:is_p2(pn) and ROLL_P2_DY or 0
|
|
put(H("roll_burst"), 0, dy, 1, 1, nil, a)
|
|
put(H("roll_word"), 0, dy, 1, 1, nil, a)
|
|
local num = H("roll_num")
|
|
if not num then return end
|
|
local cnt = s.roll
|
|
for i = 0, #tostring(cnt) - 1 do
|
|
local x = cnt < 10 and 239.4 or 248.2 - 25.1 * i
|
|
local p = pop(frames_since(n, s.roll_pops[i]))
|
|
put(num, ROLL_X0 + x * K, ROLL_Y0 + 67.6 * K + dy + num.h * (1 - p), 1, p, (cnt // 10 ^ i) % 10, a)
|
|
end
|
|
end
|
|
|
|
function M:draw_fore_pre()
|
|
local n = now_ms(self)
|
|
for pn = 1, (self.two and 2 or 1) do
|
|
self:draw_combo(self.p[pn], n, (self.two and pn == 2) and P2_DY or 0)
|
|
end
|
|
end
|
|
|
|
function M:draw_fore_post()
|
|
local n = now_ms(self)
|
|
for pn = 1, (self.two and 2 or 1) do
|
|
local s = self.p[pn]
|
|
self:draw_roll(s, n, pn)
|
|
self:draw_callout(s, n, pn)
|
|
end
|
|
end
|
|
|
|
function M:destroy()
|
|
G_reset(); H_reset()
|
|
tex.unload_folder(SCR, GSUB)
|
|
tex.unload_folder(SCR, HSUB)
|
|
end
|
|
|
|
---------------------------------------------------------------------------------------------
|
|
local function guard(f, ...)
|
|
local ok, err = pcall(f, ...)
|
|
if not ok then tex.log("t14_hud: " .. tostring(err)) end
|
|
end
|
|
|
|
function M.attach(obj, player_num)
|
|
if type(obj) ~= "table" or rawget(obj, "__t14_hud") then return obj end
|
|
local ok, hud = pcall(M.new, player_num)
|
|
if not ok then tex.log("t14_hud: " .. tostring(hud)); return obj end
|
|
rawset(obj, "__t14_hud", hud)
|
|
|
|
local function chain(name, pre, post)
|
|
local orig = obj[name]
|
|
rawset(obj, name, function(self, ...)
|
|
if pre then guard(pre, hud, ...) end
|
|
local r
|
|
if type(orig) == "function" then r = orig(self, ...) end
|
|
if post then guard(post, hud, ...) end
|
|
return r
|
|
end)
|
|
end
|
|
chain("update", M.update)
|
|
chain("handle_good", M.handle_good)
|
|
chain("handle_ok", M.handle_ok)
|
|
chain("handle_bad", M.handle_bad)
|
|
chain("handle_drumroll", M.handle_drumroll)
|
|
chain("handle_gauge", M.handle_gauge)
|
|
chain("draw_gauge", nil, M.draw_gauge) -- after anything the background draws there
|
|
chain("draw_fore", M.draw_fore_pre, M.draw_fore_post)
|
|
chain("destroy", nil, M.destroy)
|
|
return obj
|
|
end
|
|
|
|
function M.install(cls)
|
|
if type(cls) ~= "table" or rawget(cls, "__t14_hud_installed") then return end
|
|
rawset(cls, "__t14_hud_installed", true)
|
|
local orig_new = cls.new
|
|
cls.new = function(player_num, ...)
|
|
return M.attach(orig_new(player_num, ...), player_num)
|
|
end
|
|
end
|
|
|
|
return M
|