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 damageRoutine()
    if player.DashAttacking then
        return
    end

    die()
end

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

local function makeDartRoutine(entity)
    return function()
        
        wait(1)

        local lifetime = 5
        local target = calc.Toward(entity.Position, player.Center, 1)

        local t = 1.5

        while lifetime > 0 do
            
            entity.Position = entity.Position + target * t
            
            t = t + 0.1

            lifetime = lifetime - engine.deltaTime

            wait(0)
        end

        entity:RemoveSelf()
        finishedRoutine = true
    end
end



function onBegin()

    finishedRoutine = false

    local offset = randVector(16)
    
    for i = 1, 10, 1 do

        log("test")

        ring[#ring+1] = getNewBasicAttackEntity(puppet.Center + randVector(32) + offset, getCircle(4), "badeline_projectile", true, damageRoutine)
        
        
        
        ring[#ring]:add(makeCoroutine(makeDartRoutine(ring[#ring])))
        
        

        scene:add(ring[#ring])
    end

    
    wait(2)

    
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