local calc = require("#monocle.calc")
local lua_helper = celeste.Mod.LuaCutscenes.LuaHelper
local ring = {}
local scene = getRoom()
local finishedRoutine = false
local lockPos = player.Center

local function makeCoroutine(func)
  return monocle.Coroutine(lua_helper.LuaCoroutineToIEnumerator(coroutine.create(func)))
end

local function moveCoroutine()
    while true do

        
        
        local lifetime = 2

        local pos = player.Position

        while lifetime > 0 do

            if getHealth() <= 0 then
                puppet:removeSelf()
            end

            while getFlag("pauseIdle") do
                puppet.Speed = vector2(0,0)
                wait(0)
            end
            
            if (puppet.Position.X > pos.X) then
                speedXTweenTo(-300, 0.1, monocle.Ease.QuadIn)
            else
                speedXTweenTo(300, 0.1, monocle.Ease.QuadIn)
            end
        
            if (puppet.Position.Y > pos.Y - 75) then
                speedYTweenTo(-300, 0.1, monocle.Ease.QuadIn)
            else
                speedYTweenTo(300, 0.1, monocle.Ease.QuadIn)
            end

            lifetime = lifetime - engine.deltaTime
            wait(0)

        end


        wait (0)
    end
end

function onBegin()

    setFlag("pauseIdle", false)

    log("test2")
    
    puppet:Add(makeCoroutine(moveCoroutine))

end

function getXSpeed()
    return sign(-(puppet.Position.X - lockPos.X)) * 150
end

function getYSpeed()
    return sign((puppet.Position.X - lockPos.X + 16)) * 150
end

function sign(x)
    return (x<0 and -1) or 1
end  

function abs(x)
    return x * sign(x)
end

function randVector(max)
    return vector2(calc.NextFloat(calc.Random, max), calc.NextFloat(calc.Random, max))
end