Tiembla = {}
Gtimer = 60
Gespera = 0
timerG3 = 0
Gcontador = "Disminuir"
Gmovimiento = "Abajo"
ConvulsionG = 1
FrameCabeza = "Primera"
CuerpoG = CreateSprite("Gaster Level/Gaster/Cuerpo/Normal","BelowPlayer")
CabezaG = CreateSprite("Gaster Level/Gaster/Cabezas/Primera","BelowPlayer")
CuerpoG.color = {0,0,0}
CabezaG.color = {0,0,0}
CuerpoG.MoveTo(320, 240)
CabezaG.MoveTo(310, 300)
Distortion = CreateSprite("Gaster Level/Gaster/Distorsion/1","Top")
Distortion.xscale = 2.0
Distortion.yscale = 2.0
Distortion.SetAnimation({"Gaster Level/Gaster/Distorsion/1","Gaster Level/Gaster/Distorsion/2","Gaster Level/Gaster/Distorsion/3"}, 0.05)
Distortion.alpha = 0
CabezaD = CreateSprite("Gaster Level/Gaster/Cabezas/Emocionado","Top")
CabezaD.xscale = 4.0
CabezaD.yscale = 5.0
CabezaD.alpha = 0
Distortion.MoveTo(320, 240)
CabezaD.MoveTo(320, 240)
table.insert(Tiembla, CabezaD)

