--- @alias State "idle"|"run"|"attack"|"hurt" --- @class Logic --- @field id Id --- @field mapLogic MapLogic --- @field state State local logic = {} logic.__index = logic --- @param id Id --- @param position? Vec3 --- @param size? Vec3 local function new(id, position, size) return setmetatable({ id = id, mapLogic = (require 'lib.character.map_logic').new(id, position, size), state = "idle" }, logic) end --- @param path Deque function logic:followPath(path) if path:is_empty() then return end self.mapLogic.path = path; self:runTo(path:peek_front()) path:pop_front() end --- @param target Vec3 function logic:runTo(target) self.mapLogic.t0 = love.timer.getTime() self.state = "run" self.mapLogic.runTarget = target 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 -- мы добежали до цели и сейчас в целевой клетке (возможно, промежуточной) self.mapLogic.position = self.mapLogic.runTarget if not self.mapLogic.path:is_empty() then -- еще есть, куда бежать self:runTo(self.mapLogic.path:peek_front()) self.mapLogic.path:pop_front() else -- мы добежали до финальной цели self.state = "idle" self.mapLogic.runTarget = nil end else -- мы не добежали до цели self.mapLogic.displayedPosition = self.mapLogic.position:add(vel:scale(fraction)) end end Tree.level.characterGrid:add(self.id) end return { new = new }