require "Dialogs"

Arena.ResizeImmediate(565, 130)
timer = 0
plattimer = 0
tableaux = 0
Piso = 0
CajaY = 90
FCajaY = 2.5
BFX = 87
BFY = 27
BAX = 240
BAY = 27
BIX = 400
BIY = 27
BMX = 555
BMY = 27
SX = 320
SY = 70.5

state = 1
maxjump = 40
jump = 0
newpX = Player.absx
newpY = Player.absy

SetPPCollision(true)

GBPaps = {}
Huesos = {}
HuesosSuelo = {}

function RecolorSoul()
	if state ~= 1 and state ~= 2 then
		Player.sprite.color = {1.0,0.0,0.0}
		Player.sprite.rotation = 0
	elseif state == 1 or state == 2 then
		Player.sprite.color = {0,60/255,1}
		Player.sprite.rotation = 0
	end
end

RecolorSoul()

function Update()
	DialogUpdate()
	timer = timer + 1
	if timer == 1 then
		Black = CreateSprite("Specials/1","BelowPlayer")
		Black.xscale = 565/575
		Black.MoveTo(Arena.x, Arena.y + 2.5)
		Black.color = {0, 0, 0}
		Caja = CreateSprite("Specials/1","BelowPlayer")
		Caja.MoveToAbs(Arena.x, Arena.y + 2.5)
		WarningCaja = CreateSprite("Specials/WarnBox","BelowPlayer")
		WarningCaja.MoveToAbs(Arena.x, Arena.y + 19.5)
		Audio.PlaySound("encounter")
	elseif timer == 41 then
		Caja.Set("Specials/2")
		Caja.MoveTo(Arena.x, Arena.y + 5.5)
		FCajaY = 5.5
		Piso = 6
	elseif timer == 43 then
		Caja.Set("Specials/3")
		Caja.MoveTo(Arena.x, Arena.y + 11.5)
		FCajaY = 11.5
		Piso = 18
		ExtraKR = 2.5
		ExtraDamage = 1
		table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", -20, Arena.y + Arena.height - 3), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Legs = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 20, Rotacion = {Current = 90}, Movement = {X = 8}, Limit = {Left = -20, Right = 660}, Color = {33/255, 197/255, 1}})
		table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 660, Arena.y + Arena.height - 3), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Legs = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 20, Rotacion = {Current = 90}, Movement = {X = -8}, Limit = {Left = -20, Right = 660}, Color = {33/255, 197/255, 1}})
	elseif timer == 45 then
		Caja.Set("Specials/4")
		Caja.MoveTo(Arena.x, Arena.y + 19.5)
		WarningCaja.Remove()
		WarningCaja = nil
		FCajaY = 19.5
		Piso = 34
	elseif timer == 125 then
		Warning1 = CreateSprite("Bullets/Bones/Body","BelowPlayer")
		Warning1.Scale(2/5, 26)
		Warning1.MoveToAbs(297, Arena.y + 52)
		Warning1.color = {0.5, 0, 0}
		Warning2 = CreateSprite("Bullets/Bones/Body","BelowPlayer")
		Warning2.Scale(2/5, 26)
		Warning2.MoveToAbs(343, Arena.y + 52)
		Warning2.color = {0.5, 0, 0}
		Warning3 = CreateSprite("Bullets/Bones/Body","BelowPlayer")
		Warning3.Scale(48/5, 2)
		Warning3.MoveToAbs(320, Arena.y + 64)
		Warning3.color = {0.5, 0, 0}
		Audio.PlaySound("encounter")
	elseif timer == 155 then
		Warning1.Remove()
		Warning2.Remove()
		Warning3.Remove()
		ExtraKR = nil
		ExtraDamage = nil
		table.insert(HuesosSuelo, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 302, Arena.y + 38), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Scale = {MaxHeight = 21, Vel = 4}, Duration = 40})
		table.insert(HuesosSuelo, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 314, Arena.y + 38), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Scale = {MaxHeight = 21, Vel = 4}, Duration = 40})
		table.insert(HuesosSuelo, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 326, Arena.y + 38), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Scale = {MaxHeight = 21, Vel = 4}, Duration = 40})
		table.insert(HuesosSuelo, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 338, Arena.y + 38), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Scale = {MaxHeight = 21, Vel = 4}, Duration = 40})
		Audio.PlaySound("pierce")
	elseif timer == 165 then
		table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = 678, Y = 520, rotation = 0}, DatosFinales = {X = 570, Y = Arena.y + 51, rotation = -90}, VelAppearing = 20}, Delay = 0, Duration = 30})
		Audio.PlaySound("gasterintro")
	elseif timer == 215 then
		ParteL = CreateSprite("Specials/ParteL","BelowPlayer")
		ParteR = CreateSprite("Specials/ParteR","BelowPlayer")
		ParteL.SetPivot(2.5/288, 2.5/39)
		ParteR.SetPivot(285.5/288, 2.5/39)
		PLX = Arena.x - 285
		PLY = Arena.y + 2.5
		PRX = Arena.x + 285
		PRY = Arena.y + 2.5
		Caja.Remove()
		Caja = nil
	elseif timer > 215 and timer <= 340 then
		if timer > 260 and timer <= 300 then
			SY = 70.5 - 90 + 90*math.cos(math.rad(90/30*math.min(timer - 260, 30)))
			if timer > 270 then
				CajaY = 90 - 90 + 90*math.cos(math.rad(90/30*(timer - 270)))
				Arena.MoveTo(320, CajaY, false, true)
			end
		elseif timer > 300 then
			if timer <= 320 then
				CajaY = 10*math.sin(math.rad(180/20*(timer - 300)))
				Arena.MoveTo(320, CajaY, false, true)
			end
			SX = -220 + 570*math.sin(math.rad(90/40*(timer - 300)))
			SY = 160.5
		end
		Piso = -20 - CajaY
		if timer <= 275 then
			ParteL.rotation = -150 + 150*math.cos(math.rad(90/60*(timer - 215)))
			ParteR.rotation = 150 - 150*math.cos(math.rad(90/60*(timer - 215)))
			if ParteR.rotation >= 16.5 and ParteR.rotation < 24.5 then
				Encounter.Call("RotateButton", {"Act", -19/8*(ParteR.rotation - 16.5)})
				Encounter.Call("RotateButton", {"Item", 19/8*(ParteR.rotation - 16.5)})
				BAX = 185 + 55*math.cos(math.rad(-19/8*(ParteR.rotation - 16.5))) + 21*math.sin(math.rad(-19/8*(ParteR.rotation - 16.5)))
				BAY = 48 - 21*math.cos(math.rad(-19/8*(ParteR.rotation - 16.5))) + 55*math.sin(math.rad(-19/8*(ParteR.rotation - 16.5)))
				BIX = 455 - 55*math.cos(math.rad(19/8*(ParteR.rotation - 16.5))) + 21*math.sin(math.rad(19/8*(ParteR.rotation - 16.5)))
				BIY = 48 - 21*math.cos(math.rad(19/8*(ParteR.rotation - 16.5))) - 55*math.sin(math.rad(19/8*(ParteR.rotation - 16.5)))
			elseif ParteR.rotation >= 24.5 then
				Encounter.Call("RotateButton", {"Act", -19 - (ParteR.rotation - 24.5)})
				Encounter.Call("RotateButton", {"Item", 19 + ParteR.rotation - 24.5})
				BAX = 35 + 156.5*math.cos(math.rad(ParteR.rotation - 8)) + 55*math.cos(math.rad(-19 - (ParteR.rotation - 24.5))) + 21*math.sin(math.rad(-19 - (ParteR.rotation - 24.5)))
				BAY = 92.5 - 156.5*math.sin(math.rad(ParteR.rotation - 8)) - 21*math.cos(math.rad(-19 - (ParteR.rotation - 24.5))) + 55*math.sin(math.rad(-19 - (ParteR.rotation - 24.5)))
				BIX = 605 - 156.5*math.cos(math.rad(ParteR.rotation - 8)) - 55*math.cos(math.rad(19 + ParteR.rotation - 24.5)) + 21*math.sin(math.rad(19 + ParteR.rotation - 24.5))
				BIY = 92.5 - 156.5*math.sin(math.rad(ParteR.rotation - 8)) - 21*math.cos(math.rad(19 + ParteR.rotation - 24.5)) - 55*math.sin(math.rad(19 + ParteR.rotation - 24.5))
			end
			if ParteR.rotation >= 30.5 and ParteR.rotation <= 100.5 then
				Encounter.Call("RotateButton", {"Fight", -92/70*(ParteR.rotation - 30.5)})
				Encounter.Call("RotateButton", {"Mercy", 92/70*(ParteR.rotation - 30.5)})
				BFX = 32 + 55*math.cos(math.rad(-92/70*(ParteR.rotation - 30.5))) + 21*math.sin(math.rad(-92/70*(ParteR.rotation - 30.5)))
				BFY = 48 - 21*math.cos(math.rad(-92/70*(ParteR.rotation - 30.5))) + 55*math.sin(math.rad(-92/70*(ParteR.rotation - 30.5)))
				BMX = 610 - 55*math.cos(math.rad(92/70*(ParteR.rotation - 30.5))) + 21*math.sin(math.rad(92/70*(ParteR.rotation - 30.5)))
				BMY = 48 - 21*math.cos(math.rad(92/70*(ParteR.rotation - 30.5))) - 55*math.sin(math.rad(92/70*(ParteR.rotation - 30.5)))
			elseif ParteR.rotation > 100.5 then
				if timer <= 260 then
					Encounter.Call("RotateButton", {"Fight", -ParteR.rotation + 8.5})
					Encounter.Call("RotateButton", {"Mercy", ParteR.rotation - 8.5})
					BFX = 35 - 44.6*math.cos(math.rad(ParteR.rotation - 3.9)) + 55*math.cos(math.rad(8.5 - ParteR.rotation)) + 21*math.sin(math.rad(8.5 - ParteR.rotation))
					BFY = 92.5 - 44.6*math.sin(math.rad(ParteR.rotation - 3.9)) - 21*math.cos(8.5 - ParteR.rotation) + 55*math.sin(math.rad(8.5 - ParteR.rotation))
					BMX = 605 + 44.6*math.cos(math.rad(ParteR.rotation - 3.9)) - 55*math.cos(math.rad(ParteR.rotation - 8.5)) + 21*math.sin(math.rad(ParteR.rotation - 8.5))
					BMY = 92.5 - 44.6*math.sin(math.rad(ParteR.rotation - 3.9)) - 21*math.cos(math.rad(ParteR.rotation - 8.5)) - 55*math.sin(math.rad(ParteR.rotation - 8.5))
				else
					Encounter.Call("RotateButton", {"Fight", -ParteR.rotation + 8.5})
					Encounter.Call("RotateButton", {"Mercy", ParteR.rotation - 8.5})
					BFX = 35 - 200 + 200*math.cos(math.rad(90/15*(timer - 260))) - 44.6*math.cos(math.rad(ParteR.rotation - 3.9)) + 55*math.cos(math.rad(8.5 - ParteR.rotation)) + 21*math.sin(math.rad(8.5 - ParteR.rotation))
					BFY = Arena.y + 2.5 - 50 + 50*math.cos(math.rad(90/15*(timer - 260))) - 44.6*math.sin(math.rad(ParteR.rotation - 3.9)) - 21*math.cos(math.rad(8.5 - ParteR.rotation)) + 55*math.sin(math.rad(8.5 - ParteR.rotation))
					BMX = 605 + 200 - 200*math.cos(math.rad(90/15*(timer - 260))) + 44.6*math.cos(math.rad(ParteR.rotation - 3.9)) - 55*math.cos(math.rad(ParteR.rotation - 8.5)) + 21*math.sin(math.rad(ParteR.rotation - 8.5))
					BMY = Arena.y + 2.5 - 50 + 50*math.cos(math.rad(90/15*(timer - 260))) - 44.6*math.sin(math.rad(ParteR.rotation - 3.9)) - 21*math.cos(math.rad(ParteR.rotation - 8.5)) - 55*math.sin(math.rad(ParteR.rotation - 8.5))
				end
			end
			if timer > 260 then
				PLX = Arena.x - 285 - 50 + 50*math.cos(math.rad(90/15*(timer - 260)))
				PLY = Arena.y + 2.5 - 50 + 50*math.cos(math.rad(90/15*(timer - 260)))
				PRX = Arena.x + 285 + 50 - 50*math.cos(math.rad(90/15*(timer - 260)))
				PRY = Arena.y + 2.5 - 50 + 50*math.cos(math.rad(90/15*(timer - 260)))
			end
		end
	elseif timer > 340 and ParteL and ParteR then
		ParteL.Remove()
		ParteR.Remove()
		ParteL = nil
		ParteR = nil
	end
	if plattimer == 0 then
		if PlatY and PlatY < 30 then
			VelY = 1.5
		else
			VelY = nil
		end
		if Bone then
			if Bone.Position then
				if not Bone.Timer then
					Bone.Timer = 0
				end
				Bone.Timer = Bone.Timer + 1
				Bone.Position.X = 540 - 40*math.sin(math.rad(2*Bone.Timer))
				Bone.Position.Y = -20 + 110*math.sin(math.rad(2*Bone.Timer))
				Bone.Rotacion.Current = (360/45*Bone.Timer)%360
				if Bone.Timer >= 45 then
					plattimer = 1
					Bone.Timer = nil
				end
			end
		end
	elseif plattimer > 0 then
		if not Stop then
			plattimer = plattimer + 1
		end
		if plattimer >= 2 and plattimer < 22 then
			if newpX < Bone.Position.X then
				localrot = 90 + math.atan((newpY - Bone.Position.Y)/(newpX - Bone.Position.X))*(180/math.pi)
			else
				localrot = -90 + math.atan((newpY - Bone.Position.Y)/(newpX - Bone.Position.X))*(180/math.pi)
			end
			D = math.sqrt((newpX - Bone.Position.X)*(newpX - Bone.Position.X) + (newpY - Bone.Position.Y)*(newpY - Bone.Position.Y))
			Bone.Rotacion.Current = (localrot*math.sin(math.rad(90/20*(plattimer - 1))))%360
		elseif plattimer >= 22 and plattimer < 42 then
			Bone.Position = {X = 500 - 30*math.sin(math.rad(90/20*(plattimer - 21)))*math.cos(math.rad(Bone.Rotacion.Current + 90)), Y = 90 - 30*math.sin(math.rad(90/20*(plattimer - 21)))*math.sin(math.rad(Bone.Rotacion.Current + 90))}
		elseif plattimer >= 42 and plattimer < 67 then
			Bone.Position = {X = 500 - 30*math.cos(math.rad(Bone.Rotacion.Current + 90)) + ((1.8*D) - (1.8*D)*math.cos(math.rad(90/25*(plattimer - 41))))*math.cos(math.rad(Bone.Rotacion.Current + 90)), Y = math.max(90 - 30*math.sin(math.rad(Bone.Rotacion.Current + 90)) + ((1.8*D) - (1.8*D)*math.cos(math.rad(90/25*(plattimer - 41))))*math.sin(math.rad(Bone.Rotacion.Current + 90)), -20)}
			localDiffX = 180 - Bone.Position.X
			localDiffY = 90 - Bone.Position.Y
			localrot = Bone.Rotacion.Current
		elseif plattimer >= 67 and plattimer < 112 then
			Bone.Position = {X = 180 - localDiffX + localDiffX*math.sin(math.rad(90/45*(plattimer - 66))), Y = 90 - localDiffY + localDiffY*math.sin(math.rad(90/45*(plattimer - 66)))}
			Bone.Rotacion.Current = (localrot - (localrot + 360)/45*(plattimer - 66))%360
		elseif plattimer >= 112 and plattimer < 132 then
			if newpX < Bone.Position.X then
				localrot = 90 + math.atan((newpY - Bone.Position.Y)/(newpX - Bone.Position.X))*(180/math.pi)
			else
				localrot = -90 + math.atan((newpY - Bone.Position.Y)/(newpX - Bone.Position.X))*(180/math.pi)
			end
			Bone.Rotacion.Current = (localrot*math.sin(math.rad(90/20*(plattimer - 111))))%360
		elseif plattimer >= 132 and plattimer < 182 and Bone then
			Bone.Position = {X = 180 + (640 - 640*math.cos(math.rad(90/50*(plattimer - 131))))*math.cos(math.rad(Bone.Rotacion.Current + 90)), Y = 90 + (640 - 640*math.cos(math.rad(90/50*(plattimer - 131))))*math.sin(math.rad(Bone.Rotacion.Current + 90))}
		elseif plattimer == 212 then
			VelX = 0.5
			ExtraDamage = nil
			ExtraKR = nil
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 - Arena.width/2, 44/2), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 44, Movement = {X = 3.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 - Arena.width/2, 135 - 52/2), Legs = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 52, Movement = {X = 3.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2, 44/2), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 44, Movement = {X = -2.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2, 135 - 52/2), Legs = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 52, Movement = {X = -2.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
		elseif plattimer == 228 then
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 - Arena.width/2, 44/2), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 44, Movement = {X = 3.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 - Arena.width/2, 135 - 52/2), Legs = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 52, Movement = {X = 3.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
		elseif plattimer == 234 then
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2, 44/2), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 44, Movement = {X = -2.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2, 135 - 52/2), Legs = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 52, Movement = {X = -2.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
		elseif plattimer == 244 then
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 - Arena.width/2, 44/2), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 44, Movement = {X = 3.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 - Arena.width/2, 135 - 52/2), Legs = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 52, Movement = {X = 3.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
		elseif plattimer == 257 then
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2, 44/2), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 44, Movement = {X = -2.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2, 135 - 52/2), Legs = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 52, Movement = {X = -2.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
		elseif plattimer == 260 then
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 - Arena.width/2, 44/2), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 44, Movement = {X = 3.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 - Arena.width/2, 135 - 52/2), Legs = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 52, Movement = {X = 3.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
		elseif plattimer == 279 then
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2, 44/2), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 44, Movement = {X = -2.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2, 135 - 52/2), Legs = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 52, Movement = {X = -2.5}, Limit = {Right = 320 + Arena.width/2, Left = 320 - Arena.width/2}})
			for i=1,#Huesos do
				if Huesos[i].Movement.X > 0 then
					Huesos[i].Movement.X = 2
				else
					Huesos[i].Movement.X = -1
				end
			end
		elseif plattimer == 390 then
			VelX = -1
			for i=1,#Huesos do
				if Huesos[i].Movement.X > 0 then
					Huesos[i].Movement.X = 0.5
				else
					Huesos[i].Movement.X = -2.5
				end
			end
		elseif plattimer == 480 then
			for i=1,#Huesos do
				if Huesos[i].Movement.X > 0 then
					Huesos[i].Movement.X = 2
				else
					Huesos[i].Movement.X = -4
				end
			end
			Stop = true
			plattimer = 481
		elseif plattimer == 481 then
			if PlatX < 280 then
				VelX = 1
			elseif PlatX > 320 then
				VelX = -1
			end
			if NewAudio.GetPlayTime("Loop") >= 5.94 and not done and Player.absx >= PlatX - Plataforma.sprite.width/2 and Player.absx <= PlatX + Plataforma.sprite.width/2 then
				VelX = 0
				VelY = 2
				done = 1
				state = 4
			elseif done and done == 1 then
				PlatY = math.min(PlatY, 50)
			end
			if NewAudio.GetPlayTime("Loop") >= 6.15 and done and done == 1 then
				VelY = -4
				done = 2
			elseif done and done == 2 then
				PlatY = math.max(PlatY, 10)
			end
			if NewAudio.GetPlayTime("Loop") >= 6.36 and done and done == 2 then
				VelY = 2
				done = 3
			elseif done and done == 3 then
				PlatY = math.min(PlatY, 30)
			end
			if NewAudio.GetPlayTime("Loop") >= 6.63 and done and done == 3 then
				VelY = 0
				VelX = -1
				plattimer = 482
				Encounter.Call("ContinueMusic")
			end
		elseif NewAudio.GetPlayTime("One") >= 1 and plattimer > 481 and plattimer <= 491 then
			VelX = (266 - PlatX)/10
			VelY = (50 - PlatY)/10
			plattimer = plattimer + 1
		elseif plattimer == 492 then
			newpY = 58
			newpX = newpX + (266 - PlatX)
			PlatX = 266
			PlatY = 50
			VelX = 0
			VelY = 0
			plattimer = 493
		elseif plattimer == 493 then
			BtA = CreateSprite("Fake UI, KR and HP Library Sprites/"..GetAlMightyGlobal("Idioma").."/ACT 0","BelowBullet")
			BtA.MoveTo(180, -30)
			plattimer = 494
		elseif NewAudio.GetPlayTime("One") >= 1.4 and plattimer > 493 and plattimer <= 513 then
			BtA.MoveTo(180, -30 + (plattimer - 493))
			Plataforma.sprite.rotation = 232.5 - 232.5*math.cos(math.rad(180/20*(plattimer - 493)))
			if OnPlatform then
				MoverPlayerX = -13
				Plataforma.SetVar("No Damage", true)
				Plataforma.SetVar("Plataforma", nil)
			end
			if plattimer == 503 then
				Plataforma.SetVar("No Damage", true)
				Plataforma.SetVar("Plataforma", nil)
			end
			plattimer = plattimer + 1
		elseif NewAudio.GetPlayTime("One") >= 2 and plattimer > 513 and plattimer <= 530 then
			BtA.MoveTo(180, -10 + (plattimer - 513)*3)
			plattimer = plattimer + 1
		elseif NewAudio.GetPlayTime("One") >= 2.5 and plattimer == 531 then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Dialog("[noskip][waittall:0.5]Le[MoveX:18]dices[MoveX:18]a[MoveX:18]Papyrus\rque fue muy fácil\rde manipular.[w:100] ", {X = 334, Y = 120}, nil, true, 1)
			elseif GetAlMightyGlobal("Idioma") == "Russian" then
				Dialog("[noskip][waittall:0.5]Вы говорите Папи-\rрусу, что им легко\rманипулировать.[w:100] ", {X = 328, Y = 120}, nil, true, 1)
			else
				Dialog("[noskip][waittall:0.5]You tell Papyrus\rhe was easy to\rmanipulate.[w:100] ", {X = 334, Y = 120}, nil, true, 1)
			end
			plattimer = 532
		elseif NewAudio.GetPlayTime("One") >= 4.9 and plattimer == 532 then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Dialog("[noskip][waittall:0.5]Le dices que se\rencontrará[MoveX:16]con[MoveX:16]sus\ramigos pronto.[w:100] ", {X = 334, Y = 120}, nil, true, 1)
			elseif GetAlMightyGlobal("Idioma") == "Russian" then
				Dialog("[noskip][waittall:0.5]Вы говорите, что\rон скоро встретит\rсвоих друзей.[w:100] ", {X = 328, Y = 120}, nil, true, 1)
			else
				Dialog("[noskip][waittall:0.5]You tell him\rhe'll meet his\rfriends soon.[w:100] ", {X = 334, Y = 120}, nil, true, 1)
			end
			BtF = CreateSprite("Fake UI, KR and HP Library Sprites/"..GetAlMightyGlobal("Idioma").."/FIGHT 0","BelowPlayer")
			BtF.MoveTo(-60, -30)
			BtI = CreateSprite("Fake UI, KR and HP Library Sprites/"..GetAlMightyGlobal("Idioma").."/ITEM 0","BelowPlayer")
			BtI.MoveTo(480, -30)
			BtM = CreateSprite("Fake UI, KR and HP Library Sprites/"..GetAlMightyGlobal("Idioma").."/MERCY 0","BelowPlayer")
			BtM.MoveTo(700, -30)
			plattimer = 533
		elseif NewAudio.GetPlayTime("One") >= 5.9 and plattimer > 532 and plattimer <= 580 then
			BtF.rotation = -180 + 180*math.cos(math.rad(180/48*(plattimer - 532)))
			BtI.rotation = 180 - 180*math.cos(math.rad(180/48*(plattimer - 532)))
			BtM.rotation = 180 - 180*math.cos(math.rad(180/48*(plattimer - 532)))
			PapsX = 315 + 5*math.cos(math.rad(180/48*(plattimer - 532)))
			PapsY = 230 + 10*math.cos(math.rad(180/48*(plattimer - 532)))
			BtF.MoveTo(10 - 70*math.cos(math.rad(180/48*(plattimer - 532))), 135 - 165*math.cos(math.rad(180/48*(plattimer - 532))))
			BtI.MoveTo(520 - 40*math.cos(math.rad(180/48*(plattimer - 532))), 135 - 165*math.cos(math.rad(180/48*(plattimer - 532))))
			BtM.MoveTo(630 + 70*math.cos(math.rad(180/48*(plattimer - 532))), 105 - 135*math.cos(math.rad(180/48*(plattimer - 532))))
			if plattimer <= 556 then
				Plataforma.sprite.rotation = 105*math.cos(math.rad(90/24*(plattimer - 532)))
				PlatX = 266 + (Player.absx - 266) - (Player.absx - 266)*math.cos(math.rad(90/24*(plattimer - 532)))
				PlatY = 58 - 8*math.cos(math.rad(90/24*(plattimer - 532)))
				if plattimer == 556 then
					Plataforma.SetVar("Plataforma", true)
					Plataforma.SetVar("No Damage", nil)
					BtA.Set("Fake UI, KR and HP Library Sprites/"..GetAlMightyGlobal("Idioma").."/ACT 0")
					state = 3
				end
			else
				BtA.rotation = 180 - 180*math.cos(math.rad(180/24*(plattimer - 556)))
				BtA.MoveTo(130 + 50*math.cos(math.rad(180/24*(plattimer - 556))), 41 + 199/2 - 199/2*math.cos(math.rad(180/24*(plattimer - 556))))
			end
			plattimer = plattimer + 1
		elseif NewAudio.GetPlayTime("One") >= 7.5 and plattimer == 581 then
			Dialog("", "", "", "", 1)
			plattimer = 582
		elseif NewAudio.GetPlayTime("One") >= 8.3 and plattimer == 582 then
			table.insert(Huesos, {Misil = CreateProjectileAbs("Bullets/Bones/Right Bone", 840, 64), Height = 1, Movement = {X = -20}, Rotacion = {Current = -135, Vel = 0}, Limit = {Right = 840, Left = -200}})
			plattimer = 583
			ExtraDamage = 3
			ExtraKR = 2.5
		elseif NewAudio.GetPlayTime("One") >= 9.5 and plattimer == 583 then
			ExtraDamage = nil
			ExtraKR = nil
			table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = 690, Y = 530, rotation = 0}, DatosFinales = {X = 540, Y = 64, rotation = -90}, VelAppearing = 27}, Delay = 3, Duration = 30})
			Audio.PlaySound("gasterintro")
			Stop = nil
		elseif NewAudio.GetPlayTime("One") >= 11.16 and plattimer == 644 then
			table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = -50, Y = 530, rotation = 179}, DatosFinales = {X = PlatX, Y = 240, rotation = 0}, VelAppearing = 27}, Scale = 0.5, Delay = 3, Duration = 30})
			Audio.PlaySound("gasterintro")
			Stop = nil
		elseif NewAudio.isStopped("One") and plattimer >= 482 then
			if plattimer >= 644 then
				Plataforma.Remove()
				Plataforma = nil
				if Black then
					Black.Remove()
				end
				Black = nil
				BtF.Remove()
				BtA.Remove()
				BtI.Remove()
				BtM.Remove()
				BtF = nil
				BtA = nil
				BtI = nil
				BtM = nil
				for i=1,#GBPaps do
					if GBPaps[i - tableaux].Laser then
						GBPaps[i - tableaux].Laser.Misil.Remove()
					end
					GBPaps[i - tableaux].Sprite.Remove()
					table.remove(GBPaps, i - tableaux)
					tableaux = tableaux + 1
				end
				tableaux = 0
				for i=1,#HuesosSuelo do
					HuesosSuelo[i - tableaux].Misil.Remove()
					if HuesosSuelo[i - tableaux].Head then
						HuesosSuelo[i - tableaux].Head.Remove()
					end
					if HuesosSuelo[i - tableaux].Legs then
						HuesosSuelo[i - tableaux].Legs.Remove()
					end
					table.remove(HuesosSuelo, i - tableaux)
					tableaux = tableaux + 1
				end
				tableaux = 0
				for i=1,#Huesos do
					Huesos[i - tableaux].Misil.Remove()
					if Huesos[i - tableaux].Head then
						Huesos[i - tableaux].Head.Remove()
					end
					if Huesos[i - tableaux].Legs then
						Huesos[i - tableaux].Legs.Remove()
					end
					table.remove(Huesos, i - tableaux)
					tableaux = tableaux + 1
				end
				tableaux = 0
				Encounter.SetVar("nextwaves", {"Phase 2 Menu"})
				State("ACTIONSELECT")
				State("DEFENDING")
			end
		end
	end
	if BtA and Player.absx < BtA.x + BtA.width/2 + 4 and Player.absx > BtA.x - BtA.width/2 - 4 and Player.absy >= BtA.y + BtA.height/2 + 4 and Player.absy <= BtA.y + BtA.height/2 + 12 and jump <= 0 and plattimer < 556 then
		newpY = BtA.y + BtA.height/2 + 8
		jump = 0
		OnPlatform = true
		impulsado = nil
		if not sonidito then
			sonidito = true
			BtA.Set("Fake UI, KR and HP Library Sprites/"..GetAlMightyGlobal("Idioma").."/ACT 1")
			Audio.PlaySound("confirm")
		end
	end
	if Plataforma and Plataforma.isactive then
		PlatX = PlatX + (VelX or 0)
		PlatY = PlatY + (VelY or 0)
		Plataforma.MoveToAbs(PlatX, PlatY)
	end
	Encounter.Call("ButtonMoveTo", {"Fight", BFX, BFY})
	Encounter.Call("ButtonMoveTo", {"Act", BAX, BAY})
	Encounter.Call("ButtonMoveTo", {"Item", BIX, BIY})
	Encounter.Call("ButtonMoveTo", {"Mercy", BMX, BMY})
	Encounter.Call("StatusMoveTo", {SX, SY})
	if ParteL and ParteR then
		ParteL.MoveTo(PLX, PLY)
		ParteR.MoveTo(PRX, PRY)
	end
	Arena.MoveTo(320, CajaY, false, true)
	Encounter.Call("PapsMoveTo", {(PapsX or 320), (PapsY or 240)})
	if BtF and BtA and BtI and BtM and plattimer > 580 then
		BtF.MoveTo(80, 300)
		BtA.MoveTo(80, 240)
		BtI.MoveTo(560, 300)
		BtM.MoveTo(560, 240)
	end
	if plattimer == 643 then
		Stop = true
		plattimer = plattimer + 1
	end
	if Caja then
		Caja.MoveTo(Arena.x, Arena.y + FCajaY)
	end
	if Black then
		Black.MoveTo(Arena.x, Arena.y + 2.5)
	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.Done = true
			end
		else
			if gb.Rotation and not gb.Done then
				gb.Sprite.rotation = gb.Rotation
			end
			gb.Delay = gb.Delay - 1
		end
		if gb.Vel then
			gb.X = gb.X - gb.Vel*math.sin(math.rad(gb.Sprite.rotation))
			gb.Y = gb.Y + gb.Vel*math.cos(math.rad(gb.Sprite.rotation))
			gb.Vel = gb.Vel*1.1
			if gb.X < -100 or gb.X > 740 or gb.Y < -100 or gb.Y > 580 then
				gb.Vel = 0
			end
		end
		gb.Sprite.MoveTo(gb.X, gb.Y)
		if gb.Delay < 0 then
			if gb.Delay == -1 then
				gb.Sprite.Set("Bullets/GB Papyrus/1")
				gb.Vel = 2
				Audio.PlaySound("gasterfire")
			elseif gb.Delay <= -2 then
				if gb.Delay == -3 then
					Misc.ShakeScreen(gb.Duration, 8, true)
				end
				if gb.Duration > 0 then
					gb.Duration = gb.Duration - 1
					if not gb.Laser then
						gb.Laser = {Misil = CreateProjectileAbs("Bullets/GB Laser", gb.X + (640 + 26*gb.Sprite.yscale)*math.sin(math.rad(gb.Sprite.rotation)), gb.Y - (640 + 26*gb.Sprite.yscale)*math.cos(math.rad(gb.Sprite.rotation)))}
						gb.Laser.Misil.sprite.xscale = 0
						gb.Laser.Misil.sprite.rotation = gb.Sprite.rotation
						gb.Laser.Scale = 0
					else
						gb.Laser.Misil.MoveToAbs(gb.X + (640 + 26*gb.Sprite.yscale)*math.sin(math.rad(gb.Sprite.rotation)), gb.Y - (640 + 26*gb.Sprite.yscale)*math.cos(math.rad(gb.Sprite.rotation)))
						if gb.Laser.Misil.sprite.xscale < 0.9*gb.Sprite.xscale and gb.Laser.Scale == 0 then
							gb.Laser.Misil.sprite.xscale = gb.Laser.Misil.sprite.xscale + 0.1*gb.Sprite.xscale
						else
							gb.Laser.Scale = gb.Laser.Scale + 50
							gb.Laser.Misil.sprite.xscale = 0.9*gb.Sprite.xscale + 0.1*gb.Sprite.xscale*math.sin(math.rad(gb.Laser.Scale))
						end
					end
				else
					gb.Laser.Misil.SetVar("No Damage", true)
					if gb.Laser.Misil.sprite.xscale > 0 then
						gb.Laser.Misil.MoveToAbs(gb.X + (640 + 26*gb.Sprite.yscale)*math.sin(math.rad(gb.Sprite.rotation)), gb.Y - (640 + 26*gb.Sprite.yscale)*math.cos(math.rad(gb.Sprite.rotation)))
						gb.Laser.Misil.sprite.xscale = gb.Laser.Misil.sprite.xscale - 0.05*gb.Sprite.xscale
						gb.Laser.Misil.sprite.alpha = gb.Laser.Misil.sprite.xscale
					else
						gb.Laser.Misil.Remove()
						gb.Sprite.Remove()
						gb = nil
						table.remove(GBPaps, i - tableaux)
						tableaux = tableaux + 1
					end
				end
			end
		end
	end
	tableaux = 0
	for i=1,#HuesosSuelo do
		local bullet = HuesosSuelo[i - tableaux]
		if not bullet.X then
			bullet.X = bullet.Misil.absx
		end
		if not bullet.Y then
			bullet.Y = bullet.Misil.absy
		end
		if not bullet.Scale.Direction then
			bullet.Scale.Direction = "Up"
		end
		if not bullet.Scale.Current then
			bullet.Scale.Current = 0
		end
		if not bullet.Rotacion then
			bullet.Rotacion = 0
		end
		if bullet.Scale.Current < bullet.Scale.MaxHeight and bullet.Scale.Direction == "Up" then
			bullet.Scale.Current = math.min(bullet.Scale.Current + bullet.Scale.Vel, bullet.Scale.MaxHeight)
			if bullet.Scale.Current >= bullet.Scale.MaxHeight then
				bullet.Scale.Direction = "Waiting"
			end
		elseif bullet.Scale.Direction == "Waiting" then
			if not bullet.Timer then
				bullet.Timer = 0
			end
			bullet.Timer = bullet.Timer + 1
			if bullet.Timer >= (bullet.Duration or 30) then
				bullet.Scale.Direction = "Down"
			end
		elseif bullet.Scale.Current > 0 and bullet.Scale.Direction == "Down" then
			bullet.Scale.Current = math.max(bullet.Scale.Current - bullet.Scale.Vel, 0)
		end
		bullet.Misil.sprite.yscale = bullet.Scale.Current
		bullet.Misil.sprite.rotation = bullet.Rotacion
		bullet.Misil.MoveToAbs(bullet.X - bullet.Scale.Current/2*math.sin(math.rad(bullet.Rotacion)), bullet.Y + bullet.Scale.Current/2*math.cos(math.rad(bullet.Rotacion)))
		if bullet.Head then
			bullet.Head.MoveToAbs(bullet.X - (bullet.Scale.Current + 2.5)*math.sin(math.rad(bullet.Rotacion)), bullet.Y + (bullet.Scale.Current + 2.5)*math.cos(math.rad(bullet.Rotacion)))
			bullet.Head.sprite.rotation = bullet.Rotacion
		end
		if bullet.Legs then
			bullet.Legs.MoveToAbs(bullet.X + (bullet.Scale.Current + 2.5)*math.sin(math.rad(bullet.Rotacion)), bullet.Y - (bullet.Scale.Current + 2.5)*math.cos(math.rad(bullet.Rotacion)))
			bullet.Legs.sprite.rotation = bullet.Rotacion + 180
		end
		if bullet.Scale.Current <= 0 and bullet.Scale.Direction == "Down" then
			bullet.Misil.Remove()
			if bullet.Head then
				bullet.Head.Remove()
			end
			if bullet.Legs then
				bullet.Legs.Remove()
			end
			bullet = nil
			table.remove(HuesosSuelo, i - tableaux)
			tableaux = tableaux + 1
		end
	end
	tableaux = 0
	for i=1,#Huesos do
		local bullet = Huesos[i - tableaux]
		if not bullet.Position then
			bullet.Position = {}
			bullet.Position.X = bullet.Misil.absx
			bullet.Position.Y = bullet.Misil.absy
		end
		if not bullet.Movement then
			bullet.Movement = {}
		end
		if not bullet.Rotacion then
			bullet.Rotacion = {}
		end
		if not bullet.Rotacion.Current then
			bullet.Rotacion.Current = 0
		end
		if not bullet.Color then
			bullet.Color = {1, 1, 1}
		end
		if not bullet.Limit then
			bullet.Limit = {}
		end
		bullet.Position = {X = bullet.Position.X + (bullet.Movement.X or 0), Y = bullet.Position.Y + (bullet.Movement.Y or 0)}
		bullet.Rotacion.Current = (bullet.Rotacion.Current + (bullet.Rotacion.Vel or 0))%360
		bullet.Misil.sprite.rotation = bullet.Rotacion.Current
		bullet.Misil.sprite.yscale = bullet.Height
		bullet.Misil.sprite.color = bullet.Color
		bullet.Misil.MoveToAbs(bullet.Position.X, bullet.Position.Y)
		if bullet.Head then
			bullet.Head.MoveToAbs(bullet.Position.X - (bullet.Height/2 + 2.5)*math.sin(math.rad(bullet.Rotacion.Current)), bullet.Position.Y + (bullet.Height/2 + 2.5)*math.cos(math.rad(bullet.Rotacion.Current)))
			bullet.Head.sprite.rotation = bullet.Rotacion.Current
			bullet.Head.sprite.color = bullet.Color
		end
		if bullet.Legs then
			bullet.Legs.MoveToAbs(bullet.Position.X + (bullet.Height/2 + 2.5)*math.sin(math.rad(bullet.Rotacion.Current)), bullet.Position.Y - (bullet.Height/2 + 2.5)*math.cos(math.rad(bullet.Rotacion.Current)))
			bullet.Legs.sprite.rotation = bullet.Rotacion.Current + 180
			bullet.Legs.sprite.color = bullet.Color
		end
		if bullet.Position.X > (bullet.Limit.Right or 640) or bullet.Position.X < (bullet.Limit.Left or 0) or bullet.Position.Y > (bullet.Limit.Up or 480) or bullet.Position.Y < (bullet.Limit.Down or 0) then
			bullet.Misil.Remove()
			if bullet.Head then
				bullet.Head.Remove()
			end
			if bullet.Legs then
				bullet.Legs.Remove()
			end
			bullet = nil
			table.remove(Huesos, i - tableaux)
			tableaux = tableaux + 1
		end
	end
	tableaux = 0
	UpdateSoulStates()
	if (newpY < 20 or timer > 300) and not Plataforma and not spawned then
		state = 3
		maxjump = 40
		PlatX = 320
		PlatY = -5
		Plataforma = CreateProjectileAbs("Bullets/Platforms/80", PlatX, PlatY, "BelowPlayer")
		Plataforma.SetVar("Plataforma", true)
		Plataforma.ppcollision = false
		Bone = {Misil = CreateProjectileAbs("Bullets/Bones/Body", 520, -20), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Legs = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Height = 20, Rotacion = {Current = 0, Vel = 0}, Limit = {Down = -20}}
		ExtraDamage = 3
		ExtraKR = 7.5
		spawned = true
		table.insert(Huesos, Bone)
	end
	if Plataforma and newpY <= PlatY + 12 and newpX > PlatX - Plataforma.sprite.width/2 - 2 and newpX < PlatX + Plataforma.sprite.width/2 + 2 and not First then
		OnHit(Plataforma)
	end
