
include('shared.lua')

CreateConVar( "cl_drawthrusterseffects", "1" )


local Thruster_Sound 	= Sound( "PhysicsCannister.ThrusterLoop" )
local matHeatWave		= Material( "sprites/heatwave" )
local matFire			= Material( "effects/fire_cloud1" )
local matPlasma			= Material( "effects/strider_muzzle" )

/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

	self.ShouldDraw = 1
	self.NextSmokeEffect = 0
	self.PlayingSound = false

end


/*---------------------------------------------------------
   Name: Draw
---------------------------------------------------------*/
function ENT:Draw()

	self.BaseClass.Draw( self )
	
	if ( self.ShouldDraw == 0 ) then return end
		
	if ( !self:IsOn() ) then return end
	if ( self:GetEffect() == "none" ) then return end
	
	local EffectThink = self[ "EffectDraw_"..self:GetEffect() ]
	if ( EffectThink ) then EffectThink( self ) end
		
end


/*---------------------------------------------------------
   Name: Think
   Desc: Client Think - called every frame
---------------------------------------------------------*/
function ENT:Think()

	self.ShouldDraw = GetConVarNumber( "cl_drawthrusterseffects" )
	

	
	local bDraw = true
	

	if ( self.ShouldDraw == 0 ) then bDraw = false end
	
	if ( !self:IsOn() ) then bDraw = false end
	if ( self:GetEffect() == "none" ) then bDraw = false end


	if ( !self.PlayingSound && bDraw ) then
		self.Entity:EmitSound( Thruster_Sound )
		self.PlayingSound = true
	elseif ( !bDraw && self.PlayingSound ) then
		self.Entity:StopSound( Thruster_Sound )
		self.PlayingSound = false
	end

	if ( !bDraw ) then return end
	
	local EffectThink = self[ "EffectThink_"..self:GetEffect() ]
	if ( EffectThink ) then EffectThink( self ) end

end

function ENT:OnRemove()

	self.Entity:StopSound( Thruster_Sound )

end


function ENT:EffectThink_fire()
end

function ENT:EffectDraw_fire()

	local vOffset = self.Entity:LocalToWorld( self:GetOffset() )
	local vNormal = (vOffset - self.Entity:GetPos()):GetNormalized()

	local scroll = CurTime() * -10
		
	render.SetMaterial( matFire )
	
	render.StartBeam( 3 )
		render.AddBeam( vOffset, 8, scroll, Color( 0, 0, 255, 128) )
		render.AddBeam( vOffset + vNormal * 60, 32, scroll + 1, Color( 255, 255, 255, 128) )
		render.AddBeam( vOffset + vNormal * 148, 32, scroll + 3, Color( 255, 255, 255, 0) )
	render.EndBeam()
	
	scroll = scroll * 0.5
	
	render.UpdateRefractTexture()
	render.SetMaterial( matHeatWave )
	render.StartBeam( 3 )
		render.AddBeam( vOffset, 8, scroll, Color( 0, 0, 255, 128) )
		render.AddBeam( vOffset + vNormal * 32, 32, scroll + 2, Color( 255, 255, 255, 255) )
		render.AddBeam( vOffset + vNormal * 128, 48, scroll + 5, Color( 0, 0, 0, 0) )
	render.EndBeam()
	
	
	scroll = scroll * 1.3
	render.SetMaterial( matFire )
	render.StartBeam( 3 )
		render.AddBeam( vOffset, 8, scroll, Color( 0, 0, 255, 128) )
		render.AddBeam( vOffset + vNormal * 60, 16, scroll + 1, Color( 255, 255, 255, 128) )
		render.AddBeam( vOffset + vNormal * 148, 16, scroll + 3, Color( 255, 255, 255, 0) )
	render.EndBeam()
	
end


function ENT:EffectDraw_plasma()

	local vOffset = self.Entity:LocalToWorld( self:GetOffset() )
	local vNormal = (vOffset - self.Entity:GetPos()):GetNormalized()

	local scroll = CurTime() * -20
		
	render.SetMaterial( matPlasma )
	
	scroll = scroll * 0.9
	
	render.StartBeam( 3 )
		render.AddBeam( vOffset, 16, scroll, Color( 0, 255, 255, 255) )
		render.AddBeam( vOffset + vNormal * 8, 16, scroll + 0.01, Color( 255, 255, 255, 255) )
		render.AddBeam( vOffset + vNormal * 64, 16, scroll + 0.02, Color( 0, 255, 255, 0) )
	render.EndBeam()
	
	scroll = scroll * 0.9
	
	render.StartBeam( 3 )
		render.AddBeam( vOffset, 16, scroll, Color( 0, 255, 255, 255) )
		render.AddBeam( vOffset + vNormal * 8, 16, scroll + 0.01, Color( 255, 255, 255, 255) )
		render.AddBeam( vOffset + vNormal * 64, 16, scroll + 0.02, Color( 0, 255, 255, 0) )
	render.EndBeam()
	
	scroll = scroll * 0.9
	
	render.StartBeam( 3 )
		render.AddBeam( vOffset, 16, scroll, Color( 0, 255, 255, 255) )
		render.AddBeam( vOffset + vNormal * 8, 16, scroll + 0.01, Color( 255, 255, 255, 255) )
		render.AddBeam( vOffset + vNormal * 64, 16, scroll + 0.02, Color( 0, 255, 255, 0) )
	render.EndBeam()
	
end
