local calc = require("#Monocle.Calc")
local dust = require("#Celeste.Dust")
local color = require("#Microsoft.Xna.Framework.Color")
local Engine = require("#monocle.engine")
local Calc = require("#monocle.calc")
local Ease = require("#monocle.ease")
local minput = require("#Monocle.MInput")

function onBegin()
    disableRetry()
	disableMovement()
	player.DummyGravity = false
	wait(1.3)
	setFlag("ZOOM", true)
	miniTextbox("RotationCollab_Lobby_Car_A")
	wait(1.5)
	player.DummyGravity = true
    playSound("event:/game/05_mirror_temple/eyewall_bounce")
	engine.TimeRate = 0.35
	player.DummyAutoAnimate = false
	player.Sprite:Play("roll")
	
	
	--local particlesA = getEntities("ParticleEmitter", "Celeste.Mod.FemtoHelper.")
	--get the badeline shp particles entity
	--local particleA
	--for i, e in ipairs(particlesA) do
		--if not particleA or particleA.Y < e.Y then
		--particleA = e
		--end
	--end
	--get the lowest one bc theres two particle emitters
	--local particleA_from = particleA.Position
	--do this one once
	--local particleA_to = vector2(particleA.Position.X - 1750, particleA.Position.Y - 0)
	--set the easing locations

	--local num_seconds = 0.1
	--for t = 0, num_seconds, Engine.DeltaTime do
		--particleA.Position = particleA_from + (particleA_to - particleA_from)
		--wait()
	--end
	
	
    player.Speed = vector2(900, -325)
	player.ForceCameraUpdate = true;
    wait(0.05)
    player.Speed = vector2(825, -125)
    wait(0.05)
    player.Speed = vector2(775, -50)
    wait(0.05)
    player.Speed = vector2(650, 50)
    wait(0.05)
    player.Speed = vector2(550, 100)
    wait(0.05)
    player.Speed = vector2(450, 150)
    wait(0.05)
    player.Speed = vector2(350, 175)
    wait(0.05)
	player.Speed = vector2(250, 200)
	
    waitUntilOnGround()
    playSound("event:/game/06_reflection/badeline_pull_impact")

	engine.TimeRate = 0.65
	local ground = player.Position.Y
	
	-- playMusic("event:/new_content/music/lvl10/cinematic/bird_crash_first", 0)
--	playMusic("music_farewell_secondbirdcrash_cinematic", 0);
	player.Sprite:Play("roll")
	player.Speed = vector2(200, 0)
	player.DummyAutoAnimate = false
	player.DummyFriction = false

	wait(0.6)
	player.DummyFriction = true
	player.Sprite:Play("rollGetUp")
	engine.TimeRate = 1
	wait(0.8)
	player.Speed = vector2(0, player.Speed.Y)
	wait(1.2)
	
	
	npc = getEntity("MoreCustomNPC", "Celeste.Mod.MaxHelpingHand.Entities.")
	--get the badeline ship entity
	local particles = getEntities("ParticleEmitter", "Celeste.Mod.FemtoHelper.")
	--get the badeline shp particles entity
	
	local particle
	for i, e in ipairs(particles) do
		if not particle or particle.Y > e.Y then
			particle = e
		end
	end
	
	--get the lowest one bc theres two particle emitters
  
	local npc_from = npc.Position
	--do this one once
	local npc_to = vector2(npc.Position.X + 350, npc.Position.Y)
	local particle_from = particle.Position
	--do this one once
	local particle_to = vector2(particle.Position.X + 350, particle.Position.Y)
	
	--set the easing locations

	local num_seconds = 5
	
	for t = 0, num_seconds, Engine.DeltaTime do
		npc.Position = npc_from + (npc_to - npc_from) * Ease.Linear(t)
		particle.Position = particle_from + (particle_to - particle_from) * Ease.Linear(t)
		wait()
	end
	say("RotationCollab_Lobby_Car_B")
	wait(0.2)
	player.DummyAutoAnimate = true
	player.DummyFriction = true;
	walk(88, nil, 0.7, nil)
	
	num_seconds = 2
	
	--minput.Disabled = true
	--player.StateMachine.Locked = true
	--celeste.Input.MoveY.Value = 1
	--wait()
	--celeste.Input.MoveY.Value = 1
	--wait()
	--celeste.Input.MoveY.Value = 1
	--wait()
	--celeste.Input.MoveY.Value = 1
	--wait()
	--minput.Disabled = false
	player.StateMachine.Locked = false
	
	
	npc.Position = vector2(npc.Position.X, npc.Position.Y - 150)
	particle.Position = vector2(particle.Position.X, particle.Position.Y - 150)
	
	say("RotationCollab_Lobby_Car_C")
	endCutscene()
end

function onEnd(room, wasSkipped)
	player.Speed = vector2(0, 0);
	player.DummyAutoAnimate = true
	player.DummyFriction = true;
	player.ForceCameraUpdate = false;
	enableMovement()
	enableRetry()
end