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()

  theo = celeste.NPC(player.Position + vector2(-40, 0))
  level:Add(theo)
  local sprite = celeste.GFX.SpriteBank:Create("theo")
  theo:Add(sprite)
  theo.Sprite = sprite
  sprite:Play("sitDown", false, false)

  walk(-6)
  player.DummyAutoAnimate = false
  player.Sprite:Play("sitDown", true)
  wait(1)
  say("CommunityVoice_Nadir_Epilogue01")
  wait(1)
  setFlag("Nightmare-End", false)
  say("CommunityVoice_Nadir_Epilogue02")
  setFlag("Nightmare-End", true)
  wait(2)
  say("CommunityVoice_Nadir_Epilogue03")
  wait(3)
  setFlag("ActuallyEnd", true)

end

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