require "Dialogs"
Arena.Resize(595, 130)

Black = CreateSprite("Rotatory Arena Pixel", "Top")
Black.Scale(660, 500)
Black.MoveTo(320, 240)
Black.color = {0,0,0}
Encounter["GameMusic"] = "Fade"
Alteracion = CreateSprite("Specials/Alteracion 1")
Alteracion.SetAnimation({"Specials/Alteracion 1","Specials/Alteracion 2","Specials/Alteracion 3","Specials/Alteracion 4","Specials/Alteracion 5"}, 0.05)
Alteracion.StopAnimation()
Alteracion.MoveTo(320, 240)
Alteracion.alpha = 0

phases = {0}

function Update()
	DialogUpdate()
	if Encounter["GameMusic"] == "End" or Encounter["phase"] == 3 then
		if phases[1] == 0 then
			Encounter["phase"] = 3
			Encounter["progress"] = 0
			Encounter.Call("ContinueMusic")
			phases[1] = 1
		elseif phases[1] == 1 and NewAudio.GetPlayTime("One") > 2.31 then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Dialog("[noskip][waittall:1.5]Dentro de las profundidades del\rALMA de El Gran Papyrus[w:20], algo resuena.[w:10000000] ", {X = 71, Y = 210}, nil, true, 1)
			elseif GetAlMightyGlobal("Idioma") == "Russian" then
				Dialog("[noskip][waittall:1.5]В глубине ДУШИ Великого Папируса[w:20],\rчто-то пульсирует.[w:10000000] ", {X = 71, Y = 210}, nil, true, 1)
			else
				Dialog("[noskip][waittall:1.5]Within the depths of The Great\rPapyrus's SOUL[w:20], something's resonating.[w:10000000] ", {X = 71, Y = 210}, nil, true, 1)
			end
			phases[1] = 2
		elseif phases[1] == 2 and NewAudio.GetPlayTime("One") > 5.35 then
			if not Sanso then
                Sanso = CreateSprite("Sans/NewAura","Top")
                Sanso.Scale(2, 2)
                Sanso.MoveTo(320, 310)
                Sanso.alpha = 0
            end
            if NewAudio.GetPlayTime("One") <= 9.05 then
                Sanso.alpha = math.min(0.693/2.7*(NewAudio.GetPlayTime("One") - 5.35), 0.693)
            else
                Sanso.alpha = 0
                Dialog("", {X = 0, Y = 0}, nil, nil, 1)
                Encounter.Call("InitiatePhase3", 1)
                Black2 = CreateSprite("Rotatory Arena Pixel", "Top")
                Black2.Scale(660, 500)
                Black2.MoveTo(320, 240)
                Black2.color = {0,0,0}
                phases[1] = 3
            end
        elseif phases[1] == 3 and NewAudio.GetPlayTime("One") > 11.61 then
            if NewAudio.GetPlayTime("One") <= 12.05 then
                Black2.alpha = 1 - 1/0.44*(NewAudio.GetPlayTime("One") - 11.61)
            else
                Black2.alpha = 0
                Sanso.Scale(0, 0)
                Sanso.layer = "Bottom"
                Sanso.layer = "Top"
				Sanso.Set("Sans/Aura")
                AnimateSans = 0
                phases[1] = 4
            end
        elseif phases[1] == 4 and NewAudio.GetPlayTime("One") > 12.05 then
            if NewAudio.GetPlayTime("One") <= 13.71 then
                Sanso.alpha = 0.4/1.66*(NewAudio.GetPlayTime("One") - 12.05)
                Sanso.Scale((1 + 0.8/1.66*(NewAudio.GetPlayTime("One") - 12.05))*1.92,( 1 + 0.8/1.66*(NewAudio.GetPlayTime("One") - 12.05))*1.92)
            else
                Encounter.Call("SetHead", "Start2")
                Sanso.alpha = 0.4
                Sanso.Scale(1.8*1.92, 1.8*1.92)
                phases[1] = 5
            end
		elseif phases[1] == 5 and NewAudio.GetPlayTime("One") > 15.51 then
			Encounter.Call("SetHead", "Start3")
			phases[1] = 6
		elseif phases[1] == 6 and NewAudio.GetPlayTime("One") > 16.41 then
			Rayos = {CreateSprite("Rotatory Arena Pixel","Top"), CreateSprite("Rotatory Arena Pixel","Top")}
			for i=1,#Rayos do
				Rayos[i].Scale(2000, 10)
				Rayos[i].alpha = 0
				Rayos[i].rotation = 90*(i - 1)
				Rayos[i].SetVar("Vel", -2 + 6*(i-1))
			end
			Encounter.Call("InitiatePhase3", 2)
			Sanso.layer = "Bottom"
			Sanso.layer = "Top"
			Black2.layer = "Bottom"
			Black2.layer = "Top"
			phases[1] = 7
		elseif phases[1] == 7 and NewAudio.GetPlayTime("One") > 16.41 then
			if NewAudio.GetPlayTime("One") <= 16.91 then
				Encounter["HandL"].MoveTo(274, Encounter["Body"].absy - 14 + 10*math.sin(math.rad(90/0.5*(NewAudio.GetPlayTime("One") - 16.41))))
				Encounter["HandR"].MoveTo(378, Encounter["Body"].absy - 4 + 10*math.sin(math.rad(90/0.5*(NewAudio.GetPlayTime("One") - 16.41))))
			else
				Encounter.Call("InitiatePhase3", 3)
				phases[1] = 8
			end
		elseif phases[1] == 8 and NewAudio.GetPlayTime("One") > 17.21 then
			ShakeTimer = 0
			ShakeHands = -1
			ShakeBody = 0
			Black2.color = {1,1,1}
			phases[1] = 9
		elseif phases[1] == 9 and NewAudio.GetPlayTime("One") > 18.01 then
			if NewAudio.GetPlayTime("One") <= 20.12 then
				Misc.ShakeScreen(1, math.ceil(10*(NewAudio.GetPlayTime("One") - 18.01)/2.11), false)
				for i=1,#Rayos do
					Rayos[i].alpha = 1/2.11*(NewAudio.GetPlayTime("One") - 18.01)
				end
				Black2.alpha = 1/2.11*(NewAudio.GetPlayTime("One") - 18.01)
			else
				Misc.ShakeScreen(1, 10, false)
				for i=1,#Rayos do
					Rayos[i].alpha = 1
				end
				Black2.alpha = 1
				Encounter.Call("PapsMoveTo", {330, 240})
				Encounter.Call("InitiatePhase3", 4)
				for i=1,#Rayos do
					Rayos[i].layer = "Bottom"
					Rayos[i].layer = "Top"
				end
				Encounter["HandL"].layer = "Bottom"
				Encounter["HandL"].layer = "Top"
				Encounter["HandR"].layer = "Bottom"
				Encounter["HandR"].layer = "Top"
				Black2.layer = "Bottom"
				Black2.layer = "Top"
				ShakeHands = nil
				ShakeTimer = nil
				ShakeBody = nil
				Sanso.Remove()
				Sanso = nil
				AnimateSans = nil
				phases[1] = 10
			end
		elseif phases[1] == 10 and NewAudio.GetPlayTime("One") > 20.15 then
			if NewAudio.GetPlayTime("One") <= 20.84 then
				Misc.ShakeScreen(1, 10, false)
				for i=1,#Rayos do
					Rayos[i].alpha = 1 - 1/0.69*(NewAudio.GetPlayTime("One") - 20.15)
				end
				Black2.alpha = 1 - 1/0.69*(NewAudio.GetPlayTime("One") - 20.15)
			else
				for i=1,#Rayos do
					Rayos[i].alpha = 0
				end
				Black2.alpha = 0
				phases[1] = 11
			end
		elseif phases[1] == 11 and NewAudio.GetPlayTime("One") > 21.12 then
			if not Sound then
				Audio.PlaySound("spearswing")
				Sound = true
			end
			if not Encounter["Sans"]["Legs"] then
				Encounter.Call("SpawnSans")
				Encounter["Legs"].layer = "Bottom"
				Encounter["Legs"].layer = "Top"
				Encounter["HandR"].layer = "Bottom"
				Encounter["HandR"].layer = "Top"
				for i=1,#Rayos do
					Rayos[i].layer = "Bottom"
					Rayos[i].layer = "Top"
				end
				Encounter["HandL"].layer = "Bottom"
				Encounter["HandL"].layer = "Top"
				Black2.layer = "Bottom"
				Black2.layer = "Top"
			end
			if NewAudio.GetPlayTime("One") <= 21.22 then
				Encounter["Head"].MoveTo(-1 - 6*math.sin(math.rad(90/0.1*(NewAudio.GetPlayTime("One") - 21.12))), 51)
				Encounter["Body"].MoveTo(-3 + 6*math.sin(math.rad(90/0.1*(NewAudio.GetPlayTime("One") - 21.12))), 121)
				Encounter["HandR"].MoveTo(376 + 6*math.sin(math.rad(90/0.1*(NewAudio.GetPlayTime("One") - 21.12))), Encounter["Body"].absy + 1 - 6*math.sin(math.rad(90/0.1*(NewAudio.GetPlayTime("One") - 21.12))))
			elseif NewAudio.GetPlayTime("One") <= 21.52 then
				Encounter["Head"].MoveTo(-1 - 6*math.sin(math.rad(90/0.3*(NewAudio.GetPlayTime("One") - 20.92))), 51)
				Encounter["Body"].MoveTo(-3 + 6*math.sin(math.rad(90/0.3*(NewAudio.GetPlayTime("One") - 20.92))), 121)
				Encounter["HandR"].Set("Papyrus/Arms/CrusherR")
				Encounter["HandR"].SetPivot(6/56, 69/72)
				Encounter["HandR"].MoveTo(370 + 6*math.sin(math.rad(90/0.3*(NewAudio.GetPlayTime("One") - 20.92))), Encounter["Body"].absy - 3)
			else
				Encounter["Head"].MoveTo(-1, 51)
				Encounter["Body"].MoveTo(-3, 121)
				Encounter["HandR"].Set("Papyrus/Arms/CrusherR")
				Encounter["HandR"].SetPivot(6/56, 69/72)
				Encounter["HandR"].MoveTo(370, Encounter["Body"].absy - 3)
			end
			if NewAudio.GetPlayTime("One") <= 21.86 then
				Encounter["Sans"]["Legs"].MoveTo(320 + 210*math.sin(math.rad(135/0.74*(NewAudio.GetPlayTime("One") - 21.12))), 261)
				for i=1,#Rayos do
					Rayos[i].alpha = 0.5*math.sin(math.rad(135/0.74*(NewAudio.GetPlayTime("One") - 21.12)))
				end
				Black2.alpha = 0.5*math.sin(math.rad(135/0.74*(NewAudio.GetPlayTime("One") - 21.12)))
			else
				Encounter["Sans"]["Legs"].MoveTo(469, 261)
				for i=1,#Rayos do
					Rayos[i].alpha = 0.35
				end
				Black2.alpha = 0.35
				phases[1] = 12
			end
		elseif phases[1] == 12 and NewAudio.GetPlayTime("One") > 21.86 then
			if not Gaster then
				for i=1,#Rayos do
					Rayos[i].Remove()
				end
				Rayos = nil
				Alteracion.SetAnimation({"Specials/Alteracion 1","Specials/Alteracion 2","Specials/Alteracion 3","Specials/Alteracion 4","Specials/Alteracion 5"}, 0.05)
				Alteracion.layer = "Bottom"
				Alteracion.layer = "Top"
				Alteracion.alpha = 1
				Black2.layer = "Bottom"
				Black2.layer = "Top"
				Audio.PlaySound("mysterious")
				Gaster = true
			end
			if NewAudio.GetPlayTime("One") <= 22.64 then
				Black2.alpha = 0.625 - 0.275*math.cos(math.rad(360/0.2*(NewAudio.GetPlayTime("One") - 21.86)))
			else
				EndEverything()
			end
		elseif NewAudio.GetPlayTime("One") > 22.62 or NewAudio.isStopped("One") then
			EndEverything()
		end
	end
	if ShakeHands then
		ShakeTimer = ShakeTimer + 1
		if ShakeTimer%2 == 0 then
			ShakeHands = math.min(ShakeHands + 0.125, 2)
			ShakeBody = math.min(ShakeBody + 0.125, 2)
			Encounter.Call("PapsMoveTo", {320 + math.random(-ShakeBody, ShakeBody), 240 + math.random(-ShakeBody, ShakeBody)})
			if ShakeHands > 0 then
				Encounter["HandL"].MoveTo(274 + math.random(-ShakeHands, ShakeHands), Encounter["Body"].absy - 4 + math.random(-ShakeHands, ShakeHands))
				Encounter["HandR"].MoveTo(378 + math.random(-ShakeHands, ShakeHands), Encounter["Body"].absy + 6 + math.random(-ShakeHands, ShakeHands))
			end
		end
	end
	if AnimateSans then
		AnimateSans = AnimateSans + 1
		Sanso.MoveTo(326 + 4*math.sin(math.rad(180/120*AnimateSans)), 225 + Sanso.yscale*Sanso.height/2 + 6*math.sin(math.rad(180/80*AnimateSans)))
	end
	if Rayos then
		for i=1,#Rayos do
			Rayos[i].MoveTo(Encounter["Head"].absx - 10, Encounter["Head"].absy)
			Rayos[i].rotation = Rayos[i].rotation + Rayos[i].GetVar("Vel")
		end
	end
end

function EndEverything()
	Misc.ShakeScreen(30, 20, true)
	Encounter.Call("InitiatePhase3", 1)
	Encounter.Call("InitiatePhase3", 4)
	for i=1,#Encounter["LegsParts"] do
		Encounter["LegsParts"][i].layer = "Top"
		Encounter["LegsParts"][i].layer = "Bottom"
	end
	Encounter.Call("InitiatePhase3", 5)
	Encounter["Sans"]["Legs"].MoveTo(490, 261)
	Encounter["HandL"].layer = "Top"
	Encounter["HandL"].layer = "Bottom"
	Encounter["HandR"].layer = "Top"
	Encounter["HandR"].layer = "Bottom"
	if Black2 then
		Black2.Remove()
		Black2 = nil
	end
	Alteracion.Remove()
	Alteracion = nil
	Black.Remove()
	Black = nil
	Audio.PlaySound("sparkle")
	Player.hp = 92
	Encounter["HPEnvenenadosPorKR"] = 0
	EndWave()
end