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

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

function onBegin()
  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()
  setFlag("Nightmare-End", false)
  setFlag("Removebgsandcolorgrade", false)
  
  wait(0.5)
  walk(16)
  wait(0.5)
  walk(-30)
  wait(0.3)
  walk(25)
  wait(0.2)
  say("CommunityVoice_Nadir_Succumb01")
  walk(-19)
  wait(1.5)
  say("CommunityVoice_Nadir_Succumb02")
  playSound("event:/game/06_reflection/badeline_freakout_1", player.Position)
  say("CommunityVoice_Nadir_Succumb02a")
  walk(-10)
  say("CommunityVoice_Nadir_Succumb03")
  walk(16)
  say("CommunityVoice_Nadir_Succumb03b")
  wait(0.5)	
  coroutineEntity:add(monocle.Coroutine(getLevel():ZoomTo(vector2(160, 130), 2.0, 3.0)))
  player.DummyAutoAnimate = false
  player.Sprite:Play("tired", true)
  wait(1)
  say("CommunityVoice_Nadir_Succumb04")
  setFlag("Nightmare-End", true)
  wait(3.0)
  setFlag("Removebgsandcolorgrade", true)
  wait(1.0)
  playSound("event:/game/04_cliffside/gondola_scaryhair_01", player.Position)
  say("CommunityVoice_Nadir_Succumb05")
  playSound("event:/game/06_reflection/badeline_freakout_4", player.Position)
  say("CommunityVoice_Nadir_Succumb06")
  wait(4.0)
  setFlag("teleportEpilogue", true)
  wait(30.0)
end

function onEnd(room, wasSkipped)
	getLevel():remove(coroutineEntity)
    if wasSkipped then
		player.ForceCameraUpdate = true
		player.DummyAutoAnimate = true
		setFlag("teleportingskip", true)
    setFlag("Removebgsandcolorgrade", true)
    setFlag("Nightmare-End", true)
		enableMovement()
    end
end