end

function UpdateSoulStates()
	if state == 1 then -- Down
		if newpY < CajaY + 15 + Piso then
			if timer > 48 then
				state = 2
				maxjump = 32
			end
			if Input.Up > 0 then
				jump = maxjump
			else
				newpY = CajaY + 13 + Piso
				jump = 0
				impulsado = false
			end
			if timer >= 45 and timer <= 48 then
				jump = 40
				impulsado = true
			end
		elseif newpY > CajaY + Arena.height - 5 and jump > 3 then
			jump = 2
		elseif jump < 3 and jump > -3 then
			jump = jump - 0.5
		elseif jump < -3 and jump > -10 then
			jump = -10
		elseif jump < -3 then
			jump = jump - 1.5
		else
			jump = (jump/1.001) - 1
		end
		if Input.Up < 0 and jump > 3 and not impulsado then
			jump = 2
		end
		newpY = math.max(math.min(newpY + (jump/10), CajaY + Arena.height - 3), CajaY + 13 + Piso)
		if newpX < 328 - Arena.width/2 then
			newpX = 328 - Arena.width/2
		elseif newpX > 312 + Arena.width/2 then
			newpX = 312 + Arena.width/2
		end
		Player.MoveToAbs(newpX, newpY, true)
	elseif state == 2 then -- Down
		if newpY < CajaY + 15 + Piso then
			if Input.Up > 0 then
				jump = maxjump
			else
				newpY = CajaY + 13 + Piso
				jump = 0
			end
		elseif jump < 3 and jump > -3 then
			jump = jump - 0.33
		elseif jump < -3 and jump > -10 then
			jump = -10
		elseif jump < -3 then
			jump = jump - 1
		else
			jump = (jump/1.001) - 0.66
		end
		if Input.Up < 0 and jump > 2 then
			jump = 1.33
		end
		newpY = math.max(newpY + (jump/10), CajaY + 13 + Piso)
		if newpX < 328 - Arena.width/2 then
			newpX = 328 - Arena.width/2
		elseif newpX > 312 + Arena.width/2 then
			newpX = 312 + Arena.width/2
		end
		Player.MoveToAbs(newpX, newpY, true)
	elseif state == 3 then -- Down
		if Input.Left > 0 then
			newpX = newpX - 2
		end
		if Input.Right > 0 then
			newpX = newpX + 2
		end
		if MoverPlayerX then
			newpX = newpX + MoverPlayerX
			MoverPlayerX = MoverPlayerX + 1
			if MoverPlayerX == 0 then
				MoverPlayerX = nil
			end
		end
		if OnPlatform then
			newpX = newpX + (VelX or 0)
			newpY = newpY + (VelY or 0)
			if Input.Up > 0 then
				jump = maxjump
				First = true
			end
			OnPlatform = nil
		elseif newpY < CajaY + 15 + Piso then
			newpY = CajaY + 13 + Piso
			jump = 40
			impulsado = true
			if Player.hp > 10 then
				Player.Hurt(10, 0)
			else
				Plataforma.Remove()
				Plataforma = nil
				Black.Remove()
				Black = nil
				if BtF then
					BtF.Remove()
					BtF = nil
				end
				if BtA then
					BtA.Remove()
					BtA = nil
				end
				if BtI then
					BtI.Remove()
					BtI = nil
				end
				if BtM then
					BtM.Remove()
					BtM = nil
				end
				for i=1,#GBPaps do
					GBPaps[i - tableaux].Laser.Misil.Remove()
					GBPaps[i - tableaux].Sprite.Remove()
					table.remove(GBPaps, i - tableaux)
					tableaux = tableaux + 1
				end
				tableaux = 0
				for i=1,#HuesosSuelo do
					HuesosSuelo[i - tableaux].Misil.Remove()
					if HuesosSuelo[i - tableaux].Head then
						HuesosSuelo[i - tableaux].Head.Remove()
					end
					if HuesosSuelo[i - tableaux].Legs then
						HuesosSuelo[i - tableaux].Legs.Remove()
					end
					table.remove(HuesosSuelo, i - tableaux)
					tableaux = tableaux + 1
				end
				tableaux = 0
				for i=1,#Huesos do
					Huesos[i - tableaux].Misil.Remove()
					if Huesos[i - tableaux].Head then
						Huesos[i - tableaux].Head.Remove()
					end
					if Huesos[i- tableaux].Legs then
						Huesos[i - tableaux].Legs.Remove()
					end
					table.remove(Huesos, i - tableaux)
					tableaux = tableaux + 1
				end
				tableaux = 0
				Encounter["nextwaves"] = {"Minecraft XD"}
				State("ACTIONSELECT")
				State("DEFENDING")
			end
			Encounter["NOHIT"] = nil
			First = true
		elseif newpY > CajaY + Arena.height - 5 and jump > 3 then
			jump = 2
		elseif jump < 3 and jump > -3 then
			jump = jump - 0.5
		elseif jump < -3 and jump > -10 then
			jump = -10
		elseif jump < -3 then
			jump = jump - 1.5
		else
			jump = (jump/1.001) - 1
		end
		if Input.Up < 0 and jump > 3 and not impulsado then
			jump = 2
		end
		newpY = math.max(math.min(newpY + (jump/10), CajaY + Arena.height - 3), CajaY + 13 + Piso)
		if newpX < 328 - Arena.width/2 then
			newpX = 328 - Arena.width/2
		elseif newpX > 312 + Arena.width/2 then
			newpX = 312 + Arena.width/2
		end
		Player.MoveToAbs(newpX, newpY, true)
	elseif state == 4 then -- Down
		if plattimer > 481 and NewAudio.GetPlayTime("One") <= 1 then
			if newpX < PlatX - Plataforma.sprite.width/2 then
				newpX = newpX + 2
			elseif newpX > PlatX + Plataforma.sprite.width/2 then
				newpX = newpX - 2
			end
		end
		if MoverPlayerX then
			newpX = newpX + MoverPlayerX
			MoverPlayerX = MoverPlayerX + 1
			if MoverPlayerX == 0 then
				MoverPlayerX = nil
			end
		end
		if OnPlatform then
			newpX = newpX + (VelX or 0)
			newpY = newpY + (VelY or 0)
			OnPlatform = nil
		elseif newpY < CajaY + 15 + Piso then
			newpY = CajaY + 13 + Piso
			jump = 40
			impulsado = true
			if Player.hp > 10 then
				Player.Hurt(10, 0)
			else
				Plataforma.Remove()
				Plataforma = nil
				Black.Remove()
				Black = nil
				if BtF then
					BtF.Remove()
					BtF = nil
				end
				if BtA then
					BtA.Remove()
					BtA = nil
				end
				if BtI then
					BtI.Remove()
					BtI = nil
				end
				if BtM then
					BtM.Remove()
					BtM = nil
				end
				for i=1,#GBPaps do
					GBPaps[i - tableaux].Laser.Misil.Remove()
					GBPaps[i - tableaux].Sprite.Remove()
					table.remove(GBPaps, i - tableaux)
					tableaux = tableaux + 1
				end
				tableaux = 0
				for i=1,#HuesosSuelo do
					HuesosSuelo[i - tableaux].Misil.Remove()
					if HuesosSuelo[i - tableaux].Head then
						HuesosSuelo[i - tableaux].Head.Remove()
					end
					if HuesosSuelo[i - tableaux].Legs then
						HuesosSuelo[i - tableaux].Legs.Remove()
					end
					table.remove(HuesosSuelo, i - tableaux)
					tableaux = tableaux + 1
				end
				tableaux = 0
				for i=1,#Huesos do
					Huesos[i - tableaux].Misil.Remove()
					if Huesos[i - tableaux].Head then
						Huesos[i - tableaux].Head.Remove()
					end
					if Huesos[i- tableaux].Legs then
						Huesos[i - tableaux].Legs.Remove()
					end
					table.remove(Huesos, i - tableaux)
					tableaux = tableaux + 1
				end
				tableaux = 0
				Encounter["nextwaves"] = {"Minecraft XD"}
				State("ACTIONSELECT")
				State("DEFENDING")
			end
			Encounter["NOHIT"] = nil
			First = true
		elseif newpY > CajaY + Arena.height - 5 and jump > 3 then
			jump = 2
		elseif jump < 3 and jump > -3 then
			jump = jump - 0.5
		elseif jump < -3 and jump > -10 then
			jump = -10
		elseif jump < -3 then
			jump = jump - 1.5
		else
			jump = (jump/1.001) - 1
		end
		newpY = math.max(math.min(newpY + (jump/10), CajaY + Arena.height - 3), CajaY + 13 + Piso)
		if newpX < 328 - Arena.width/2 then
			newpX = 328 - Arena.width/2
		elseif newpX > 312 + Arena.width/2 then
			newpX = 312 + Arena.width/2
		end
		Player.MoveToAbs(newpX, newpY, true)
    end
