refactor lerp
This commit is contained in:
parent
37eb712518
commit
df22b9ea3f
@ -1,3 +1,5 @@
|
||||
local utils = require "lib.utils.utils"
|
||||
|
||||
--- @alias State "idle"|"run"|"attack"|"hurt"
|
||||
|
||||
--- @class Logic
|
||||
@ -35,10 +37,9 @@ end
|
||||
|
||||
function logic:update(dt)
|
||||
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 fraction = delta / 0.5
|
||||
if fraction >= 1 then -- мы добежали до цели и сейчас в целевой клетке (возможно, промежуточной)
|
||||
local fraction = delta / 0.5 -- бежим одну клетку за 500 мс
|
||||
if fraction >= 1 then -- анимация перемещена завершена
|
||||
self.mapLogic.position = self.mapLogic.runTarget
|
||||
if not self.mapLogic.path:is_empty() then -- еще есть, куда бежать
|
||||
self:runTo(self.mapLogic.path:peek_front())
|
||||
@ -47,8 +48,8 @@ function logic:update(dt)
|
||||
self.state = "idle"
|
||||
self.mapLogic.runTarget = nil
|
||||
end
|
||||
else -- мы не добежали до цели
|
||||
self.mapLogic.displayedPosition = self.mapLogic.position:add(vel:scale(fraction))
|
||||
else -- анимация перемещения не завершена
|
||||
self.mapLogic.displayedPosition = utils.lerp(self.mapLogic.position, self.mapLogic.runTarget, fraction) -- линейный интерполятор
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -69,4 +69,9 @@ function P.keys(t)
|
||||
return _t
|
||||
end
|
||||
|
||||
--- Linear interpolation
|
||||
function P.lerp(from, to, t)
|
||||
return from + (to - from) * t
|
||||
end
|
||||
|
||||
return P
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user