TIME = 6
leakTime = 0

function init()
	shape = FindShape("")
	snd = LoadLoop("firehydrant-loop.ogg")
end


function rnd(mi, ma)
	return math.random()*(ma-mi)+mi
end


function tick(dt)
	if leakTime < TIME then
		if IsShapeBroken(shape) then
			if not emitPos then
				emitPos = TransformToParentPoint(GetShapeWorldTransform(shape), Vec(0.3, 0.3, 0.2))
			end
			leakTime = leakTime + dt
			local q = 1.0-leakTime/TIME
			local v = q*8.0+2.0
			local r = q*0.4+0.1
			local pc = math.ceil(q*5)+3
			ParticleType("plain")
			ParticleTile(1)
			ParticleColor(0.9, 0.95, 1.0)
			ParticleAlpha(1, 0)
			ParticleGravity(-10)
			ParticleStretch(q, 0)
			ParticleCollide(0, 1, "constant", 0.2, 0.1)
			local ep = emitPos
			if not IsBodyDynamic(GetShapeBody(shape)) then
				local hit,cp = GetShapeClosestPoint(shape, VecAdd(emitPos, Vec(0,1.5,0)))
				if hit then
					ep = cp
				end
			end
			for i=1, pc do
				ParticleRadius(r*0.5, r*1.5)
				local pos = ep
				local vel = VecScale(Vec(rnd(-0.15, 0.15), 1, rnd(-0.15, 0.15)), rnd(0.7, 1.0)*v)
				SpawnParticle(pos, vel, q + rnd(1, 2.0))
			end
			PlayLoop(snd, ep, q)
		end
	end
end
