local engine = require("#monocle.engine")
local monocle = require("#monocle")
local celeste = require("#celeste")

local coroutineEntity = monocle.Entity()
local sfxSoundSource = celeste.SoundSource()

function onBegin()
  setFlag("attempt", false)
  local level = getLevel()
  local luanet = _G.luanet
  local return_pos = getLevel().Camera.Position
  level:add(coroutineEntity)
  vector = player.Position + vector2(0, 0)
  player.ForceCameraUpdate = true
  disableMovement()


  wait(1)
  player.DummyAutoAnimate = false
  player.Sprite:Play("tired", true)
  wait(1.5)
  say("CommunityVoice_Nadir_Attempt01")
  wait(0.5)
  player.Sprite:Play("tired", false)
  player.DummyAutoAnimate = true
	player.Sprite:Play("idle", true)
  wait(0.5)
  say("CommunityVoice_Nadir_Attempt02")
  wait(0.5)
  walk(28)
  wait(0.5)
  coroutineEntity:add(monocle.Coroutine(getLevel():ZoomTo(vector2(160, 120), 2.0, 3.0)))
  wait(1)
  say("CommunityVoice_Nadir_Attempt03")

  setFlag("attempt", true)
  wait(3)
  playSound("event:/char/madeline/predeath", player.Position)
  wait(0.5)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.5)
  playSound("event:/char/madeline/predeath", player.Position)
  wait(0.3)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.3)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.4)
  --- dont judge i dont know how to make while loops work even if i tried lolol
  playSound("event:/char/madeline/death", player.Position)
  wait(0.3)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.3)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.3)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.3)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.3)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.3)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.3)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.3)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.3)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.3)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.3)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.5)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/death", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/revive", player.Position)
  wait(0.2)
  playSound("event:/char/madeline/death", player.Position)
  wait(1)
  playSound("event:/char/madeline/revive", player.Position)
  setFlag("attempt", false)
  wait(3)
  say("CommunityVoice_Nadir_Attempt04")
  coroutine.yield(level:ZoomBack(2.0))
  enableMovement()

end

function onEnd(room, wasSkipped)
	getLevel():remove(coroutineEntity)
    if wasSkipped then
		player.ForceCameraUpdate = true
		player.DummyAutoAnimate = true
    setFlag("attempt", false)
		enableMovement()
    end
end