intervalactive = GetFloatParam("intervalactive",-1)
intervalpause = GetFloatParam("intervalpause",-1)

broken = nil

function init()
	origin = FindLocation("origin")
	direction = FindLocation("direction")

	particleshape = FindShape("particleshape")

	interval = 0
	pause = false

	broken = false
end


--Helper to return a random vector of particular length
function rndVec(length)
	local v = VecNormalize(Vec(math.random(-100,100), math.random(-100,100), math.random(-100,100)))
	return VecScale(v, length)	
end


--Helper to return a random number in range mi to ma
function rnd(mi, ma)
	return math.random(1000)/1000*(ma-mi) + mi
end


--Update function handles smoke emission
--It is important to put it in update and not tick for constant emission rate
function update(dt)
	
	if broken then		
		return
	end

	local t = GetLocationTransform(origin)
	local dir_t = GetLocationTransform(direction)

	if intervalactive ~= -1 then
		interval = interval + dt

		if pause and interval > intervalpause then
			pause = not pause
			interval = 0
		end
		if not pause and interval > intervalactive then
			pause = not pause
			interval = 0
		end

		if pause then
			return
		end
	end

	if particleshape ~= 0 then
		if not broken and IsShapeBroken(particleshape) then
			broken = true
		end
	end

	local pos = t.pos
	local dir = VecSub(dir_t.pos,t.pos)


	-- THINGS YOU CAN TWEAK

	-- radius, particle spawn randomness from origin in meters
	local radius = .35
	-- life, particle lifetime in seconds
	local life = rnd(1,3)
	
	-- amount of particles emitted every frame
	local count = 3
	-- velocity of the particles
	local vel = 1
	-- particle drag
	local drag = 0.5
	-- particle gravity
	local gravity = 0.5
	
	-- aplha of the particles, is also affected by the lifetime of the particle
	local alpha = 0.15

	-- Start values for the particle color
	local start_red = 0.6
	local start_green = 0.575
	local start_blue = 0.55
	
	-- End values for the particle color
	local end_red = 0.75
	local end_green = 0.75
	local end_blue = 0.75

	--Set up the particle state
	ParticleReset()
	ParticleType("smoke")
	ParticleRadius(radius)
	ParticleAlpha(alpha, alpha, "constant", 0.1/life, 0.5)	-- Ramp up fast, ramp down after 50%
	ParticleGravity(gravity * rnd(0.5, 1.5))				-- Slightly randomized gravity looks better
	ParticleDrag(drag)
	ParticleColor(start_red, start_green, start_blue, end_red, end_green, end_blue)			-- Animating color towards white
	
	--Emit particles
	for i=1, count do
		--Randomize position slightly. This is important when spawning multiple particles at the same time
		local p = VecAdd(pos, VecAdd(VecScale(dir, radius), rndVec(radius)))
		
--		DebugCross(p, 1,0,0)
		--Randomize velocity slightly
		local v = VecScale(VecAdd(dir, rndVec(0.2)), vel)
		
		--Randomize lifetime
		local l = rnd(life*0.5, life*1.5)

		--Spawn particle into the world
		SpawnParticle(p, v, l)
	end
end
