
local tMats = { };
tMats.Glow1 = Material( "sprites/light_glow02" );
tMats.Glow2 = Material( "sprites/flare1" );

for _, mat in pairs( tMats ) do

	mat:SetMaterialInt( "$spriterendermode", 9 );
	mat:SetMaterialInt( "$ignorez", 1 );
	mat:SetMaterialInt( "$illumfactor", 8 );
	
end

local SmokeParticleUpdate = function( particle )

	if( particle:GetStartAlpha() == 0 and particle:GetLifeTime() >= 0.5 * particle:GetDieTime() ) then
	
		particle:SetStartAlpha( particle:GetEndAlpha() );
		particle:SetEndAlpha( 0 );
		particle:SetNextThink( -1 );
		
	else
	
		particle:SetNextThink( CurTime() + 0.1 );
		
	end

	return particle;

end


function EFFECT:Init( data )

	self.Scale = 1
	self.ScaleSlow = math.sqrt(self.Scale);
	self.ScaleSlowest = math.sqrt(self.ScaleSlow);
	self.Normal = data:GetNormal()
	self.RightAngle = self.Normal:Angle():Right():Angle()
	self.Position = data:GetOrigin() - 12*self.Normal
	self.Position2 = self.Position + self.Scale*64*self.Normal

	local CurrentTime = CurTime()
	self.Duration = 0.5 * self.Scale 
	self.KillTime = CurrentTime + self.Duration
	self.GlowAlpha = 200
	self.GlowSize = 100 * self.Scale
	self.FlashAlpha = 100
	self.FlashSize = 0

	local emitter = ParticleEmitter(self.Position)

	--Smoke Plumes
	for i=1,math.ceil(self.Scale*60) do
		
		local vecang = VectorRand()*16
		local spawnpos = self.Position 	
		local velocity = math.Rand(50,300)*vecang			
		local particle = emitter:Add("modulus/particles/Smoke6",spawnpos - vecang*9*k)
		local dietime = math.Rand(20.5,20.9)*self.Scale
		particle:SetVelocity(velocity*self.Scale)
		particle:SetDieTime(dietime)
		particle:SetAirResistance(150)
		particle:SetStartAlpha(100)
		particle:SetStartSize(math.Rand(300,300)*self.ScaleSlow)
		particle:SetEndSize(math.Rand(900,900)*self.ScaleSlow)
		particle:SetRoll(math.Rand(10,10))
		particle:SetRollDelta(0.6*math.random(-1,1))
		particle:SetColor(200,200,200)
		
	end

	--dirt
	for i = 1, math.ceil( self.Scale*60 ) do
		
		local vecang = ( self.Normal + VectorRand() * math.Rand( 0, 3 ) ):GetNormalized();
		local particle = emitter:Add( "modulus/particles/Smoke6", self.Position + vecang * 64 * self.Scale );
		local size = math.Rand( 500, 1000 );
		particle:SetVelocity( vecang * math.Rand( 600, 1100 ) * self.ScaleSlow );
		particle:SetGravity(Vector(0,0,-600));
		particle:SetAirResistance(1);
		particle:SetDieTime(math.Rand(20.5,20.9)*self.ScaleSlow);
		particle:SetStartAlpha(100);
		particle:SetEndAlpha(0);
		particle:SetStartSize(size);
		particle:SetEndSize(size);
		particle:SetRoll(math.Rand(10,10));
		particle:SetRollDelta(0.6*math.random(-1,1));
		particle:SetCollide(true);
		particle:SetColor(200,200,200);
		
	end

	emitter:Finish();
	
	surface.PlaySound( "ambient/explosions/explode_8.wav" );
	self.Entity:EmitSound( "ambient/explosions/explode_4.wav" );

end


--THINK
-- Returning false makes the entity die
function EFFECT:Think()
	local TimeLeft = self.KillTime - CurTime()
	local TimeScale = TimeLeft/self.Duration
	local FTime = FrameTime()
	if TimeLeft > 0 then 

		self.FlashAlpha = self.FlashAlpha - 200*FTime
		self.FlashSize = self.FlashSize + 60000*FTime
		
		self.GlowAlpha = 200*TimeScale
		self.GlowSize = TimeLeft*self.Scale

		return true
	else
		return false	
	end
end



-- Draw the effect
function EFFECT:Render()

	--base glow
	render.SetMaterial( tMats.Glow1 );
	render.DrawSprite( self.Position2, 7000 * self.GlowSize, 5500 * self.GlowSize, Color( 255, 240, 220, self.GlowAlpha ) );

	--blinding flash
	if( self.FlashAlpha > 0 ) then
	
		render.SetMaterial( tMats.Glow2 );
		render.DrawSprite( self.Position2, self.FlashSize, self.FlashSize, Color( 255, 245, 215, self.FlashAlpha ) );
		
	end


end