function AnimGaster()
	Gestirado = 0.2/60 * Gtimer
	if Gespera > 0 then
		Gespera = Gespera + 1
		if Gespera == 11 then
			Gespera = 0
		end
	elseif Gespera == 0 then
		if Gcontador == "Disminuir" then
			Gtimer = Gtimer - 1
			if Gtimer == 0 then
				Gespera = 1
				if Gmovimiento == "Abajo" then
					Gmovimiento = "Arriba"
				elseif Gmovimiento == "Arriba" then
					Gmovimiento = "Abajo"
				end
				Gcontador = "Aumentar"
			end
		elseif Gcontador == "Aumentar" then
			Gtimer = Gtimer + 1
			if Gtimer == 60 then
				Gcontador = "Disminuir"
			end
		end
	end
	if Invertir == "Y" or Invertir == "All" then
		if Gmovimiento == "Abajo" then
			CuerpoG.yscale = CuerpoG.yscale - (Gestirado/140)*CuerpoG.yscale
			CuerpoG.MoveTo(CuerpoG.x, CuerpoG.y + Gestirado/2*CuerpoG.yscale)
			CabezaG.MoveTo(CabezaG.x, CabezaG.y + Gestirado*1.5*CabezaG.yscale)
		elseif Gmovimiento == "Arriba" then
			CuerpoG.yscale = CuerpoG.yscale + (Gestirado/140)*CuerpoG.yscale
			CuerpoG.MoveTo(CuerpoG.x, CuerpoG.y - Gestirado/2*CuerpoG.yscale)
			CabezaG.MoveTo(CabezaG.x, CabezaG.y - Gestirado*1.5*CabezaG.yscale)
		end
	else
		if Gmovimiento == "Abajo" then
			CuerpoG.yscale = CuerpoG.yscale - (Gestirado/140)*CuerpoG.yscale
			CuerpoG.MoveTo(CuerpoG.x, CuerpoG.y - Gestirado/2*CuerpoG.yscale)
			CabezaG.MoveTo(CabezaG.x, CabezaG.y - Gestirado*1.5*CabezaG.yscale)
		elseif Gmovimiento == "Arriba" then
			CuerpoG.yscale = CuerpoG.yscale + (Gestirado/140)*CuerpoG.yscale
			CuerpoG.MoveTo(CuerpoG.x, CuerpoG.y + Gestirado/2*CuerpoG.yscale)
			CabezaG.MoveTo(CabezaG.x, CabezaG.y + Gestirado*1.5*CabezaG.yscale)
		end
	end
	if ConvulsionG == 1 then
		timerG3 = timerG3 + 1
		if timerG3%20 == 0 then
			local random = math.random(5)
			local randomizable = math.random(5)
			if random == 3 then
				if Invertir == "Y" or Invertir == "X" then
					sprait = "EmocionadoR"
				else
					sprait = "Emocionado"
				end
				CabezaG.SetAnimation({"Gaster Level/Gaster/Cabezas/" .. sprait,"Gaster Level/Gaster/Cabezas/" .. FrameCabeza .. "Glitch1","Gaster Level/Gaster/Cabezas/" .. FrameCabeza .. "Glitch2","Gaster Level/Gaster/Cabezas/" .. FrameCabeza .. "Glitch3","Gaster Level/Gaster/Cabezas/" .. FrameCabeza .. "Glitch4","Gaster Level/Gaster/Cabezas/" .. FrameCabeza .. "Glitch5","Gaster Level/Gaster/Cabezas/" .. FrameCabeza}, 0.05)
				CabezaG.loopmode = "ONESHOT"
			end
			if randomizable == 3 then
				if Invertir == "Y" or Invertir == "X" then
					sprait = "R"
				else
					sprait = ""
				end
				CuerpoG.SetAnimation({"Gaster Level/Gaster/Cuerpo/Normal" .. sprait,"Gaster Level/Gaster/Cuerpo/" .. sprait .. "Glitch1","Gaster Level/Gaster/Cuerpo/" .. sprait .. "Glitch2","Gaster Level/Gaster/Cuerpo/" .. sprait .. "Glitch3","Gaster Level/Gaster/Cuerpo/" .. sprait .. "Glitch4","Gaster Level/Gaster/Cuerpo/" .. sprait .. "Glitch5","Gaster Level/Gaster/Cuerpo/Normal" .. sprait}, 0.05)
				CuerpoG.loopmode = "ONESHOT"
			end
		end
	elseif ConvulsionG == 2 then
		timerG3 = timerG3 + 1
		if timerG3%10 == 0 then
			local random = math.random(3)
			local randomizable = math.random(3)
			if random == 2 then
				if Invertir == "Y" or Invertir == "X" then
					sprait = "EmocionadoR"
				else
					sprait = "Emocionado"
				end
				CabezaG.SetAnimation({"Gaster Level/Gaster/Cabezas/" .. sprait,"Gaster Level/Gaster/Cabezas/" .. FrameCabeza .. "Glitch1","Gaster Level/Gaster/Cabezas/" .. FrameCabeza .. "Glitch2","Gaster Level/Gaster/Cabezas/" .. FrameCabeza .. "Glitch3","Gaster Level/Gaster/Cabezas/" .. FrameCabeza .. "Glitch4","Gaster Level/Gaster/Cabezas/" .. FrameCabeza .. "Glitch5","Gaster Level/Gaster/Cabezas/" .. FrameCabeza}, 0.05)
				CabezaG.loopmode = "ONESHOT"
			end
			if randomizable == 2 then
				if Invertir == "Y" or Invertir == "X" then
					sprait = "R"
				else
					sprait = ""
				end
				CuerpoG.SetAnimation({"Gaster Level/Gaster/Cuerpo/Normal" .. sprait,"Gaster Level/Gaster/Cuerpo/" .. sprait .. "Glitch1","Gaster Level/Gaster/Cuerpo/" .. sprait .. "Glitch2","Gaster Level/Gaster/Cuerpo/" .. sprait .. "Glitch3","Gaster Level/Gaster/Cuerpo/" .. sprait .. "Glitch4","Gaster Level/Gaster/Cuerpo/" .. sprait .. "Glitch5","Gaster Level/Gaster/Cuerpo/Normal" .. sprait}, 0.05)
				CuerpoG.loopmode = "ONESHOT"
			end
		end
	elseif ConvulsionG == 3 then
		timerG3 = timerG3 + 1
		if timerG3%3 == 0 then
			local number1 = math.random(5)
			local number2 = math.random(5)
			if Invertir == "Y" or Invertir == "X" then
				sprait = "R"
			else
				sprait = ""
			end
			CabezaG.Set("Gaster Level/Gaster/Cabezas/" .. FrameCabeza .. "Glitch" .. number1)
			CuerpoG.Set("Gaster Level/Gaster/Cuerpo/" .. sprait .. "Glitch" .. number2)
		end
	end
	if EfectosG then
		NewAudio.SetVolume("Gaster", Volumen)
		Distortion.alpha = AlphaS
		CabezaD.alpha = AlphaS
	else
		NewAudio.SetVolume("Gaster", 0)
		Distortion.alpha = 0
		CabezaD.alpha = 0
	end
	for i=1,#Tiembla do
		local sprite = Tiembla[i]
		if sprite.isactive then
			local rot = math.random(-8,8)
			sprite.rotation = rot
		end
	end
end

function Distorsion(dato, alpha, volumen)
	EfectosG = dato
	AlphaS = alpha or 1
	Volumen = volumen or 0.5
end

function Alpha(dato, scale)
	CabezaG.color = {dato,dato,dato}
	CuerpoG.color = {dato,dato,dato}
	CuerpoG.Scale(scale or 1,scale or 1)
	CabezaG.Scale(scale or 1,scale or 1)
	CuerpoG.MoveTo(320, 270 - 30*CuerpoG.yscale)
	CabezaG.MoveTo(320 - 9*CabezaG.yscale, 270 + 30*CabezaG.yscale)
	Gtimer = 60
	Gespera = 0
	timerG3 = 0
	Gcontador = "Disminuir"
	Gmovimiento = "Abajo"
end