require "Dialogs"
require "Frisk Character"
Arena.ResizeImmediate(565, 130)
FriskDirection = "R"
AlowFriskMovement(false, false)
Player.sprite.alpha = 0

timezone = string.gsub(package.loaded.os.date("%Z"), ":00", "")
timezone = string.gsub(timezone, ":30", "")
hour = tonumber(package.loaded.os.date("%H")) + tonumber(timezone)
while hour >= 24 do
	hour = hour - 24
end
while hour < 0 do
	hour = hour + 24
end
day = tonumber(package.loaded.os.date("%d"))
month = tonumber(package.loaded.os.date("%m"))

Heart = CreateSprite("HeartFlee 1","BelowBullet")
Heart.MoveTo(65, 160)
Heart.color = Player.sprite.color
BlackDefinitive = CreateSprite("Bullets/Bones/Body","BelowBullet")
Corredor = CreateSprite("Overworld/Maps/Corredor","BelowBullet")
CorredorN = CreateSprite("Overworld/Maps/Corredor Noche","BelowBullet")
CorredorN.SetParent(Corredor)
PilarOw = CreateSprite("Overworld/Maps/PilarOW","BelowBullet")
PilarOwN = CreateSprite("Overworld/Maps/PilarOW Noche","BelowBullet")
PilarOw.SetParent(Corredor)
PilarOwN.SetParent(Corredor)
Papaya = CreateSprite("Overworld/Characters/Papyrus/Crying 1","BelowBullet")
Papaya.SetAnimation({"Overworld/Characters/Papyrus/Crying 1","Overworld/Characters/Papyrus/Crying 2"}, 0.25)
Papaya.SetPivot(0.5, 0)
Papaya.SetParent(Corredor)
Pilares = {CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top"), CreateSprite("Overworld/Maps/Pilar","Top")}
Black = CreateSprite("Bullets/Bones/Body","Top")
Frisk.SetParent(Corredor)
for i=1,#Pilares do
	Pilares[i].SetParent(Corredor)
end
Black.color = {0,0,0}
BlackDefinitive.color = {0,0,0}
Black.Scale(640/5, 480)
BlackDefinitive.Scale(640/5, 480)
Frisk.MoveTo(221, -146)
Corredor.MoveTo(-100, -200)
Black.MoveTo(320, 240)
Black.alpha = 0
BlackDefinitive.MoveTo(320, 240)
BlackDefinitive.alpha = 0
PilarOw.MoveTo(100, 40)
PilarOwN.MoveTo(100, 40)
Papaya.MoveTo(466, -156)
Pilares[1].SetVar("Datos", -935)
Pilares[2].SetVar("Datos", -475)
Pilares[3].SetVar("Datos", -15)
Pilares[4].SetVar("Datos", 465)
Pilares[5].SetVar("Datos", 945)
Pilares[6].SetVar("Datos", 1347)
Pilares[7].SetVar("Datos", 1587)
Pilares[8].SetVar("Datos", 1827)
Pilares[9].SetVar("Datos", 2287)
Corredor.alpha = 0.5 + 0.5*math.sin(math.rad(180/12*(hour - 8)))
CorredorN.alpha = 0.5 + 0.5*math.sin(math.rad(180/12*(hour - 20)))
PilarOw.alpha = Corredor.alpha
PilarOwN.alpha = CorredorN.alpha
for i=1,#Pilares do
	Pilares[i].color = {0.6 + 0.4*math.sin(math.rad(180/12*(hour - 8))), 0.6 + 0.4*math.sin(math.rad(180/12*(hour - 8))), 0.6 + 0.4*math.sin(math.rad(180/12*(hour - 8)))}
end

timer = -120
if GetAlMightyGlobal("Idioma") == "Spanish" then
	Dialog("[noskip]Me voy de aquí.[w:1000000000] ", {X = 91, Y = 210}, nil, true, 1)
elseif GetAlMightyGlobal("Idioma") == "Russian" then
	Dialog("[noskip]Я сваливаю отсюда.[w:1000000000] ", {X = 91, Y = 210}, nil, true, 1)
else
	Dialog("[noskip]I'm out of here.[w:1000000000] ", {X = 91, Y = 210}, nil, true, 1)
end
Audio.PlaySound("flee")
tableaux = 0

HuesosSuelo = {}

function Update()
	FraskoX = Frisk.x
	FriskUpdate()
	DialogUpdate()
	Black.SendToTop()
	if timer < 0 then
		timer = timer + 1
		Heart.MoveTo(Heart.x - 1, Heart.y)
		if timer%10 == 5 then
			Heart.Set("HeartFlee 2")
		elseif timer%10 == 0 then
			Heart.Set("HeartFlee 1")
		end
	elseif timer == 0 and Black.alpha < 1 then
		Black.alpha = Black.alpha + 0.04
		if Black.alpha >= 1 then
			Corredor.MoveTo(-100, 280)
			Dialog("", nil, nil, nil, 1)
			if Encounter["Bone"] then
				Encounter["Bone"].Remove()
				Encounter["Bone"] = nil
				Encounter["BoneAnimation"] = nil
			end
			Heart.Remove()
			BlackDefinitive.alpha = 1
			timer = 1
		end
	elseif timer > 0 and timer <= 60 then
		timer = timer + 1
	elseif timer == 61 and Black.alpha > 0 then
		Black.alpha = Black.alpha - 0.04
		if Black.alpha <= 0 then
			timer = 62
		end
	elseif timer > 61 and timer <= 121 then
		timer = timer + 1
	elseif timer == 122 then
		FriskAuto("Left")
		timer = 123
	elseif timer == 123 then
		Corredor.x = Corredor.x + 4
		if Frisk.x <= -3 then
			for i=0,7 do
				table.insert(HuesosSuelo, {Misil = CreateProjectileAbs("Bullets/Bones/Body", 140 + 6*i, 82 + 20*i), Head = CreateProjectileAbs("Bullets/Bones/Head", 0, 0), Scale = {MaxHeight = 80, Vel = 10}, Duration = 1/0})
			end
			Audio.PlaySound("pierce")
			Papaya.StopAnimation()
			Papaya.Set("Overworld/Characters/Papyrus/Bone")
			Papaya.MoveTo(466, -146)
			AlowFriskMovement(false, false)
			timer = 124
		end
	elseif timer > 123 and timer <= 213 then
		timer = timer + 1
	elseif timer == 214 then
		FriskDirection = "R"
		Corredor.x = Corredor.x - 1
		for i=1,#HuesosSuelo do
			local bullet = HuesosSuelo[i]
			bullet.X = bullet.X - 1
		end
		if Corredor.x <= 140 then
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Box("[noskip][voice:papyrus]A DÓNDE CREES QUE VAS?", "papyrus", "", "", 0, 400, nil, "", 1)
			elseif GetAlMightyGlobal("Idioma") == "Russian" then
				Box("[noskip][voice:papyrus]И КУДА ЭТО ТЫ СОБРАЛСЯ?", "papyrus", "", "", 0, 400, nil, "", 1)
			else
				Box("[noskip][voice:papyrus]WHERE DO YOU THINK YOU\rARE GOING?", "papyrus", "", "", 0, 400, nil, "", 1)
			end
			timer = 2000
		end
	elseif timer >= 2000 and Boxes[1] and not Boxes[1].Activo then
		timer = timer + 1
		if timer == 2001 then
			TipoFrisk = "N"
			Frisk.alpha = 0
			Heart = CreateSprite("Overworld/Objects/Soul","Top")
			Heart.color = Player.sprite.color
			Heart.MoveTo(Frisk.absx, Frisk.absy + Frisk.height/2)
			for i=1,#Pilares do
				Pilares[i - tableaux].Remove()
				table.remove(Pilares, i - tableaux)
				tableaux = tableaux + 1
			end
			tableaux = 0
			Papaya.Remove()
			Papaya = nil
			PilarOw.Remove()
			PilarOw = nil
			PilarOwN.Remove()
			PilarOwN = nil
			Corredor.alpha = 0
			CorredorN.alpha = 0
			Audio.PlaySound("st")
		elseif timer == 2004 then
			Frisk.alpha = 1
			Heart.alpha = 0
		elseif timer == 2007 then
			Frisk.alpha = 0
			Heart.alpha = 1
			Audio.PlaySound("st")
		elseif timer == 2010 then
			Frisk.alpha = 1
			Heart.alpha = 0
		elseif timer == 2013 then
			Frisk.alpha = 0
			Heart.alpha = 1
			Heart.SetVar("diff", {515 - Heart.absx, 25 - Heart.absy})
			Audio.PlaySound("st")
		elseif timer >= 2033 and timer < 2053 then
			Heart.MoveTo(Heart.absx + Heart.GetVar("diff")[1]/20, Heart.absy + Heart.GetVar("diff")[2]/20)
			if timer == 2033 then
				Audio.PlaySound("start")
			end
		elseif timer == 2053 and Heart then
			timer = timer - 1
			Heart.alpha = Heart.alpha - 0.05
			if Heart.alpha <= 0 then
				Heart.Remove()
				Heart = nil
			end
		elseif timer == 2054 then
			Encounter.Call("ContinueMusic")
		elseif timer == 2106 then
			if string.sub(GetAlMightyGlobal("ProgresoProgresivamenteProgresante2"), 7, 7) == "0" then
				SetAlMightyGlobal("ProgresoProgresivamenteProgresante2", string.sub(GetAlMightyGlobal("ProgresoProgresivamenteProgresante2"), 1, 6).."1")
				if GetAlMightyGlobal("ProgresoProgresivamenteProgresante2") == "1111111" then
					SetAlMightyGlobal("Archivimentazo", string.sub(GetAlMightyGlobal("Archivimentazo"), 1, 9).."1"..string.sub(GetAlMightyGlobal("Archivimentazo"), 11))
					Encounter["LogroDesbloqueado"] = {Timer = 0, Icon = 10}
				end
			end
			Encounter.Call("ResetAnim")
			Encounter.Call("GlowAlpha", 0.7)
			Encounter["progress"] = 7
			if GetAlMightyGlobal("Idioma") == "Spanish" then
				Encounter["enemies"][1].SetVar("currentdialogue", {"[noskip][font:papyrus][voice:papyrus][effect:none]REALMENTE NO\nTIENES CORAZÓN.",
																   "[noskip][font:papyrus][voice:papyrus][effect:none]INTENTANDO\nESCAPAR DE\nALGUIEN QUE\nSOLO QUIERE\nAYUDARTE...",
																   "[noskip][font:papyrus][waitall:2][voice:papyrus][effect:none]POR QUÉ?[w:20][waitall:1]\nCUÁL ES EL\nPUNTO???"})
			elseif GetAlMightyGlobal("Idioma") == "Russian" then
				Encounter["enemies"][1].SetVar("currentdialogue", {"[noskip][font:papyrus][voice:papyrus][effect:none]ТЫ ПРАВДА\nБЕССЕРДЕЧНЫЙ.",
																   "[noskip][font:papyrus][voice:papyrus][effect:none]ПЫТАЕШЬСЯ\nСБЕЖАТЬ ОТ ТОГО[w:5],\nКТО ХОЧЕТ ТЕБЕ\nПОМОЧЬ...",
																   "[noskip][font:papyrus][waitall:2][voice:papyrus][effect:none]ЗАЧЕМ?[w:20][waitall:1]\nВ ЧЁМ СМЫСЛ???"})
			else
				Encounter["enemies"][1].SetVar("currentdialogue", {"[noskip][font:papyrus][voice:papyrus][effect:none]YOU REALLY\nARE HEARTLESS.",
																   "[noskip][font:papyrus][voice:papyrus][effect:none]TRYING TO RUN\nAWAY FROM\nSOMEONE WHO\nWANTS TO HELP\nYOU...",
																   "[noskip][font:papyrus][waitall:2][voice:papyrus][effect:none]WHY?[w:20][waitall:1]\nWHAT'S THE\nPOINT???"})
			end
			Encounter.Call("PapsMoveTo", {320, 285})
			Frisk.Remove()
			Corredor.Remove()
			CorredorN.Remove()
			Black.Remove()
			BlackDefinitive.Remove()
			for i=1,#HuesosSuelo do
				local bullet = HuesosSuelo[i - tableaux]
				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
			tableaux = 0
			Player.sprite.alpha = 1
			State("ENEMYDIALOGUE")
		end
	end
	for i=1,#Pilares do
		Pilares[i].x = Pilares[i].GetVar("Datos") + 1*(Corredor.x - 1400)
	end
	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.sprite.color = {0,0,0}
		if shake then
			bullet.Misil.MoveToAbs(bullet.X + shake[1] - bullet.Scale.Current/2*math.sin(math.rad(bullet.Rotacion)), bullet.Y + shake[2] + bullet.Scale.Current/2*math.cos(math.rad(bullet.Rotacion)))
			if bullet.Head then
				bullet.Head.MoveToAbs(bullet.X + shake[1] - (bullet.Scale.Current + 2.5)*math.sin(math.rad(bullet.Rotacion)), bullet.Y + shake[2] + (bullet.Scale.Current + 2.5)*math.cos(math.rad(bullet.Rotacion)))
				bullet.Head.sprite.rotation = bullet.Rotacion
				bullet.Head.sprite.color = {0,0,0}
			end
			if bullet.Legs then
				bullet.Legs.MoveToAbs(bullet.X + shake[1] + (bullet.Scale.Current + 2.5)*math.sin(math.rad(bullet.Rotacion)), bullet.Y + shake[2] - (bullet.Scale.Current + 2.5)*math.cos(math.rad(bullet.Rotacion)))
				bullet.Legs.sprite.rotation = bullet.Rotacion + 180
				bullet.Legs.sprite.color = {0,0,0}
			end
		else
			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
				bullet.Head.sprite.color = {0,0,0}
			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
				bullet.Legs.sprite.color = {0,0,0}
			end
		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
end

function OnHit()
	
end