Arena.Move(0, 1, false, false)
Arena.ResizeImmediate(460, 150)
SetPPCollision(true)
Player.SetControlOverride(true)

jump = 0
maxjump = 40
posX = Player.absx
posY = Player.absy
times = 1
cansancio = 90
desciende = false
critico = false
subir = false
color = 216
tableaux = 0
Volumen = 1
SetRealGlobal("Time", 0)
Player.lv = 1
Player.hp = 20
Encounter.Call("ConfigurateKR", {false, false})

timeranim = -62
timerattacks = -120
framefingers = 0
framesprite = 1
velfingers = 5
lenghtfingers = 34
framelenght = 1
Parts = {}
Targets = {}
Lenghts = {}
Throw = {}
Expand = {}
Fall = {}
EggBots = {}
Spawner = {}
Fires = {}
Contador = {CreateProjectileAbs("Fake UI, KR and HP Library Sprites/0", 305, 265), CreateProjectileAbs("Fake UI, KR and HP Library Sprites/0", 320, 265), CreateProjectileAbs("Fake UI, KR and HP Library Sprites/0", 335, 265)}
Black1 = CreateSprite("BlackL","BelowBullet")
Black2 = CreateSprite("BlackR","BelowBullet")
Black3 = CreateSprite("Rotatory Arena Pixel","Top")
Black4 = CreateProjectileAbs("Rotatory Arena Pixel", 320, 30, "BelowArena")
Black5 = CreateProjectileAbs("Rotatory Arena Pixel", 60, 60, "BelowArena")
Black3.color = {0,0,0}
Black3.Scale(640, 480)
Black4.sprite.color = {0,0,0}
Black4.sprite.Scale(640, 60)
Black5.sprite.color = {0,0,0}
Black5.sprite.Scale(120, 60)
Black3.MoveTo(320, 240)
Black1.MoveTo(Arena.x - Arena.width/2 - Black1.width/2, Arena.y + 5 + Arena.height/2)
Black2.MoveTo(Arena.x + Arena.width/2 + Black1.width/2, Arena.y + 5 + Arena.height/2)
Body = CreateProjectileAbs("Death Egg Robot/body", 120, 600)
Arm2 = CreateProjectileAbs("Death Egg Robot/arm", Body.absx - 1, Body.absy + 23, "BelowBullet")
ElbowArm2 = CreateProjectileAbs("Death Egg Robot/elbow arm", Arm2.absx, Arm2.absy - 22, "BelowBullet")
Hand2 = CreateProjectileAbs("Death Egg Robot/hand", ElbowArm2.absx + 24, ElbowArm2.absy, "BelowBullet")
Fingers2 = CreateProjectileAbs("Death Egg Robot/fingers/1", Hand2.absx + 34, Hand2.absy, "BelowBullet")
Fingers2.sprite.SetAnimation({"Death Egg Robot/fingers/1","Death Egg Robot/fingers/6","Death Egg Robot/fingers/5","Death Egg Robot/fingers/4","Death Egg Robot/fingers/3","Death Egg Robot/fingers/2"}, 0.08)
ElbowLeg1 = CreateProjectileAbs("Death Egg Robot/elbow leg", Body.absx + 9, Body.absy - 38)
Leg1 = CreateProjectileAbs("Death Egg Robot/leg", ElbowLeg1.absx, ElbowLeg1.absy - 20)
Foot1 = CreateProjectileAbs("Death Egg Robot/foot", Leg1.absx + 13, Leg1.absy - 21)
ElbowLeg2 = CreateProjectileAbs("Death Egg Robot/elbow leg", Body.absx + 9, Body.absy - 38, "BelowBullet")
Leg2 = CreateProjectileAbs("Death Egg Robot/leg", ElbowLeg2.absx, ElbowLeg2.absy - 20, "BelowBullet")
Foot2 = CreateProjectileAbs("Death Egg Robot/foot", Leg2.absx + 13, Leg2.absy - 21, "BelowBullet")
Eggman = CreateProjectileAbs("Death Egg Robot/Eggman/1", Body.absx + 2, Body.absy + 48, "BelowBullet")
Head = CreateProjectileAbs("Death Egg Robot/head", Body.absx - 25, Body.absy + 61, "BelowBullet")
Arm1 = CreateProjectileAbs("Death Egg Robot/arm", Body.absx - 1, Body.absy + 23)
ElbowArm1 = CreateProjectileAbs("Death Egg Robot/elbow arm", Arm1.absx, Arm1.absy - 22)
Hand1 = CreateProjectileAbs("Death Egg Robot/hand", ElbowArm1.absx + 24, ElbowArm1.absy)
Fingers1 = CreateProjectileAbs("Death Egg Robot/fingers/1", Hand1.absx + 34, Hand1.absy)
Head.sprite.rotation = 90
table.insert(Parts, Body)
table.insert(Parts, Eggman)
table.insert(Parts, Head)
table.insert(Parts, Arm1)
table.insert(Parts, ElbowArm1)
table.insert(Parts, Hand1)
table.insert(Parts, Fingers1)
table.insert(Parts, Arm2)
table.insert(Parts, ElbowArm2)
table.insert(Parts, Hand2)
table.insert(Parts, Fingers2)
table.insert(Parts, Leg1)
table.insert(Parts, ElbowLeg1)
table.insert(Parts, Foot1)
table.insert(Parts, Leg2)
table.insert(Parts, ElbowLeg2)
table.insert(Parts, Foot2)

NewAudio.CreateChannel("Egg Robot Song")
NewAudio.CreateChannel("Intro")
NewAudio.PlaySound("Egg Robot Song","Death Egg Robot",true,1)
NewAudio.PlaySound("Intro","Intro Death Egg Robot",false,1)
NewAudio.Pause("Egg Robot Song")
NewAudio.Pause("Intro")

