require "Dialogs"

Arena.Resize(360, 130)
timer = 30
tableaux = 0

state = 1
maxjump = 52
jump = 0
newpX = Player.absx
newpY = Player.absy
Audio.PlaySound("ding")

SetPPCollision(true)

Bones = {}
GBPaps = {}
SpamBones = {}

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

RecolorSoul()

function Update()
	DialogUpdate()
	if not timerEnd then
		timer = timer + 1
		if timer%2 == 0 and timer >= 60 and timer <= 76 then
			if timer == 60 then
				aux = 6
			elseif timer == 62 then
				aux = aux + 16
			elseif timer == 64 then
				aux = aux + 12
			elseif timer == 66 then
				aux = aux + 8
			elseif timer == 68 then
				aux = aux + 12
			elseif timer == 70 then
				aux = aux - 12
			elseif timer == 72 then
				aux = aux - 8
			elseif timer == 74 then
				aux = aux - 12
			elseif timer == 76 then
				aux = aux - 16
			end
			Body = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2 - 1, 95 + aux/2)
			Body.sprite.yscale = aux
			Body.SetVar("vel", -8)
			table.insert(Bones, Body)
		elseif timer%2 == 0 and timer >= 100 and timer <= 116 then
			if timer == 100 then
				aux = 6
			elseif timer == 102 then
				aux = aux + 16
			elseif timer == 104 then
				aux = aux + 12
			elseif timer == 106 then
				aux = aux + 8
			elseif timer == 108 then
				aux = aux + 12
			elseif timer == 110 then
				aux = aux - 12
			elseif timer == 112 then
				aux = aux - 8
			elseif timer == 114 then
				aux = aux - 12
			elseif timer == 116 then
				aux = aux - 16
			end
			Body = CreateProjectileAbs("Bullets/Bones/Body", 320 - Arena.width/2 + 1, 95 + aux/2)
			Body.sprite.yscale = aux
			Body.SetVar("vel", 8)
			table.insert(Bones, Body)
		elseif timer%2 == 0 and timer >= 140 and timer <= 156 then
			if timer == 140 then
				aux = 6
			elseif timer == 142 then
				aux = aux + 16
			elseif timer == 144 then
				aux = aux + 12
			elseif timer == 146 then
				aux = aux + 8
			elseif timer == 148 then
				aux = aux + 12
			elseif timer == 150 then
				aux = aux - 12
			elseif timer == 152 then
				aux = aux - 8
			elseif timer == 154 then
				aux = aux - 12
			elseif timer == 156 then
				aux = aux - 16
			end
			Body = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2 - 1, 95 + aux/2)
			Body.sprite.yscale = aux
			Body.SetVar("vel", -8)
			table.insert(Bones, Body)
		elseif timer == 180 then
			table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = 678, Y = 530, rotation = 0}, DatosFinales = {X = 470, Y = 294, rotation = 0}, VelAppearing = 40}, Delay = 40, Duration = 40})
			Audio.PlaySound("gasterintro")
		elseif timer == 190 then
			table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = -38, Y = 530, rotation = 0}, DatosFinales = {X = 170, Y = 294, rotation = 0}, VelAppearing = 40}, Delay = 30, Duration = 40})
			Audio.PlaySound("gasterintro")
		elseif timer == 200 then
			table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = 320, Y = 530, rotation = 0}, DatosFinales = {X = 70, Y = 186, rotation = 90}, VelAppearing = 40}, Delay = 70, Duration = 40})
			table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = 320, Y = 530, rotation = 0}, DatosFinales = {X = 570, Y = 186, rotation = -90}, VelAppearing = 40}, Delay = 70, Duration = 40})
			state = 0
			RecolorSoul()
			Audio.PlaySound("ding")
			Audio.PlaySound("gasterintro2")
		elseif timer == 202 then
			Audio.PlaySound("gasterintro2")
		elseif timer == 210 then
			table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = 678, Y = 530, rotation = 0}, DatosFinales = {X = 70, Y = 294, rotation = 45}, VelAppearing = 40}, Delay = 60, Duration = 40})
			Audio.PlaySound("gasterintro")
		elseif timer == 220 then
			table.insert(GBPaps, {Sprite = CreateSprite("Bullets/GB Papyrus/0","BelowBullet"), Animacion = {DatosIniciales = {X = -38, Y = 530, rotation = 0}, DatosFinales = {X = 570, Y = 294, rotation = -45}, VelAppearing = 40}, Delay = 50, Duration = 40})
			Audio.PlaySound("gasterintro2")
		elseif timer%25 == 10 and timer >= 260 and timer < 435 then
			local randomizador = math.random(2)
			for i=1,4 do
				Spam = CreateProjectileAbs("Bullets/Bones/Spam", 320 - 256 + 104*i, 581)
				Spam.sprite.rotation = 90
				if randomizador == 1 then
					Spam.sprite.color = {33/255, 197/255, 1}
					Spam.SetVar("tipo", "Blue")
				else
					Spam.sprite.color = {1, 150/255, 0}
					Spam.SetVar("tipo", "Orange")
				end
				Spam.SetVar("X", 320 - 256 + 104*i)
				Spam.SetVar("Y", 581)
				table.insert(SpamBones, Spam)
			end
		elseif timer == 470 then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Bubble("[noskip][font:papyrus][voice:papyrus]No quiero\nlastimarte.", "rightwide", 490.5, 385, 40, 90, 1)
			elseif GetAlMightyGlobal("Idioma") == "Russian" then
				Bubble("[noskip][font:papyrus][voice:papyrus]Я не хочу тебе\nнавредить.", "rightwide", 490.5, 385, 40, 90, 1)
			else
				Bubble("[noskip][font:papyrus][voice:papyrus]I don't want to\nhurt you.", "rightwide", 490.5, 385, 40, 90, 1)
			end
			dialogs = 1
		end
		if dialogs then
			if dialogs == 1 and Bubbles[1] and not Bubbles[1].Activo then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Bubble("[noskip][font:papyrus][voice:papyrus]Solo necesito\nenseñarte una\nlección.", "rightwide", 490.5, 385, 40, 90, 1)
				elseif GetAlMightyGlobal("Idioma") == "Russian" then
					Bubble("[noskip][font:papyrus][voice:papyrus]Я просто должен\nпреподать тебе\nурок.", "rightwide", 490.5, 385, 40, 90, 1)
				else
					Bubble("[noskip][font:papyrus][voice:papyrus]I just need to\nteach you a\nlesson.", "rightwide", 490.5, 385, 40, 90, 1)
				end
				dialogs = 2
			elseif dialogs == 2 and Bubbles[1] and not Bubbles[1].Activo then
				EndWave()
			end
		end
	else
		if timerEnd < 180 then
			timerEnd = timerEnd + 1
			if timerEnd == 10 then
				Encounter["GameMusic"] = "Fade"
			end
		else
			EndWave()
		end
	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, 16, 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,#SpamBones do
		local bullet = SpamBones[i - tableaux]
		bullet.MoveToAbs(bullet.GetVar("X"), bullet.GetVar("Y") - 14)
		bullet.SetVar("Y", bullet.GetVar("Y") - 14)
		if bullet.absy < -bullet.sprite.width/2 then
			bullet.Remove()
			table.remove(SpamBones, i - tableaux)
			tableaux = tableaux + 1
		end
	end
	tableaux = 0
	for i=1,#Bones do
		local bullet = Bones[i - tableaux]
		if not bullet.GetVar("head") then
			bullet.SetVar("head", CreateProjectileAbs("Bullets/Bones/Head", bullet.absx, 97.5 + bullet.sprite.yscale))
		end
		bullet.GetVar("head").MoveToAbs(bullet.absx, 97.5 + bullet.sprite.yscale)
		bullet.MoveToAbs(bullet.absx + bullet.GetVar("vel"), bullet.absy)
		bullet.GetVar("head").Move(bullet.GetVar("vel"), 0)
		if bullet.absx <= 320 - Arena.width/2 or bullet.absx >= 320 + Arena.width/2 then
			bullet.Remove()
			bullet.GetVar("head").Remove()
			table.remove(Bones, i - tableaux)
			tableaux = tableaux + 1
		end
	end
	tableaux = 0
	if state ~= 1 and state ~= 2 and state ~= 3 and state ~= 4 then
		if Input.Right > 0 then
			newpX = newpX + 2
		end
		if Input.Left > 0 then
			newpX = newpX - 2
		end
		if Input.Up > 0 then
			newpY = newpY + 2
		end
		if Input.Down > 0 then
			newpY = newpY - 2
		end
		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
		if newpY < 103 then
			newpY = 103
		elseif newpY > 87 + Arena.height then
			newpY = 87 + Arena.height
		end
		Player.MoveToAbs(newpX, newpY, true)
    end
    if state == 1 then -- Down
		if Input.Right > 0 then
			newpX = newpX + 3
		end
		if Input.Left > 0 then
			newpX = newpX - 3
		end
		if newpY < 105 then
			if Input.Up > 0 then
				jump = maxjump
			else
				newpY = 103
				jump = 0
			end
		elseif jump < 3 and jump > -3 then
			jump = jump - 1
		elseif jump < -3 and jump > -10 then
			jump = -10
		elseif jump < -3 then
			jump = jump - 3
		else
			jump = (jump/1.001) - 2
		end
		if Input.Up < 0 and jump > 6 then
			jump = 4
		end
		newpY = math.max(newpY + (jump/10), 103)
		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") then
		if bullet.GetVar("tipo") then
			if bullet.GetVar("tipo") == "Blue" and (Input.Down > 0 or Input.Up > 0 or Input.Left > 0 or Input.Right > 0) and not Player.isHurting then
				if Player.hp == 1 then
					Player.Hurt(0, 2)
					Encounter["GameOver"] = true
					timerEnd = 0
				end
				if Encounter["HPEnvenenadosPorKR"] == Player.hp - 1 and Player.hp > 1 then
					Player.hp = Player.hp - 1
					Player.Hurt(0, 2/60)
				elseif Player.hp > 1 then
					Player.Hurt(0, 2/60)
					if Encounter["Glow"].alpha >= 0.9 then
						Encounter.Call("AddKR", 2)
					elseif Encounter["Glow"].alpha >= 0.6 then
						Encounter.Call("AddKR", 1.5)
					elseif Encounter["Glow"].alpha >= 0.3 then
						Encounter.Call("AddKR", 1)
					end
				end
				Encounter["NOHIT"] = nil
			elseif bullet.GetVar("tipo") == "Orange" and (Input.Down <= 0 and Input.Up <= 0 and Input.Left <= 0 and Input.Right <= 0) and not Player.isHurting then
				if Player.hp == 1 then
					Player.Hurt(0, 2)
					Encounter["GameOver"] = true
					timerEnd = 0
				end
				if Encounter["HPEnvenenadosPorKR"] == Player.hp - 1 and Player.hp > 1 then
					Player.hp = Player.hp - 1
					Player.Hurt(0, 2/60)
				elseif Player.hp > 1 then
					Player.Hurt(0, 2/60)
					if Encounter["Glow"].alpha >= 0.9 then
						Encounter.Call("AddKR", 2)
					elseif Encounter["Glow"].alpha >= 0.6 then
						Encounter.Call("AddKR", 1.5)
					elseif Encounter["Glow"].alpha >= 0.3 then
						Encounter.Call("AddKR", 1)
					end
				end
				Encounter["NOHIT"] = nil
			end
		elseif not Player.isHurting then
			if Player.hp == 1 then
				Player.Hurt(0, 2)
				Encounter["GameOver"] = true
				timerEnd = 0
			end
			if Encounter["HPEnvenenadosPorKR"] == Player.hp - 1 and Player.hp > 1 then
				Player.hp = Player.hp - 1
				Player.Hurt(0, 2/60)
			elseif Player.hp > 1 then
				Player.Hurt(0, 2/60)
				if Encounter["Glow"].alpha >= 0.9 then
					Encounter.Call("AddKR", 2)
				elseif Encounter["Glow"].alpha >= 0.6 then
					Encounter.Call("AddKR", 1.5)
				elseif Encounter["Glow"].alpha >= 0.3 then
					Encounter.Call("AddKR", 1)
				end
			end
			Encounter["NOHIT"] = nil
		end
	end
end