refactor lerp

This commit is contained in:
PeaAshMeter 2025-08-15 06:12:53 +03:00
parent 37eb712518
commit df22b9ea3f
2 changed files with 11 additions and 5 deletions

View File

@ -1,3 +1,5 @@
local utils = require "lib.utils.utils"
--- @alias State "idle"|"run"|"attack"|"hurt" --- @alias State "idle"|"run"|"attack"|"hurt"
--- @class Logic --- @class Logic
@ -35,10 +37,9 @@ end
function logic:update(dt) function logic:update(dt)
if self.state == "run" and self.mapLogic.runTarget then if self.state == "run" and self.mapLogic.runTarget then
local vel = self.mapLogic.runTarget:subtract(self.mapLogic.position) --[[@as Vec3]]
local delta = love.timer.getTime() - self.mapLogic.t0 or love.timer.getTime() local delta = love.timer.getTime() - self.mapLogic.t0 or love.timer.getTime()
local fraction = delta / 0.5 local fraction = delta / 0.5 -- бежим одну клетку за 500 мс
if fraction >= 1 then -- мы добежали до цели и сейчас в целевой клетке (возможно, промежуточной) if fraction >= 1 then -- анимация перемещена завершена
self.mapLogic.position = self.mapLogic.runTarget self.mapLogic.position = self.mapLogic.runTarget
if not self.mapLogic.path:is_empty() then -- еще есть, куда бежать if not self.mapLogic.path:is_empty() then -- еще есть, куда бежать
self:runTo(self.mapLogic.path:peek_front()) self:runTo(self.mapLogic.path:peek_front())
@ -47,8 +48,8 @@ function logic:update(dt)
self.state = "idle" self.state = "idle"
self.mapLogic.runTarget = nil self.mapLogic.runTarget = nil
end end
else -- мы не добежали до цели else -- анимация перемещения не завершена
self.mapLogic.displayedPosition = self.mapLogic.position:add(vel:scale(fraction)) self.mapLogic.displayedPosition = utils.lerp(self.mapLogic.position, self.mapLogic.runTarget, fraction) -- линейный интерполятор
end end
end end

View File

@ -69,4 +69,9 @@ function P.keys(t)
return _t return _t
end end
--- Linear interpolation
function P.lerp(from, to, t)
return from + (to - from) * t
end
return P return P