function Update()
	topedge = Arena.y - 3 + Arena.height
	bottomedge = Arena.y + 13
	AnimateDeathEggRobot()
	if AnimAll then
		timerattacks = timerattacks + 1
		if timerattacks%60 == 0 then
			SetRealGlobal("Time", math.min(GetRealGlobal("Time") + 1, 999))
			Contador[1].sprite.Set("Fake UI, KR and HP Library Sprites/"..(GetRealGlobal("Time") - GetRealGlobal("Time")%100)/100)
			Contador[2].sprite.Set("Fake UI, KR and HP Library Sprites/"..(GetRealGlobal("Time")%100 - GetRealGlobal("Time")%10)/10)
			Contador[3].sprite.Set("Fake UI, KR and HP Library Sprites/"..GetRealGlobal("Time")%10)
		end
		if timerattacks%600 == 0 then
			Attack("Arm")
		elseif timerattacks%300 == 0 then
			local randomizador = math.random(1,2)
			if randomizador == 1 then
				Attack("EggBot1")
			else
				Attack("EggBot2")
			end
		elseif timerattacks%150 == 0 then
			Attack("Bomb")
		end
	else
		for i=1,#EggBots do
			local Bot = EggBots[i]
			if Bot.isactive then
				Bot.SetVar("times", 6)
			end
		end
	end
	if GetRealGlobal("Time") >= 999 then
		if GetRealGlobal("Time") == 999 then
			AnimAll = nil
			frecuencia = 20
			times2 = 0
			SetRealGlobal("Time", 1001)
		elseif GetRealGlobal("Time") >= 1001 and GetRealGlobal("Time") <= 1007 then
			SetRealGlobal("Time", GetRealGlobal("Time") + 1)
			if GetRealGlobal("Time")%1007 == 0 then
				Audio.PlaySound("Bomb")
				Explotion = CreateProjectileAbs("boom", Body.absx + math.random(40, -40), Body.absy + math.random(40, -40), "Top")
				Explotion.sprite.Scale(0, 0)
				table.insert(Expand, Explotion)
				SetRealGlobal("Time", 1001)
				times2 = times2 + 1
			end
			if times2 == 30 then
				SetRealGlobal("Time", 1016)
			end
		elseif GetRealGlobal("Time") == 1016 then
			Y = 10
			Eggman.sprite.Set("Death Egg Robot/Eggman/ded")
			for i=1,#Parts do
				if Parts[i].isactive then
					if i == 1 or i == 2 then
						Parts[i].SetVar("Y", 10)
						Parts[i].SetVar("nohit", true)
					else
						Parts[i].SetVar("rot", math.random(4,-4))
						Parts[i].SetVar("X", math.random(2,-2))
					end
				end
			end
			SetRealGlobal("Time", 1017)
			Audio.PlaySound("LedgeBreak")
		elseif GetRealGlobal("Time") == 1017 then
			for i=3,#Parts do
				if Parts[i].isactive then
					Parts[i].sprite.rotation = Parts[i].sprite.rotation + Parts[i].GetVar("rot")
					Parts[i].MoveToAbs(Parts[i].absx + Parts[i].GetVar("X"), Parts[i].absy + Y)
				end
			end
			for i=1,2 do
				if Parts[i].isactive then
					Parts[i].MoveToAbs(Parts[i].absx, Parts[i].absy + Parts[i].GetVar("Y"))
					if Parts[i].GetVar("Y") > -6 then
						Parts[i].SetVar("Y", Parts[i].GetVar("Y") - 0.3)
					end
					if Body.absy <= (Arena.y + 5 + Body.sprite.height/2) and Parts[i].GetVar("nohit") then
						Parts[i].SetVar("nohit", nil)
						Parts[i].SetVar("Y", 8)
						Audio.PlaySound("BossHit")
					elseif Body.absy <= (Arena.y - 60) and not Body.GetVar("sound") then
						Audio.PlaySound("Drop")
						Parts[i].SetVar("sound", true)
					end
				end
			end
			if Body.absy <= (Arena.y - 800) then
				Black3.alpha = Black3.alpha + 0.01
				Volumen = Volumen - 0.01
				NewAudio.SetVolume("Egg Robot Song", Volumen)
				NewAudio.SetVolume("Intro", Volumen)
				if Black3.alpha >= 1 then
					wait = 0
				end
			end
			if Y > -6 then
				Y = Y - 0.3
			end
		end
	end
	if wait then
		wait = wait + 1
		if wait == 60 then
			Black1.Remove()
			Black2.Remove()
			Black3.Remove()
			for i=1,#Fall do
				Fall[i].Remove()
			end
			for i=1,#Targets do
				for j=1,4 do
					Targets[i].GetVar("Arrow"..j).Remove()
				end
				if Targets[i].GetVar("Pointer") then
					Targets[i].GetVar("Pointer").Remove()
				end
				Targets[i].Remove()
			end
			NewAudio.DestroyChannel("Egg Robot Song")
			NewAudio.DestroyChannel("Intro")
			Player.sprite.color = {1,0,0}
			Encounter["nextwaves"] = {"Menu"}
			SetRealGlobal("Extras", 5)
			State("ACTIONSELECT")
			State("DEFENDING")
		end
	end
	if song then
		if NewAudio.isStopped("Intro") then
			NewAudio.Unpause("Egg Robot Song")
			song = nil
		end
	end
	Player.sprite.color = {255/255, color/255, 0/255}
	posX = Player.absx
	posY = Player.absy
	if Input.Right > 0 then
		posX = Player.absx + 2
	end
	if Input.Left > 0 then
		posX = Player.absx - 2
	end
	if posY > topedge then
		if jump > 0 then
			jump = 0
		end
	end
	if Player.absy < bottomedge + 2 then
		if Input.Up > 0 then
			jump = maxjump
		else
			desciende = false
			posY = bottomedge
			jump = 0
		end
	end
	if desciende == true and Input.Up > 0 and critico == false then
		cansancio = cansancio - 1
		if jump < -5 then
			subir = true
		end
		if cansancio <= 0 then
			critico = true
		end
	elseif critico == true and cansancio < 90 then
		cansancio = cansancio + 0.6
	elseif subir == false and cansancio < 90 and critico == false then
		cansancio = cansancio + 0.8
	end
	if subir == true then
		if jump > 5 then
			subir = false
		else
			jump = jump + 3
		end
	end
	if jump < 3 and jump > -3 then
		jump = jump - 0.5
		if jump < 0 then
			desciende = true
		end
	elseif jump < -3 then
		jump = jump - 1.5
	else
		jump = (jump/1.001) - 1
	end
	if Input.Up < 0 and jump > 3 then
		jump = 2
	end
	posY = posY + (jump/10)
	Player.MoveToAbs(posX, posY, false)
	if critico == true then
		if times == 1 then
			color = color + 8
			if color >= 216 then
				if cansancio >= 90 then
					critico = false
				else
					times = 2
				end
			end
		elseif times == 2 then
			color = color - 8
			if color <= 0 then
				times = 1
			end
		end
	else
		if cansancio > 0 then
			color = 2.4 * cansancio
		else
			color = 0
		end
	end
end

function Attack(attack)
	target = CreateSprite("target/target 1","Top")
	target.SetVar("Follow", true)
	target.SetVar("timer", 0)
	target.SetVar("attack", attack)
	table.insert(Targets, target)
end

