

CreateConVar( "cl_drawthrusterseffects", "1" )

ENT.Spawnable			= false
ENT.AdminSpawnable		= false

local Thruster_Sound = "PhysicsCannister.ThrusterLoop"

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

	self.Refraction = Material( "sprites/heatwave" )
	self.Glow		= Material( "effects/muzzleflash4" )
	self.ShouldDraw = 1
	
	self.NextSmokeEffect = 0
	self.PlayingSound = false

end


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

	self.Entity:DrawModel()
	
	if ( self.ShouldDraw == 0 ) then return end
		
	// Not active
	if ( self.Entity:GetNetworkedInt( 0 ) == 0 ) then return end
	// No effects
	if ( self.Entity:GetNetworkedInt( 1 ) == self.EFFECT_NONE ) then return end

	local vOffset = self.Entity:GetNetworkedVector( 0 )
	vOffset = self.Entity:LocalToWorld( vOffset )
	local ThrusterWorldForce = (vOffset - self.Entity:GetPos()):GetNormalized()

	self.Glow:SetMaterialInt( "$spriteRenderMode", RENDERMODE_TRANSADD )
	render.SetMaterial( self.Glow )	
	render.DrawSprite( vOffset, 64, 64, Color(255, mathx.Rand( 150, 230 ), 2, mathx.Rand( 50, 120 )) )
	render.DrawSprite( vOffset, 16, 16, Color(255, 255, 255, 255) )
	render.DrawSprite( vOffset, 13, 13, Color(255, 255, 255, 255) )

	//render.UpdateRefractTexture()
	render.SetMaterial( self.Glow )	
	
	
	i = 2
	while ( i < 3 ) do
	
		local dist = math.abs( math.sin( CurTime() * i * 100 )) * 84
		local size = 48 - mathx.Clamp( dist, 16, 48 )
		render.DrawSprite( vOffset + ThrusterWorldForce * dist, size, size, Color(255, 255, 255, 50) )
		i = i + 0.1
		
	end
	
	render.UpdateRefractTexture()
	render.SetMaterial( self.Refraction )	
	
	i = 2
	while ( i < 3 ) do
	
		local dist = math.abs( math.sin( CurTime() * i * 50 )) * 64
		local size = mathx.Clamp( dist, 8, 32 )
		render.DrawSprite( vOffset + ThrusterWorldForce * dist, size, size, color_white )
		i = i + 0.1
		
	end
		
end


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

	self.ShouldDraw = GetConVarNumber( "cl_drawthrusterseffects" )
	
	local vOffset = self.Entity:GetNetworkedVector( 0 )
	vOffset = self.Entity:LocalToWorld( vOffset )
	local ThrusterWorldForce = (vOffset - self.Entity:GetPos()):GetNormalized()
	
	local bDraw = true
	

	if ( self.ShouldDraw == 0 ) then bDraw = false end
	
	// Not active
	if ( self.Entity:GetNetworkedInt( 0 ) == 0 ) then bDraw = false end
	// No effects
	if ( self.Entity:GetNetworkedInt( 1 ) == self.EFFECT_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
	
	if (self.NextSmokeEffect < CurTime()) then
	
		self.NextSmokeEffect = CurTime() + mathx.Rand( 0.01, 0.03 )

		local effectdata = EffectData()
			effectdata:SetOrigin( self.Entity:GetPos() )
			effectdata:SetNormal( ThrusterWorldForce * 130 )
			effectdata:SetScale( 1 )
		util.Effect( "WheelDust", effectdata )
	
	end

end

function ENT:OnRemove()

	self.Entity:StopSound( Thruster_Sound )

end

include('shared.lua')