Neckrat af792bd2d5 refactor character & grid again
Co-authored-by: Ivan Yuriev <ivanyr44@gmail.com>
2025-08-09 23:22:34 +03:00

46 lines
1.6 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

--- @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)
}, logic)
end
--- @param target Vec3
function logic:runTo(target)
self.state = "run"
self.mapLogic.runTarget = target
end
function logic:update(dt)
if self.state == "run" and self.mapLogic.runTarget then
if self.mapLogic.position:floor() == self.mapLogic.runTarget:floor() then -- мы добежали до цели и сейчас в целевой клетке
self.state = "idle"
self.mapLogic.runTarget = nil
else -- мы не добежали до цели
local vel = (self.mapLogic.runTarget:subtract(self.mapLogic.position):normalize() --[[@as Vec3]]
):scale(2 * dt) -- бежим 2 условных метра в секунду
self.mapLogic.position = self.mapLogic.position:add(vel)
end
end
if self.mapLogic.position ~= self.mapLogic.latestPosition then
-- типа уведомление о том, что положение (на уровне клеток) изменилось
Tree.level.characterGrid:remove(Tree.level.characters[self.id])
Tree.level.characterGrid:add(Tree.level.characters[self.id])
end
end
return { new = new }