Files
PyTaikoGreen/Scripts/background/bg_objects/donbg/lumen.lua
T

84 lines
3.2 KiB
Lua

local DonBGBase = require("background/bg_objects/donbg/base")
local Timing = require("background/bg_objects/timing")
local chain_moves, sum, Loop = Timing.chain_moves, Timing.sum, Timing.Loop
-- LumenDonBG: the Lumen collabs' don band (Miku, GUMI). A static base with two parallax
-- layers (fast, slow) scrolling over it. Clearing fades in
-- a recoloured set plus a pulsing sparkle layer that rides with the slow layer.
local DONBG_SCROLL_MS = 2000 -- fast layer moves two tiles, slow layer one
local DONBG_FADE_MS = 150
local SPARKLE_PULSE = { { 500, -500 }, { 500, 500 } } -- opacity in thousandths
local LumenDonBG = setmetatable({}, { __index = DonBGBase })
LumenDonBG.__index = LumenDonBG
-- fast_x: the fast layer's tile phase
function LumenDonBG.new(player_num, path, fast_x)
local self = setmetatable(DonBGBase.new(1, player_num, path), LumenDonBG)
local function id(key) return tex.get_texture(self.path .. "/" .. self.name .. "/" .. key) or 0 end
self._id_fast = id("fast")
self._id_slow = id("slow")
self._id_sparkle = id("sparkle")
self.tile = self.bg_texture.width
self.fast_x = math.floor((fast_x or 0) * tex.get_screen_scale())
self.fast = anim.move(DONBG_SCROLL_MS, { total_distance = -2 * self.tile, loop = true })
self.slow = anim.move(DONBG_SCROLL_MS, { total_distance = -self.tile, loop = true })
self.fast:start()
self.slow:start()
self.sparkle = Loop.new(1000, function() return { pulse = chain_moves(SPARKLE_PULSE, 1, 1) } end)
self.clear_fade = nil
return self
end
-- fades the clear set in and back out, instead of the base's cut back to normal
function LumenDonBG:set_clear(is_clear)
if is_clear == self.is_clear then return end
self.is_clear = is_clear
self.clear_fade = anim.fade(DONBG_FADE_MS, {
initial_opacity = is_clear and 0 or 1,
final_opacity = is_clear and 1 or 0,
})
self.clear_fade:start()
end
function LumenDonBG:update(current_time_ms)
self:update_dark(current_time_ms)
self.fast:update(current_time_ms)
self.slow:update(current_time_ms)
self.sparkle:update(current_time_ms)
if self.clear_fade then self.clear_fade:update(current_time_ms) end
end
local function draw_tiles(id, frame, fade, x, y, width)
local screen_width = tex.get_screen_width()
while x < screen_width do
tex.draw_texture(id, { frame = frame, fade = fade, x = x, y = y })
x = x + width
end
end
function LumenDonBG:draw_textures(fade, y, frame)
draw_tiles(self._id_bg, frame, fade, 0, y, self.tile)
draw_tiles(self._id_fast, frame, fade, self.fast_x + self.fast.attribute, y, self.tile)
draw_tiles(self._id_slow, frame, fade, self.slow.attribute, y, self.tile)
if frame == 1 then
local pulse = 1 + sum(self.sparkle.anims.pulse) / 1000
draw_tiles(self._id_sparkle, 0, fade * pulse, self.slow.attribute, y, self.tile)
end
end
function LumenDonBG:draw_band(y)
self:draw_textures(1.0, y, 0)
if self.clear_fade and self.clear_fade.attribute > 0 then
self:draw_textures(self.clear_fade.attribute, y, 1)
end
end
function LumenDonBG:draw(y)
if not self.sparkle.anims then return end -- not updated yet
self:draw_dark(LumenDonBG.draw_band, y or 0)
end
return LumenDonBG