require "Dialogs"

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

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

if Encounter["Glow"].alpha >= 0.9 then
	Danio = 3
elseif Encounter["Glow"].alpha >= 0.6 then
	Danio = 2
elseif Encounter["Glow"].alpha >= 0.3 then
	Danio = 1
end

SetPPCollision(true)

Bones = {}
DeathBones = {} --these bones, kills you instantly XD                                                                                                                                                                                                                                                                JK xdxdxddddd

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()
	if not timerEnd then
		if not dialogs then
			timer = timer + 1
		end
		if timer == 1 then
			Body = CreateProjectileAbs("Bullets/Bones/Body", 710, Arena.y + 5 + Arena.height/2)
			Body.sprite.rotation = 20
			Body.sprite.yscale = 120
			Body.SetVar("X", 710)
			Body.SetVar("Y", Arena.y + 5 + Arena.height/2)
			Body.SetVar("velrot", 3)
			table.insert(Bones, Body)
			Body = CreateProjectileAbs("Bullets/Bones/Body", 710, Arena.y + 5 + Arena.height/2)
			Body.sprite.rotation = 110
			Body.sprite.yscale = 120
			Body.SetVar("X", 710)
			Body.SetVar("Y", Arena.y + 5 + Arena.height/2)
			Body.SetVar("velrot", 3)
			table.insert(Bones, Body)
		elseif timer%10 == 1 and timer >= 210 and timer <= 440 then
			Body = CreateProjectileAbs("Bullets/Bones/Body", Arena.x + Arena.width/2 + 4 - 14*(timer - 200)/10, Arena.y + 5 + Arena.height)
			Body.sprite.yscale = 0
			Body.SetVar("vel", 2)
			table.insert(DeathBones, Body)
		elseif timer == 450 then
			Bone = CreateProjectileAbs("Bullets/Bones/Long Bone", 164, Arena.y + 5 + Arena.height + 286/2)
			Bone.sprite.rotation = 90
			Black1 = CreateSprite("Bullets/Bones/Body", "Top")
			Black1.MoveTo(Bone.absx - 8, Bone.absy)
			Black1.Scale(Bone.sprite.height/5, Bone.sprite.width)
			Black1.color = {0,0,0}
			Black2 = CreateSprite("Bullets/Bones/Body", "Top")
			Black2.MoveTo(Bone.absx + 2, Bone.absy + Bone.sprite.width/4)
			Black2.Scale(Bone.sprite.height/5, Bone.sprite.width/2)
			Black2.color = {0,0,0}
			White = CreateSprite("Bullets/Bones/Body", "Top")
			White.MoveTo(Bone.absx - 8, Bone.absy - Bone.sprite.width/2 + 2.5)
			White.Scale(Bone.sprite.height/5, 5)
		elseif timer > 450 and timer <= 510 then
			Bone.MoveToAbs(164, Arena.y + 5 + Arena.height + 286/2 - 110*math.sin(math.rad(1.5*(timer - 450))))
		elseif timer > 520 and timer <= 550 then
			Bone.MoveToAbs(164, Arena.y - 105 + Arena.height + 286/2 + 80*math.sin(math.rad(3*(timer - 520))))
		elseif timer > 550 and timer <= 558 then
			if timer == 551 then
				if GetAlMightyGlobal("Idioma") == "Russian" then
					Bubble("[noskip][waittall:0.5][font:papyrus][voice:papyrus]НЕТ!!!![w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
				else
					Bubble("[noskip][waittall:0.5][font:papyrus][voice:papyrus]NOO!!!![w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
				end
				Encounter.Call("AttackAnim", 3)
				Protector = CreateProjectileAbs("Bullets/Bones/Body", 74, Arena.y - 91 + Arena.height)
				Protector.sprite.yscale = 130
				Protector.sprite.rotation = 90
				Protector.sprite.alpha = 0
				Protector.SetVar("Head", CreateProjectileAbs("Bullets/Bones/Head", Protector.absx + Protector.sprite.yscale/2 + 2.5, Arena.y - 95 + Arena.height))
				Protector.GetVar("Head").sprite.rotation = -90
				Protector.GetVar("Head").sprite.alpha = 0
				Protector.SetVar("Legs", CreateProjectileAbs("Bullets/Bones/Head", Protector.absx - Protector.sprite.yscale/2 - 2.5, Arena.y - 95 + Arena.height))
				Protector.GetVar("Legs").sprite.rotation = 90
				Protector.GetVar("Legs").sprite.alpha = 0
			end
			Bone.MoveToAbs(164, Arena.y - 25 + Arena.height + 286/2 - 8.6*(timer - 550))
			Protector.MoveToAbs(Protector.absx + 4, Arena.y - 95 + Arena.height)
			Protector.GetVar("Head").MoveToAbs(Protector.GetVar("Head").absx + 4, Arena.y - 95 + Arena.height)
			Protector.GetVar("Legs").MoveToAbs(Protector.GetVar("Legs").absx + 4, Arena.y - 95 + Arena.height)
			Protector.sprite.alpha = Protector.sprite.alpha + 0.125
			Protector.GetVar("Head").sprite.alpha = Protector.GetVar("Head").sprite.alpha + 0.125
			Protector.GetVar("Legs").sprite.alpha = Protector.GetVar("Legs").sprite.alpha + 0.125
		elseif timer > 590 and timer <= 598 then
			Bone.MoveToAbs(164, Arena.y - 94 + Arena.height + 286/2 + 26*math.sin(math.rad(90/8*(timer - 590))))
		elseif timer > 598 and timer <= 603 then
			Bone.MoveToAbs(164, Arena.y - 94 + Arena.height + 286/2 + 26*math.cos(math.rad(18*(timer - 598))))
		elseif timer > 603 and timer <= 611 then
			Bone.MoveToAbs(164, Arena.y - 94 + Arena.height + 286/2 + 26*math.sin(math.rad(90/8*(timer - 603))))
		elseif timer > 611 and timer <= 616 then
			Bone.MoveToAbs(164, Arena.y - 94 + Arena.height + 286/2 + 26*math.cos(math.rad(18*(timer - 611))))
		elseif timer > 626 and timer <= 646 then
			Bone.MoveToAbs(164, Arena.y - 94 + Arena.height + 286/2 + 50*math.sin(math.rad(4.5*(timer - 626))))
		elseif timer > 646 and timer <= 651 and not dialogs then
			Bone.MoveToAbs(164, Arena.y - 94 + Arena.height + 286/2 + 50*math.cos(math.rad(18*(timer - 646))))
			if timer == 651 then
				Encounter.Call("ContinueMusic")
				Bubble("[noskip][font:papyrus][voice:papyrus]. . .[w:40][next]", "rightwide", 490.5, 395, 40, 90, 1)
				Encounter.Call("AttackAnim", 4)
				dialogs = 1
			end
		end
		if dialogs and Bubbles[1] and not Bubbles[1].Activo then
			if dialogs == 1 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Bubble("[font:papyrus][voice:papyrus]Realmente lo\nsiento.", "rightwide", 490.5, 395, 40, 90, 1)
				elseif GetAlMightyGlobal("Idioma") == "Russian" then
					Bubble("[font:papyrus][voice:papyrus]Мне правда жаль.", "rightwide", 490.5, 395, 40, 90, 1)
				else
					Bubble("[font:papyrus][voice:papyrus]I'm really sorry.", "rightwide", 490.5, 395, 40, 90, 1)
				end
				Encounter.Call("AttackAnim", 5)
				dialogs = 2
			elseif dialogs == 2 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Bubble("[font:papyrus][voice:papyrus]No debí de haber\ndejado de creer\nen ti.", "rightwide", 490.5, 395, 40, 90, 1)
				elseif GetAlMightyGlobal("Idioma") == "Russian" then
					Bubble("[font:papyrus][voice:papyrus]Я не должен\nбыл переставать\nверить в тебя.", "rightwide", 490.5, 395, 40, 90, 1)
				else
					Bubble("[font:papyrus][voice:papyrus]I shouldn't have\nstopped believing\nin you.", "rightwide", 490.5, 395, 40, 90, 1)
				end
				Encounter.Call("SetHead", "Forgiving")
				dialogs = 3
			elseif dialogs == 3 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Bubble("[font:papyrus][voice:papyrus]A puesto a que\nte arrepientes\nde todo lo que\nhiciste.", "rightwide", 490.5, 395, 40, 90, 1)
				elseif GetAlMightyGlobal("Idioma") == "Russian" then
					Bubble("[font:papyrus][voice:papyrus]Я уверен[w:10], что ты\nсожалеешь обо\nвсём[w:10], что ты\nсделал.", "rightwide", 490.5, 395, 40, 90, 1)
				else
					Bubble("[font:papyrus][voice:papyrus]I bet you regret\neverything you\ndid.", "rightwide", 490.5, 395, 40, 90, 1)
				end
				Encounter.Call("SetHead", "Remembering")
				dialogs = 4
			elseif dialogs == 4 then
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Bubble("[font:papyrus][voice:papyrus]Sólo que yo\nrealmente\nextraño a mis\namigos...", "rightwide", 490.5, 395, 40, 90, 1)
				elseif GetAlMightyGlobal("Idioma") == "Russian" then
					Bubble("[font:papyrus][voice:papyrus]Я просто очень\nскучаю по своим\nдрузьям...", "rightwide", 490.5, 395, 40, 90, 1)
				else
					Bubble("[font:papyrus][voice:papyrus]I just really\nmiss my friends...", "rightwide", 490.5, 395, 40, 90, 1)
				end
				Encounter.Call("SetHead", "Missing")
				dialogs = 5
			elseif dialogs == 5 then
				Encounter.Call("ContinueMusic")
				if GetAlMightyGlobal("Idioma") == "Spanish" then
					Bubble("[noskip][font:papyrus][voice:papyrus][effect:shake]Lo siento mucho.[w:60][next]", "rightwide", 490.5, 395, 40, 90, 1)
				elseif GetAlMightyGlobal("Idioma") == "Russian" then
					Bubble("[noskip][font:papyrus][voice:papyrus][effect:shake]Мне так жаль.[w:60][next]", "rightwide", 490.5, 395, 40, 90, 1)
				else
					Bubble("[noskip][font:papyrus][voice:papyrus][effect:shake]I'm so sorry.[w:60][next]", "rightwide", 490.5, 395, 40, 90, 1)
				end
				Encounter.Call("SetHead", "Crying")
				dialogs = 6
				PapyrusMove = true
				papsvel = 0
			elseif dialogs == 6 then
				dialogs = nil
			end
		end
		if PapyrusMove then
			papsvel = papsvel + 1;
			if papsvel > 20 and papsvel <= 35 then
				Encounter.Call("PapsMoveTo", {320 + 15*math.sin(math.rad(24*(papsvel - 20))), 285})
			elseif papsvel > 65 and papsvel <= 80 then
				Encounter.Call("PapsMoveTo", {320 - 30*math.sin(math.rad(24*(papsvel - 65))), 285})
			elseif papsvel > 120 and papsvel <= 180 then
				if papsvel == 121 then
					Bubble("", "", "", "", "", "", 1)
				end
				Encounter.Call("PapsMoveTo", {320, 285 + (papsvel - 120)/4})
			elseif papsvel > 180 and papsvel <= 240 then
				local shake = {math.random(-(papsvel - 180)/10, (papsvel - 180)/10), math.random(-(papsvel - 180)/10, (papsvel - 180)/10)}
				Encounter.Call("PapsMoveTo", {320 + shake[1], 285 + (papsvel - 120)/4 + shake[2]})
			elseif papsvel > 240 and papsvel <= 246 then
				if papsvel == 241 then
					CantDie = true
					Encounter.Call("Cry")
				end
				Encounter.Call("PapsMoveTo", {320, 294 - 26/6*(papsvel - 240)})
			elseif papsvel == 267 then
				Bone.Remove()
				Black1.Remove()
				Black2.Remove()
				White.Remove()
				EndWave()
			end
		end
	else
		timerEnd = timerEnd + 1
		if timerEnd == 1 then
			if GetAlMightyGlobal("Idioma") == "Russian" then
				Bubble("[instant][font:papyrus][voice:papyrus]НЕТ!!!![w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
			else
				Bubble("[instant][font:papyrus][voice:papyrus]NOO!!!![w:200] ", "rightwide", 490.5, 395, 40, 90, 1)
			end
			Encounter.Call("AttackAnim", 3)
			Bubbles[1].BurbujaW = 0
			Bubbles[1].BurbujaSkip = "Instantaneo"
			DialogUpdate()
		elseif timerEnd >= 6 then
			for i=1,#Bones do
				if Bones[i - tableaux].GetVar("legs") then
					Bones[i - tableaux].GetVar("legs").Remove()
					Bones[i - tableaux].SetVar("legs", nil)
				end
				if Bones[i - tableaux].GetVar("head") then
					Bones[i - tableaux].GetVar("head").Remove()
					Bones[i - tableaux].SetVar("head", nil)
				end
				Bones[i - tableaux].Remove()
				table.remove(Bones, i - tableaux)
				tableaux = tableaux + 1
			end
			for i=1,#DeathBones do
				if DeathBones[i - tableaux].GetVar("legs") then
					DeathBones[i - tableaux].GetVar("legs").Remove()
					DeathBones[i - tableaux].SetVar("legs", nil)
				end
				DeathBones[i - tableaux].Remove()
				table.remove(DeathBones, i - tableaux)
				tableaux = tableaux + 1
			end
			if Black1 then
				Black1.Remove()
			end
			if Black2 then
				Black2.Remove()
			end
			if White then
				White.Remove()
			end
			Bubble("", "", "", "", "", "", 1)
			Encounter["X"] = Player.absx
			Encounter["Y"] = Player.absy
			Encounter["nextwaves"] = {"Game Over"}
			State("ACTIONSELECT")
			State("DEFENDING")
		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 - (bullet.sprite.yscale/2 + 2.5)*math.sin(math.rad(bullet.sprite.rotation)), bullet.absy + (bullet.sprite.yscale/2 + 2.5)*math.cos(math.rad(bullet.sprite.rotation))))
		end
		if not bullet.GetVar("legs") then
			bullet.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", bullet.absx + (bullet.sprite.yscale/2 + 2.5)*math.sin(math.rad(bullet.sprite.rotation)), bullet.absy - (bullet.sprite.yscale/2 + 2.5)*math.cos(math.rad(bullet.sprite.rotation))))
		end
		bullet.sprite.rotation = bullet.sprite.rotation + bullet.GetVar("velrot")
		bullet.GetVar("head").sprite.rotation = bullet.sprite.rotation
		bullet.GetVar("legs").sprite.rotation = bullet.sprite.rotation + 180
		bullet.SetVar("X", bullet.GetVar("X") - 3)
		bullet.MoveToAbs(bullet.GetVar("X"), bullet.GetVar("Y"))
		bullet.GetVar("head").MoveToAbs(bullet.GetVar("X") - (bullet.sprite.yscale/2 + 2.5)*math.sin(math.rad(bullet.sprite.rotation)), bullet.GetVar("Y") + (bullet.sprite.yscale/2 + 2.5)*math.cos(math.rad(bullet.sprite.rotation)))
		bullet.GetVar("legs").MoveToAbs(bullet.GetVar("X") + (bullet.sprite.yscale/2 + 2.5)*math.sin(math.rad(bullet.sprite.rotation)), bullet.GetVar("Y") - (bullet.sprite.yscale/2 + 2.5)*math.cos(math.rad(bullet.sprite.rotation)))
		if bullet.GetVar("X") < -70 then
			bullet.Remove()
			bullet.GetVar("head").Remove()
			bullet.GetVar("legs").Remove()
			table.remove(Bones, i - tableaux)
			tableaux = tableaux + 1
		end
	end
	tableaux = 0
	for i=1,#DeathBones do
		local bullet = DeathBones[i]
		if not bullet.GetVar("legs") then
			bullet.SetVar("legs", CreateProjectileAbs("Bullets/Bones/Head", bullet.absx + (bullet.sprite.yscale/2 + 2.5)*math.sin(math.rad(bullet.sprite.rotation)), bullet.absy - (bullet.sprite.yscale/2 + 2.5)*math.cos(math.rad(bullet.sprite.rotation))))
			bullet.GetVar("legs").sprite.rotation = 180
		end
		bullet.sprite.yscale = math.min(Arena.height - 5, bullet.sprite.yscale + bullet.GetVar("vel"))
		bullet.SetVar("vel", bullet.GetVar("vel") + 0.2)
		if bullet.sprite.yscale >= Arena.height - 5 then
			if not bullet.GetVar("bounce") then
				bullet.SetVar("bounce", true)
				bullet.SetVar("vel", -3)
			end
		end
		bullet.MoveToAbs(bullet.absx, Arena.y + 5 + Arena.height - bullet.sprite.yscale/2)
		bullet.GetVar("legs").MoveToAbs(bullet.absx, Arena.y + 5 + Arena.height - bullet.sprite.yscale - 2.5)
	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 and not timerEnd and not CantDie then
			timerEnd = 0
		end
		if Encounter["HPEnvenenadosPorKR"] == Player.hp - 1 and Player.hp > 1 and not Player.isHurting then
			Player.hp = Player.hp - 1
			Player.Hurt(0, 2/60)
		elseif Player.hp > 1 then
			Player.Hurt(0, 2/60)
			Encounter.Call("AddKR", 0.5*(Danio + 1))
		end
		Encounter["NOHIT"] = nil
	end
end