require "Dialogs"

Arena.Resize(480, 140)
timer = 0

state = 0
maxjump = 52
jump = 0
newpX = Player.absx
newpY = Player.absy

SetPPCollision(true)
Black1 = CreateSprite("Bullets/Bones/Body","Top")
Black1.color = {0, 0, 0}
Black1.Scale(1/5*200, Arena.height)
Black1.MoveTo(665, 165)
Black2 = CreateSprite("Bullets/Bones/Body","Top")
Black2.color = {0, 0, 0}
Black2.Scale(1/5*200, Arena.height)
Black2.MoveTo(-25, 165)

Bones = {}

function RecolorSoul()
	if state ~= 1 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
	end
end

RecolorSoul()

function Update()
	DialogUpdate()
	Encounter.Call("PapsMoveTo", {320, 155 + Arena.height})
	if not timerEnd or timer >= 350 and timer <= 980 then
		timer = timer + 1
		if timer == 10 then
			state = 1
			RecolorSoul()
			Audio.PlaySound("ding")
		elseif timer == 20 then
			for i=1,9 do
				if i == 1 then
					aux = 6
				elseif i == 2 then
					aux = aux + 16
				elseif i == 3 then
					aux = aux + 12
				elseif i == 4 then
					aux = aux + 8
				elseif i == 5 then
					aux = aux + 12
				elseif i == 6 then
					aux = aux - 12
				elseif i == 7 then
					aux = aux - 8
				elseif i == 8 then
					aux = aux - 12
				elseif i == 9 then
					aux = aux - 16
				end
				Body = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2 + 16*(i - 1), 95 + aux/2)
				Body.sprite.yscale = aux
				Body.SetVar("vel", -12)
				table.insert(Bones, Body)
			end
		elseif timer == 55 then
			for i=1,9 do
				if i == 1 then
					aux = 6
				elseif i == 2 then
					aux = aux + 16
				elseif i == 3 then
					aux = aux + 12
				elseif i == 4 then
					aux = aux + 8
				elseif i == 5 then
					aux = aux + 12
				elseif i == 6 then
					aux = aux - 12
				elseif i == 7 then
					aux = aux - 8
				elseif i == 8 then
					aux = aux - 12
				elseif i == 9 then
					aux = aux - 16
				end
				Body = CreateProjectileAbs("Bullets/Bones/Body", 320 - Arena.width/2 - 16*(i - 1), 95 + aux/2)
				Body.sprite.yscale = aux
				Body.SetVar("vel", 12)
				table.insert(Bones, Body)
			end
		elseif timer == 90 then
			for i=1,9 do
				if i == 1 then
					aux = 6
				elseif i == 2 then
					aux = aux + 16
				elseif i == 3 then
					aux = aux + 12
				elseif i == 4 then
					aux = aux + 8
				elseif i == 5 then
					aux = aux + 12
				elseif i == 6 then
					aux = aux - 12
				elseif i == 7 then
					aux = aux - 8
				elseif i == 8 then
					aux = aux - 12
				elseif i == 9 then
					aux = aux - 16
				end
				Body = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2 + 16*(i - 1), 95 + aux/2)
				Body.sprite.yscale = aux
				Body.SetVar("vel", -12)
				table.insert(Bones, Body)
			end
		elseif timer == 125 then
			for i=1,9 do
				if i == 1 then
					aux = 6
				elseif i == 2 then
					aux = aux + 16
				elseif i == 3 then
					aux = aux + 12
				elseif i == 4 then
					aux = aux + 8
				elseif i == 5 then
					aux = aux + 12
				elseif i == 6 then
					aux = aux - 12
				elseif i == 7 then
					aux = aux - 8
				elseif i == 8 then
					aux = aux - 12
				elseif i == 9 then
					aux = aux - 16
				end
				Body = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2 + 16*(i - 1), 95 + aux/2)
				Body.sprite.yscale = aux
				Body.SetVar("vel", -12)
				table.insert(Bones, Body)
			end
		elseif timer == 143 then
			for i=1,9 do
				if i == 1 then
					aux = 6
				elseif i == 2 then
					aux = aux + 16
				elseif i == 3 then
					aux = aux + 12
				elseif i == 4 then
					aux = aux + 8
				elseif i == 5 then
					aux = aux + 12
				elseif i == 6 then
					aux = aux - 12
				elseif i == 7 then
					aux = aux - 8
				elseif i == 8 then
					aux = aux - 12
				elseif i == 9 then
					aux = aux - 16
				end
				Body = CreateProjectileAbs("Bullets/Bones/Body", 320 + Arena.width/2 + 16*(i - 1), 95 + aux/2)
				Body.sprite.yscale = aux
				Body.SetVar("vel", -6)
				table.insert(Bones, Body)
			end
		elseif timer == 213 then
			for i=1,9 do
				if i == 1 then
					aux = 6
				elseif i == 2 then
					aux = aux + 16
				elseif i == 3 then
					aux = aux + 12
				elseif i == 4 then
					aux = aux + 8
				elseif i == 5 then
					aux = aux + 12
				elseif i == 6 then
					aux = aux - 12
				elseif i == 7 then
					aux = aux - 8
				elseif i == 8 then
					aux = aux - 12
				elseif i == 9 then
					aux = aux - 16
				end
				Body = CreateProjectileAbs("Bullets/Bones/Body", 320 - Arena.width/2 - 16*(i - 1), 95 + aux/2)
				Body.sprite.yscale = aux
				Body.SetVar("vel", 6)
				table.insert(Bones, Body)
			end
		elseif timer == 283 then
			Body = CreateProjectileAbs("Bullets/Bones/Body", 320 - Arena.width/2 + 1, 95 + aux/2)
			Body.sprite.yscale = 6
			Body.SetVar("vel", 1)
			table.insert(Bones, Body)
		elseif timer == 350 then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Bubble("[noskip][font:papyrus][voice:papyrus]Lo siento,[w:15] es un\nhábito.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
			elseif GetAlMightyGlobal("Idioma") == "Russian" then
				Bubble("[noskip][font:papyrus][voice:papyrus]Прости,[w:15] просто\nпривычка.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
			else
				Bubble("[noskip][font:papyrus][voice:papyrus]Sorry,[w:15] just a\nhabit.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
			end
		elseif timer == 520 then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Bubble("[noskip][font:papyrus][voice:papyrus]Nombre ese\nataque flojo\nen honor a mi\nhermano.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
			elseif GetAlMightyGlobal("Idioma") == "Russian" then
				Bubble("[noskip][font:papyrus][voice:papyrus]Я назвал это\nленивой атакой\nв честь моего\nбрата.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
			else
				Bubble("[noskip][font:papyrus][voice:papyrus]I named that\nlazy attack\nafter my\nbrother.[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
			end
		elseif timer == 720 then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Bubble("[noskip][font:papyrus][voice:papyrus]Hasta pronto[w:15]\nsans...[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
			elseif GetAlMightyGlobal("Idioma") == "Russian" then
				Bubble("[noskip][font:papyrus][voice:papyrus]Скоро увидимся,[w:15]\nСанс...[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
			else
				Bubble("[noskip][font:papyrus][voice:papyrus]See you soon[w:15]\nsans...[w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
			end
		elseif timer == 980 then
			Bubble("", "", "", "", "", "", 1)
			if not timerEnd then
				Black1.Remove()
				Black2.Remove()
				Arena.Resize(565, 130)
			end
		elseif timer > 980 and Arena.height == 130 then
			EndWave()
		end
	else
		if timerEnd < 180 then
			timerEnd = timerEnd + 1
			if timerEnd == 10 then
				Encounter["GameMusic"] = "Fade"
			end
		elseif timerEnd >= 180 then
			Black1.Remove()
			Black2.Remove()
			EndWave()
		end
	end
	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 <= 160 - Arena.width/2 or bullet.absx >= 480 + 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 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 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