TheCodingBeast

#2 Basic HUD

Jul 21st, 2014
2,327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.52 KB | None | 0 0
  1. local function Base()
  2.  
  3.     draw.RoundedBox(6, ScrW() - 400 - 10, ScrH () - 200 - 10, 400, 200, Color(40, 40, 40, 250))
  4.  
  5.     draw.RoundedBox(6, ScrW() - 400, ScrH () - 200, 400-20, 30, Color(100,100,100,255))
  6.  
  7.     local DrawHealth = LocalPlayer():GetNWInt( "tcb_stamina" ) or 0
  8.     local EchoHealth = LocalPlayer():GetNWInt( "tcb_stamina" ) or 0
  9.  
  10.     if DrawHealth > 100 then DrawHealth = 100 end
  11.     if DrawHealth < 0 then DrawHealth = 0 end
  12.  
  13.     if DrawHealth != 0 then
  14.         draw.RoundedBox(6, ScrW() - 400 + 2, ScrH () - 200 + 2, (400-24) * DrawHealth / 100, 30-4, Color(255,0,0,255))
  15.     end
  16.  
  17.     draw.DrawText( "Health: "..EchoHealth, "Trebuchet24", ScrW() - 400 + 2, ScrH () - 200 + 2, Color(255,255,255,255))
  18.  
  19. end
  20.  
  21.  
  22. /*---------------------------------------------------------------------------
  23. HUD ConVars
  24. ---------------------------------------------------------------------------*/
  25. local ConVars = {}
  26. local HUDWidth
  27. local HUDHeight
  28.  
  29. local Color = Color
  30. local cvars = cvars
  31. local DarkRP = DarkRP
  32. local CurTime = CurTime
  33. local draw = draw
  34. local GetConVar = GetConVar
  35. local IsValid = IsValid
  36. local Lerp = Lerp
  37. local localplayer
  38. local math = math
  39. local pairs = pairs
  40. local ScrW, ScrH = ScrW, ScrH
  41. local SortedPairs = SortedPairs
  42. local string = string
  43. local surface = surface
  44. local table = table
  45. local timer = timer
  46. local tostring = tostring
  47.  
  48. CreateClientConVar("weaponhud", 0, true, false)
  49.  
  50. local colors = {}
  51. colors.black = Color(0, 0, 0, 255)
  52. colors.blue = Color(0, 0, 255, 255)
  53. colors.brightred = Color(200, 30, 30, 255)
  54. colors.darkred = Color(0, 0, 70, 100)
  55. colors.darkblack = Color(0, 0, 0, 200)
  56. colors.gray1 = Color(0, 0, 0, 155)
  57. colors.gray2 = Color(51, 58, 51,100)
  58. colors.red = Color(255, 0, 0, 255)
  59. colors.white = Color(255, 255, 255, 255)
  60. colors.white1 = Color(255, 255, 255, 200)
  61.  
  62. local function ReloadConVars()
  63.     ConVars = {
  64.         background = {0,0,0,100},
  65.         Healthbackground = {0,0,0,200},
  66.         Healthforeground = {140,0,0,180},
  67.         HealthText = {255,255,255,200},
  68.         Job1 = {0,0,150,200},
  69.         Job2 = {0,0,0,255},
  70.         salary1 = {0,150,0,200},
  71.         salary2 = {0,0,0,255}
  72.     }
  73.  
  74.     for name, Colour in pairs(ConVars) do
  75.         ConVars[name] = {}
  76.         for num, rgb in SortedPairs(Colour) do
  77.             local CVar = GetConVar(name..num) or CreateClientConVar(name..num, rgb, true, false)
  78.             table.insert(ConVars[name], CVar:GetInt())
  79.  
  80.             if not cvars.GetConVarCallbacks(name..num, false) then
  81.                 cvars.AddChangeCallback(name..num, function() timer.Simple(0,ReloadConVars) end)
  82.             end
  83.         end
  84.         ConVars[name] = Color(unpack(ConVars[name]))
  85.     end
  86.  
  87.  
  88.     HUDWidth = (GetConVar("HudW") or  CreateClientConVar("HudW", 240, true, false)):GetInt()
  89.     HUDHeight = (GetConVar("HudH") or CreateClientConVar("HudH", 115, true, false)):GetInt()
  90.  
  91.     if not cvars.GetConVarCallbacks("HudW", false) and not cvars.GetConVarCallbacks("HudH", false) then
  92.         cvars.AddChangeCallback("HudW", function() timer.Simple(0,ReloadConVars) end)
  93.         cvars.AddChangeCallback("HudH", function() timer.Simple(0,ReloadConVars) end)
  94.     end
  95. end
  96. ReloadConVars()
  97.  
  98. local Scrw, Scrh, RelativeX, RelativeY
  99. /*---------------------------------------------------------------------------
  100. HUD Seperate Elements
  101. ---------------------------------------------------------------------------*/
  102.  
  103. local Page = Material("icon16/page_white_text.png")
  104. local function GunLicense()
  105.     if localplayer:getDarkRPVar("HasGunlicense") then
  106.         surface.SetMaterial(Page)
  107.         surface.SetDrawColor(255, 255, 255, 255)
  108.         surface.DrawTexturedRect(RelativeX + HUDWidth, ScrH() - 34, 32, 32)
  109.     end
  110. end
  111.  
  112. local function Agenda()
  113.     local ply = LocalPlayer()
  114.  
  115.     local agenda = ply:getAgendaTable()
  116.     if not agenda then return end
  117.  
  118.     draw.RoundedBox(10, 10, 10, 460, 110, colors.gray1)
  119.     draw.RoundedBox(10, 12, 12, 456, 106, colors.gray2)
  120.     draw.RoundedBox(10, 12, 12, 456, 20, colors.darkred)
  121.  
  122.     draw.DrawNonParsedText(agenda.Title, "DarkRPHUD1", 30, 12, colors.red, 0)
  123.  
  124.     local text = ply:getDarkRPVar("agenda") or ""
  125.  
  126.     text = text:gsub("//", "\n"):gsub("\\n", "\n")
  127.     text = DarkRP.textWrap(text, "DarkRPHUD1", 440)
  128.     draw.DrawNonParsedText(text, "DarkRPHUD1", 30, 35, colors.white, 0)
  129. end
  130.  
  131. local VoiceChatTexture = surface.GetTextureID("voice/icntlk_pl")
  132. local function DrawVoiceChat()
  133.     if localplayer.DRPIsTalking then
  134.         local chbxX, chboxY = chat.GetChatBoxPos()
  135.  
  136.         local Rotating = math.sin(CurTime()*3)
  137.         local backwards = 0
  138.         if Rotating < 0 then
  139.             Rotating = 1-(1+Rotating)
  140.             backwards = 180
  141.         end
  142.         surface.SetTexture(VoiceChatTexture)
  143.         surface.SetDrawColor(ConVars.Healthforeground)
  144.         surface.DrawTexturedRectRotated(ScrW() - 100, chboxY, Rotating*96, 96, backwards)
  145.     end
  146. end
  147.  
  148. CreateConVar("DarkRP_LockDown", 0, {FCVAR_REPLICATED, FCVAR_SERVER_CAN_EXECUTE})
  149. local function LockDown()
  150.     local chbxX, chboxY = chat.GetChatBoxPos()
  151.     if util.tobool(GetConVarNumber("DarkRP_LockDown")) then
  152.         local cin = (math.sin(CurTime()) + 1) / 2
  153.         local chatBoxSize = math.floor(ScrH() / 4)
  154.         draw.DrawNonParsedText(DarkRP.getPhrase("lockdown_started"), "ScoreboardSubtitle", chbxX, chboxY + chatBoxSize, Color(cin * 255, 0, 255 - (cin * 255), 255), TEXT_ALIGN_LEFT)
  155.     end
  156. end
  157.  
  158. local Arrested = function() end
  159.  
  160. usermessage.Hook("GotArrested", function(msg)
  161.     local StartArrested = CurTime()
  162.     local ArrestedUntil = msg:ReadFloat()
  163.  
  164.     Arrested = function()
  165.         if CurTime() - StartArrested <= ArrestedUntil and localplayer:getDarkRPVar("Arrested") then
  166.         draw.DrawNonParsedText(DarkRP.getPhrase("youre_arrested", math.ceil(ArrestedUntil - (CurTime() - StartArrested))), "DarkRPHUD1", ScrW()/2, ScrH() - ScrH()/12, colors.white, 1)
  167.         elseif not localplayer:getDarkRPVar("Arrested") then
  168.             Arrested = function() end
  169.         end
  170.     end
  171. end)
  172.  
  173. local AdminTell = function() end
  174.  
  175. usermessage.Hook("AdminTell", function(msg)
  176.     timer.Destroy("DarkRP_AdminTell")
  177.     local Message = msg:ReadString()
  178.  
  179.     AdminTell = function()
  180.         draw.RoundedBox(4, 10, 10, ScrW() - 20, 100, colors.darkblack)
  181.         draw.DrawNonParsedText(DarkRP.getPhrase("listen_up"), "GModToolName", ScrW() / 2 + 10, 10, colors.white, 1)
  182.         draw.DrawNonParsedText(Message, "ChatFont", ScrW() / 2 + 10, 80, colors.brightred, 1)
  183.     end
  184.  
  185.     timer.Create("DarkRP_AdminTell", 10, 1, function()
  186.         AdminTell = function() end
  187.     end)
  188. end)
  189.  
  190. /*---------------------------------------------------------------------------
  191. Entity HUDPaint things
  192. ---------------------------------------------------------------------------*/
  193. local function DrawPlayerInfo(ply)
  194.     local pos = ply:EyePos()
  195.  
  196.     pos.z = pos.z + 10 -- The position we want is a bit above the position of the eyes
  197.     pos = pos:ToScreen()
  198.     pos.y = pos.y - 50 -- Move the text up a few pixels to compensate for the height of the text
  199.  
  200.     if GAMEMODE.Config.showname and not ply:getDarkRPVar("wanted") then
  201.         draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x + 1, pos.y + 1, colors.black, 1)
  202.         draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x, pos.y, team.GetColor(ply:Team()), 1)
  203.     end
  204.  
  205.     if GAMEMODE.Config.showhealth and not ply:getDarkRPVar("wanted") then
  206.         draw.DrawNonParsedText(DarkRP.getPhrase("health", ply:Health()), "DarkRPHUD2", pos.x + 1, pos.y + 21, colors.black, 1)
  207.         draw.DrawNonParsedText(DarkRP.getPhrase("health", ply:Health()), "DarkRPHUD2", pos.x, pos.y + 20, colors.white1, 1)
  208.     end
  209.  
  210.     if GAMEMODE.Config.showjob then
  211.         local teamname = team.GetName(ply:Team())
  212.         draw.DrawNonParsedText(ply:getDarkRPVar("job") or teamname, "DarkRPHUD2", pos.x + 1, pos.y + 41, colors.black, 1)
  213.         draw.DrawNonParsedText(ply:getDarkRPVar("job") or teamname, "DarkRPHUD2", pos.x, pos.y + 40, colors.white1, 1)
  214.     end
  215.  
  216.     if ply:getDarkRPVar("HasGunlicense") then
  217.         surface.SetMaterial(Page)
  218.         surface.SetDrawColor(255,255,255,255)
  219.         surface.DrawTexturedRect(pos.x-16, pos.y + 60, 32, 32)
  220.     end
  221. end
  222.  
  223. local function DrawWantedInfo(ply)
  224.     if not ply:Alive() then return end
  225.  
  226.     local pos = ply:EyePos()
  227.     if not pos:isInSight({localplayer, ply}) then return end
  228.  
  229.     pos.z = pos.z + 14
  230.     pos = pos:ToScreen()
  231.  
  232.     if GAMEMODE.Config.showname then
  233.         draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x + 1, pos.y + 1, colors.black, 1)
  234.         draw.DrawNonParsedText(ply:Nick(), "DarkRPHUD2", pos.x, pos.y, team.GetColor(ply:Team()), 1)
  235.     end
  236.  
  237.     local wantedText = DarkRP.getPhrase("wanted", tostring(ply:getDarkRPVar("wantedReason")))
  238.  
  239.     draw.DrawNonParsedText(wantedText, "DarkRPHUD2", pos.x, pos.y - 40, colors.white1, 1)
  240.     draw.DrawNonParsedText(wantedText, "DarkRPHUD2", pos.x + 1, pos.y - 41, colors.red, 1)
  241. end
  242.  
  243. /*---------------------------------------------------------------------------
  244. The Entity display: draw HUD information about entities
  245. ---------------------------------------------------------------------------*/
  246. local function DrawEntityDisplay()
  247.  
  248.     local shootPos = localplayer:GetShootPos()
  249.     local aimVec = localplayer:GetAimVector()
  250.  
  251.     for k, ply in pairs(players or player.GetAll()) do
  252.         if not ply:Alive() or ply == localplayer then continue end
  253.         local hisPos = ply:GetShootPos()
  254.         if ply:getDarkRPVar("wanted") then DrawWantedInfo(ply) end
  255.  
  256.         if GAMEMODE.Config.globalshow then
  257.             DrawPlayerInfo(ply)
  258.         -- Draw when you're (almost) looking at him
  259.         elseif not GAMEMODE.Config.globalshow and hisPos:DistToSqr(shootPos) < 160000 then
  260.             local pos = hisPos - shootPos
  261.             local unitPos = pos:GetNormalized()
  262.             if unitPos:Dot(aimVec) > 0.95 then
  263.                 local trace = util.QuickTrace(shootPos, pos, localplayer)
  264.                 if trace.Hit and trace.Entity ~= ply then return end
  265.                 DrawPlayerInfo(ply)
  266.             end
  267.         end
  268.     end
  269.  
  270.     local tr = localplayer:GetEyeTrace()
  271.  
  272.     if IsValid(tr.Entity) and tr.Entity:isKeysOwnable() and tr.Entity:GetPos():Distance(localplayer:GetPos()) < 200 then
  273.         tr.Entity:drawOwnableInfo()
  274.     end
  275. end
  276.  
  277. /*---------------------------------------------------------------------------
  278. Drawing death notices
  279. ---------------------------------------------------------------------------*/
  280. function GAMEMODE:DrawDeathNotice(x, y)
  281.     if not GAMEMODE.Config.showdeaths then return end
  282.     self.BaseClass:DrawDeathNotice(x, y)
  283. end
  284.  
  285. /*---------------------------------------------------------------------------
  286. Display notifications
  287. ---------------------------------------------------------------------------*/
  288. local function DisplayNotify(msg)
  289.     local txt = msg:ReadString()
  290.     GAMEMODE:AddNotify(txt, msg:ReadShort(), msg:ReadLong())
  291.     surface.PlaySound("buttons/lightswitch2.wav")
  292.  
  293.     -- Log to client console
  294.     print(txt)
  295. end
  296. usermessage.Hook("_Notify", DisplayNotify)
  297.  
  298. /*---------------------------------------------------------------------------
  299. Remove some elements from the HUD in favour of the DarkRP HUD
  300. ---------------------------------------------------------------------------*/
  301. function GAMEMODE:HUDShouldDraw(name)
  302.     if name == "CHudHealth" or
  303.         name == "CHudBattery" or
  304.         name == "CHudSuitPower" or
  305.         (HelpToggled and name == "CHudChat") then
  306.             return false
  307.     else
  308.         return true
  309.     end
  310. end
  311.  
  312. /*---------------------------------------------------------------------------
  313. Disable players' names popping up when looking at them
  314. ---------------------------------------------------------------------------*/
  315. function GAMEMODE:HUDDrawTargetID()
  316.    return false
  317. end
  318.  
  319. /*---------------------------------------------------------------------------
  320. Actual HUDPaint hook
  321. ---------------------------------------------------------------------------*/
  322. function DrawHUD()
  323.     localplayer = localplayer and IsValid(localplayer) and localplayer or LocalPlayer()
  324.     if not IsValid(localplayer) then return end
  325.    
  326.     -- Custom
  327.     Base()
  328.    
  329.     -- Default
  330.     GunLicense()
  331.     Agenda()
  332.     DrawVoiceChat()
  333.     LockDown()
  334.     Arrested()
  335.     AdminTell()
  336.     DrawEntityDisplay()
  337.    
  338. end
  339. hook.Add("HUDPaint", "DrawHUD", DrawHUD)
Advertisement
Add Comment
Please, Sign In to add comment