require "Dialogs"

timer = 0
tableaux = 0

GBPaps = {}

function Update()
	DialogUpdate()
	if not Black2 and timer == 0 then
		Black2 = CreateSprite("Bullets/Bones/Body","Top")
		Black2.Scale(640/5, 250)
		Black2.alpha = 0
		Black2.color = {0, 0, 0}
		Black2.MoveTo(320, 355)
		if Encounter["NOHIT"] and string.sub(GetAlMightyGlobal("Archivimentazo"), 16, 16) == "0" then
			SetAlMightyGlobal("Archivimentazo", string.sub(GetAlMightyGlobal("Archivimentazo"), 1, 15).."1"..string.sub(GetAlMightyGlobal("Archivimentazo"), 17))
			Encounter["LogroDesbloqueado"] = {Timer = 0, Icon = 16}
		end
		timer = 1
	elseif timer == 1 then
		Black2.alpha = Black2.alpha + 0.1
		if Black2.alpha >= 1 then
			Encounter.Call("ContinueMusic")
			Encounter.Call("StartPhase2", 1)
			timer = 2
		end
	elseif timer > 1 and timer <= 11 then
		timer = timer + 1
	elseif timer == 12 then
		Black2.alpha = Black2.alpha - 0.1
		if Black2.alpha <= 0 then
			timer = 13
		end
	elseif timer == 13 then
		if GetAlMightyGlobal("Idioma") == "Spanish" then
			Bubble("[noskip][font:papyrus][voice:mad papyrus]Eso es todo.[w:300][next]", "rightwide", 410.5, 395, 40, 90, 1)
		elseif GetAlMightyGlobal("Idioma") == "Russian" then
			Bubble("[noskip][font:papyrus][voice:mad papyrus]Ну всё.[w:300][next]", "rightwide", 410.5, 395, 40, 90, 1)
		else
			Bubble("[noskip][font:papyrus][voice:mad papyrus]That's it.[w:300][next]", "rightwide", 410.5, 395, 40, 90, 1)
		end
		timer = 14
	elseif timer > 13 and timer <= 119 then
		timer = timer + 1
	elseif timer == 120 then
		if GetAlMightyGlobal("Idioma") == "Spanish" then
			Bubble("[noskip][font:papyrus][voice:mad papyrus]No más juegos.[w:300][next]", "rightwide", 410.5, 395, 40, 90, 1)
		elseif GetAlMightyGlobal("Idioma") == "Russian" then
			Bubble("[noskip][font:papyrus][voice:mad papyrus]Больше никаких\nигр.[w:300][next]", "rightwide", 410.5, 395, 40, 90, 1)
		else
			Bubble("[noskip][font:papyrus][voice:mad papyrus]No more games.[w:300][next]", "rightwide", 410.5, 395, 40, 90, 1)
		end
		timer = 121
	elseif timer > 120 and timer <= 226 then
		timer = timer + 1
	elseif timer == 227 then
		if GetAlMightyGlobal("Idioma") == "Spanish" then
			Bubble("[noskip][font:papyrus][voice:mad papyrus]No más puzzles.[w:300][next]", "rightwide", 410.5, 395, 40, 90, 1)
		elseif GetAlMightyGlobal("Idioma") == "Russian" then
			Bubble("[noskip][font:papyrus][voice:mad papyrus]Больше никаких\nголоволомок.[w:300][next]", "rightwide", 410.5, 395, 40, 90, 1)
		else
			Bubble("[noskip][font:papyrus][voice:mad papyrus]No more puzzles.[w:300][next]", "rightwide", 410.5, 395, 40, 90, 1)
		end
		timer = 228
	elseif timer > 227 and timer <= 333 then
		timer = timer + 1
	elseif timer == 334 then
		Black2.alpha = 1
		Bubble("", "", "", "", "", "", 1)
		Encounter.Call("StartPhase2", 2)
		timer = 335
	elseif timer > 334 and timer <= 394 then
		timer = timer + 1
	elseif timer == 395 then
		Black2.Remove()
		Black2 = nil
		table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = -38, Y = 530, rotation = 179}, DatosFinales = {X = 90, Y = 294, rotation = 0}}})
		table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = 678, Y = 530, rotation = 181}, DatosFinales = {X = 550, Y = 294, rotation = 0}}})
		Audio.PlaySound("gasterintro")
		timer = 396
		Player.hp = 1
	elseif timer > 395 and timer <= 485 then
		timer = timer + 1
	elseif timer == 486 then
		timer = 487
		Audio.PlaySound("ding")
		Encounter.Call("ShowStatus", 1)
		FHP = CreateSprite("Fake UI, KR and HP Library Sprites/"..GetAlMightyGlobal("Idioma").."/HP","Top")
		FBar = CreateSprite("Fake UI, KR and HP Library Sprites/Barra","Top")
		FBar.Scale(110, 20)
		FBar.color = {1, 0, 0}
		FHP.MoveTo(317.75 - 61.25*FHP.xscale, 70)
		FBar.MoveTo(317.75 + 13.25*(FBar.xscale/110), 69.5)
	elseif timer > 486 and timer <= 578 then
		timer = timer + 1
		Player.Heal(1)
	elseif timer == 579 then
		timer = 580
		Audio.PlaySound("ding")
		Encounter.Call("ShowStatus", 2)
		FHPNumbers = CreateSprite("92 HP "..GetAlMightyGlobal("Idioma"),"Top")
		FHPNumbers.MoveTo(464.25, 70.5)
	elseif timer > 579 and timer <= 609 then
		timer = timer + 1
	elseif timer == 610 then
		timer = 611
		Encounter.Call("StartPhase2", 3)
		Audio.PlaySound("begin")
	elseif timer > 610 and timer <= 620 then
		timer = timer + 1
		Encounter.Call("PapsMoveTo", {320 - 6*math.sin(math.rad(360/10*(timer - 610))), 286})
	elseif timer > 620 and timer <= 720 then
		timer = timer + 1
		Misc.ShakeScreen(1, math.ceil(10*(timer - 620)/130), false)
		if timer == 691 then
			Audio.PlaySound("ding")
			Encounter.Call("AnimateShadow", 2)
			Encounter.Call("ShowStatus", 3)
			FName = CreateSprite("Chara","Top")
			FName.MoveTo(65.75, 70.5)
		elseif timer == 711 then
			Audio.PlaySound("ding")
			Encounter.Call("ShowStatus", 4)
			FLV = CreateSprite("LV 19 "..GetAlMightyGlobal("Idioma"),"Top")
			FLV.MoveTo(166.75, 70.5)
		end
		Encounter.Call("PapsMoveTo", {320 - 1*math.sin(math.rad(3240/80*(timer - 620))), 286})
	elseif timer == 721 then
		timer = 722
		Misc.ShakeScreen(1, math.ceil(10*(timer - 620)/130), false)
		Encounter.Call("SetHead", "Upseted")
		Encounter.Call("AnimateShadow", 3)
	elseif timer > 721 and timer <= 751 then
		timer = timer + 1
		Misc.ShakeScreen(1, math.ceil(10*(timer - 620)/130), false)
	elseif timer == 752 then
		timer = 753
		Audio.PlaySound("sparkle")
		Encounter.Call("ShowStatus", 5)
		Encounter.Call("AnimateShadow", 4)
	elseif timer > 752 and timer <= 772 then
		timer = timer + 1
	elseif timer == 773 then
		Encounter.Call("StartPhase2", 4)
		EndWave()
	end
	if FLV then
		FLV.Scale(FLV.xscale + 1/80, FLV.yscale + 1/80)
		FLV.alpha = FLV.alpha - 0.05
		if FLV.alpha <= 0 then
			FLV.Remove()
			FLV = nil
		end
	end
	if FName then
		FName.Scale(FName.xscale + 1/80, FName.yscale + 1/80)
		FName.alpha = FName.alpha - 0.05
		if FName.alpha <= 0 then
			FName.Remove()
			FName = nil
		end
	end
	if FHPNumbers then
		FHPNumbers.Scale(FHPNumbers.xscale + 1/110, FHPNumbers.yscale + 1/110)
		FHPNumbers.alpha = FHPNumbers.alpha - 0.05
		if FHPNumbers.alpha <= 0 then
			FHPNumbers.Remove()
			FHPNumbers = nil
		end
	end
	if FHP and FBar then
		FHP.Scale(FHP.xscale + 1/110, FHP.yscale + 1/110)
		FBar.Scale(FBar.xscale + 1, FBar.yscale + 1/110)
		FHP.MoveTo(317.75 - 61.25*FHP.xscale, 70)
		FBar.MoveTo(317.75 + 13.25*(FBar.xscale/110), 69.5)
		FHP.alpha = FHP.alpha - 0.05
		FBar.alpha = FBar.alpha - 0.05
		if FHP.alpha <= 0 then
			FHP.Remove()
			FBar.Remove()
			FHP = nil
			FBar = nil
		end
	end
	for i=1,#GBPaps do
		local gb = GBPaps[i - tableaux]
		if not gb.X then
			gb.X = gb.Sprite.x
		end
		if not gb.Y then
			gb.Y = gb.Sprite.y
		end
		gb.Sprite.xscale = (gb.Scale or 1)
		if gb.Animacion then
			if not gb.Animacion.timer then
				gb.Animacion.timer = 0
			end
			gb.Animacion.timer = gb.Animacion.timer + 1
			gb.X = gb.Animacion.DatosIniciales.X + (gb.Animacion.DatosFinales.X - gb.Animacion.DatosIniciales.X)*math.sin(math.rad(gb.Animacion.timer*(90/(gb.Animacion.VelAppearing or 30))))
			gb.Y = gb.Animacion.DatosIniciales.Y + (gb.Animacion.DatosFinales.Y - gb.Animacion.DatosIniciales.Y)*math.sin(math.rad(gb.Animacion.timer*(90/(gb.Animacion.VelAppearing or 30))))
			gb.Sprite.MoveTo(gb.X, gb.Y)
			while gb.Animacion.DatosIniciales.rotation >= 360 or gb.Animacion.DatosIniciales.rotation < 0 do
				if gb.Animacion.DatosIniciales.rotation < 0 then
					gb.Animacion.DatosIniciales.rotation = gb.Animacion.DatosIniciales.rotation + 360
				else
					gb.Animacion.DatosIniciales.rotation = gb.Animacion.DatosIniciales.rotation - 360
				end
			end
			while gb.Animacion.DatosFinales.rotation >= 360 or gb.Animacion.DatosFinales.rotation < 0 do
					if gb.Animacion.DatosFinales.rotation < 0 then
					gb.Animacion.DatosFinales.rotation = gb.Animacion.DatosFinales.rotation + 360
				else
					gb.Animacion.DatosFinales.rotation = gb.Animacion.DatosFinales.rotation - 360
				end
			end
			if gb.Animacion.DatosFinales.rotation - gb.Animacion.DatosIniciales.rotation <= 180 and gb.Animacion.DatosFinales.rotation - gb.Animacion.DatosIniciales.rotation >= -180 then
				gb.Sprite.rotation = gb.Animacion.DatosIniciales.rotation - (gb.Animacion.DatosIniciales.rotation - gb.Animacion.DatosFinales.rotation)*math.sin(math.rad(gb.Animacion.timer*(90/(gb.Animacion.VelAppearing or 30))))
			else
				gb.Sprite.rotation = gb.Animacion.DatosIniciales.rotation - 360 - (gb.Animacion.DatosFinales.rotation - 180 + gb.Animacion.DatosIniciales.rotation - 180)*math.sin(math.rad(gb.Animacion.timer*(90/(gb.Animacion.VelAppearing or 30))))
			end
			if gb.Animacion.timer >= (gb.Animacion.VelAppearing or 30) then
				gb.Animacion = nil
				gb.timer = 0
			end
		else
			gb.timer = gb.timer + 1
			if gb.timer <= 60 then
				gb.Y = (244 + gb.Sprite.height/2) + (350 - (244 + gb.Sprite.height/2)) - (350 - (244 + gb.Sprite.height/2))*math.cos(math.rad(90/60*gb.timer))
			else
				gb.Y = 350 - 30*math.sin(math.rad(gb.timer - 60))
			end
			gb.Sprite.MoveTo(gb.X, gb.Y)
			if timer > 620 then
				gb.Sprite.alpha = gb.Sprite.alpha - 1/60
				if gb.Sprite.alpha <= 0 then
					gb.Sprite.Remove()
					gb = nil
					table.remove(GBPaps, i - tableaux)
					tableaux = tableaux + 1
				end
			end
		end
	end
	tableaux = 0
end