#include "script/common.lua"

function init()
	lights = FindLights()
	bodies = FindBodies()
	target = FindBody("target")
	offsets = {}
	for i=1, #lights do
		offsets[i] = math.random()
	end
	done = false
end

function tick(dt)
	if not done then
		for i=1, #lights do
			local _,f = math.modf(GetTime()+offsets[i])
			SetLightEnabled(lights[i], f < 0.5)
		end
		local dist = VecLength(VecSub(GetCameraTransform().pos, GetBodyTransform(target).pos))
		local a = clamp(10 - dist, 0.0, 1.0)
		if a > 0.0 then
			for i=1, #bodies do
				DrawBodyOutline(bodies[i], 1, 1, 1, 0.5*a)
			end
		end
		if not IsHandleValid(target) then
			for i=1, #bodies do
				Delete(bodies[i])
			end
			done = true
		end
	end
end

