--- Умеет асинхронно ждать какое-то время (для анимаций)
--- @class ResidentSleeperBehavior : Behavior
--- @field private t0 number?
--- @field private sleepTime number?
--- @field private callback voidCallback?
--- @field private state 'running' | 'finished'
local behavior = {}
behavior.__index = behavior
behavior.id = "residentsleeper"

function behavior.new() return setmetatable({}, behavior) end

function behavior:update(_)
    if self.state ~= 'running' then return end

    local t = love.timer.getTime()
    if t >= self.t0 + self.sleepTime then
        self.state = 'finished'
        self.callback()
    end
end

--- @return Task<nil>
function behavior:sleep(ms)
    self.sleepTime = ms / 1000
    return function(callback)
        if self.state == 'running' then
            self.callback()
        end

        self.t0 = love.timer.getTime()
        self.callback = callback
        self.state = 'running'
    end
end

return behavior
