Numbers = {CreateSprite("Undertale/HP/0","Top"), CreateSprite("Undertale/HP/0","Top"), Contador = 90}
Player.SetControlOverride(true)

function HPUpdate()
	if Long then
		Long = Long + 1
		if Long == 1 then
			if not Corazon then
				Corazon = CreateSprite("Undertale/Medidor/Heart","Top")
				Corazon.MoveTo(320, 468)
			end
		elseif Long == 4 then
			Corazon.alpha = 0
		elseif Long == 7 then
			Corazon.alpha = 1
		elseif Long == 10 then
			Corazon.alpha = 0
		elseif Long == 13 then
			Corazon.alpha = 1
		elseif Long >= 33 and Long < 53 then
			Corazon.MoveTo(320 - 152/20*(Long - 33), 468)
		elseif Long > 60 and Long <= 90 then
			if not MedidorT and not Save then
				MedidorT = CreateSprite("Undertale/Medidor/Medidor","Top")
				Save = CreateSprite("Undertale/Medidor/Save 1","Top")
				Save.SetAnimation({"Undertale/Medidor/Save 1","Undertale/Medidor/Save 2"}, 0.16)
				MedidorT.MoveTo(320, 468)
				Save.MoveTo(472, 468)
				MedidorT.alpha = 0
				Save.alpha = 0
			end
			MedidorT.alpha = math.min(MedidorT.alpha + 0.01, 0.25)
			Save.alpha = Save.alpha + 0.04
			if Long == 90 then
				Long = nil
			end
		end
	end
	if Unlong and not Long then
		if Corazon then
			Unlong = Unlong + 1
			if Unlong <= 30 then
				MedidorT.alpha = MedidorT.alpha - 0.01
				Save.alpha = Save.alpha - 0.04
				if Unlong == 30 then
					MedidorT.Remove()
					Save.Remove()
					MedidorT = nil
					Save = nil
				end
			elseif Unlong == 40 then
				Corazon.alpha = 0
			elseif Unlong == 43 then
				Corazon.alpha = 1
			elseif Unlong == 46 then
				Corazon.alpha = 0
			elseif Unlong == 49 then
				Corazon.alpha = 1
			elseif Unlong == 52 then
				Corazon.Remove()
				Corazon = nil
				Unlong = nil
			end
		else
			Unlong = nil
		end
	end
	if Corazon and MedidorT then
		if SongTime > Largo or (timer > Empezar and NewAudio.isStopped("Song")) and not Unlong then
			Unlong = 0
		end
		if NewAudio.isStopped("Song") and timer > Empezar then
			Corazon.MoveTo(472, 468)
		else
			Corazon.MoveTo(168 + 304/Largo*math.min(SongTime, Largo), 468)
		end
	end
	Numbers[1].Set("Undertale/HP/"..(Player.hp - Player.hp%10)%100/10)
	Numbers[2].Set("Undertale/HP/"..Player.hp%10)
	Numbers[1].MoveTo(Player.absx - 9, Player.absy + 20)
	Numbers[2].MoveTo(Player.absx + 9, Player.absy + 20)
	if Numbers.Contador <= 0 and Numbers[1].alpha > 0 and Numbers[2].alpha > 0 then
		Numbers[1].alpha = Numbers[1].alpha - 0.02
		Numbers[2].alpha = Numbers[2].alpha - 0.02
	else
		Numbers.Contador = Numbers.Contador - 1
	end
	if Player.isHurting then
		Numbers.Contador = 90
		Numbers[1].alpha = 1
		Numbers[2].alpha = 1
	end
	if MedidorT then
		MedidorT.SendToTop()
	end
	if Save then
		Save.SendToTop()
	end
	if Corazon then
		Corazon.SendToTop()
	end
end

function SpawnMeter(num)
	Long = 0
	Largo = num
end

function RemoveAll()
	Numbers[1].Remove()
	Numbers[2].Remove()
	Numbers = nil
	if MedidorT then
		MedidorT.Remove()
	end
	if Corazon then
		Corazon.Remove()
	end
	if Save then
		Save.Remove()
	end
	MedidorT = nil
	Corazon = nil
	Save = nil
end