#include "common.lua"

timer = nil
trigger = nil
body = nil
explosions = nil
fires = nil
sound = nil
transform = nil

shapes = nil
	
emissive = nil
	
smokeTimer = nil
smokePos = nil
	
done = false

function init()
	timer = 0
	trigger = FindTrigger("lightning")
	body = FindBody("lightning")
	explosions = FindLocations("explosion")
	fires = FindLocations("fire")
	sound = LoadSound("thunder-strike.ogg")
	transform = GetBodyTransform(body)

	shapes = GetBodyShapes(body)
	
	--Move lightning out of sight
	emissive = 0
	SetBodyTransform(body, Transform(Vec(0,10000,0)))
	setEmissive(0)
	
	smokeTimer = 0
	smokePos = Vec()
	
	done = false
end


function setEmissive(value)
	for i=1,#shapes do
		SetShapeEmissiveScale(shapes[i], value)
	end
end

function tick(dt)
	if not done and IsPointInTrigger(trigger, GetPlayerPos()) then
		timer = 0.7
		SetBodyTransform(body, transform)
		emissive=1
		
		smokePos = transform.pos
		smokeTimer = 2

		for i=1, #explosions do
			local t = GetLocationTransform(explosions[i])
			Explosion(t.pos, 1.5)
		end
		for i=1, #fires do
			local t = GetLocationTransform(fires[i])
			SpawnFire(t.pos)
		end
		PlaySound(sound)
		done = true
	else
		if timer > 0 then
			timer = timer - dt
			if timer <= 0 then
				SetBodyTransform(body, Transform(Vec(0,10000,0)))
				setEmissive(0)
			else
				emissive = clamp(emissive + math.random(-10,10)*0.1, 0.1, 1.0)
				setEmissive(emissive)
			end
		end
	end
	
	if smokeTimer > 0 then
		local size = 2.0
		ParticleReset()
		ParticleType("smoke")
		ParticleDrag(0)
		ParticleGravity(-2, -4)
		ParticleColor(1, 1, 1)
		ParticleAlpha(0.5, 0.0, "linear", 0.05)
		ParticleRadius(0.5 * size, size)
		ParticleCollide(0, 1)

		SpawnParticle(smokePos, Vec(0, 1.0+math.random(1,10)*0.1, 0), 4.0)
		smokeTimer = smokeTimer - dt
	end
end

