Advertisement
TheCodingBeast

DarkRP 2.5 Coding | #1 HUD

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