Arena.Resize(360, 130)

timer = 0
tableaux = 0
Black1 = CreateSprite("Pixel","Top")
Black1.Scale(640, 27)
Black1.MoveTo(320, 243.5)
Black2 = CreateSprite("Pixel","Top")
Black2.Scale(135, 130)
Black2.MoveTo(67.5, 160)
Black3 = CreateSprite("Pixel","Top")
Black3.Scale(135, 130)
Black3.MoveTo(572.5, 160)
Black1.color = {0,0,0}
Black2.color = {0,0,0}
Black3.color = {0,0,0}
Encounter["EggDog"]["Neck"].layer = "Top"
Encounter["EggDog"]["Body"].layer = "Top"

Eggs = {}
Shards = {}
Dogs = {}

SetPPCollision(true)

function Update()
	timer = timer + 1
	if timer%50 == 0 then
		if math.random(2) == 1 then
			table.insert(Eggs, {Bullet = CreateProjectileAbs("Egg Dog/Bullets/Egg", 320 + math.random(-163, -80), 241)})
		else
			table.insert(Eggs, {Bullet = CreateProjectileAbs("Egg Dog/Bullets/Egg", 320 + math.random(80, 163), 241)})
		end
	end
	for i=1,#Eggs do
		local Datos = Eggs[i - tableaux]
		if not Datos.X then
			Datos.X = Datos.Bullet.absx
		end
		if not Datos.Y then
			Datos.Y = Datos.Bullet.absy
		end
		if not Datos.Vel then
			Datos.Vel = 0
		end
		Datos.Vel = Datos.Vel + 0.05
		Datos.Y = Datos.Y - Datos.Vel
		Datos.Bullet.MoveToAbs(Datos.X, Datos.Y)
		if Datos.Y <= 111 then
			for j=1,6 do
				local spruait = CreateProjectileAbs("Egg Dog/Bullets/Shard 1", Datos.X, Datos.Y)
				spruait.sprite.SetAnimation({"Egg Dog/Bullets/Shard 1","Egg Dog/Bullets/Shard 2","Egg Dog/Bullets/Shard 3","Egg Dog/Bullets/Shard 4"}, 1/6)
				spruait.sprite.layer = "Top"
				table.insert(Shards, {Sprite = spruait, X = math.random(-25, 25), Y = math.random(5, 40)})
			end
			local generar = {}
			if Player.absx >= Datos.X then
				generar[1] = CreateProjectileAbs("Egg Dog/Bullets/DogR 1", Datos.X, Datos.Y)
				generar[1].sprite.SetAnimation({"Egg Dog/Bullets/DogR 1","Egg Dog/Bullets/DogR 2"}, 0.3)
				generar[2] = "R"
			else
				generar[1] = CreateProjectileAbs("Egg Dog/Bullets/DogL 1", Datos.X, Datos.Y)
				generar[1].sprite.SetAnimation({"Egg Dog/Bullets/DogL 1","Egg Dog/Bullets/DogL 2"}, 0.3)
				generar[2] = "L"
			end
			table.insert(Dogs, {Bullet = generar[1], Direction = generar[2], Impulso = 10, Vel = 1})
			Datos.Bullet.Remove()
			Audio.PlaySound("st")
			table.remove(Eggs, i - tableaux)
			tableaux = tableaux + 1
		end
	end
	tableaux = 0
	for i=1,#Dogs do
		local Datos = Dogs[i - tableaux]
		if not Datos.X then
			Datos.X = Datos.Bullet.absx
		end
		if not Datos.Y then
			Datos.Y = Datos.Bullet.absy
		end
		Datos.Vel = Datos.Vel + 0.03
		if Datos.Direction == "R" then
			Datos.X = Datos.X + Datos.Vel
		else
			Datos.X = Datos.X - Datos.Vel
		end
		if ((Datos.X >= Player.absx - 80 and Datos.X <= Player.absx and Datos.Direction == "R") or (Datos.X >= Player.absx and Datos.X <= Player.absx + 80 and Datos.Direction == "L")) and Datos.Impulso <= 0 and Datos.Y <= 112 then
			Datos.Impulso = (Player.absy - 95)/2.6
		end
		Datos.Y = math.max(Datos.Y + Datos.Impulso/10, 112)
		Datos.Bullet.MoveToAbs(Datos.X, Datos.Y)
		Datos.Impulso = Datos.Impulso - 1
		if Datos.X >= 520 or Datos.X <= 120 then
			Datos.Bullet.Remove()
			table.remove(Dogs, i - tableaux)
			tableaux = tableaux + 1
		end
	end
	tableaux = 0
	for i=1,#Shards do
		local Datos = Shards[i - tableaux]
		Datos.Sprite.MoveToAbs(Datos.Sprite.absx + Datos.X/10, Datos.Sprite.absy + Datos.Y/10)
		Datos.Y = Datos.Y - 1
		if Datos.Sprite.absy <= -10 then
			Datos.Sprite.Remove()
			Datos = nil
			table.remove(Shards, i - tableaux)
			tableaux = tableaux + 1
		end
	end
	tableaux = 0
	if timer > 600 then
		Black1.Remove()
		Black2.Remove()
		Black3.Remove()
		Encounter["EggDog"]["Neck"].layer = "BelowArena"
		Encounter["EggDog"]["Body"].layer = "BelowArena"
		EndWave()
	end
end

function OnHit()
	if Player.hp > 10 then
		Player.Hurt(10)
	elseif Player.hp > 1 then
		Player.Hurt(1)
	elseif not Player.isHurting then
		Black1.Remove()
		Black2.Remove()
		Black3.Remove()
		Encounter["X"] = Player.absx
		Encounter["Y"] = Player.absy
		Encounter["nextwaves"] = {"Game Over"}
		State("ACTIONSELECT")
		State("DEFENDING")
	end
end