Arena.MoveToAndResize(320, 90, 460, 16, true, true)
Player.sprite.rotation = 0
Black = CreateSprite("Pixel","BelowPlayer")
Black.Scale(640,480)
Black.MoveTo(320, 240)
Black.color = {0,0,0}

require "GasterAnim"

Player.sprite.alpha = 1
Distorsion(true, 1, 0.75)
Alpha(0.3, 2)
CuerpoG.MoveTo(320, 160)
CabezaG.MoveTo(302, 280)
timer = 0

NewAudio.CreateChannel("Gaster")
NewAudio.PlaySound("Gaster", "Distorsion", true, 0)

function Update()
	if CabezaG and CuerpoG then
		AnimGaster()
	end
	if timer >= 0 then
		timer = timer + 1
	end
	if timer == 30 then
		Distorsion(false)
		Titulo = CreateSprite("Menu/TituloG","Top")
		Glitch = CreateSprite("Menu/Glitch1","Top")
		Start = CreateSprite("Menu/"..GetAlMightyGlobal("Idioma").."/Start","Top")
		Exit = CreateSprite("Menu/"..GetAlMightyGlobal("Idioma").."/Exit","Top")
		Titulo.MoveTo(320, 420)
		Glitch.MoveTo(320, 420)
		Start.MoveTo(100, 104)
		Exit.MoveTo(540, 104)
	elseif timer == 31 then
		timer = 30
		if Player.absx <= 142 then
			Start.color = {1,1,0}
			if Input.Confirm == 1 then
				timer = 31
			end
		else
			Start.color = {1,1,1}
		end
		if Player.absx >= 506 then
			Exit.color = {1,1,0}
			if Input.Confirm == 1 then
				Audio.PlaySound("confirm")
				Back = 0
				Player.SetControlOverride(true)
				timer = -1
			end
		else
			Exit.color = {1,1,1}
		end
	elseif timer == 32 then
		Start.Remove()
		Exit.Remove()
		CabezaG.Remove()
		CuerpoG.Remove()
		CabezaG = nil
		Distortion.Remove()
		CabezaD.Remove()
		Titulo.Remove()
		Glitch.Remove()
		Glitch = nil
		Player.SetControlOverride(true)
		Audio.PlaySound("st")
		Player.sprite.alpha = 1
	elseif timer == 35 then
		Player.sprite.alpha = 0
	elseif timer == 38 then
		Audio.PlaySound("st")
		Player.sprite.alpha = 1
	elseif timer == 41 then
		Player.sprite.alpha = 0
	elseif timer == 44 then
		Audio.PlaySound("st")
		Player.sprite.alpha = 1
	elseif timer == 64 then
		Audio.PlaySound("start")
		diffX = 320 - Player.absx
		diffY = 240 - Player.absy
	elseif timer >= 65 and timer < 85 then
		Player.MoveToAbs(Player.absx + diffX/20, Player.absy + diffY/20, true)
	elseif timer == 85 then
		Black.Remove()
		Arena.MoveToAndResize(320, -5, 640, 480, true, true)
		Player.SetControlOverride(false)
		NewAudio.DestroyChannel("Gaster")
		Encounter["nextwaves"] = {"Gaster Level"}
		State("ACTIONSELECT")
		State("DEFENDING")
	end
	if Back then
		Back = Back + 1
		if Back == 1 then
			Black2 = CreateSprite("Pixel", "Top")
			Black2.Scale(640, 480)
			Black2.MoveTo(320, 240)
			Black2.color = {0,0,0}
			Black2.alpha = 0
		elseif Back > 60 and Back <= 180 then
			Black2.alpha = (Back - 60)/120
		elseif Back > 240 then
			Black2.Remove()
			Start.Remove()
			Exit.Remove()
			CabezaG.Remove()
			CuerpoG.Remove()
			CabezaG = nil
			Distortion.Remove()
			CabezaD.Remove()
			Titulo.Remove()
			Glitch.Remove()
			Glitch = nil
			Black.Remove()
			NewAudio.DestroyChannel("Gaster")
			Encounter["nextwaves"] = {"Menu"}
			SetRealGlobal("Extras", 1)
			State("ACTIONSELECT")
			State("DEFENDING")
		end
	end
	if Glitch and timer%10 == 0 then
		if math.random(5) == 3 then
			Glitchear = 0
		end
	end
	if Glitchear and Glitch then
		Glitchear = Glitchear + 1
		if Glitchear == 1 then
			Glitch.Set("Menu/Glitch2")
		elseif Glitchear == 4 then
			Glitch.Set("Menu/Glitch3")
		elseif Glitchear == 7 then
			Glitch.Set("Menu/Glitch4")
		elseif Glitchear == 10 then
			Glitch.Set("Menu/Glitch5")
		elseif Glitchear == 13 then
			Glitch.Set("Menu/Glitch6")
		elseif Glitchear == 16 then
			Glitch.Set("Menu/Glitch1")
			Glitchear = nil
		end
	end
end