295 lines
9.4 KiB
Lua
295 lines
9.4 KiB
Lua
local M = {}
|
|
|
|
local SCREEN_W, SCREEN_H = 1920, 1080
|
|
local DOOR_W = 1200 -- door panel local width (main__shape5)
|
|
local KANJI = 400 -- each kanji cell is 400x400 (main__shape7/9/11/13)
|
|
|
|
local SCRIM_SRC = { x = 1200, y = 800, width = 64, height = 64 }
|
|
|
|
-- the four kanji: source cell, rest centre, and the frame each lands on. A kanji swells in from 1.75x
|
|
-- over the ten frames before it lands, then jiggles for four frames (steps of 10 px, in x or y).
|
|
local CELLS = {
|
|
{ sx = 1200, sy = 0, cx = 455, cy = 255, land = 75, jiggle = { { 0, 10 }, { -10, 0 }, { 10, 0 }, { 0, 0 } } }, -- 段
|
|
{ sx = 1600, sy = 0, cx = 1420, cy = 255, land = 100, jiggle = { { 0, 10 }, { -10, 0 }, { 10, 0 }, { 0, 0 } } }, -- 位
|
|
{ sx = 1200, sy = 400, cx = 455, cy = 765, land = 126, jiggle = { { -10, 0 }, { 0, 10 }, { 0, -10 }, { 0, 0 } } }, -- 道
|
|
{ sx = 1600, sy = 400, cx = 1420, cy = 765, land = 153, jiggle = { { 10, 0 }, { 0, -10 }, { 0, 10 }, { 0, 0 } } }, -- 場
|
|
}
|
|
local DOOR_JIGGLE = { 10, -10, 6, 0 } -- the doors shake with each landing
|
|
|
|
local CLOSE_POSE = 62 -- shut doors, scrim already eased to 0.5
|
|
local STAMP_FROM = 62 -- the arcade's scrim has just settled to 0.5
|
|
local STAMP_TO = 182 -- all four landed, before the fade
|
|
local OPEN_FROM = 182 -- kanji fade (182-185), doors part (187-200),
|
|
local OPEN_TO = 262 -- scrim clears (203), bg push-in + `out` fade
|
|
|
|
local MS_PER_FRAME = 1000.0 / 60.0
|
|
|
|
local CLOSE_MS = 300.0
|
|
local STAMP_MS = (STAMP_TO - STAMP_FROM) * MS_PER_FRAME -- 120 frames = 2000 ms
|
|
local OPEN_MS = (OPEN_TO - OPEN_FROM) * MS_PER_FRAME -- 80 frames = 1333 ms
|
|
|
|
local DURATION_MS = 2300.0
|
|
local function duration_ms() return DURATION_MS end
|
|
|
|
local function clamp01(v) return math.max(0.0, math.min(1.0, v)) end
|
|
|
|
local _id, _id_tried
|
|
local function tex_id()
|
|
if not _id_tried then
|
|
_id_tried = true
|
|
_id = tex.get_texture("song_select/dan_transition/background")
|
|
end
|
|
return _id
|
|
end
|
|
|
|
local _bg, _bg_tried
|
|
local function bg_id()
|
|
if not _bg_tried then
|
|
_bg_tried = true
|
|
_bg = tex.get_texture("dan_select/global/bg")
|
|
end
|
|
return _bg
|
|
end
|
|
|
|
local function fire_sfx()
|
|
if M.sfx_done then return end
|
|
if audio and audio.play_sound then audio.play_sound("dan_intro", "sound") end
|
|
M.sfx_done = true
|
|
end
|
|
|
|
local function phases()
|
|
local d = duration_ms()
|
|
local move = CLOSE_MS + STAMP_MS
|
|
local k = (d >= move) and 1.0 or (d / move) -- compress only if we must
|
|
local c = CLOSE_MS * k
|
|
local st = STAMP_MS * k
|
|
return c / d, (c + st) / d
|
|
end
|
|
|
|
-- u (0..1 across our transition) -> arcade frame.
|
|
local function frame_of(u)
|
|
local close_u, stamp_u = phases()
|
|
if u <= close_u then
|
|
return CLOSE_POSE
|
|
elseif u <= stamp_u then
|
|
local v = (u - close_u) / (stamp_u - close_u)
|
|
return STAMP_FROM + (STAMP_TO - STAMP_FROM) * v
|
|
end
|
|
return STAMP_TO -- parked: doors shut, kanji up
|
|
end
|
|
|
|
local function lerp(f, f0, f1, v0, v1)
|
|
if f <= f0 then return v0 end
|
|
if f >= f1 then return v1 end
|
|
return v0 + (v1 - v0) * (f - f0) / (f1 - f0)
|
|
end
|
|
|
|
-- offset t frames after a landing: straight lines through the four jiggle points
|
|
local function jiggle(t, pts)
|
|
if t <= 0 or t >= 4 then return 0, 0 end
|
|
local k = math.floor(t)
|
|
local a, b = pts[k] or { 0, 0 }, pts[k + 1]
|
|
local u = t - k
|
|
return a[1] + (b[1] - a[1]) * u, a[2] + (b[2] - a[2]) * u
|
|
end
|
|
|
|
local function draw_frame(f, op)
|
|
local id = tex_id()
|
|
if not id then return false end
|
|
op = (op == nil) and 1.0 or clamp01(op)
|
|
if op <= 0 then return true end
|
|
|
|
-- backdrop: pushes in from frame 187 and fades out over the last nine frames
|
|
local ba = lerp(f, 253, 262, 1, 0)
|
|
local bid = bg_id()
|
|
if bid and ba > 0 then
|
|
local bs = 1 + 0.25 * (1 - (1 - lerp(f, 187, 253, 0, 1)) ^ 2.2)
|
|
local w, h = SCREEN_W * bs, SCREEN_H * bs
|
|
tex.draw_texture(bid, {
|
|
x = 960 - w * 0.5, y = 540 - h * 0.5,
|
|
x2 = w - SCREEN_W, y2 = h - SCREEN_H, fade = ba * op,
|
|
})
|
|
end
|
|
|
|
-- doors: shut until frame 182, shaking at each landing, then parting
|
|
local dtop = 0
|
|
for i = 1, #CELLS do
|
|
local t = f - CELLS[i].land
|
|
if t > 0 and t < 4 then
|
|
local k = math.floor(t)
|
|
local a, b = DOOR_JIGGLE[k] or 0, DOOR_JIGGLE[k + 1]
|
|
dtop = dtop + a + (b - a) * (t - k)
|
|
end
|
|
end
|
|
local sx, rx, lx0
|
|
if f < 187 then
|
|
sx, rx, lx0 = lerp(f, 182, 187, 1, 1.1), lerp(f, 182, 187, 960, 863.95), lerp(f, 182, 187, 960, 1055.9)
|
|
else
|
|
sx, rx, lx0 = lerp(f, 187, 200, 1.1, 1), lerp(f, 187, 200, 863.95, 1923.7), lerp(f, 187, 200, 1055.9, -1.65)
|
|
end
|
|
local dw = DOOR_W * sx
|
|
local lx = lx0 - DOOR_W * sx
|
|
local scrim = lerp(f, 55, 62, 1, 0.5) - lerp(f, 193, 203, 0, 0.5)
|
|
|
|
-- left door = the same panel mirrored (a negative src width is raylib's flip)
|
|
tex.draw_texture(id, { src = { x = 0, y = 0, width = -DOOR_W, height = SCREEN_H },
|
|
x = lx, y = dtop, x2 = dw, y2 = SCREEN_H, fade = op })
|
|
tex.draw_texture(id, { src = { x = 0, y = 0, width = DOOR_W, height = SCREEN_H },
|
|
x = rx, y = dtop, x2 = dw, y2 = SCREEN_H, fade = op })
|
|
|
|
-- the full-screen black at depth 3: above both doors, below all four kanji
|
|
if scrim > 0 then
|
|
tex.draw_texture(id, { src = SCRIM_SRC,
|
|
x = 0, y = 0, x2 = SCREEN_W, y2 = SCREEN_H,
|
|
fade = scrim * op })
|
|
end
|
|
|
|
for i = 1, #CELLS do
|
|
local cell = CELLS[i]
|
|
local s = lerp(f, cell.land - 10, cell.land, 1.75, 1)
|
|
local a = lerp(f, cell.land - 10, cell.land, 0, 1) * lerp(f, 182, 185, 1, 0)
|
|
if a > 0 then
|
|
local dx, dy = jiggle(f - cell.land, cell.jiggle)
|
|
local w = KANJI * s
|
|
tex.draw_texture(id, {
|
|
src = { x = cell.sx, y = cell.sy, width = KANJI, height = KANJI },
|
|
x = cell.cx + dx - w * 0.5,
|
|
y = cell.cy + dy - w * 0.5,
|
|
x2 = w, y2 = w, fade = a * op,
|
|
})
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
M.armed = false
|
|
M.engine_u = nil
|
|
M.has_draw_top = false
|
|
M.t0 = nil
|
|
M.armed_open = false
|
|
M.open_t0 = nil
|
|
M.sfx_done = false
|
|
|
|
function M.arm(active)
|
|
M.armed = active and true or false
|
|
if not M.armed then
|
|
M.t0 = nil -- let the fallback clock restart cleanly next time
|
|
end
|
|
end
|
|
|
|
function M.set_progress(p)
|
|
M.has_draw_top = true
|
|
if p and p >= 0 then
|
|
M.engine_u = p
|
|
else
|
|
M.engine_u = nil
|
|
M.armed_open = false
|
|
M.open_t0 = nil
|
|
end
|
|
end
|
|
|
|
function M.progress()
|
|
if M.has_draw_top then
|
|
-- the engine supplies real progress every frame; nil/negative = not running
|
|
if M.engine_u and M.engine_u >= 0 then return clamp01(M.engine_u) end
|
|
return nil
|
|
end
|
|
-- fallback: no draw_top on this engine build, self-clock off arm()
|
|
if not M.armed then return nil end
|
|
local now = tex.get_current_ms()
|
|
if not M.t0 then M.t0 = now end
|
|
local d = duration_ms()
|
|
local u = (now - M.t0) / d
|
|
if u >= 1.0 then return nil end
|
|
return u
|
|
end
|
|
|
|
local function off_screen()
|
|
local scr = tex.current_screen and tex.current_screen() or nil
|
|
if scr == nil or scr == "SONG_SELECT" or scr == "DAN_SELECT" then return false end
|
|
M.engine_u = nil
|
|
M.armed = false
|
|
M.armed_open = false
|
|
M.open_t0 = nil
|
|
M.t0 = nil
|
|
M.open_held = false
|
|
return true
|
|
end
|
|
|
|
function M.draw()
|
|
if off_screen() then return false end
|
|
local u = M.progress()
|
|
if not u then return false end
|
|
|
|
fire_sfx(u)
|
|
|
|
local close_u = phases()
|
|
local f = frame_of(u)
|
|
if not draw_frame(f, (u <= close_u) and (u / close_u) or nil) then return false end
|
|
|
|
if not M.armed_open then
|
|
M.armed_open = true
|
|
M.open_t0 = nil
|
|
end
|
|
M._drawn_ms = tex.get_current_ms()
|
|
return true
|
|
end
|
|
|
|
-- true iff M.draw() already painted this exact engine frame
|
|
function M.drawn_this_frame()
|
|
return M._drawn_ms ~= nil and M._drawn_ms == tex.get_current_ms()
|
|
end
|
|
|
|
local INTRO_FROM = 5 -- clip label `in`
|
|
|
|
local REVEAL_F = OPEN_FROM -- 182
|
|
|
|
function M.draw_open()
|
|
if off_screen() then __hss_dan_intro = nil; return false end
|
|
local req = __hss_dan_intro
|
|
if req ~= nil then
|
|
__hss_dan_intro = nil
|
|
if req == "full" and not M.armed_open then
|
|
M.armed_open = true
|
|
M.open_t0 = nil
|
|
M.open_held = false
|
|
M.open_from = INTRO_FROM
|
|
fire_sfx()
|
|
elseif req == "open" then
|
|
M.open_from = OPEN_FROM
|
|
end
|
|
end
|
|
if not M.armed_open then return false end
|
|
local now = tex.get_current_ms()
|
|
if not M.open_t0 then M.open_t0 = now end
|
|
local from = M.open_from or OPEN_FROM
|
|
local f = from + (now - M.open_t0) / MS_PER_FRAME
|
|
|
|
local ready = (__hss_dan_ready ~= false)
|
|
if not ready then
|
|
if f > REVEAL_F then
|
|
f = REVEAL_F
|
|
M.open_held = true
|
|
end
|
|
elseif M.open_held then
|
|
M.open_held = false
|
|
M.open_t0 = now - (REVEAL_F - from) * MS_PER_FRAME
|
|
f = REVEAL_F
|
|
end
|
|
M.open_f = f
|
|
|
|
if f >= OPEN_TO then
|
|
M.armed_open = false
|
|
M.open_t0 = nil
|
|
M.open_held = false
|
|
M.open_from = nil
|
|
M.sfx_done = false
|
|
M.engine_u = nil
|
|
M.t0 = nil
|
|
return false
|
|
end
|
|
return draw_frame(f, nil)
|
|
end
|
|
|
|
return M
|