end

function OnHit(bullet)
	if not bullet.GetVar("No Damage") and not bullet.GetVar("Plataforma") and not Player.isHurting then
		if Player.hp > (ExtraDamage or 1) then
			Encounter.Call("AddKR", (ExtraKR or 1))
			Player.hp = Player.hp - (ExtraDamage or 1)
		else
			for i=1,#GBPaps do
				if GBPaps[i - tableaux].Laser and GBPaps[i - tableaux].Laser.Misil then
					GBPaps[i - tableaux].Laser.Misil.Remove()
				end
				GBPaps[i - tableaux].Sprite.Remove()
				table.remove(GBPaps, i - tableaux)
				tableaux = tableaux + 1
			end
			tableaux = 0
			if Black then
				Black.Remove()
				Black = nil
			end
			if Caja then
				Caja.Remove()
				Caja = nil
			end
			if Warning1 then
				Warning1.Remove()
				Warning1 = nil
			end
			if Warning2 then
				Warning2.Remove()
				Warning2 = nil
			end
			if Warning3 then
				Warning3.Remove()
				Warning3 = nil
			end
			if WarningCaja then
				WarningCaja.Remove()
				WarningCaja = nil
			end
			if ParteL then
				ParteL.Remove()
				ParteL = nil
			end
			if ParteR then
				ParteR.Remove()
				ParteR = nil
			end
			if BtF then
				BtF.Remove()
				BtF = nil
			end
			if BtA then
				BtA.Remove()
				BtA = nil
			end
			if BtI then
				BtI.Remove()
				BtI = nil
			end
			if BtM then
				BtM.Remove()
				BtM = nil
			end
			Dialog("", {X = 0, Y = 0}, nil, nil, 1)
			Encounter["X"] = Player.absx
			Encounter["Y"] = Player.absy
			Encounter["nextwaves"] = {"Game Over"}
			State("ACTIONSELECT")
			State("DEFENDING")
		end
		Player.Hurt(0, 2/60)
		Encounter["NOHIT"] = nil
	elseif bullet.GetVar("Plataforma") and jump <= 0 then
		if newpY >= PlatY + 4 and newpY <= PlatY + 12 then
			newpY = PlatY + 8
			jump = 0
			OnPlatform = true
			impulsado = nil
		end
	end
end