Files
PyTaikoGreen/Scripts/background/bg_objects/donbg/base.lua
T
2026-10-07 13:45:15 -04:00

109 lines
3.0 KiB
Lua

DonBGBase = {}
DonBGBase.__index = DonBGBase
function DonBGBase.new(index, player_num, path)
local self = setmetatable({}, DonBGBase)
self.name = string.format("%d_%d", index - 1, player_num)
self.path = path
tex.load_folder(path, string.format("donbg/%s", self.name))
self.bg_texture = tex.get_texture_info(self.name, "background")
self.move = anim.move(3000, {
total_distance = -self.bg_texture.width,
loop = true
})
self.move:start()
self.is_clear = false
self.clear_fade = tex.get_animation(1, "background")
self._id_bg = tex.get_texture(self.path .. "/" .. self.name .. "/" .. "background") or 0
return self
end
-- miss streak: the band is multiplied towards black (colour of draw_texture), eased in/out
local DARK_MIN, DARK_FADE_MS = 0.65, 300
function DonBGBase:set_dark(on)
self.dark_target = on and 1 or 0
end
function DonBGBase:update_dark(current_time_ms)
local dt = current_time_ms - (self.dark_last_ms or current_time_ms)
self.dark_last_ms = current_time_ms
local d, target = self.dark or 0, self.dark_target or 0
local step = dt / DARK_FADE_MS
self.dark = d + math.max(-step, math.min(step, target - d))
end
function DonBGBase:dark_color()
if not self.dark or self.dark <= 0 then return nil end
local v = math.floor(255 * (1 - (1 - DARK_MIN) * self.dark))
return { v, v, v, 255 }
end
function DonBGBase:set_clear(is_clear)
if is_clear and not self.is_clear then
self.is_clear = true
self.clear_fade:start()
end
if not is_clear and self.is_clear then
self.is_clear = false
self.clear_fade:reset()
end
end
function DonBGBase:update(current_time_ms)
self:update_dark(current_time_ms)
self.move:update(current_time_ms)
self.clear_fade:update(current_time_ms)
end
function DonBGBase:draw_textures(fade, y, frame)
for i = 0, 4 do
tex.draw_texture(self._id_bg, {
frame = frame,
fade = fade,
x = (i * self.bg_texture.width) + self.move.attribute,
y = y
})
end
end
function DonBGBase:draw_band(y)
y = y or 0
self:draw_textures(1.0, y, 0)
if self.is_clear then
self:draw_textures(self.clear_fade.attribute, y, 1)
end
end
-- Draws the band through `inner`, multiplying every texture it draws by the miss-streak
-- darkness. Subclasses draw with their own tex.draw_texture calls, so wrap that once here.
function DonBGBase:draw_dark(inner, y)
local c = self:dark_color()
if not c then return inner(self, y) end
local orig = tex.draw_texture
tex.draw_texture = function(id, p)
p = p or {}
p.color = c
return orig(id, p)
end
local ok, err = pcall(inner, self, y)
tex.draw_texture = orig
if not ok then error(err, 0) end
end
function DonBGBase:draw(y)
self:draw_dark(DonBGBase.draw_band, y or 0)
end
function DonBGBase:destroy()
tex.unload_folder(self.path, string.format("donbg/%s", self.name))
end
return DonBGBase