require "Dialogs"
require "Frisk Character"
FriskDirection = "R"
TipoFrisk = "N"
AlowFriskMovement(false, false)

Throne = CreateSprite("Overworld/Maps/Throne","Top")
Asgore = CreateSprite("Overworld/Characters/Asgore/Up","Top")
Shadows = CreateSprite("Overworld/Maps/Throne Shadow","Top")
Black = CreateSprite("Pixel","Top")
Asgore.SetPivot(0.5, 0)
Frisk.SetParent(Throne)
Asgore.SetParent(Throne)
Shadows.SetParent(Throne)
Black.Scale(640, 480)
Black.color = {0,0,0}
Black.MoveTo(320, 240)
Throne.MoveTo(320, 600)
Frisk.MoveTo(0, -900)
Asgore.MoveTo(0, -120)

timer = 0
Volumen = 0

function Update()
	FriskUpdate()
	DialogUpdate()
	if timer == 0 then
		if not Logo then
			Logo = CreateSprite("Menu/Disbelief","Top")
			Logo.Scale(300/826, 300/826)
			Logo.alpha = 0
		else
			Logo.alpha = Logo.alpha + 0.004
			Logo.Scale(Logo.xscale + 0.5/826, Logo.yscale + 0.5/826)
			if Logo.xscale > 500/826 then
				Audio.PlaySound("suspenso")
				Logo.Remove()
				Logo = nil
				Me = CreateSprite("Credits/"..GetAlMightyGlobal("Idioma").."/Cezar","Top")
				Me.MoveTo(320, 240)
				timer = 1
			end
		end
	elseif timer >= 1 and timer < 570 then
		timer = timer + 1
		if not Flames then
			Flames = CreateSprite("Credits/"..GetAlMightyGlobal("Idioma").."/Flames","Top")
			Flames.MoveTo(320, 240)
			Flames.alpha = 0
			Me.SendToTop()
		end
		if timer > 120 and timer <= 210 then
			Me.MoveTo(245 + 75*math.cos(math.rad(180/90*(timer - 120))), 240)
			Flames.MoveTo(395 - 75*math.cos(math.rad(180/90*(timer - 120))), 240)
			Flames.alpha = 0.5 - 0.5*math.cos(math.rad(180/90*(timer - 120)))
		end
	elseif timer == 570 then
		Audio.PlaySound("suspenso")
		Me.Remove()
		Flames.Remove()
		Me = nil
		Flames = nil
		timer = 571
	elseif timer >= 571 and timer < 810 then
		timer = timer + 1
	elseif timer == 810 then
		Black.alpha = Black.alpha - 0.04
		if Black.alpha <= 0 then
			timer = 811
			FriskAuto("Up")
			NewAudio.PlaySound("Loop", "Birds", true, 1)
		end
	elseif timer == 811 then
		if Frisk.y >= -458 then
			AlowFriskMovement(false, false)
			timer = 812
		end
	elseif timer >= 812 and timer < 902 then
		timer = timer + 1
	elseif timer == 902 then
		FriskFrameVel = 20
		FriskVel = 1
		FriskAuto("Up")
		timer = 903
	elseif timer == 903 then
		if Frisk.absy + 29 > 240 then
			Throne.MoveTo(320, Throne.y - 1)
			if Throne.y <= 474 then
				AlowFriskMovement(false, false)
				timer = 904
			end
		end
	elseif timer >= 904 and timer < 964 then
		timer = timer + 1
	elseif timer == 964 then
		Throne.MoveTo(320, Throne.y - 1)
		if Throne.y <= 290 then
			timer = 965
		end
	elseif timer >= 965 and timer < 1025 then
		timer = timer + 1
	elseif timer == 1025 then
		if GetAlMightyGlobal("Idioma") == "Spanish" then
			Box("[noskip][voice:asgore][SetSpace:4][waittall:1.5]Curioso.[w:10]\nNunca he visto una planta...[w:10]\nLlorar antes.", "", "", "", 0, 400, nil, "", 1)
		elseif GetAlMightyGlobal("Idioma") == "Russian" then
			Box("[noskip][voice:asgore][SetSpace:4][waittall:1.5]Интересно.[w:10]\nЯ раньше не видал, чтобы...[w:10]\nЦветок плакал.", "", "", "", 0, 400, nil, "", 1)
		else
			Box("[noskip][voice:asgore][SetSpace:4][waittall:1.5]Curious.[w:10]\nI've never seen a plant...[w:10]\nCry before.", "", "", "", 0, 400, nil, "", 1)
		end
		timer = 1026
	elseif timer == 1026 and Boxes[1] and not Boxes[1].Activo then
		Asgore.Set("Overworld/Characters/Asgore/Right")
		timer = 1027
	elseif timer >= 1027 and timer < 1042 then
		timer = timer + 1
	elseif timer == 1042 then
		Asgore.Set("Overworld/Characters/Asgore/Down 1")
		timer = 1043
	elseif timer >= 1043 and timer < 1058 then
		timer = timer + 1
	elseif timer == 1058 then
		if GetAlMightyGlobal("Idioma") == "Russian" then
			Box("[noskip][voice:asgore][SetSpace:4][waittall:1.5][SetAnimation:Asgore 2]... хмм?[StopAnimation][SetCara:Asgore 1][func:Trigger]", "", "Asgore 1", "", 0, 400, nil, "", 1)
		else
			Box("[noskip][voice:asgore][SetSpace:4][waittall:1.5][SetAnimation:Asgore 2]... huh?[StopAnimation][SetCara:Asgore 1][func:Trigger]", "", "Asgore 1", "", 0, 400, nil, "", 1)
		end
		Asgore.SetAnimation({"Overworld/Characters/Asgore/Down 1","Overworld/Characters/Asgore/Down 2"}, 0.15)
		timer = 1059
	elseif timer == 1059 and Boxes[1] and not Boxes[1].Activo then
		if GetAlMightyGlobal("Idioma") == "Spanish" then
			Box("[noskip][voice:asgore][SetSpace:4][waittall:1.5][SetAnimation:Asgore 2]Tú debes ser el que\resa flor me acaba de-[StopAnimation][SetCara:Asgore 1][func:Trigger][next]", "", "Asgore 1", "", 0, 400, nil, "", 1)
		elseif GetAlMightyGlobal("Idioma") == "Russian" then
			Box("[noskip][voice:asgore][SetSpace:4][waittall:1.5][SetAnimation:Asgore 2]Ты должно быть тот,\rо ком меня предупреждал цве-[StopAnimation][SetCara:Asgore 1][func:Trigger][next]", "", "Asgore 1", "", 0, 400, nil, "", 1)
		else
			Box("[noskip][voice:asgore][SetSpace:4][waittall:1.5][SetAnimation:Asgore 2]You must be the one\rthat flower just war-[StopAnimation][SetCara:Asgore 1][func:Trigger][next]", "", "Asgore 1", "", 0, 400, nil, "", 1)
		end
		Asgore.SetAnimation({"Overworld/Characters/Asgore/Down 1","Overworld/Characters/Asgore/Down 2"}, 0.15)
		timer = 1060
	elseif timer == 1060 and Boxes[1] and not Boxes[1].Activo then
		NewAudio.Stop("Loop")
		Audio.PlaySound("HappySlice")
		Black.alpha = 1
		timer = 1061
	elseif timer >= 1061 and timer < 1301 then
		timer = timer + 1
	elseif timer == 1301 then
		if not GameOver then
			if GetAlMightyGlobal("Idioma") == "Russian" then
				GameOver = CreateSprite("Undertale/Game Over/Game Over Russian", "Top")
			else
				GameOver = CreateSprite("Undertale/Game Over/Game Over", "Top")
			end
			Thanks = CreateSprite("Credits/"..GetAlMightyGlobal("Idioma").."/Thanks","Top")
			Chaleco = CreateSprite("Specials/Ded","Top")
			Chaleco.Scale(1.5,1.5)
			GameOver.MoveTo(320, 360)
			Thanks.MoveTo(320, 200)
			Chaleco.MoveTo(320, 80)
			GameOver.alpha = 0
		end
		GameOver.alpha = GameOver.alpha + 0.02
		Thanks.alpha = GameOver.alpha
		Chaleco.alpha = GameOver.alpha
		if GameOver.alpha >= 1 then
			Audio.PlaySound("Game Over Paps")
			timer = 1302
		end
	elseif timer >= 1302 and timer < 3820 then
		timer = timer + 1
		if timer >= 1502 then
			if timer == 1900 then
				NewAudio.PlaySound("Loop", "Papyrus", true, 0)
			elseif timer > 1900 and timer <= 2200 then
				Volumen = 0.005/300*(timer - 1900)
				NewAudio.SetVolume("Loop", Volumen)
			elseif timer >= 3040 and timer < 3820 then
				if timer%60 == 40 then
					NewAudio.SetPitch("Loop", 1 + math.random(-15, 5)/100)
				end
				if timer >= 3520 then
					Volumen = 0.005/300*(3820 - timer)
					NewAudio.SetVolume("Loop", Volumen)
				end
			end
			if timer > 1900 then
				Chaleco.alpha = 1/1920*(3820 - timer)
			end
			if Input.Confirm > 0 then
				timer = 3820
			end
		end
	elseif timer == 3820 then
		if Volumen > 0 then
			Volumen = math.max(Volumen - 0.005/60, 0)
			if Volumen <= 0 then
				NewAudio.Stop("Loop")
			end
		end
		if not Black2 then
			Black2 = CreateSprite("Pixel","Top")
			Black2.color = {0,0,0}
			Black2.MoveTo(320, 240)
			Black2.Scale(640, 480)
			Black2.alpha = 0
		else
			Black2.alpha = Black2.alpha + 1/60
			if Black2.alpha >= 1 then
				GameOver.Remove()
				Thanks.Remove()
				Chaleco.Remove()
				Black2.Remove()
				timer = 3821
				if string.sub(GetAlMightyGlobal("Archivimentazo"), 4, 4) == "0" then
					SetAlMightyGlobal("Archivimentazo", string.sub(GetAlMightyGlobal("Archivimentazo"), 1, 3).."1"..string.sub(GetAlMightyGlobal("Archivimentazo"), 5))
					Encounter["LogroDesbloqueado"] = {Timer = 0, Icon = 4}
				end
			end
		end
	elseif timer >= 3821 and timer < 4061 then
		timer = timer + 1
	elseif timer >= 4061 then
		Asgore.Remove()
		Frisk.Remove()
		Shadows.Remove()
		Throne.Remove()
		Black.Remove()
		SetAlMightyGlobal("ShortDialog", false)
		Encounter["nextwaves"] = {"Menu"}
		State("DEFENDING")
	end
	if Encounter["Triggered"] then
		Encounter["Triggered"] = nil
		Asgore.StopAnimation()
		Asgore.Set("Overworld/Characters/Asgore/Down 1")
	end
end

function OnHit()
	
end