-------------------------------
-- SSTRP
-- Author: MNWRP
--
-- cl_hud.lua 
-- General HUD stuff.
-------------------------------

LocalPlayer( ).MyModel = "" -- Has to be blank for the initial value, so it will create a spawnicon in the first place.

surface.CreateFont( "ChatFont", 22, 100, true, false, "PlInfoFont" );
surface.CreateFont( "TargetID", 14, 500, true, false, "TargetFont" );

local function DrawTime( )

	draw.DrawText( GetGlobalString( "time" ), "PlInfoFont", 10, 10, Color( 255,255,255,255 ), 0 );
	
end

function DrawTargetInfo( )
	
	local tr = LocalPlayer( ):GetEyeTrace( )
	
	if( !tr.HitNonWorld ) then return; end
	
	if( tr.Entity:GetClass( ) == "item_prop" and tr.Entity:GetPos( ):Distance( LocalPlayer( ):GetPos( ) ) < 100 ) then
	
		local screenpos = tr.Entity:GetPos( ):ToScreen( )
		draw.DrawText( tr.Entity:GetNWString( "Name" ), "ChatFont", screenpos.x + 2, screenpos.y + 2, Color( 0, 0, 0, 255 ), 1 );	
		draw.DrawText( tr.Entity:GetNWString( "Name" ), "ChatFont", screenpos.x, screenpos.y, Color( 255, 255, 255, 255 ), 1 );
		draw.DrawText( tr.Entity:GetNWString( "Description" ), "ChatFont", screenpos.x + 2, screenpos.y + 22, Color( 0, 0, 0, 255 ), 1 );	
		draw.DrawText( tr.Entity:GetNWString( "Description" ), "ChatFont", screenpos.x, screenpos.y + 20, Color( 255, 255, 255, 255 ), 1 );

	end
	
end
		
function GM:HUDShouldDraw( name )

	if( LocalPlayer( ):GetNWInt( "charactercreate" ) == 1 or LocalPlayer( ):GetNWInt( "charactercreate" ) == nil ) then return false; end
	
	local nodraw = 
	{ 
	
		"CHudHealth",
		"CHudAmmo",
		"CHudSecondaryAmmo",
		"CHudBattery",
	
	 }
	
	for k, v in pairs( nodraw ) do
	
		if( name == v ) then return false; end
	
	end
	
	return true;

end

function DrawDeath( )
	if( not LocalPlayer():Alive() ) then
	--draw.RoundedBox( 0, 0, 0, ScrW(), ScrH(), Color( 0, 0, 0, 255 ) )
	draw.DrawText( "You will respawn in 100 seconds", "ChatFont", ScrW( ) / 2 - 90, 25 + 10, Color( 255,255,255,255 ), 0 );
	draw.DrawText( "Type !acd to respawn manually", "ChatFont", ScrW( ) / 2 - 93, 25 - 5, Color( 255,255,255,255 ), 0 );
	
	end
end

function DrawPlayerInfo( )

	for k, v in pairs( player.GetAll( ) ) do	
		if( v != LocalPlayer( ) ) then
			if( v:Alive( ) ) then
				local tr = LocalPlayer( ):GetEyeTrace( )
				if( tr.HitWorld ) then return end

				local alpha = 0
				local position = v:GetPos( )
				local position = Vector( position.x, position.y, position.z + 75 )
				local screenpos = position:ToScreen( )
				local dist = position:Distance( LocalPlayer( ):GetPos( ) )
				local dist = dist / 2
				local dist = math.floor( dist )
				
				if( dist > 100 ) then
				
					alpha = 255 - ( dist - 100 )
					
				else
				
					alpha = 255
					
				end
				
				if( alpha > 255 ) then
				
					alpha = 255
					
				elseif( alpha < 0 ) then
				
					alpha = 0
					
				end
			
				if( SeeAll )then
					draw.DrawText( v:Nick( ), "TargetFont", screenpos.x, screenpos.y, Color( 0, 0, 0, 255 ), 1 )
				else
					draw.DrawText( v:Nick( ), "TargetFont", screenpos.x, screenpos.y, Color( 33, 0, 255, alpha ), 1 )
				end
				draw.DrawText( v:Nick( ), "TargetFont", screenpos.x, screenpos.y, Color( 33, 0, 255, alpha ), 1 )
				draw.DrawText( team.GetName( v:Team( ) ), "TargetFont", screenpos.x, screenpos.y + 10, Color( 255, 255, 255, alpha ), 1 )
				draw.DrawText( v:GetNWString( "title" ), "TargetFont", screenpos.x, screenpos.y + 20, Color( 255, 255, 255, alpha ), 1 )
				
				if( v:GetNWInt( "chatopen" ) == 1 ) then
				
					draw.DrawText( "Typing..", "ChatFont", screenpos.x, screenpos.y - 50, Color( 255, 255, 255, alpha ), 1 )
					
				end
				
			end
			
		end
		
	end
	
end


