78 lines
3.0 KiB
Lua
78 lines
3.0 KiB
Lua
local utils = require "lib.utils.utils"
|
|
|
|
--- Отвечает за размещение и перемещение по локации
|
|
--- @class MapBehavior : Behavior
|
|
--- @field position Vec3
|
|
--- @field runTarget Vec3 точка, в которую в данный момент бежит персонаж
|
|
--- @field displayedPosition Vec3 точка, в которой персонаж отображается
|
|
--- @field t0 number время начала движения для анимациии
|
|
--- @field path Deque путь, по которому сейчас бежит персонаж
|
|
--- @field size Vec3
|
|
local mapBehavior = {}
|
|
mapBehavior.__index = mapBehavior
|
|
mapBehavior.id = "map"
|
|
|
|
|
|
--- @param position? Vec3
|
|
--- @param size? Vec3
|
|
function mapBehavior.new(position, size)
|
|
return setmetatable({
|
|
position = position or Vec3({}),
|
|
displayedPosition = position or Vec3({}),
|
|
size = size or Vec3({ 1, 1 }),
|
|
}, mapBehavior)
|
|
end
|
|
|
|
--- @param path Deque
|
|
function mapBehavior:followPath(path)
|
|
if path:is_empty() then return end
|
|
self.position = self.displayedPosition
|
|
self.owner:try(Tree.behaviors.animated, function(animated)
|
|
animated:play("run", true)
|
|
end)
|
|
self.path = path;
|
|
---@type Vec3
|
|
local nextCell = path:peek_front()
|
|
self:runTo(nextCell)
|
|
path:pop_front()
|
|
end
|
|
|
|
--- @param target Vec3
|
|
function mapBehavior:runTo(target)
|
|
self.t0 = love.timer.getTime()
|
|
self.runTarget = target
|
|
self.owner:try(Tree.behaviors.animated,
|
|
function(animated)
|
|
if target.x < self.position.x then
|
|
animated.side = Tree.behaviors.animated.LEFT
|
|
elseif target.x > self.position.x then
|
|
animated.side = Tree.behaviors.animated.RIGHT
|
|
end
|
|
end
|
|
)
|
|
end
|
|
|
|
function mapBehavior:update(dt)
|
|
if self.runTarget then
|
|
local delta = love.timer.getTime() - self.t0 or love.timer.getTime()
|
|
local fraction = delta /
|
|
(0.5 * self.runTarget:subtract(self.position):length()) -- бежим одну клетку за 500 мс, по диагонали больше
|
|
if fraction >= 1 then -- анимация перемещена завершена
|
|
self.position = self.runTarget
|
|
if not self.path:is_empty() then -- еще есть, куда бежать
|
|
self:runTo(self.path:peek_front())
|
|
self.path:pop_front()
|
|
else -- мы добежали до финальной цели
|
|
self.owner:try(Tree.behaviors.animated, function(animated)
|
|
animated:play("idle", true)
|
|
end)
|
|
self.runTarget = nil
|
|
end
|
|
else -- анимация перемещения не завершена
|
|
self.displayedPosition = utils.lerp(self.position, self.runTarget, fraction) -- линейный интерполятор
|
|
end
|
|
end
|
|
end
|
|
|
|
return mapBehavior
|