Guest User

Untitled

a guest
Dec 5th, 2010
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. nametag = {}
  2. local nametags = {}
  3. local g_screenX,g_screenY = guiGetScreenSize()
  4. local bHideNametags = false
  5.  
  6. local NAMETAG_SCALE = 0.3 --Overall adjustment of the nametag, use this to resize but constrain proportions
  7. local NAMETAG_ALPHA_DISTANCE = 50 --Distance to start fading out
  8. local NAMETAG_DISTANCE = 120 --Distance until we're gone
  9. local NAMETAG_ALPHA = 120 --The overall alpha level of the nametag
  10. --The following arent actual pixel measurements, they're just proportional constraints
  11. local NAMETAG_TEXT_BAR_SPACE = 2
  12. local NAMETAG_WIDTH = 50
  13. local NAMETAG_HEIGHT = 5
  14. local NAMETAG_TEXTSIZE = 0.7
  15. local NAMETAG_OUTLINE_THICKNESS = 1.2
  16. --
  17. local NAMETAG_ALPHA_DIFF = NAMETAG_DISTANCE - NAMETAG_ALPHA_DISTANCE
  18. NAMETAG_SCALE = 1/NAMETAG_SCALE * 800 / g_screenY
  19.  
  20. -- Ensure the name tag doesn't get too big
  21. local maxScaleCurve = { {0, 0}, {3, 3}, {13, 5} }
  22. -- Ensure the text doesn't get too small/unreadable
  23. local textScaleCurve = { {0, 0.8}, {0.8, 1.2}, {99, 99} }
  24. -- Make the text a bit brighter and fade more gradually
  25. local textAlphaCurve = { {0, 0}, {25, 100}, {120, 190}, {255, 190} }
  26.  
  27. function nametag.create ( player )
  28. nametags[player] = true
  29. end
  30.  
  31. function nametag.destroy ( player )
  32. nametags[player] = nil
  33. end
  34.  
  35. addEventHandler ( "onClientRender", g_Root,
  36. function()
  37. -- Hideous quick fix --
  38. for i,player in ipairs(g_Players) do
  39. if player ~= g_Me then
  40. setPlayerNametagShowing ( player, false )
  41. if not nametags[player] then
  42. nametag.create ( player )
  43. end
  44. end
  45. end
  46. if bHideNametags then
  47. return
  48. end
  49. local x,y,z = getCameraMatrix()
  50. for player in pairs(nametags) do
  51. while true do
  52. if not isPedInVehicle(player) or isPlayerDead(player) then break end
  53. local vehicle = getPedOccupiedVehicle(player)
  54. local px,py,pz = getElementPosition ( vehicle )
  55. local pdistance = getDistanceBetweenPoints3D ( x,y,z,px,py,pz )
  56. if pdistance <= NAMETAG_DISTANCE then
  57. --Get screenposition
  58. local sx,sy = getScreenFromWorldPosition ( px, py, pz+0.95, 0.06 )
  59. if not sx or not sy then break end
  60. --Calculate our components
  61. local scale = 1/(NAMETAG_SCALE * (pdistance / NAMETAG_DISTANCE))
  62. local alpha = ((pdistance - NAMETAG_ALPHA_DISTANCE) / NAMETAG_ALPHA_DIFF)
  63. alpha = (alpha < 0) and NAMETAG_ALPHA or NAMETAG_ALPHA-(alpha*NAMETAG_ALPHA)
  64. scale = math.evalCurve(maxScaleCurve,scale)
  65. local textscale = math.evalCurve(textScaleCurve,scale)
  66. local textalpha = math.evalCurve(textAlphaCurve,alpha)
  67. local outlineThickness = NAMETAG_OUTLINE_THICKNESS*(scale)
  68. --Draw our text
  69. local team = getPlayerTeam(player)
  70. if team then
  71. r,g,b = getTeamColor(team)
  72. end
  73. local offset = (scale) * NAMETAG_TEXT_BAR_SPACE/2
  74. local col=getElementData(player, "color")
  75. local r=col[1]
  76. local g=col[2]
  77. local b=col[3]
  78. dxDrawText (getPlayerName(player), sx, sy - offset, sx, sy - offset, tocolor(r,g,b,textalpha), textscale*NAMETAG_TEXTSIZE,"default", "center", "bottom", false, false, false )
  79. --We draw three parts to make the healthbar. First the outline/background
  80. local drawX = sx - NAMETAG_WIDTH*scale/2
  81. drawY = sy + offset
  82. local width,height = NAMETAG_WIDTH*scale, NAMETAG_HEIGHT*scale
  83. dxDrawRectangle ( drawX, drawY, width, height, tocolor(0,0,0,alpha) )
  84. --Next the inner background
  85. local health = getElementHealth(vehicle)
  86. health = math.max(health - 250, 0)/750
  87. local p = -510*(health^2)
  88. local r,g = math.max(math.min(p + 255*health + 255, 255), 0), math.max(math.min(p + 765*health, 255), 0)
  89. dxDrawRectangle ( drawX + outlineThickness,
  90. drawY + outlineThickness,
  91. width - outlineThickness*2,
  92. height - outlineThickness*2,
  93. tocolor(r,g,0,0.4*alpha)
  94. )
  95. --Finally, the actual health
  96. dxDrawRectangle ( drawX + outlineThickness,
  97. drawY + outlineThickness,
  98. health*(width - outlineThickness*2),
  99. height - outlineThickness*2,
  100. tocolor(r,g,0,alpha)
  101. )
  102. end
  103. break
  104. end
  105. end
  106. end
  107. )
  108.  
  109.  
  110. ---------------THE FOLLOWING IS THE MANAGEMENT OF NAMETAGS-----------------
  111. addEventHandler('onClientResourceStart', g_ResRoot,
  112. function()
  113. for i,player in ipairs(getElementsByType"player") do
  114. if player ~= g_Me then
  115. nametag.create ( player )
  116. end
  117. end
  118. end
  119. )
  120.  
  121. addEventHandler ( "onClientPlayerJoin", g_Root,
  122. function()
  123. if source == g_Me then return end
  124. setPlayerNametagShowing ( source, false )
  125. nametag.create ( source )
  126. end
  127. )
  128.  
  129. addEventHandler ( "onClientPlayerQuit", g_Root,
  130. function()
  131. nametag.destroy ( source )
  132. end
  133. )
  134.  
  135.  
  136. addEvent ( "onClientScreenFadedOut", true )
  137. addEventHandler ( "onClientScreenFadedOut", g_Root,
  138. function()
  139. bHideNametags = true
  140. end
  141. )
  142.  
  143. addEvent ( "onClientScreenFadedIn", true )
  144. addEventHandler ( "onClientScreenFadedIn", g_Root,
  145. function()
  146. bHideNametags = false
  147. end
  148. )
Advertisement
Add Comment
Please, Sign In to add comment