forked from GanYiZhong/YataiDON14
202 lines
7.4 KiB
Lua
202 lines
7.4 KiB
Lua
GameOver = {}
|
|
GameOver.__index = GameOver
|
|
|
|
-- Taiko 14 gameover_curtain.swg, then (logged-in credits) gameover_qrcode.swg with the site's QR.
|
|
-- Positions are the swg's 640x480 px (K scales them), times are 60 fps frames.
|
|
local PATH = "game_over/global/"
|
|
local FRAME_MS = 1000 / 60
|
|
|
|
local FOX_SPAN, FOX_FROM, FOX_TO = 116, 642.7, -556.1
|
|
local TEXT_AT = 148
|
|
local FADE_AT, FADE_LEN = 414, 24
|
|
local CURTAIN_LEN = FADE_AT + FADE_LEN + 2
|
|
|
|
local QUIT_AT, PANEL_AT, CONTENT_AT, GIRL_AT = 36, 50, 80, 94
|
|
local QR_TIMEOUT, QR_FADE = 1200, 20
|
|
local PATTERN_SPEED, PATTERN_WRAP = 0.357, 214
|
|
|
|
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_in(p) return p * 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
|
|
|
|
-- falls from y0 to rest in `fall` frames, then bounces with height a, period p, decaying over d frames
|
|
local function drop(e, y0, rest, fall, a, p, d)
|
|
if e < fall then return lerp(y0, rest, ease_in(e / fall)) end
|
|
local t = e - fall
|
|
return rest - a * math.exp(-t / d) * math.abs(math.sin(math.pi * t / p))
|
|
end
|
|
|
|
function GameOver.new(overlay)
|
|
local self = setmetatable({}, GameOver)
|
|
self.qr = overlay and true or false
|
|
self.K = tex.get_screen_width() / 640
|
|
self.tex, self.info = {}, {}
|
|
for _, name in ipairs({ "fox_walk", "fox_feet", "drape", "bye_message", "bye_bachi", "qr_back", "qr_pattern",
|
|
"qr_bar", "qr_header", "qr_quit", "qr_panel", "qr_title", "qr_girl", "qr_code" }) do
|
|
self.tex[name] = tex.get_texture(PATH .. name)
|
|
local info = tex.get_texture_info("global", 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.title_frame = tex.player_num() == 2 and 1 or 0
|
|
self.t0 = nil
|
|
self.f = 0
|
|
self.qr_end = nil
|
|
self.finished = false
|
|
self.fired = {}
|
|
return self
|
|
end
|
|
|
|
function GameOver:tl(name, x, y, o)
|
|
o = o or {}
|
|
local t = self.tex[name]
|
|
if not t then return end
|
|
local i = self.info[name]
|
|
local sx, sy = o.sx or 1, o.sy or 1
|
|
tex.draw_texture(t, { frame = o.frame or 0, x = x * self.K - i[1], y = y * self.K - i[2], x2 = i[3] * (sx - 1),
|
|
y2 = i[4] * (sy - 1), fade = o.fade, mirror = o.mirror, blend = o.blend })
|
|
end
|
|
|
|
function GameOver:once(key, cond, fn)
|
|
if cond and not self.fired[key] then
|
|
self.fired[key] = true
|
|
fn()
|
|
end
|
|
end
|
|
|
|
function GameOver:update(current_ms)
|
|
if not self.t0 then self.t0 = current_ms end
|
|
self.f = (current_ms - self.t0) / FRAME_MS
|
|
local f = self.f
|
|
self:once("drag", f >= 10, function() snd("curtain_drag") end)
|
|
self:once("closing", f >= 45, function() snd("closing", "music") end)
|
|
self:once("bye", f >= TEXT_AT, function() snd("voice_bye", "voice") end)
|
|
if not self.qr then
|
|
if f >= CURTAIN_LEN then self.finished = true end
|
|
return
|
|
end
|
|
local q = f - CURTAIN_LEN
|
|
self:once("qr_voice", q >= 30, function() snd("voice_qr", "voice") end)
|
|
self:once("qr_bgm", q >= 47, function() snd("qr_bgm", "music") end)
|
|
if not self.qr_end then
|
|
local skip_from = self.t0 + (CURTAIN_LEN + CONTENT_AT) * FRAME_MS
|
|
if q >= QR_TIMEOUT or (q >= CONTENT_AT and tex.last_input_ms() > skip_from) then
|
|
self.qr_end = q
|
|
end
|
|
elseif q >= self.qr_end + QR_FADE then
|
|
if audio.is_sound_playing("qr_bgm") then audio.stop_sound("qr_bgm") end
|
|
self.finished = true
|
|
end
|
|
end
|
|
|
|
function GameOver:is_finished()
|
|
return self.finished
|
|
end
|
|
|
|
function GameOver:fox_x(f)
|
|
if f < FOX_SPAN then return lerp(FOX_FROM, FOX_TO, ease_out(f / FOX_SPAN)) end
|
|
return lerp(FOX_TO, -505, seg(f, FOX_SPAN, 32))
|
|
end
|
|
|
|
function GameOver:draw_curtain()
|
|
local f = self.f
|
|
local x = self:fox_x(f)
|
|
local walk = math.floor(f / 5) % 10
|
|
local on_screen = x > -210
|
|
if on_screen then self:tl("fox_walk", x, 185, { frame = walk, sx = 2, sy = 2 }) end
|
|
local frame, dx, y = 0, -127, -7
|
|
if f >= 138 then frame, dx, y = 3, -134, -6
|
|
elseif f >= 124 then frame, dx, y = 2, -136, -6
|
|
elseif f >= 110 then frame, dx, y = 1, -136, -6 end
|
|
self:tl("drape", x + dx, y, { frame = frame, sx = 2, sy = 2 })
|
|
if on_screen then self:tl("fox_feet", x, 185, { frame = walk, sx = 2, sy = 2 }) end
|
|
if f >= TEXT_AT then
|
|
local e = f - TEXT_AT
|
|
self:tl("bye_message", 85.7, drop(e, -181.5, 86.5, 38, 60, 28, 30))
|
|
self:tl("bye_bachi", 69.7, drop(e, -85.5, 250.5, 34, 120, 28, 30))
|
|
end
|
|
local fade = seg(f, FADE_AT, 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
|
|
|
|
-- girl with the phone: slides in from the lower left, overshoots, settles
|
|
function GameOver:girl_pos(e)
|
|
if e < 8 then
|
|
local p = ease_out(e / 8)
|
|
return lerp(-59.3, 64, p), lerp(228.7, 103, p), lerp(0.21, 1, e / 8)
|
|
end
|
|
local p = seg(e, 8, 18)
|
|
p = p * p * (3 - 2 * p)
|
|
return lerp(64, 28, p), lerp(103, 143, p), 1
|
|
end
|
|
|
|
function GameOver:draw_qr()
|
|
local q = self.f - CURTAIN_LEN
|
|
local W, H = tex.get_screen_width(), tex.get_screen_height()
|
|
tex.draw_rect(0, 0, W, H, 0, 0, 0, 255)
|
|
local a = seg(q, 0, 30)
|
|
self:tl("qr_back", 0, 0, { fade = a })
|
|
self:tl("qr_pattern", -((PATTERN_SPEED * q) % PATTERN_WRAP), 0, { fade = 0.6 * a })
|
|
self:tl("qr_bar", 0, 0, { fade = a })
|
|
self:tl("qr_bar", 0, 432.1, { sy = 1.143, fade = a })
|
|
self:tl("qr_header", 12, 5.4, { fade = a })
|
|
|
|
if q >= QUIT_AT then
|
|
local e = q - QUIT_AT
|
|
local y, flash = 29.1, 0
|
|
if e < 8 then
|
|
y = lerp(-28.7, 29.1, ease_in(e / 8))
|
|
else
|
|
local t = (e - 8) % 30
|
|
if t < 17 then
|
|
flash = math.sin(math.pi * t / 17)
|
|
y = 29.1 - 20 * flash
|
|
else
|
|
y = 29.1 - 7 * math.sin(math.pi * (t - 17) / 13)
|
|
end
|
|
end
|
|
local fade = e < 8 and lerp(0.11, 1, e / 8) or 1
|
|
self:tl("qr_quit", 374, y, { fade = fade })
|
|
if flash > 0 then self:tl("qr_quit", 374, y, { fade = flash, blend = "additive" }) end
|
|
end
|
|
|
|
if q >= PANEL_AT then
|
|
local e = q - PANEL_AT
|
|
local sy = 1
|
|
if e < 10 then sy = lerp(0.01, 1.1, ease_out(e / 10))
|
|
elseif e < 16 then sy = lerp(1.1, 0.9, (e - 10) / 6)
|
|
elseif e < 22 then sy = lerp(0.9, 1, (e - 16) / 6) end
|
|
self:tl("qr_panel", 322, 247 - 184 * sy, { sy = sy, fade = math.min(1, e / 8) })
|
|
end
|
|
if q >= CONTENT_AT then
|
|
local fade = math.min(1, (q - CONTENT_AT) / 6)
|
|
self:tl("qr_title", 366, 88.4, { frame = self.title_frame, fade = fade })
|
|
local w = self.info.qr_code[3] / self.K
|
|
self:tl("qr_code", 474 - w / 2, 248 - w / 2, { fade = fade })
|
|
end
|
|
if q >= GIRL_AT then
|
|
local x, y, fade = self:girl_pos(q - GIRL_AT)
|
|
self:tl("qr_girl", x, y, { fade = fade, mirror = "horizontal" })
|
|
end
|
|
if self.qr_end then
|
|
local fade = seg(q, self.qr_end, QR_FADE)
|
|
if fade > 0 then tex.draw_rect(0, 0, W, H, 0, 0, 0, math.floor(fade * 255)) end
|
|
end
|
|
end
|
|
|
|
function GameOver:draw()
|
|
if self.qr and self.f >= CURTAIN_LEN then
|
|
self:draw_qr()
|
|
else
|
|
self:draw_curtain()
|
|
end
|
|
end
|