Files
YataiDONNijiiro/Scripts/game/score_counter.lua
T

173 lines
6.3 KiB
Lua

local SKIN = {
delay_score_addition = false, -- the counter catches up when a popup finishes, not at once
margin = 30,
pos = { x = 255, y = 277.5 },
popup_p2_offset = 750,
}
ScoreCounter = {}
ScoreCounter.__index = ScoreCounter
local AIB = require("global.ai_battle") -- Z-DON AI battle (nothing on other engines / modes)
local MAX_POPUPS = 5 -- add_popup(value, true) drops a popup past this many
local POPUP_COLOR = { { 254, 102, 0 }, { 84, 250, 238 } } -- by player
local function digit(ch)
return (ch >= 48 and ch <= 57) and ch - 48 or 0
end
function ScoreCounter.new(is_2p, player_num)
local self = setmetatable({}, ScoreCounter)
self.is_2p = is_2p
self.player_num = player_num
self.score = 0
self.stretch = tex.get_animation(4, true)
self.t_cover = tex.get_texture("lane/lane_score_cover")
self.t_number = tex.get_texture("lane/score_number")
self.popups = {}
self.best = 0
self.best_ms = nil
self.best_done = false
self.t_best_plate = tex.get_texture("best_score/plate")
self.t_best_glow = tex.get_texture("best_score/glow")
self.t_best_shine = tex.get_texture("best_score/shine")
return self
end
function ScoreCounter:set_best(score)
self.best = score
end
function ScoreCounter:set_count(score)
if score ~= self.score then
self.score = score
self.stretch:start()
end
end
function ScoreCounter:add_popup(value, limited)
if limited and #self.popups >= MAX_POPUPS then return end
local anim = function(id)
local a = tex.get_animation(id, true)
a:start()
return a
end
local str = tostring(math.max(math.floor(value), 0))
local base = POPUP_COLOR[self.player_num] or POPUP_COLOR[1]
self.popups[#self.popups + 1] = {
str = str, base = base, alpha = 255, y_list = {},
fade_1 = anim(35), move_x = anim(36), fade_2 = anim(37), move_y_pre = anim(38), move_y_fan = anim(39),
}
end
-- score: the player's current score. With delay_score_addition the counter only catches up
-- once a popup has finished, otherwise it follows at once.
function ScoreCounter:update(ms, score)
local delay = SKIN.delay_score_addition
local i = 1
while i <= #self.popups do
local p = self.popups[i]
p.fade_1:update(ms)
p.move_x:update(ms)
p.move_y_pre:update(ms)
p.move_y_fan:update(ms)
p.fade_2:update(ms)
local fade = p.fade_1.is_finished and p.fade_2.attribute or p.fade_1.attribute
p.alpha = 255 * fade
for k = 1, #p.str do p.y_list[k] = p.move_y_fan.attribute + (k - 1) * 5 end
if p.fade_2.is_finished then
table.remove(self.popups, i)
if delay then self:set_count(score) end
else
i = i + 1
end
end
if not delay then self:set_count(score) end
self.stretch:update(ms)
if self.best > 0 and not self.best_ms and not self.best_done and score > self.best then
self.best_ms = ms
end
self.now = ms
end
local BEST_FRAME_MS = 1000 / 60
-- the personal best plate: drops in over 14 frames, pulses and shines until frame 34, holds, fades at 330..335
function ScoreCounter:draw_best(y, p2_offset)
local ms = self.best_ms
if not ms or not self.t_best_plate then return end
local f = (self.now - ms) / BEST_FRAME_MS
if f >= 335 then
self.best_ms, self.best_done = nil, true
return
end
f = f + 5
local scale = f < 14 and 1.2 - 0.25 * (f - 5) / 9 or (f < 19 and 0.95 + 0.05 * (f - 14) / 5 or 1.0)
local alpha = math.min(1, math.max(0, (f - 5) / 9)) * (f > 330 and (335 - f) / 5 or 1)
local x = self.is_2p and 7 or 0
local dy = y + (self.is_2p and (p2_offset - 126) or 0)
tex.draw_texture(self.t_best_plate, { x = x, y = dy, scale = scale, center = true, fade = alpha })
if f >= 14 and f < 34 then
local pulse = f < 19 and 0.45 * (f - 14) / 5 or 0.45 - 0.03 * (f - 19)
local glow_scale = f < 19 and scale or 1.0 + 0.2 * (f - 19) / 15
tex.draw_texture(self.t_best_glow, {
x = x, y = dy, scale = glow_scale, center = true, fade = pulse, blend = "additive",
})
end
if f >= 19 and f < 33 then
tex.draw_texture(self.t_best_shine, { x = x, y = dy, frame = math.min(3, math.floor((f - 19) / 3.5)), fade = alpha })
end
self.best_text = self.best_text or text.create_text("enso_best_score", { 255, 255, 255, 255 }, { 70, 35, 10, 255 },
false, 4.0 / tex.get_screen_scale(), 0)
local label = self.best_text
if label then
label:draw({ x = math.floor(156 + x - label.width / 2 + 0.5), y = math.floor(101 + dy - label.height / 2 + 0.5),
fade = alpha })
end
end
function ScoreCounter:draw(y)
if self.is_2p and tex.ai_active and AIB.active() then -- Z-DON AI battle: the AI lane shows its plate and board, no score
AIB.draw_game()
return
end
if self.is_2p and not self.p2_offset then
self.p2_offset = tex.get_skin_config("game_score_counter_2p_y_offset").y
end
local p2_offset = self.p2_offset or 0
if self.is_2p then
tex.draw_texture(self.t_cover, { mirror = "vertical", y = y + p2_offset })
else
tex.draw_texture(self.t_cover, { y = y })
end
local str = tostring(math.max(self.score, 0))
local margin, stretch = SKIN.margin, self.stretch.attribute
local start_x = SKIN.pos.x - #str * margin
local y_pos = y + SKIN.pos.y + p2_offset
for i = 1, #str do
tex.draw_texture(self.t_number, {
frame = str:byte(i) - 48, x = start_x + (i - 1) * margin, y = y_pos - stretch, y2 = stretch,
})
end
self:draw_best(y, p2_offset)
local direction = self.is_2p and -1 or 1
for _, p in ipairs(self.popups) do
local x = p.move_x.attribute
if math.abs(x) >= 1e-6 then
local px = x - #p.str * margin
local color = { p.base[1], p.base[2], p.base[3], p.alpha }
for i = 1, #p.str do
local y_i = p.move_y_pre.is_finished and p.y_list[i] or p.move_y_pre.attribute
tex.draw_texture(self.t_number, {
color = color, frame = digit(p.str:byte(i)), x = px + (i - 1) * margin,
y = y_i * direction + y + (self.is_2p and SKIN.popup_p2_offset or 0),
})
end
end
end
end