code_gs

Chat indicator update

Sep 21st, 2020 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.55 KB | None | 0 0
  1. local function drawIndicator(ply, rag)
  2.     local indicator = ply.indicator
  3.  
  4.     if not ply:IsTyping() then
  5.         if isentity(indicator) and indicator:IsValid() then
  6.             indicator:Remove()
  7.         end
  8.  
  9.         return
  10.     end
  11.  
  12.     if not isentity(indicator) or not indicator:IsValid() then
  13.         indicator = ClientsideModel("models/extras/info_speech.mdl", RENDERGROUP_OPAQUE)
  14.         if not indicator:IsValid() then return end
  15.  
  16.         ply.indicator = indicator
  17.        
  18.         indicator:SetNoDraw(true)
  19.         indicator:SetModelScale(0.6)
  20.     end
  21.  
  22.     local curTime = CurTime()
  23.     local lastDraw = indicator.lastDraw
  24.  
  25.     if not isnumber(lastDraw) or lastDraw < 0 then
  26.         lastDraw = 0
  27.     end
  28.  
  29.     indicator.lastDraw = curTime
  30.  
  31.     local pos
  32.  
  33.     if rag == nil then
  34.         indicator:SetParent(ply)
  35.        
  36.         pos = Vector(0, 0, ply:GetModelScale() * 72 + 12)
  37.     else
  38.         indicator:SetParent(rag)
  39.  
  40.         pos = rag:LocalToWorld(rag:EyePos())
  41.         pos[3] = rag:GetModelScale() * 72 + 12
  42.     end
  43.  
  44.     indicator:SetLocalPos(pos)
  45.  
  46.     local angle = indicator:GetAngles()
  47.     angle[2] = (angle[2] + 360 * (curTime - lastDraw)) % 360
  48.     indicator:SetAngles(angle)
  49.  
  50.     indicator:SetupBones()
  51.     indicator:DrawModel()
  52. end
  53.  
  54. hook.Add("PostPlayerDraw", "DarkRP_ChatIndicator", drawIndicator)
  55.  
  56. local function indicatorOverride(rag)
  57.     if rag:IsRagdoll() then
  58.         local owner = rag:GetRagdollOwner()
  59.  
  60.         if owner:IsPlayer() then
  61.             drawIndicator(owner, rag)
  62.         end
  63.     end
  64.  
  65.     rag:DrawModel()
  66. end
  67.  
  68. hook.Add("CreateClientsideRagdoll", "DarkRP_ChatIndicator", function(ply, rag)
  69.     if not ply:IsPlayer() then return end
  70.  
  71.     local oldRenderOverride = rag.RenderOverride
  72.  
  73.     if isfunction(oldRenderOverride) then
  74.         -- This one cannot be stateless since the old render override must be called
  75.         rag.RenderOverride = function(rag)
  76.             if rag:IsRagdoll() then
  77.                 local owner = rag:GetRagdollOwner()
  78.  
  79.                 if owner:IsPlayer() then
  80.                     drawIndicator(owner, rag)
  81.                 end
  82.             end
  83.  
  84.             return oldRenderOverride(rag)
  85.         end
  86.     else
  87.         rag.RenderOverride = indicatorOverride
  88.     end
  89. end)
  90.  
  91. -- CSEnts aren't GC'd.
  92. -- https://github.com/Facepunch/garrysmod-issues/issues/1387
  93. hook.Add("EntityRemoved", "DarkRP_ChatIndicator", function(ply)
  94.     if not ply:IsPlayer() then return end
  95.  
  96.     local indicator = ply.indicator
  97.  
  98.     if isentity(indicator) and indicator:IsValid() then
  99.         indicator:Remove()
  100.     end
  101. end)
Add Comment
Please, Sign In to add comment