
#include "baloon.lua"

pStartTime = GetFloatParam("starttime", 15)
pSpawnTime = GetFloatParam("spawntime", 10)

function init()
	baloonInit()
	spawnPos = GetBodyTransform(baloon.body).pos
	startPos = GetLocationTransform(FindLocation("start")).pos
	endPos = GetLocationTransform(FindLocation("end")).pos
	escapeVehicle = FindBody("escapevehicle", true)
end

local spawnCompleted = false

function tick(dt)
	if GetBool("level.alarm") then

		local startPoint = spawnPos
		local endPoint = startPos
		if spawnCompleted then
			startPoint = startPos
			endPoint = endPos
		end

		local timer = GetFloat("level.alarmtimer")
		if timer <= pStartTime then

			local pos
			if spawnCompleted == false then
				local t = 1
				if pStartTime > 0 then
					t = 1-((timer-(pStartTime-pSpawnTime)) / pSpawnTime)
					t = math.abs(t)
				end
				pos = VecLerp(startPoint, endPoint, t)
				if (pStartTime - timer) > pSpawnTime then
					spawnCompleted = true
					--pStartTime = pStartTime - pSpawnTime
				end
			else
				local t = 0
				if pStartTime > 0 then
					t = 1-(timer/(pStartTime - pSpawnTime))
				end
				pos = VecLerp(startPoint, endPoint, t)
			end

			local lookAt = GetPlayerPos()
			if escapeVehicle ~= 0 then
				lookAt = GetBodyTransform(escapeVehicle).pos
			end

			baloonTick(dt, pos, lookAt, lookAt)
		end
	end
end

function update(dt)
	SetBodyDynamic(baloon.basket, true)
	SetBodyActive(baloon.basket, true)
end
