101 lines
3.9 KiB
Lua
101 lines
3.9 KiB
Lua
local K = tex.get_screen_width() / 640
|
|
local FRAME_MS = 1000 / 60
|
|
local MAX_POPUPS = 5 -- add_popup(value, true) drops a popup past this many
|
|
local ONES_X = 604 * K -- the ones digit's centre
|
|
local STEP = 20 * K
|
|
-- score centre y (swg px; the bottom lane's at 380) and the lane y the engine passes for each lane
|
|
local SCORE_Y = { [false] = 96 * K, [true] = 380 * K }
|
|
local LANE_Y = { [false] = 184 * tex.get_screen_scale(), [true] = tex.get_skin_config("game_p2_y").y }
|
|
local POPUP_RISE = -27 * K -- popup centre above the score's
|
|
local SLIDE_FROM, SLIDE_OVER = -98 * K, 10 * K
|
|
local LIFT, DROP = -20 * K, 47 * K
|
|
|
|
ScoreCounter = {}
|
|
ScoreCounter.__index = ScoreCounter
|
|
|
|
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.pops = {}
|
|
self.t_number = tex.get_texture("game/t14_hud/score_num")
|
|
self.t_add = tex.get_texture("game/t14_hud/" .. (player_num == 2 and "score_add_2p" or "score_add_1p"))
|
|
local info = tex.get_texture_info("t14_hud", "score_num")
|
|
self.w, self.h = info and info.width or 48, info and info.height or 48
|
|
self.popups = {}
|
|
self.ms = 0
|
|
return self
|
|
end
|
|
|
|
function ScoreCounter:set_count(score)
|
|
if score == self.score then return end
|
|
for i = 0, 7 do
|
|
local p = 10 ^ i
|
|
if (self.score // p) % 10 ~= (score // p) % 10 or (self.score < p) ~= (score < p) then self.pops[i] = self.ms end
|
|
end
|
|
self.score = score
|
|
end
|
|
|
|
function ScoreCounter:add_popup(value, limited)
|
|
if limited and #self.popups >= MAX_POPUPS then return end
|
|
self.popups[#self.popups + 1] = { str = tostring(math.max(math.floor(value), 0)), ms = self.ms }
|
|
end
|
|
|
|
local function popup_frames(p) return 25 + #p.str - 1 end
|
|
|
|
function ScoreCounter:update(ms, score)
|
|
self.ms = ms
|
|
local i = 1
|
|
while i <= #self.popups do
|
|
local p = self.popups[i]
|
|
if (ms - p.ms) / FRAME_MS >= popup_frames(p) then
|
|
table.remove(self.popups, i)
|
|
self:set_count(score)
|
|
else
|
|
i = i + 1
|
|
end
|
|
end
|
|
if #self.popups == 0 then self:set_count(score) end
|
|
end
|
|
|
|
local function pop(f)
|
|
if f < 0 or f >= 9 then return 1 end
|
|
if f < 2 then return 1 + .1 * f end
|
|
return 1.2 - .2 * (f - 2) / 7
|
|
end
|
|
|
|
-- Taiko 14 score (enso_score): right-aligned digits under the soul gauge, each stretching up when it changes. The
|
|
-- points slide in above it, lift and drop into it one digit at a time, ones first; the score catches up as they land.
|
|
function ScoreCounter:draw(y)
|
|
local cy = y + SCORE_Y[self.is_2p] - LANE_Y[self.is_2p]
|
|
local str = tostring(math.max(self.score, 0))
|
|
for i = 0, #str - 1 do
|
|
local p = pop((self.ms - (self.pops[i] or -1e9)) / FRAME_MS)
|
|
local h = self.h * p
|
|
tex.draw_texture(self.t_number, { frame = (self.score // 10 ^ i) % 10, x = ONES_X - STEP * i - self.w / 2,
|
|
y = cy + self.h / 2 - h, y2 = h - self.h })
|
|
end
|
|
|
|
for _, p in ipairs(self.popups) do
|
|
local f = (self.ms - p.ms) / FRAME_MS
|
|
local dx, a = 0, 1
|
|
if f < 6 then dx, a = SLIDE_FROM + (SLIDE_OVER - SLIDE_FROM) * f / 6, f / 6
|
|
elseif f < 9 then dx = SLIDE_OVER * (9 - f) / 3 end
|
|
local n = #p.str
|
|
for k = 0, n - 1 do -- k = place, ones first
|
|
local dy, ak = 0, a
|
|
local up_end = 21 + k
|
|
if f >= 17 and f < up_end then dy = LIFT * (f - 17) / (up_end - 17)
|
|
elseif f >= up_end then
|
|
local d = math.min(1, (f - up_end) / 4)
|
|
dy, ak = LIFT + (DROP - LIFT) * d, 1 - d
|
|
end
|
|
if ak > 0 then
|
|
tex.draw_texture(self.t_add, { frame = p.str:byte(n - k) - 48, x = ONES_X - STEP * k - self.w / 2 + dx,
|
|
y = cy + POPUP_RISE - self.h / 2 + dy, fade = ak < 1 and ak or nil })
|
|
end
|
|
end
|
|
end
|
|
end
|