ExperimentalGear/scripts/titlescreen/splash.lua

127 lines
3.5 KiB
Lua

local Common = require("common.util")
local Dim = require("common.dimensions")
local splashSample = "titlescreen/splash/splash1.wav"
game.LoadSkinSample(splashSample)
local splash1SfxPlayed = false
require "common.globals"
require "common.class"
local Page = require "api.page.page"
local KShootManiaPage = require "titlescreen.pages.splash.kshootmaniapage"
local USCPage = require "titlescreen.pages.splash.uscpage"
local TeamExceedPage = require "titlescreen.pages.splash.teamexceedpage"
local CreditsPage = require "titlescreen.pages.splash.creditspage"
---@class SplashPage : Page
---@field pages Page[]
---@field currentPage integer
---@field _isTransitioning boolean # actively fading between pages
local SplashPage = {
__name = "SplashScreen",
BACKGROUND_COLOR = {255, 255, 255},
FADE_DURATION = 0.5
}
---Create a new SplashScreen instance
---@param params? SplashPage
---@return SplashPage
function SplashPage.new(params)
local self = CreateInstance(SplashPage, params, Page)
self.currentPage = 1
self.content = {
KShootManiaPage.new(),
USCPage.new(),
TeamExceedPage.new(),
CreditsPage.new()
}
self._isTransitioning = false
-- set callbacks
for index, page in ipairs(self.content) do
if index < #self.content then
page.onInvalidation = function (page_inst)
self._isTransitioning = true
self.currentPage = index + 1
end
else -- last index
page.onInvalidation = function (page_inst)
self:onInvalidation()
end
end
end
return self
end
function SplashPage:init()
self.currentPage = 1
self._isTransitioning = false
Page.init(self)
end
function SplashPage:handleButtonInput(button)
if button == game.BUTTON_STA then
game.StopSample(splashSample)
self:onInvalidation()
end
end
---Fade between pages
---@param fadeDuration? number
---@param fadeColor? integer[]
function SplashPage:fadeTransition(deltaTime, fadeDuration, fadeColor)
fadeDuration = fadeDuration or 0.5
fadeColor = fadeColor or {255, 255, 255}
local fadeAlpha = 0.0
-- reset variables on first call
if not self["__fadeStarted"] then
self.__fadeTimer = 0.0
self.__fadeStarted = true
end
local halfDuration = fadeDuration / 2
if self.__fadeTimer < halfDuration then
fadeAlpha = Common.lerp(self.__fadeTimer, 0, 0, halfDuration, 255) -- fade out
elseif self.__fadeTimer < fadeDuration then
fadeAlpha = Common.lerp(self.__fadeTimer, halfDuration, 255, fadeDuration, 0) -- fade in
else
self.__fadeStarted = false -- fade done, reset variable
self._isTransitioning = false
end
local fillColor = {table.unpack(fadeColor, 1, 3)} -- copy color table
table.insert(fillColor, fadeAlpha) -- add alpha
gfx.BeginPath()
gfx.FillColor(table.unpack(fillColor))
gfx.Rect(0, 0, Dim.design.width, Dim.design.height)
gfx.Fill()
self.__fadeTimer = self.__fadeTimer + deltaTime
end
function SplashPage:drawBackground(deltaTime)
gfx.BeginPath()
gfx.FillColor(table.unpack(self.BACKGROUND_COLOR))
gfx.Rect(0, 0, Dim.design.width, Dim.design.height)
gfx.Fill()
end
function SplashPage:drawContent(deltaTime)
self.content[self.currentPage]:render(deltaTime)
end
function SplashPage:drawForeground(deltaTime)
local fadeColor = {255, 255, 255}
if self._isTransitioning then
self:fadeTransition(deltaTime, self.FADE_DURATION, fadeColor)
end
end
return SplashPage