
local matLight 		= Material( "sprites/light_ignorez" )
local matBeam		= Material( "effects/lamp_beam" )

ENT.RenderGroup 	= RENDERGROUP_BOTH

include('shared.lua')

function ENT:Initialize()

	self.PixVis = util.GetPixelVisibleHandle()
	
end

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

	self.BaseClass.Draw( self, true )
	
end

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

	local dlight = DynamicLight( self:EntIndex() )
	if ( dlight ) then
		local r, g, b, a = self:GetColor()
		dlight.Pos = self:GetPos()
		dlight.r = r
		dlight.g = g
		dlight.b = b
		dlight.Brightness = self:GetBrightness()
		dlight.Decay = self:GetLightSize() * 5
		dlight.Size = self:GetLightSize()
		dlight.DieTime = CurTime() + 1
	end
	
end

/*---------------------------------------------------------
   Name: DrawTranslucent
   Desc: Draw translucent
---------------------------------------------------------*/
function ENT:DrawTranslucent()
	
	self.BaseClass.DrawTranslucent( self, true )
	
	local ViewNormal = self:GetPos() - EyePos()
	local Distance = ViewNormal:Length()
	ViewNormal:Normalize()

	local r, g, b, a = self:GetColor()
	local LightPos = self:GetPos()
	
	render.SetMaterial( matLight )
	local Visibile	= util.PixelVisible( LightPos, 8, self.PixVis )	
	
	if (!Visibile) then return end
	
	local Size = math.Clamp( Distance * Visibile * 2, 64, 512 )
	
	Distance = math.Clamp( Distance, 32, 800 )
	local Alpha = math.Clamp( (1000 - Distance) * Visibile, 0, 100 )
	local Col = Color( r, g, b, Alpha )
	
	render.DrawSprite( LightPos, Size * 0.5, Size * 0.5, Col, Visibile )
	
	render.DrawSprite( LightPos, 16, 16, Color(255, 255, 255, Alpha), Visibile )
	render.DrawSprite( LightPos, 16, 16, Color(255, 255, 255, Alpha), Visibile )
	render.DrawSprite( LightPos, 16, 16, Color(255, 255, 255, Alpha), Visibile )
	render.DrawSprite( LightPos, 16, 16, Color(255, 255, 255, Alpha), Visibile )

	
end

/*---------------------------------------------------------
   Overridden because I want to show the name of the 
   player that spawned it..
---------------------------------------------------------*/
function ENT:GetOverlayText()

	return self:GetPlayerName()	
	
end
