From eb45ffade42c008c6b8ee03c403e31a8cf635df0 Mon Sep 17 00:00:00 2001 From: PeaAshMeter Date: Fri, 7 Nov 2025 09:19:27 +0300 Subject: [PATCH] Make the AnimationNode handle its own state --- lib/animation_node.lua | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/animation_node.lua b/lib/animation_node.lua index f3ef786..a5c159f 100644 --- a/lib/animation_node.lua +++ b/lib/animation_node.lua @@ -1,3 +1,5 @@ +local easing = require "lib.utils.easing" + --- @alias voidCallback fun(): nil --- @alias animationRunner fun(node: AnimationNode) @@ -28,6 +30,9 @@ --- @field children AnimationNode[] --- @field finish voidCallback --- @field onEnd voidCallback? +--- @field duration number продолжительность в миллисекундах +--- @field easing ease функция смягчения +--- @field t number прогресс анимации local animation = {} animation.__index = animation @@ -50,7 +55,21 @@ function animation:chain(children) return self end ---- @param data {[1]: animationRunner?, onEnd?: voidCallback, children?: AnimationNode[]} +--- Возвращает текущий прогресс анимации с учетом смягчения +function animation:getValue() + return self.easing(self.t) +end + +function animation:update(dt) + if self.t < 1 then + self.t = self.t + dt * 1000 / self.duration -- в знаменателе продолжительность анимации в секундах + else + self.t = 1 + self:finish() + end +end + +--- @param data {[1]: animationRunner?, onEnd?: voidCallback, duration: number?, easing: ease?, children?: AnimationNode[]} --- @return AnimationNode local function new(data) local t = setmetatable({}, animation) @@ -61,6 +80,9 @@ local function new(data) t.count = 1 -- своя анимация t.children = {} t:chain(data.children or {}) + t.duration = data.duration or 1000 + t.easing = data.easing or easing.linear + t.t = 0 t.finish = function() t:bubbleUp() for _, anim in ipairs(t.children) do