flickerSpeed = GetFloatParam("flickerspeed", 2.0)
maxEmissive = GetIntParam("emissive",4)
done = false

function init()
	mLight = FindShape("flicker")
	mLightTimer = 0.0
	mLightEmissive = 0
	timer = 0.0
	sparkSound = LoadSound("MOD/snd/map/light_spark.ogg", 5.0)
end

function tick(dt)
	if done then 
		return
	end
	if IsShapeBroken(mLight) then
		SetShapeEmissiveScale(mLight, 0)
		done = true	
		return	
	end

	timer = timer + dt
	if timer - mLightTimer > 0 then
		mLightEmissiveNew = math.random(1,10)/10.0 * maxEmissive
		if mLightEmissiveNew > mLightEmissive then
			mLightEmissive = mLightEmissiveNew
			PlaySound(sparkSound, GetShapeWorldTransform(mLight).pos, 0.6)
		end
		mLightTimer = timer + (math.random(1,flickerSpeed)/10);
	end

	mLightEmissive = mLightEmissive - dt * 4.0

	if mLightEmissive <= 0 then
		mLightEmissive = 0
	end

	SetShapeEmissiveScale(mLight, mLightEmissive)
end