function DrawHudInfo()
if LocalPlayer():GetActiveWeapon() == NULL or LocalPlayer():GetActiveWeapon() == "Camera" then return end  
	surface.CreateFont( "TargetID", 26, 800, true, false, "BigTargetID" );
	surface.CreateFont( "TargetID", 14, 800, true, false, "SmallerTargetID" );

		local lolhealth = LocalPlayer():Health()
		local lolarmor = LocalPlayer():Armor()
		local realhealth = LocalPlayer():Health()
		local realarmor = LocalPlayer():Armor()
		if( lolarmor > 100 ) then
			lolarmor = 100 
		end
		if( lolhealth > 100 ) then
			lolarmor = 100
		end
		draw.RoundedBox( 0, 0, 0, ScrW(), 40, Color(10,10,10,50))
		draw.RoundedBox( 0, 25+7, 5, 200, 10, Color(150,150,150,255))
		draw.RoundedBox( 0, 25+7, 5, lolhealth*2, 6, Color(255,0,0,255))
		draw.RoundedBox( 0, 25+7, 11, lolhealth*2, 4, Color(175,0,0,255))
		draw.RoundedBox( 0, 25+55, 5, 2, 10, Color(10,10,10,255))
		draw.RoundedBox( 0, 25+105, 5, 2, 10, Color(10,10,10,255))
		draw.RoundedBox( 0, 25+155, 5, 2, 10, Color(10,10,10,255))
		draw.RoundedBox( 0, 25+205, 5, 2, 10, Color(10,10,10,255))
		draw.RoundedBox( 0, 25+5, 5, 2, 10, Color(10,10,10,255))
		draw.RoundedBox( 0, 25+5, 15, 202, 2, Color(10,10,10,255))
		draw.RoundedBox( 0, 25+5, 3, 202, 2, Color(10,10,10,255))
		
		if( lolhealth > 25 ) then
		draw.DrawTextGlowing( "Health: " .. realhealth .. "", "SmallerTargetID", 25+215, 2, Color(255,255,255,255), 0, nil, 6, Color(0,0,0,215))
		end
		if( lolhealth < 25 ) then
		draw.DrawTextGlowing( "CRITICAL", "SmallerTargetID", 25+215, 2, Color(255,0,0,255), 0, nil, 6, Color(100,0,0,215))
		end
		
		
		draw.RoundedBox( 0, 25+7, 5+15, 200, 10, Color(150,150,150,255))
		draw.RoundedBox( 0, 25+7, 5+15, lolarmor*2, 6, Color(0,0,255,255))
		draw.RoundedBox( 0, 25+7, 11+15, lolarmor*2, 4, Color(0,0,175,255))
		draw.RoundedBox( 0, 25+55, 5+15, 2, 10, Color(10,10,10,255))
		draw.RoundedBox( 0, 25+105, 5+15, 2, 10, Color(10,10,10,255))
		draw.RoundedBox( 0, 25+155, 5+15, 2, 10, Color(10,10,10,255))
		draw.RoundedBox( 0, 25+205, 5+15, 2, 10, Color(10,10,10,255))
		draw.RoundedBox( 0, 25+5, 5+15, 2, 10, Color(10,10,10,255))
		draw.RoundedBox( 0, 25+5, 15+15, 202, 2, Color(10,10,10,255))
		draw.RoundedBox( 0, 25+5, 3+15, 202, 2, Color(10,10,10,255))
		draw.DrawTextGlowing( "Armor: " .. realarmor .. "", "SmallerTargetID", 25+215, 2+15, Color(255,255,255,255), 0, nil, 6, Color(0,0,0,215))
		
		local clip = LocalPlayer():GetActiveWeapon():Clip1()
		local reserve = LocalPlayer():GetAmmoCount(LocalPlayer():GetActiveWeapon():GetPrimaryAmmoType())
		draw.DrawTextGlowing( clip .. " / " .. reserve, "SmallerTargetID", 350, 17, Color(255,255,255,255), 0, nil, 6, Color(10,10,10,215))
		draw.DrawTextGlowing( "" .. LocalPlayer():GetActiveWeapon():GetPrintName() ..  "", "SmallerTargetID", 350, 2, Color(255,255,255,255), 0, nil, 6, Color(10,10,10,215))
		draw.DrawTextGlowing( LocalPlayer():GetNWString( "title" ), "SmallerTargetID", 700, 2, Color(255,255,255,255), 0, nil, 6, Color(0,0,0,215))
		draw.DrawTextGlowing( LocalPlayer():GetNWString( "money" ) .. " credits", "SmallerTargetID", 700, 17, Color( 255, 255, 255, 255 ), 0, nil, 6, Color( 0, 0, 0, 200 ) )
	
end

 function draw.DrawTextGlowing(text, font, x, y, colour, xalign, yalign, passes, outlinecolour) 
   
 	for _x=-1, 1 do 
 		for _y=-1, 1 do 
 			for n = 1, passes do
 				draw.DrawText(text, font, x + (_x*(n/2)), y + (_y*(n/2)), Color( outlinecolour.r, outlinecolour.g, outlinecolour.b, math.Clamp( ( ( 1 / n ) * 255 ), 0, outlinecolour.a )), xalign, yalign) 
 			end
 		end 
 	end 
 	 
 	draw.DrawText(text, font, x, y, colour, xalign, yalign) 
 	 
 end 

function GM:HUDPaint( )
	DrawDeath( )
	--DrawTime( );
	DrawPlayerInfo( );
	DrawTargetInfo( );
	DrawHudInfo( );
end