function AnimateDeathEggRobot()
	framefingers = framefingers + 1
	if framefingers%velfingers == 0 then
		framesprite = framesprite + 1
		if framesprite == 7 then
			framesprite = 1
		end
		Fingers1.sprite.Set("Death Egg Robot/fingers/"..framesprite)
	end
	if timeranim < -2 then
		timeranim = timeranim + 1
		if timeranim >= -32 then
			Black3.alpha = 1/30*(30 - (timeranim + 33))
		end
	elseif timeranim == -2 then
		Black3.alpha = 0
		if not target then
			target = CreateSprite("target/target 1","Top")
			target.MoveTo(140, Arena.y + 50)
			target.SetVar("timer", 0)
			table.insert(Targets, target)
			timeranim = -1
		end
	elseif timeranim == -1 then
		if not target then
			timeranim = 0
			NewAudio.Unpause("Intro")
			song = true
		end
	elseif timeranim >= 0 and timeranim < 120 then
		if Foot1.absy > (Arena.y + 8 + Foot1.sprite.height/2) and not AnimAll then
			for i=1,#Parts do
				Parts[i].MoveToAbs(Parts[i].absx, Parts[i].absy - 6)
			end
			if Foot1.absy <= (Arena.y + 5 + Foot1.sprite.height/2 + 240) and not faserot then
				Eggman.sprite.SetAnimation({"Death Egg Robot/Eggman/1","Death Egg Robot/Eggman/2","Death Egg Robot/Eggman/3","Death Egg Robot/Eggman/4","Death Egg Robot/Eggman/5","Death Egg Robot/Eggman/6","Death Egg Robot/Eggman/5","Death Egg Robot/Eggman/6","Death Egg Robot/Eggman/5","Death Egg Robot/Eggman/4","Death Egg Robot/Eggman/3","Death Egg Robot/Eggman/4","Death Egg Robot/Eggman/3","Death Egg Robot/Eggman/4","Death Egg Robot/Eggman/3","Death Egg Robot/Eggman/2","Death Egg Robot/Eggman/1"}, 0.08)
				Eggman.sprite.loopmode = "ONESHOT"
				faserot = 0
			end
		else
			if timeranim == 0 then
				for i=1,11 do
					Parts[i].SetVar("X", Parts[i].absx)
					Parts[i].SetVar("Y", Parts[i].absy - 17)
				end
				Audio.PlaySound("LedgeBreak")
			end
			if not AnimAll then
				for i=1,11 do
					Parts[i].MoveToAbs(Parts[i].GetVar("X") + 17 * math.sin(-ElbowLeg1.sprite.rotation*math.pi/180), Parts[i].GetVar("Y") + 17 * math.cos(-ElbowLeg1.sprite.rotation*math.pi/180))
				end
				if faserot >= 0 and faserot < 30 then
					ElbowLeg1.sprite.rotation = ElbowLeg1.sprite.rotation - 3
					ElbowLeg2.sprite.rotation = ElbowLeg2.sprite.rotation - 3
					faserot = faserot + 1
				elseif faserot >= 30 and faserot < 60 then
					ElbowLeg1.sprite.rotation = ElbowLeg1.sprite.rotation + 3
					ElbowLeg2.sprite.rotation = ElbowLeg2.sprite.rotation + 3
					faserot = faserot + 1
				elseif faserot == 60 then
					AnimAll = true
					velrot = 4
					rotleg1 = 0
					rotleg2 = 0
					roles = 1
				end
			end
			if timeranim < 90 then
				timeranim = timeranim + 1
				if timeranim == 60 then
					Eggman.sprite.SetAnimation({"Death Egg Robot/Eggman/7","Death Egg Robot/Eggman/8","Death Egg Robot/Eggman/9","Death Egg Robot/Eggman/9","Death Egg Robot/Eggman/9","Death Egg Robot/Eggman/10","Death Egg Robot/Eggman/11","Death Egg Robot/Eggman/12","Death Egg Robot/Eggman/13","Death Egg Robot/Eggman/14"}, 0.1)
					Eggman.sprite.loopmode = "ONESHOT"
				end
			elseif timeranim >= 90 and timeranim < 120 then
				timeranim = timeranim + 1
				if timeranim == 100 then
					Audio.PlaySound("HullClose")
				end
				Head.sprite.rotation = Head.sprite.rotation - 3
				Head.MoveToAbs(Body.absx - 13 - 12/90 * Head.sprite.rotation + 25 * math.cos(Head.sprite.rotation*math.pi/180), Body.absy + 36 + 13/90 * (90-Head.sprite.rotation) + 25 * math.sin(Head.sprite.rotation*math.pi/180))
			end
		end
	end
	if AnimAll then
		if armattack then
			if armattack == "Pointing" then
				attackingarm = attackingarm + 1
				if attackingarm == 1 then
					if Arm1.sprite.rotation >= 270 and Arm1.sprite.rotation < 360 then
						diffpos = (400 - Arm1.sprite.rotation)/30
					elseif Arm1.sprite.rotation >= 0 and Arm1.sprite.rotation < 90 then
						diffpos = (40 - Arm1.sprite.rotation)/30
					end
				end
				if attackingarm > 0 and attackingarm <= 30 then
					Arm1.sprite.rotation = Arm1.sprite.rotation + diffpos
					ElbowArm1.sprite.rotation = ElbowArm1.sprite.rotation + diffpos
				end
				local angl = math.atan2(Player.absx - Hand1.absx, Player.absy - Hand1.absy)/math.pi * 180 * - 1 + 90
				while angl >= 360 do
					angl = angl - 360
				end
				while angl < 0 do
					angl = angl + 360
				end
				local ang2l = Hand1.sprite.rotation - angl - 180
				while ang2l >= 360 do
					ang2l = ang2l - 360
				end
				while ang2l < 0 do
					ang2l = ang2l + 360
				end
				if((ang2l < 180)) then
					ang2l = (360-ang2l)*-1
				end
				if(ang2l > 2) then
					Hand1.sprite.rotation = Hand1.sprite.rotation - 2
				elseif(ang2l < -2) then
					Hand1.sprite.rotation = Hand1.sprite.rotation + 2
				else 
					Hand1.sprite.rotation = angl
				end
				Fingers1.sprite.rotation = Hand1.sprite.rotation
			elseif armattack == "Attacking" then
				if attackingarm >= 0 and attackingarm <= 180 then
					attackingarm = attackingarm + 1
				end
				if attackingarm == 1 then
					velfingers = 1
					multiplicador = 0
				elseif attackingarm == 61 then
					Audio.PlaySound("buzzsaw")
				end
				if attackingarm > 60 and attackingarm <= 180 then
					if Fingers1.absx <= (Arena.x + Arena.width/2 + 10) and Fingers1.absy >= (Arena.y - 10) and Fingers1.absy <= (Arena.y + Arena.height + 10) and not stoplenght then
						lenghtfingers = lenghtfingers + 10
						for i=1,#Lenghts do
							if Lenghts[i].isactive then
								Lenghts[i].SetVar("radio", Lenghts[i].GetVar("radio") + 10)
							end
						end
						if (lenghtfingers - 34 - 16 * multiplicador) > 0 then
							Lenght1 = CreateProjectileAbs("Death Egg Robot/lenght 1", Hand1.absx, Hand1.absy)
							Lenght1.sprite.rotation = Fingers1.sprite.rotation
							Lenght1.SetVar("radio", lenghtfingers - 13 - 8 - 16*multiplicador)
							table.insert(Lenghts, Lenght1)
							ElbowArm1.sprite.layer = "Top"
							Hand1.sprite.layer = "Top"
							Fingers1.sprite.layer = "Top"
							multiplicador = multiplicador + 1
						end
					else
						stoplenght = true
					end
				else
					if lenghtfingers > 34 then
						lenghtfingers = lenghtfingers - 10
						for i=1,#Lenghts do
							if Lenghts[i - tableaux].isactive then
								if Lenghts[i - tableaux].GetVar("mover") then
									Lenghts[i - tableaux].SetVar("radio", Lenghts[i].GetVar("radio") - 10)
								else
									Lenghts[i - tableaux].SetVar("mover", true)
								end
								if Lenghts[i - tableaux].GetVar("radio") <= 13 then
									Lenghts[i - tableaux].Remove()
									table.remove(Lenghts, i - tableaux)
									tableaux = tableaux + 1
								end
							end
						end
						tableaux = 0
					else
						for i=1,#Lenghts do
							if Lenghts[i - tableaux].isactive then
								Lenghts[i - tableaux].Remove()
								table.remove(Lenghts, i - tableaux)
								tableaux = tableaux + 1
							end
						end
						tableaux = 0
						if attackingarm == 181 then
							velfingers = 5
						end
						if attackingarm > 180 and attackingarm <= 210 then
							if Hand1.sprite.rotation >= 270 and Hand1.sprite.rotation < 360 then
								diffpos = (360 - Hand1.sprite.rotation)/15
							elseif Hand1.sprite.rotation >= 0 and Hand1.sprite.rotation < 90 then
								diffpos = -Hand1.sprite.rotation/15
							end
							Arm1.sprite.rotation = Arm1.sprite.rotation - 40/30
							ElbowArm1.sprite.rotation = ElbowArm1.sprite.rotation - 40/30
							Hand1.sprite.rotation = Hand1.sprite.rotation + diffpos
							Fingers1.sprite.rotation = Fingers1.sprite.rotation + diffpos
						elseif attackingarm > 210 then
							armattack = "Done"
							stoplenght = nil
						end
						attackingarm = attackingarm + 1
					end
				end
			end
		end
		if roles == 1 then
			rotleg2 = rotleg2 - 1.5
			rotleg1 = rotleg1 + velrot
			rotdiff = Arm1.sprite.rotation
			if rotdiff > 0 then
				rotdiff = 0 + Arm2.sprite.rotation
			end
			if not armattack then
				Arm1.sprite.rotation = Arm1.sprite.rotation - (90-rotdiff)/270
				ElbowArm1.sprite.rotation = ElbowArm1.sprite.rotation - (90-rotdiff)/270
				Hand1.sprite.rotation = Hand1.sprite.rotation - (90-rotdiff)/270
				Fingers1.sprite.rotation = Fingers1.sprite.rotation - (90-rotdiff)/270
			end
			Arm2.sprite.rotation = Arm2.sprite.rotation + (90-rotdiff)/270
			ElbowArm2.sprite.rotation = ElbowArm2.sprite.rotation + (90-rotdiff)/270
			Hand2.sprite.rotation = Hand2.sprite.rotation + (90-rotdiff)/270
			Fingers2.sprite.rotation = Fingers2.sprite.rotation + (90-rotdiff)/270
			if rotleg1 > 90 and velrot > -4 then
				velrot = velrot - 0.5
			end
			if Foot1.absy <= (Arena.y + 5 + Foot1.sprite.height/2) and velrot < 0 then
				roles = 2
				velrot = 4
				rotleg1 = 36
				rotleg2 = -76
				Audio.PlaySound("Impact2")
			end
			ElbowLeg2.sprite.rotation = rotleg2
			ElbowLeg1.sprite.rotation = rotleg1
			ElbowLeg2.MoveToAbs(Body.absx + 9 + 17 * math.sin(rotleg2*math.pi/180), Body.absy - 21 - 17 * math.cos(rotleg2*math.pi/180))
			Leg2.MoveToAbs(ElbowLeg2.absx, ElbowLeg2.absy - 20)
			Foot2.MoveToAbs(Leg2.absx + 13, Leg2.absy - 21)
			ElbowLeg1.MoveToAbs(Body.absx + 9 + 17 * math.sin(rotleg1*math.pi/180), Body.absy - 21 - 17 * math.cos(rotleg1*math.pi/180))
			Leg1.MoveToAbs(ElbowLeg1.absx, ElbowLeg1.absy - 20)
			Foot1.MoveToAbs(Leg1.absx + 13, Leg1.absy - 21)
			ElbowArm1.MoveToAbs(Arm1.absx + 22 * math.sin(ElbowArm1.sprite.rotation*math.pi/180), Arm1.absy - 22 * math.cos(ElbowArm1.sprite.rotation*math.pi/180))
			Hand1.MoveToAbs(ElbowArm1.absx + 24 * math.cos(-Hand1.sprite.rotation*math.pi/180), ElbowArm1.absy - 24 * math.sin(-Hand1.sprite.rotation*math.pi/180))
			Fingers1.MoveToAbs(Hand1.absx + lenghtfingers * math.cos(-Fingers1.sprite.rotation*math.pi/180), Hand1.absy - lenghtfingers * math.sin(-Fingers1.sprite.rotation*math.pi/180))
			ElbowArm2.MoveToAbs(Arm2.absx + 22 * math.sin(ElbowArm2.sprite.rotation*math.pi/180), Arm2.absy - 22 * math.cos(ElbowArm2.sprite.rotation*math.pi/180))
			Hand2.MoveToAbs(ElbowArm2.absx + 24 * math.cos(-Hand2.sprite.rotation*math.pi/180), ElbowArm2.absy - 24 * math.sin(-Hand2.sprite.rotation*math.pi/180))
			Fingers2.MoveToAbs(Hand2.absx + 34 * math.cos(-Fingers2.sprite.rotation*math.pi/180), Hand2.absy - 34 * math.sin(-Fingers2.sprite.rotation*math.pi/180))
			local diff = 0
			if Foot2.absy > (Arena.y + 5 + Foot2.sprite.height/2) then
				diff = Foot2.absy - (Arena.y + 5 + Foot2.sprite.height/2)
				for i=1,#Parts do
					Parts[i].MoveToAbs(Parts[i].absx, Parts[i].absy - diff)
				end
			elseif Foot2.absy < (Arena.y + 5 + Foot2.sprite.height/2) then
				diff = (Arena.y + 5 + Foot2.sprite.height/2) - Foot2.absy
				for i=1,#Parts do
					Parts[i].MoveToAbs(Parts[i].absx, Parts[i].absy + diff)
				end
			end
		elseif roles == 2 then
			rotleg2 = rotleg2 + 4
			rotleg1 = rotleg1 - 36/19
			if not armattack then
				Arm1.sprite.rotation = Arm1.sprite.rotation + 15/19
				ElbowArm1.sprite.rotation = ElbowArm1.sprite.rotation + 15/19
				Hand1.sprite.rotation = Hand1.sprite.rotation + 15/19
				Fingers1.sprite.rotation = Fingers1.sprite.rotation + 15/19
			end
			Arm2.sprite.rotation = Arm2.sprite.rotation - 15/19
			ElbowArm2.sprite.rotation = ElbowArm2.sprite.rotation - 15/19
			Hand2.sprite.rotation = Hand2.sprite.rotation - 15/19
			Fingers2.sprite.rotation = Fingers2.sprite.rotation - 15/19
			if rotleg2 == 0 then
				roles = 3
				rotleg1 = 0
				rotleg2 = 0
				if not armattack then
					Arm1.sprite.rotation = 0
					ElbowArm1.sprite.rotation = 0
					Hand1.sprite.rotation = 0
					Fingers1.sprite.rotation = 0
				else
					if armattack == "Done" then
						armattack = nil
						Arm1.sprite.rotation = 0
						ElbowArm1.sprite.rotation = 0
						Hand1.sprite.rotation = 0
						Fingers1.sprite.rotation = 0
					end
				end
				Arm2.sprite.rotation = 0
				ElbowArm2.sprite.rotation = 0
				Hand2.sprite.rotation = 0
				Fingers2.sprite.rotation = 0
			end
			ElbowLeg2.sprite.rotation = rotleg2
			ElbowLeg1.sprite.rotation = rotleg1
			ElbowLeg2.MoveToAbs(Body.absx + 9 + 17 * math.sin(rotleg2*math.pi/180), Body.absy - 21 - 17 * math.cos(rotleg2*math.pi/180))
			Leg2.MoveToAbs(ElbowLeg2.absx, ElbowLeg2.absy - 20)
			Foot2.MoveToAbs(Leg2.absx + 13, Leg2.absy - 21)
			ElbowLeg1.MoveToAbs(Body.absx + 9 + 17 * math.sin(rotleg1*math.pi/180), Body.absy - 21 - 17 * math.cos(rotleg1*math.pi/180))
			Leg1.MoveToAbs(ElbowLeg1.absx, ElbowLeg1.absy - 20)
			Foot1.MoveToAbs(Leg1.absx + 13, Leg1.absy - 21)
			ElbowArm1.MoveToAbs(Arm1.absx + 22 * math.sin(ElbowArm1.sprite.rotation*math.pi/180), Arm1.absy - 22 * math.cos(ElbowArm1.sprite.rotation*math.pi/180))
			Hand1.MoveToAbs(ElbowArm1.absx + 24 * math.cos(-Hand1.sprite.rotation*math.pi/180), ElbowArm1.absy - 24 * math.sin(-Hand1.sprite.rotation*math.pi/180))
			Fingers1.MoveToAbs(Hand1.absx + lenghtfingers * math.cos(-Fingers1.sprite.rotation*math.pi/180), Hand1.absy - lenghtfingers * math.sin(-Fingers1.sprite.rotation*math.pi/180))
			ElbowArm2.MoveToAbs(Arm2.absx + 22 * math.sin(ElbowArm2.sprite.rotation*math.pi/180), Arm2.absy - 22 * math.cos(ElbowArm2.sprite.rotation*math.pi/180))
			Hand2.MoveToAbs(ElbowArm2.absx + 24 * math.cos(-Hand2.sprite.rotation*math.pi/180), ElbowArm2.absy - 24 * math.sin(-Hand2.sprite.rotation*math.pi/180))
			Fingers2.MoveToAbs(Hand2.absx + 34 * math.cos(-Fingers2.sprite.rotation*math.pi/180), Hand2.absy - 34 * math.sin(-Fingers2.sprite.rotation*math.pi/180))
			local diff = 0
			if Foot1.absy > (Arena.y + 5 + Foot1.sprite.height/2) then
				diff = Foot1.absy - (Arena.y + 5 + Foot1.sprite.height/2)
				for i=1,#Parts do
					Parts[i].MoveToAbs(Parts[i].absx, Parts[i].absy - diff)
				end
			elseif Foot1.absy < (Arena.y + 5 + Foot1.sprite.height/2) then
				diff = (Arena.y + 5 + Foot1.sprite.height/2) - Foot1.absy
				for i=1,#Parts do
					Parts[i].MoveToAbs(Parts[i].absx, Parts[i].absy + diff)
				end
			end
		elseif roles == 3 then
			rotleg2 = rotleg2 + velrot
			rotleg1 = rotleg1 - 1.5
			rotdiff = Arm2.sprite.rotation
			if rotdiff > 0 then
				rotdiff = 360 - Arm2.sprite.rotation
			end
			if not armattack then
				Arm1.sprite.rotation = Arm1.sprite.rotation + (90-rotdiff)/270
				ElbowArm1.sprite.rotation = ElbowArm1.sprite.rotation + (90-rotdiff)/270
				Hand1.sprite.rotation = Hand1.sprite.rotation + (90-rotdiff)/270
				Fingers1.sprite.rotation = Fingers1.sprite.rotation + (90-rotdiff)/270
			end
			Arm2.sprite.rotation = Arm2.sprite.rotation - (90-rotdiff)/270
			ElbowArm2.sprite.rotation = ElbowArm2.sprite.rotation - (90-rotdiff)/270
			Hand2.sprite.rotation = Hand2.sprite.rotation - (90-rotdiff)/270
			Fingers2.sprite.rotation = Fingers2.sprite.rotation - (90-rotdiff)/270
			if rotleg2 > 90 and velrot > -4 then
				velrot = velrot - 0.5
			end
			if Foot2.absy <= (Arena.y + 5 + Foot2.sprite.height/2) and velrot < 0 then
				roles = 4
				velrot = 4
				rotleg2 = 36
				rotleg1 = -76
				Audio.PlaySound("Impact2")
			end
			ElbowLeg2.sprite.rotation = rotleg2
			ElbowLeg1.sprite.rotation = rotleg1
			ElbowLeg2.MoveToAbs(Body.absx + 9 + 17 * math.sin(rotleg2*math.pi/180), Body.absy - 21 - 17 * math.cos(rotleg2*math.pi/180))
			Leg2.MoveToAbs(ElbowLeg2.absx, ElbowLeg2.absy - 20)
			Foot2.MoveToAbs(Leg2.absx + 13, Leg2.absy - 21)
			ElbowLeg1.MoveToAbs(Body.absx + 9 + 17 * math.sin(rotleg1*math.pi/180), Body.absy - 21 - 17 * math.cos(rotleg1*math.pi/180))
			Leg1.MoveToAbs(ElbowLeg1.absx, ElbowLeg1.absy - 20)
			Foot1.MoveToAbs(Leg1.absx + 13, Leg1.absy - 21)
			ElbowArm1.MoveToAbs(Arm1.absx + 22 * math.sin(ElbowArm1.sprite.rotation*math.pi/180), Arm1.absy - 22 * math.cos(ElbowArm1.sprite.rotation*math.pi/180))
			Hand1.MoveToAbs(ElbowArm1.absx + 24 * math.cos(-Hand1.sprite.rotation*math.pi/180), ElbowArm1.absy - 24 * math.sin(-Hand1.sprite.rotation*math.pi/180))
			Fingers1.MoveToAbs(Hand1.absx + lenghtfingers * math.cos(-Fingers1.sprite.rotation*math.pi/180), Hand1.absy - lenghtfingers * math.sin(-Fingers1.sprite.rotation*math.pi/180))
			ElbowArm2.MoveToAbs(Arm2.absx + 22 * math.sin(ElbowArm2.sprite.rotation*math.pi/180), Arm2.absy - 22 * math.cos(ElbowArm2.sprite.rotation*math.pi/180))
			Hand2.MoveToAbs(ElbowArm2.absx + 24 * math.cos(-Hand2.sprite.rotation*math.pi/180), ElbowArm2.absy - 24 * math.sin(-Hand2.sprite.rotation*math.pi/180))
			Fingers2.MoveToAbs(Hand2.absx + 34 * math.cos(-Fingers2.sprite.rotation*math.pi/180), Hand2.absy - 34 * math.sin(-Fingers2.sprite.rotation*math.pi/180))
			local diff = 0
			if Foot1.absy > (Arena.y + 5 + Foot1.sprite.height/2) then
				diff = Foot1.absy - (Arena.y + 5 + Foot1.sprite.height/2)
				for i=1,#Parts do
					Parts[i].MoveToAbs(Parts[i].absx, Parts[i].absy - diff)
				end
			elseif Foot1.absy < (Arena.y + 5 + Foot1.sprite.height/2) then
				diff = (Arena.y + 5 + Foot1.sprite.height/2) - Foot1.absy
				for i=1,#Parts do
					Parts[i].MoveToAbs(Parts[i].absx, Parts[i].absy + diff)
				end
			end
		elseif roles == 4 then
			rotleg2 = rotleg2 - 36/19
			rotleg1 = rotleg1 + 4
			if not armattack then
				Arm1.sprite.rotation = Arm1.sprite.rotation - 15/19
				ElbowArm1.sprite.rotation = ElbowArm1.sprite.rotation - 15/19
				Hand1.sprite.rotation = Hand1.sprite.rotation - 15/19
				Fingers1.sprite.rotation = Fingers1.sprite.rotation - 15/19
			end
			Arm2.sprite.rotation = Arm2.sprite.rotation + 15/19
			ElbowArm2.sprite.rotation = ElbowArm2.sprite.rotation + 15/19
			Hand2.sprite.rotation = Hand2.sprite.rotation + 15/19
			Fingers2.sprite.rotation = Fingers2.sprite.rotation + 15/19
			if rotleg1 == 0 then
				roles = 1
				rotleg1 = 0
				rotleg2 = 0
				if not armattack then
					Arm1.sprite.rotation = 0
					ElbowArm1.sprite.rotation = 0
					Hand1.sprite.rotation = 0
					Fingers1.sprite.rotation = 0
				else
					if armattack == "Done" then
						armattack = nil
						Arm1.sprite.rotation = 0
						ElbowArm1.sprite.rotation = 0
						Hand1.sprite.rotation = 0
						Fingers1.sprite.rotation = 0
					end
				end
				Arm2.sprite.rotation = 0
				ElbowArm2.sprite.rotation = 0
				Hand2.sprite.rotation = 0
				Fingers2.sprite.rotation = 0
			end
			ElbowLeg2.sprite.rotation = rotleg2
			ElbowLeg1.sprite.rotation = rotleg1
			ElbowLeg2.MoveToAbs(Body.absx + 9 + 17 * math.sin(rotleg2*math.pi/180), Body.absy - 21 - 17 * math.cos(rotleg2*math.pi/180))
			Leg2.MoveToAbs(ElbowLeg2.absx, ElbowLeg2.absy - 20)
			Foot2.MoveToAbs(Leg2.absx + 13, Leg2.absy - 21)
			ElbowLeg1.MoveToAbs(Body.absx + 9 + 17 * math.sin(rotleg1*math.pi/180), Body.absy - 21 - 17 * math.cos(rotleg1*math.pi/180))
			Leg1.MoveToAbs(ElbowLeg1.absx, ElbowLeg1.absy - 20)
			Foot1.MoveToAbs(Leg1.absx + 13, Leg1.absy - 21)
			ElbowArm1.MoveToAbs(Arm1.absx + 22 * math.sin(ElbowArm1.sprite.rotation*math.pi/180), Arm1.absy - 22 * math.cos(ElbowArm1.sprite.rotation*math.pi/180))
			Hand1.MoveToAbs(ElbowArm1.absx + 24 * math.cos(-Hand1.sprite.rotation*math.pi/180), ElbowArm1.absy - 24 * math.sin(-Hand1.sprite.rotation*math.pi/180))
			Fingers1.MoveToAbs(Hand1.absx + lenghtfingers * math.cos(-Fingers1.sprite.rotation*math.pi/180), Hand1.absy - lenghtfingers * math.sin(-Fingers1.sprite.rotation*math.pi/180))
			ElbowArm2.MoveToAbs(Arm2.absx + 22 * math.sin(ElbowArm2.sprite.rotation*math.pi/180), Arm2.absy - 22 * math.cos(ElbowArm2.sprite.rotation*math.pi/180))
			Hand2.MoveToAbs(ElbowArm2.absx + 24 * math.cos(-Hand2.sprite.rotation*math.pi/180), ElbowArm2.absy - 24 * math.sin(-Hand2.sprite.rotation*math.pi/180))
			Fingers2.MoveToAbs(Hand2.absx + 34 * math.cos(-Fingers2.sprite.rotation*math.pi/180), Hand2.absy - 34 * math.sin(-Fingers2.sprite.rotation*math.pi/180))
			local diff = 0
			if Foot2.absy > (Arena.y + 5 + Foot2.sprite.height/2) then
				diff = Foot2.absy - (Arena.y + 5 + Foot2.sprite.height/2)
				for i=1,#Parts do
					Parts[i].MoveToAbs(Parts[i].absx, Parts[i].absy - diff)
				end
			elseif Foot2.absy < (Arena.y + 5 + Foot2.sprite.height/2) then
				diff = (Arena.y + 5 + Foot2.sprite.height/2) - Foot2.absy
				for i=1,#Parts do
					Parts[i].MoveToAbs(Parts[i].absx, Parts[i].absy + diff)
				end
			end
		end
		Player.MoveToAbs(Player.absx - 0.5, Player.absy, false)
	end
	if framelenght == 1 then
		framelenght = 2
	else
		framelenght = 1
	end
	for i=1,#Lenghts do
		if Lenghts[i].isactive then
			local largo = Lenghts[i].GetVar("radio")
			Lenghts[i].sprite.Set("Death Egg Robot/lenght "..framelenght)
			Lenghts[i].MoveToAbs(Hand1.absx + largo * math.cos(-Lenghts[i].sprite.rotation*math.pi/180), Hand1.absy - largo * math.sin(-Lenghts[i].sprite.rotation*math.pi/180))
		end
	end
	for i=1,#Throw do
		local objeto = Throw[i - tableaux]
		if objeto.isactive then
			objeto.SetVar("timer", objeto.GetVar("timer") + 1)
			if objeto.GetVar("timer") <= 60 then
				objeto.MoveToAbs(objeto.absx + objeto.GetVar("diff"), objeto.absy + objeto.GetVar("Y"))
				if objeto.GetVar("Y") > -10 then
					objeto.SetVar("Y", objeto.GetVar("Y") - 0.4)
				end
				if objeto.absy <= (Arena.y + 5 + objeto.sprite.height/2) then
					objeto.MoveToAbs(objeto.absx, Arena.y + 5 + objeto.sprite.height/2)
					objeto.SetVar("timer", 61)
				end
			else
				if objeto.GetVar("type") == "Explotion" then
					Audio.PlaySound("Bomb")
					explotion = CreateProjectileAbs("boom", objeto.absx, objeto.absy)
					explotion.sprite.Scale(0.4,0.4)
					table.insert(Expand, explotion)
					objeto.Remove()
				elseif objeto.GetVar("type") == "EggBot1" then
					Audio.PlaySound("Pon")
					if math.random(2) == 1 then
						bottom = CreateSprite("Eggs/bottomeggL 1","Top")
					else
						bottom = CreateSprite("Eggs/bottomeggR 1","Top")
					end
					if math.random(2) == 1 then
						top = CreateSprite("Eggs/topeggL","Top")
					else
						top = CreateSprite("Eggs/topeggR","Top")
					end
					bottom.MoveTo(objeto.absx, objeto.absy - 9)
					bottom.SetVar("X", math.random(2,-2))
					bottom.SetVar("Y", math.random(3))
					top.MoveTo(objeto.absx, objeto.absy + 9)
					top.SetVar("X", math.random(2,-2))
					top.SetVar("Y", math.random(3))
					table.insert(Fall, bottom)
					table.insert(Fall, top)
					if objeto.absx < Player.absx then
						EggBot = CreateProjectileAbs("Egg Bots/R/Egg Bot 1-1", objeto.absx, objeto.absy + 8, "BelowPlayer")
						EggBot.SetVar("direction", "R")
					else
						EggBot = CreateProjectileAbs("Egg Bots/L/Egg Bot 1-1", objeto.absx, objeto.absy + 8, "BelowPlayer")
						EggBot.SetVar("direction", "L")
					end
					EggBot.sprite.Scale(0, 0)
					EggBot.SetVar("Y", math.random(1,1.5))
					EggBot.SetVar("type", 1)
					EggBot.SetVar("frame", 1)
					EggBot.SetVar("times", 1)
					table.insert(EggBots, EggBot)
					objeto.Remove()
				elseif objeto.GetVar("type") == "EggBot2" then
					Audio.PlaySound("Pon")
					if math.random(2) == 1 then
						bottom = CreateSprite("Eggs/bottomeggL 2","Top")
					else
						bottom = CreateSprite("Eggs/bottomeggR 2","Top")
					end
					if math.random(2) == 1 then
						top = CreateSprite("Eggs/topeggL","Top")
					else
						top = CreateSprite("Eggs/topeggR","Top")
					end
					bottom.MoveTo(objeto.absx, objeto.absy - 9)
					bottom.SetVar("X", math.random(2,-2))
					bottom.SetVar("Y", math.random(3))
					top.MoveTo(objeto.absx, objeto.absy + 9)
					top.SetVar("X", math.random(2,-2))
					top.SetVar("Y", math.random(3))
					table.insert(Fall, bottom)
					table.insert(Fall, top)
					if objeto.absx < Player.absx then
						EggBot = CreateProjectileAbs("Egg Bots/R/Egg Bot 2-1", objeto.absx, objeto.absy + 8, "BelowPlayer")
						EggBot.SetVar("direction", "R")
					else
						EggBot = CreateProjectileAbs("Egg Bots/L/Egg Bot 2-1", objeto.absx, objeto.absy + 8, "BelowPlayer")
						EggBot.SetVar("direction", "L")
					end
					EggBot.sprite.Scale(0, 0)
					EggBot.SetVar("type", 2)
					EggBot.SetVar("frame", 1)
					EggBot.SetVar("times", 1)
					table.insert(EggBots, EggBot)
					objeto.Remove()
					objeto = nil
					table.remove(Throw, i - tableaux)
					tableaux = tableaux + 1
				end
			end
		end
	end
	tableaux = 0
	for i=1,#EggBots do
		local Bot = EggBots[i - tableaux]
		if Bot.isactive then
			if Bot.GetVar("type") == 1 then
				if not Bot.GetVar("mover") then
					Bot.MoveToAbs(Bot.absx - 0.5, Bot.absy + Bot.GetVar("Y"))
					if Bot.sprite.xscale < 1 then
						Bot.sprite.Scale(Bot.sprite.xscale + 0.05, Bot.sprite.yscale + 0.05)
					end
					if Bot.GetVar("Y") > -1 then
						Bot.SetVar("Y", Bot.GetVar("Y") - 0.05)
					end
					if Bot.absy <= (Arena.y + 5 + Bot.sprite.height/2) then
						Bot.SetVar("mover", true)
						Bot.MoveToAbs(Bot.absx, Arena.y + 5 + Bot.sprite.height/2)
					end
				else
					Bot.sprite.Set("Egg Bots/"..Bot.GetVar("direction").."/Egg Bot 1-"..Bot.GetVar("frame"))
					if not Bot.GetVar("timer") then
						Bot.SetVar("frame", Bot.GetVar("frame") + 1)
						Bot.SetVar("timer", 1)
					else
						Bot.SetVar("timer", nil)
					end
					if Bot.GetVar("frame") == 7 then
						Bot.SetVar("frame", 1)
					end
					if Bot.GetVar("direction") == "L" then
						Bot.MoveToAbs(Bot.absx - 2.5, Bot.absy)
					else
						Bot.MoveToAbs(Bot.absx + 1.5, Bot.absy)
					end
					if Bot.absx < (Arena.x - Arena.width/2 - Bot.sprite.width/2 - 10) or Bot.absx > (Arena.x + Arena.width/2 + Bot.sprite.width/2 + 10) then
						if Bot.GetVar("direction") == "L" then
							Bot.SetVar("direction", "R")
							Bot.MoveToAbs(Arena.x - Arena.width/2 - Bot.sprite.width/2 - 10, Bot.absy)
						else
							Bot.SetVar("direction", "L")
							Bot.MoveToAbs(Arena.x + Arena.width/2 + Bot.sprite.width/2 + 10, Bot.absy)
						end
						Bot.SetVar("times", Bot.GetVar("times") + 1)
						if Bot.GetVar("times") == 7 then
							Bot.Remove()
							table.remove(EggBots, i - tableaux)
							tableaux = tableaux + 1
						end
					end
				end
			else
				if not Bot.GetVar("mover") then
					Bot.MoveToAbs(Bot.absx, Bot.absy + 1)
					if Bot.sprite.xscale < 1 then
						Bot.sprite.Scale(Bot.sprite.xscale + 0.05, Bot.sprite.yscale + 0.05)
					end
					if Bot.absy >= (Arena.y + Arena.height - 20) then
						Bot.SetVar("mover", true)
						Bot.SetVar("timer", 100)
						Bot.SetVar("Y", -3)
					end
				else
					Bot.SetVar("timer", Bot.GetVar("timer") + 1)
					if Bot.GetVar("direction") == "L" then
						if Bot.GetVar("Y") > -3 then
							Bot.MoveToAbs(Bot.absx - 2, Bot.absy + Bot.GetVar("Y"))
						else
							Bot.MoveToAbs(Bot.absx - 2, Bot.absy)
						end
					else
						if Bot.GetVar("Y") > -3 then
							Bot.MoveToAbs(Bot.absx + 2, Bot.absy + Bot.GetVar("Y"))
						else
							Bot.MoveToAbs(Bot.absx + 2, Bot.absy)
						end
					end
					if Bot.GetVar("Y") > -3 and Bot.GetVar("Y") < 2.2 then
						Bot.SetVar("Y", Bot.GetVar("Y") + 0.04)
					else
						Bot.SetVar("Y", -3)
					end
					if Bot.GetVar("timer")%180 == 0 then
						Bot.SetVar("Y", -2.2)
					end
					if Bot.GetVar("timer")%120 == 0 then
						Audio.PlaySound("Fireball")
						if Bot.GetVar("direction") == "R" then
							Flame = CreateProjectileAbs("Flames/Falling/1", Bot.absx + 4, Bot.absy - 17, "BelowPlayer")
						else
							Flame = CreateProjectileAbs("Flames/Falling/1", Bot.absx - 4, Bot.absy - 17, "BelowPlayer")
						end
						Flame.sprite.SetAnimation({"Flames/Falling/1","Flames/Falling/2","Flames/Falling/3","Flames/Falling/4","Flames/Falling/3","Flames/Falling/2"}, 0.08)
						Flame.SetVar("pos", -35)
						table.insert(Spawner, Flame)
					end
					if Bot.absx < (Arena.x - Arena.width/2 - Bot.sprite.width/2 + 100) or Bot.absx > (Arena.x + Arena.width/2 + Bot.sprite.width/2 + 10) then
						if Bot.GetVar("times") < 6 then
							if Bot.GetVar("direction") == "L" then
								Bot.SetVar("direction", "R")
								Bot.MoveToAbs(Arena.x - Arena.width/2 - Bot.sprite.width/2 + 100, Bot.absy)
							else
								Bot.SetVar("direction", "L")
								Bot.MoveToAbs(Arena.x + Arena.width/2 + Bot.sprite.width/2 + 10, Bot.absy)
							end
						end
						Bot.SetVar("times", Bot.GetVar("times") + 1)
						if Bot.GetVar("times") >= 7 and (Bot.absx < (Arena.x - Arena.width/2 - Bot.sprite.width/2 - 10) or Bot.absx > (Arena.x + Arena.width/2 + Bot.sprite.width/2 + 10)) then
							Bot.Remove()
							table.remove(EggBots, i - tableaux)
							tableaux = tableaux + 1
						end
					end
				end
				Bot.sprite.Set("Egg Bots/"..Bot.GetVar("direction").."/Egg Bot 2-"..Bot.GetVar("frame"))
				if not Bot.GetVar("appear") then
					Bot.SetVar("frame", Bot.GetVar("frame") + 1)
					Bot.SetVar("appear", 1)
				else
					Bot.SetVar("appear", nil)
				end
				if Bot.GetVar("frame") == 3 then
					Bot.SetVar("frame", 1)
				end
			end
		end
	end
	tableaux = 0
	for i=1,#Spawner do
		if Spawner[i - tableaux].isactive then
			Spawner[i - tableaux].MoveToAbs(Spawner[i - tableaux].absx, Spawner[i - tableaux].absy - 2)
			if Spawner[i - tableaux].absy <= (Arena.y + 5 + Spawner[i - tableaux].sprite.height/2) then
				for j=1,8 do
					Fire = CreateProjectileAbs("Flames/Normal/1", Spawner[i - tableaux].absx + Spawner[i - tableaux].GetVar("pos"), Arena.y + 10, "BelowPlayer")
					Fire.sprite.SetAnimation({"Flames/Normal/5","Flames/Normal/4","Flames/Normal/3","Flames/Normal/2","Flames/Normal/1","Flames/Normal/2","Flames/Normal/3","Flames/Normal/1","Flames/Normal/2","Flames/Normal/3","Flames/Normal/4","Flames/Normal/5","Flames/Normal/6","Flames/Normal/7","Flames/Normal/8","empty"}, 0.08)
					Fire.sprite.loopmode = "ONESHOT"
					Fire.SetVar("timer", 0)
					table.insert(Fires, Fire)
					Spawner[i - tableaux].SetVar("pos", Spawner[i - tableaux].GetVar("pos") + 10)
				end
				Spawner[i - tableaux].Remove()
				table.remove(Spawner, i - tableaux)
				tableaux = tableaux + 1
			end
		end
	end
	tableaux = 0
	for i=1,#Fires do
		if Fires[i - tableaux].isactive then
			Fires[i - tableaux].SetVar("timer", Fires[i - tableaux].GetVar("timer") + 1)
			if Fires[i - tableaux].GetVar("timer") == 120 then
				Fires[i - tableaux].Remove()
				table.remove(Fires, i - tableaux)
				tableaux = tableaux + 1
			end
		end
	end
	tableaux = 0
	for i=1,#Fall do
		if Fall[i - tableaux].isactive then
			Fall[i - tableaux].MoveTo(Fall[i - tableaux].x + Fall[i - tableaux].GetVar("X"), Fall[i - tableaux].y + Fall[i - tableaux].GetVar("Y"))
			if Fall[i - tableaux].GetVar("Y") > -2 then
				Fall[i - tableaux].SetVar("Y", Fall[i - tableaux].GetVar("Y") - 0.1)
			end
			if Fall[i - tableaux].y <= (Arena.y - 8) then
				Fall[i - tableaux].alpha = Fall[i - tableaux].alpha - 0.02
			end
			if Fall[i - tableaux].alpha <= 0 then
				Fall[i - tableaux].Remove()
				table.remove(Fall, i - tableaux)
				tableaux = tableaux + 1
			end
		end
	end
	tableaux = 0
	for i=1,#Expand do
		if Expand[i - tableaux].isactive then
			Expand[i - tableaux].sprite.Scale(Expand[i - tableaux].sprite.xscale + 4/46, Expand[i - tableaux].sprite.yscale + 4/46)
			Expand[i - tableaux].sprite.alpha = Expand[i - tableaux].sprite.alpha - 0.02
			if Expand[i - tableaux].sprite.alpha <= 0 then
				Expand[i - tableaux].Remove()
				table.remove(Expand, i - tableaux)
				tableaux = tableaux + 1
			end
		end
	end
	tableaux = 0
	for i=1,#Targets do
		local objetivo = Targets[i - tableaux]
		if objetivo.isactive then
			objetivo.SetVar("timer", objetivo.GetVar("timer") + 1)
			if objetivo.GetVar("timer") == 1 then
				if objetivo.GetVar("attack") == "Arm" then
					attackingarm = 0
					armattack = "Pointing"
				end
				objetivo.SetAnimation({"target/target 1","target/target 2","target/target 3"}, 0.02)
				for j=1,4 do
					objetivo.SetVar("Arrow"..j, CreateSprite("target/arrow "..j,"Top"))
					if j == 1 then
						objetivo.GetVar("Arrow"..j).MoveTo(objetivo.x - 45, objetivo.y + 45)
						objetivo.GetVar("Arrow"..j).SetVar("velx", 1.5)
						objetivo.GetVar("Arrow"..j).SetVar("vely", -1.5)
					elseif j == 2 then
						objetivo.GetVar("Arrow"..j).MoveTo(objetivo.x + 45, objetivo.y + 45)
						objetivo.GetVar("Arrow"..j).SetVar("velx", -1.5)
						objetivo.GetVar("Arrow"..j).SetVar("vely", -1.5)
					elseif j == 3 then
						objetivo.GetVar("Arrow"..j).MoveTo(objetivo.x - 45, objetivo.y - 45)
						objetivo.GetVar("Arrow"..j).SetVar("velx", 1.5)
						objetivo.GetVar("Arrow"..j).SetVar("vely", 1.5)
					elseif j == 4 then
						objetivo.GetVar("Arrow"..j).MoveTo(objetivo.x + 45, objetivo.y - 45)
						objetivo.GetVar("Arrow"..j).SetVar("velx", -1.5)
						objetivo.GetVar("Arrow"..j).SetVar("vely", 1.5)
					end
					objetivo.GetVar("Arrow"..j).SetParent(objetivo)
				end
				Audio.PlaySound("Targeting1")
			elseif objetivo.GetVar("timer") == 45 then
				objetivo.SetVar("Pointer", CreateSprite("target/pointer 1","Top"))
				objetivo.GetVar("Pointer").SetAnimation({"target/pointer 1","target/pointer 2"}, 0.01)
				objetivo.GetVar("Pointer").MoveTo(objetivo.x, objetivo.y)
				objetivo.GetVar("Pointer").SetParent(objetivo)
			end
			if objetivo.GetVar("timer")%20 == 0 then
				objetivo.GetVar("Arrow1").MoveTo(objetivo.GetVar("Arrow1").x - 30, objetivo.GetVar("Arrow1").y + 30)
				objetivo.GetVar("Arrow2").MoveTo(objetivo.GetVar("Arrow2").x + 30, objetivo.GetVar("Arrow2").y + 30)
				objetivo.GetVar("Arrow3").MoveTo(objetivo.GetVar("Arrow3").x - 30, objetivo.GetVar("Arrow3").y - 30)
				objetivo.GetVar("Arrow4").MoveTo(objetivo.GetVar("Arrow4").x + 30, objetivo.GetVar("Arrow4").y - 30)
			end
			for j=1,4 do
				objetivo.GetVar("Arrow"..j).MoveTo(objetivo.GetVar("Arrow"..j).x + objetivo.GetVar("Arrow"..j).GetVar("velx"), objetivo.GetVar("Arrow"..j).y + objetivo.GetVar("Arrow"..j).GetVar("vely"))
			end
			if objetivo.GetVar("Follow") then
				objetivo.MoveTo(Player.absx, Player.absy)
			end
			if objetivo.GetVar("timer") == 100 then
				target = nil
			elseif objetivo.GetVar("timer") == 120 then
				if objetivo.GetVar("attack") == "Bomb" then
					Audio.PlaySound("Drop")
					Bomb = CreateProjectileAbs("bomb 1", Body.absx - 45, Body.absy + 20, "BelowBullet")
					Bomb.sprite.SetAnimation({"bomb 1","bomb 2"}, 0.08)
					Bomb.SetVar("diff", (Player.absx - Bomb.absx)/60)
					Bomb.SetVar("type", "Explotion")
					Bomb.SetVar("timer", 0)
					Bomb.SetVar("Y", 10)
					table.insert(Throw, Bomb)
				elseif objetivo.GetVar("attack") == "EggBot1" then
					Audio.PlaySound("Drop")
					Capsule = CreateProjectileAbs("Eggs/egg 1", Body.absx - 45, Body.absy + 20, "BelowBullet")
					Capsule.SetVar("diff", (Player.absx - Capsule.absx)/60)
					Capsule.SetVar("type", "EggBot1")
					Capsule.SetVar("timer", 0)
					Capsule.SetVar("Y", 10)
					table.insert(Throw, Capsule)
				elseif objetivo.GetVar("attack") == "EggBot2" then
					Audio.PlaySound("Drop")
					Capsule = CreateProjectileAbs("Eggs/egg 2", Body.absx - 45, Body.absy + 20, "BelowBullet")
					Capsule.SetVar("diff", (Player.absx - Capsule.absx)/60)
					Capsule.SetVar("type", "EggBot2")
					Capsule.SetVar("timer", 0)
					Capsule.SetVar("Y", 10)
					table.insert(Throw, Capsule)
				elseif objetivo.GetVar("attack") == "Arm" then
					attackingarm = 0
					armattack = "Attacking"
				end
				for j=1,4 do
					objetivo.GetVar("Arrow"..j).Remove()
				end
				objetivo.GetVar("Pointer").Remove()
				objetivo.Remove()
				table.remove(Targets, i - tableaux)
				tableaux = tableaux + 1
			end
		end
	end
	tableaux = 0
end

function OnHit(bullet)
	if bullet.sprite.alpha >= 0.4 then
		if Player.hp > 3 then
			Player.Hurt(3)
		elseif not Player.isHurting then
			Black1.Remove()
			Black2.Remove()
			Black3.Remove()
			for i=1,#Fall do
				Fall[i].Remove()
			end
			for i=1,#Targets do
				for j=1,4 do
					Targets[i].GetVar("Arrow"..j).Remove()
				end
				if Targets[i].GetVar("Pointer") then
					Targets[i].GetVar("Pointer").Remove()
				end
				Targets[i].Remove()
			end
			NewAudio.DestroyChannel("Egg Robot Song")
			NewAudio.DestroyChannel("Intro")
			Encounter["nextwaves"] = {"Undertale Game Over D"}
			State("ACTIONSELECT")
			State("DEFENDING")
		end
	end
end