Files
PyTaikoGreen/Scripts/background/bg_collabs/ia.lua
T
2026-10-21 22:14:26 -04:00

362 lines
14 KiB
Lua

local DonBGBase = require("background/bg_objects/donbg/base")
local DonBG = require("background/bg_objects/donbg")
local Fever = require("background/bg_objects/fever")
local Dancer = require("background/bg_objects/dancer")
local RendaController = require("background/bg_objects/renda")
local ChibiController = require("background/bg_objects/chibi")
local BaseChibi = require("background/bg_objects/chibi/base")
local Timing = require("background/bg_objects/timing")
local COLLAB_PATH = "background/collab/ia"
local MAX_DANCERS = 5
local chain_moves, sum = Timing.chain_moves, Timing.sum
-- No footer: the stage in bg_normal reaches the bottom of the screen.
-- Normal background: the stage, with confetti falling in front of five spotlights. The
-- confetti is a 20-frame loop at 30 fps (ambient, in ms), the same frame on every copy.
local CONFETTI_COPIES = 5
local CONFETTI_FPS = 30
local IaBGNormal = {}
IaBGNormal.__index = IaBGNormal
function IaBGNormal.new()
local self = setmetatable({}, IaBGNormal)
self.name = "bg_0"
tex.load_folder(COLLAB_PATH, "bg_normal/" .. self.name)
local function id(key) return tex.get_texture(COLLAB_PATH .. "/" .. self.name .. "/" .. key) or 0 end
self._id_bg = id("background")
self._id_confetti = id("confetti")
self._id_lights = id("lights")
self.frames = tex.get_texture_info(self.name, "confetti").frame_count
self.frame = 0
return self
end
function IaBGNormal:update(current_time_ms)
self.frame = math.floor(current_time_ms * CONFETTI_FPS / 1000) % self.frames
end
function IaBGNormal:draw()
tex.draw_texture(self._id_bg)
for i = 0, CONFETTI_COPIES - 1 do
tex.draw_texture(self._id_confetti, { index = i, frame = self.frame })
end
tex.draw_texture(self._id_lights)
end
function IaBGNormal:destroy() tex.unload_folder(COLLAB_PATH, "bg_normal/" .. self.name) end
-- Clear background: a bubble backdrop drops in over the stage and bounces, scrolling left
-- 320px every 2 beats (it repeats every 320px). A beat later, spotlights fade in and flash
-- in three pairs every 1.5 beats, and two sparkle layers pulse in turn. Losing clear slides
-- the backdrop down off the screen while the lights fade. Timed in beats; drawn over
-- bg_normal, which shows through before and after.
local BG_FEVER_DROP = { { 1, 360, "in" }, { 0.45, -50, "out" }, { 0.3, 50, "in" } } -- from 360 above
local BG_FEVER_EXIT = 1 -- beats to slide 400px down
local BG_FEVER_SCROLL = 160 -- px a beat
local BG_FEVER_TILE = 640
local LIGHTS_DELAY = 1 -- beats after the drop starts
local SPOTS_FADE_IN = 0.75
local LIGHTS_FADE_OUT = 0.35
-- each pair: { texture.json index 0 key, index 1 key (mirrored), beat in the 1.5-beat cycle }
local SPOT_CYCLE = 1.5
local SPOT_PAIRS = {
{ "spot_pink", "spot_pink", 0 },
{ "spot_green", "spot_cyan", 0.5 },
{ "spot_cyan", "spot_green", 1 },
}
local SPARKLE_CYCLE = 2
-- a spotlight flash: quick fade in, hold, fade out over half a beat
local function flash(u)
if u < 0.125 then return u / 0.125 end
if u < 0.5 then return 1 end
if u < 1 then return (1 - u) / 0.5 end
return 0
end
-- a sparkle pulse over 2 beats: dark, fade in to the beat, fade out, dark
local function pulse(u)
if u < 0.25 or u >= 1.75 then return 0 end
if u < 1 then return (u - 0.25) / 0.75 end
return (1.75 - u) / 0.75
end
local IaBGFever = {}
IaBGFever.__index = IaBGFever
function IaBGFever.new(bpm)
local self = setmetatable({}, IaBGFever)
self.name = "bg_fever_0"
tex.load_folder(COLLAB_PATH, "bg_fever/" .. self.name)
local function id(key) return tex.get_texture(COLLAB_PATH .. "/" .. self.name .. "/" .. key) or 0 end
self._id_bg = id("background")
self._id_sparkle = id("sparkle")
self._id_spot = { spot_cyan = id("spot_cyan"), spot_pink = id("spot_pink"), spot_green = id("spot_green") }
self.beat = 60000 / bpm
self.scale = tex.get_screen_scale()
self.state = nil -- nil (hidden), "in", "out"
return self
end
function IaBGFever:y() return self.y0 + sum(self.drop) end
function IaBGFever:lights(moves, offset) return (offset + sum(moves)) / 1000 end
function IaBGFever:start()
self.state = "in"
self.t = 0 -- beats since the drop started: the scroll and light cycles
self.y0 = math.floor(-360 * self.scale)
self.drop = chain_moves(BG_FEVER_DROP, self.beat, self.scale)
self.spots = chain_moves({ { LIGHTS_DELAY, 0 }, { SPOTS_FADE_IN, 1000 } }, self.beat, 1)
self.sparkle = chain_moves({ { LIGHTS_DELAY, 0 }, { 0, 1000 } }, self.beat, 1)
self.spots0, self.sparkle0 = 0, 0
end
-- slides down and fades the lights from wherever they are
function IaBGFever:stop()
if self.state ~= "in" then return end
local y, spots, sparkle = self:y(), self:lights(self.spots, self.spots0), self:lights(self.sparkle, self.sparkle0)
self.state = "out"
self.y0 = y
self.drop = chain_moves({ { BG_FEVER_EXIT, 400 - y / self.scale } }, self.beat, self.scale)
self.spots0, self.sparkle0 = math.floor(spots * 1000), math.floor(sparkle * 1000)
self.spots = chain_moves({ { LIGHTS_FADE_OUT, -self.spots0 } }, self.beat, 1)
self.sparkle = chain_moves({ { LIGHTS_FADE_OUT, -self.sparkle0 } }, self.beat, 1)
end
local function update_all(moves, current_time_ms)
for _, m in ipairs(moves) do m:update(current_time_ms) end
end
function IaBGFever:update(current_time_ms, bpm)
if bpm then self.beat = 60000 / bpm end
local dt = current_time_ms - (self.last_ms or current_time_ms)
self.last_ms = current_time_ms
if not self.state then return end
self.t = self.t + dt / self.beat
update_all(self.drop, current_time_ms)
update_all(self.spots, current_time_ms)
update_all(self.sparkle, current_time_ms)
if self.state == "out" and self.drop[#self.drop].is_finished then self.state = nil end
end
function IaBGFever:draw()
if not self.state then return end
local y = self:y()
local tile = BG_FEVER_TILE * self.scale
local x = (-160 - (self.t * BG_FEVER_SCROLL) % 320) * self.scale
while x < tex.get_screen_width() do
tex.draw_texture(self._id_bg, { x = x, y = y })
x = x + tile
end
local t = self.t - LIGHTS_DELAY
if t < 0 then return end
local spots = self:lights(self.spots, self.spots0)
for _, pair in ipairs(SPOT_PAIRS) do
local fade = spots * flash((t - pair[3]) % SPOT_CYCLE)
if fade > 0 then
tex.draw_texture(self._id_spot[pair[1]], { index = 0, fade = fade })
tex.draw_texture(self._id_spot[pair[2]], { index = 1, fade = fade, mirror = "horizontal" })
end
end
local sparkle = self:lights(self.sparkle, self.sparkle0)
for frame = 0, 1 do -- the second layer pulses a beat later
local fade = sparkle * pulse((t + frame) % SPARKLE_CYCLE)
for i = 0, 1 do tex.draw_texture(self._id_sparkle, { index = i, frame = frame, fade = fade }) end
end
end
function IaBGFever:destroy() tex.unload_folder(COLLAB_PATH, "bg_fever/" .. self.name) end
-- Fever line-up: the yokai crowd (left, right) with additive glow sticks over them, two poses
local FEVER_SPEC = {
rise = 270, peak = 70, bounce = 70, drop = 340, poses = true,
keys = { "overlay_l", "overlay_r", "glow_l", "glow_r" },
}
-- Dancers
-- Timed in beats. Each jumps up from below, lands with a squash, then loops a 32-beat
-- routine; the small ones hop off when removed. Poses pivot about the feet (bottom-left).
-- a pose with an accent: stretches (sy) or narrows (sx) over a quarter beat and settles back
local function accent(frame, beats, s)
return {
{ frame, 0.25, to = { nil, nil, "out", sx = s.sx, sy = s.sy } },
{ frame, 0.25, sx = s.sx, sy = s.sy, to = { nil, nil, "in", sx = 1, sy = 1 } },
{ frame, beats - 0.5 },
}
end
local SWAY = { { 1, 1, x = 0, y = 0 }, { 0, 1 }, { 2, 1 }, { 0, 1 } }
local STRETCH, NARROW = { sy = 1.04 }, { sx = 0.96 }
local SMALL_DANCER = {
origin = { 0, 184 },
enter = {
beats = 2,
{ 0, 0, 0, 358 },
{ 0.35, 0, 0, 358, to = { 0, -38, "out" } }, -- jumps up from below
{ 0.95, 0, 0, -38, to = { 0, 0, "in" } },
{ 1.2, 0, 0, 0, to = { nil, nil, "out", sy = 0.96 } }, -- lands with a squash
{ 1.325, 0, 0, 0, sy = 0.96, to = { nil, nil, "in", sy = 1 } },
{ 1.45, 0, 0, 0 },
},
dance = Timing.steps(SWAY, SWAY, SWAY, SWAY,
{ { 3, 2 } }, accent(4, 2, STRETCH), { { 3, 2 } }, accent(5, 2, STRETCH),
{ { 6, 0.5 }, { 7, 0.5 }, { 8, 0.5 }, { 9, 0.5 } },
accent(10, 2, NARROW), accent(11, 3, STRETCH), { { 0, 1 } }),
leave = {
beats = 1.35, key = "loop",
{ 0, 12, 0, 0, to = { nil, nil, "out", sy = 0.96 } }, -- crouches
{ 0.15, 12, 0, 0, sy = 0.96, to = { nil, nil, "in", sy = 1 } },
{ 0.275, 12, 0, -13, to = { 0, -62, "out" } }, -- hops up and drops away
{ 0.5, 12, 0, -62, to = { 0, 358, "in" } },
},
}
-- IA in the centre: jumps in on her entry frame and lands on her standing pose; never leaves
local IA_SWAY = { { 1, 1, x = 0, y = 0 }, { 2, 1 }, { 3, 1 }, { 2, 1 } }
local IA_DANCER = {
origin = { 0, 328 },
enter = {
beats = 2,
{ 0, 0, 0, 357 },
{ 0.35, 0, 0, 357, to = { 0, -38, "out" } },
{ 0.95, 0, 0, -38, to = { 0, -7, "in" } },
{ 1.175, 1, 0, 0, to = { nil, nil, "out", sy = 0.96 } },
{ 1.325, 1, 0, 0, sy = 0.96, to = { nil, nil, "in", sy = 1 } },
{ 1.45, 1, 0, 0 },
},
dance = Timing.steps(IA_SWAY, IA_SWAY, IA_SWAY, IA_SWAY,
{ { 4, 2 } }, accent(0, 2, { sy = 1.02 }), { { 4, 2 } }, accent(5, 2, { sy = 1.02 }),
{ { 6, 0.5 }, { 7, 0.5 }, { 8, 0.5 }, { 9, 0.5 }, { 10, 0.5 },
{ 11, 0.5 }, { 12, 0.5 }, { 13, 0.5 }, { 14, 0.5 }, { 15, 0.5 }, { 16, 2 }, { 2, 1 } }),
}
local IA_INDEX = 4
local IA_DANCERS = Dancer.define({ default = SMALL_DANCER, [IA_INDEX] = IA_DANCER })
-- Fixed slots (far-left .. far-right), from Lumen's dancer positions
local DANCER_CENTERS = { 183, 417, 640, 863, 1097 }
local DANCER_DRAW_ORDER = { 5, 1, 4, 2, 3 } -- outer pairs first, IA on top
-- dancer index by join order: joins fill the centre, left, right, far-left, far-right slots
local DANCER_JOIN_ORDER = { IA_INDEX, 3, 2, 1, 0 }
-- Chibi IA for good hits: hops in growing from 70% size, then floats across the don band,
-- bobbing through three poses every 2 beats
local IA_CHIBI = {
key = "1",
period = 2,
poses = {
{ 0, 0, 0, -12, to = { 0, 1 } },
{ 0.4, 1, 0, 0, to = { 0, 6 } },
{ 0.75, 2, 8, 4, to = { 8, 6 } },
{ 0.875, 2, 8, 5, to = { 8, -14 } },
{ 1.35, 1, 8, -14, to = { 8, -17 } },
{ 1.475, 1, 8, -17, to = { 8, -12 } },
},
}
-- Don band: miku/GUMI's parallax band; the base tiles from x 148
local function donbg_spec()
local spec = DonBG.parallax(-164)
spec.layers[1].x = 148
return spec
end
-- Background
local IaBackground = {}
IaBackground.__index = IaBackground
function IaBackground.new(player_num, bpm)
local self = setmetatable({}, IaBackground)
self.donbg = DonBGBase.new(1, player_num, COLLAB_PATH, donbg_spec())
self.bg_normal = IaBGNormal.new()
self.bg_fever = IaBGFever.new(bpm)
self.fever = Fever.new(bpm, COLLAB_PATH, FEVER_SPEC)
self.dancer = Dancer.BaseDancerGroup.new(0, bpm, MAX_DANCERS, COLLAB_PATH, IA_DANCERS)
local by_index = {}
for _, d in ipairs(self.dancer.dancers) do by_index[d.index] = d end
for i, index in ipairs(DANCER_JOIN_ORDER) do self.dancer.dancers[i] = by_index[index] end
self.renda = RendaController.new(COLLAB_PATH, 0, RendaController.ACROSS)
self.chibi = ChibiController.new(bpm, COLLAB_PATH, 1)
self.is_clear = false
self.is_rainbow = false
self.last_milestone = 1
return self
end
function IaBackground:update(current_ms, bpm)
self.donbg:update(current_ms, bpm)
self.bg_normal:update(current_ms)
self.bg_fever:update(current_ms, bpm)
self.fever:update(current_ms, bpm)
self.dancer:update(current_ms, bpm)
self.renda:update(current_ms)
self.chibi:update(current_ms, bpm)
end
function IaBackground:handle_good(player_num) BaseChibi.add(self.chibi, IA_CHIBI, COLLAB_PATH, player_num) end
function IaBackground:handle_ok(player_num) BaseChibi.add(self.chibi, IA_CHIBI, COLLAB_PATH, player_num) end
function IaBackground:handle_bad(player_num) self.chibi:add_chibi(player_num, true) end
function IaBackground:handle_drumroll(player_num) self.renda:add_renda() end
function IaBackground:handle_balloon(player_num) self.renda:add_renda() end
function IaBackground:handle_gauge(player_num, progress, is_clear, is_rainbow)
self.donbg:set_clear(is_clear)
if is_clear and not self.is_clear then self.bg_fever:start() end
if not is_clear and self.is_clear then self.bg_fever:stop() end
if is_rainbow and not self.is_rainbow then self.fever:start() end
if not is_rainbow and self.is_rainbow then self.fever:stop() end
self.is_clear = is_clear
self.is_rainbow = is_rainbow
local current_milestone
if is_clear then
current_milestone = MAX_DANCERS
else
current_milestone = math.min(MAX_DANCERS - 1, math.floor(progress * MAX_DANCERS))
end
if current_milestone > self.last_milestone then
for _ = 1, current_milestone - self.last_milestone do self.dancer:add_dancer() end
self.last_milestone = current_milestone
elseif current_milestone < self.last_milestone then
for _ = 1, self.last_milestone - current_milestone do self.dancer:leave_dancer() end
self.last_milestone = current_milestone
end
end
function IaBackground:draw_back()
self.bg_normal:draw()
self.bg_fever:draw()
self.donbg:draw()
self.renda:draw()
self.dancer:draw_slots(DANCER_CENTERS, DANCER_DRAW_ORDER)
self.fever:draw()
self.chibi:draw()
end
function IaBackground:draw_fore() end
function IaBackground:destroy()
self.donbg:destroy()
self.bg_normal:destroy()
self.bg_fever:destroy()
self.fever:destroy()
self.dancer:destroy()
self.renda:destroy()
self.chibi:destroy()
end
return IaBackground