Advertisement
Guest User

Untitled

a guest
Jul 11th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. if !CLIENT then return end
  2.  
  3. local meta = FindMetaTable("Player")
  4.  
  5. function meta:HasWeapons()
  6. if table.Count(self:GetWeapons()) > 0 then
  7. return true
  8. end
  9.  
  10. return false
  11. end
  12.  
  13. GUISELECTOR = {}
  14.  
  15. local selector = {}
  16.  
  17. selector.Weapons = {}
  18. selector.Slots = 5
  19. selector.SelectedWeapon = 0
  20. selector.SelectedSlot = 0
  21. selector.UpdateTime = 0
  22.  
  23. selector.Width = ScrW() * 0.05
  24. selector.Height = 30
  25. selector.SlotWidth = 30
  26. selector.SlotHeight = 30
  27.  
  28. hook.Add("HUDPaint", "Draw", function()
  29. selector:Sort()
  30. end)
  31.  
  32. function selector:Initialize()
  33. if !LocalPlayer():HasWeapons() then return end
  34. selector.Slots = selector:GetMaxSlot()
  35. selector.SelectedSlot = selector:GetSlot(LocalPlayer():GetActiveWeapon())
  36. selector.SelectedWeapon = selector:GetSlotWeapon(LocalPlayer():GetActiveWeapon(), selector.SelectedSlot)
  37. selector:Sort()
  38.  
  39. if !timer.Exists("UpdateWeapon") then
  40. timer.Create("UpdateWeapon", 0.3, 0, function()
  41. selector:Sort()
  42. end)
  43. end
  44. end
  45.  
  46. function selector:GetSlot(weapon)
  47. if weapon:IsValid() then
  48. for k, v in pairs(selector.Weapons) do
  49. if selector.Weapons[table.KeyFromValue(v, weapon)] then
  50. return k
  51. end
  52. end
  53. end
  54.  
  55. return 0
  56. end
  57.  
  58. function selector:GetSlotWeapon(weapon, slot)
  59. return table.KeyFromValue(selector.Weapons[(slot or selector:GetSlot(LocalPlayer():GetActiveWeapon()))], weapon)
  60. end
  61.  
  62. function selector:GetMaxSlot()
  63. if !LocalPlayer():HasWeapons() then return end
  64. local number = 0
  65. for k, v in pairs(LocalPlayer():GetWeapons()) do
  66. if v:GetSlot() > number then
  67. number = v:GetSlot()
  68. end
  69. end
  70.  
  71. return number
  72. end
  73.  
  74. function selector:Sort()
  75. if !selector.Weapons then
  76. selector.Weapons = {}
  77. end
  78.  
  79. for slot = 0, selector.Slots do
  80. if selector.Weapons[slot] then
  81. table.Empty(selector.Weapons[slot])
  82. end
  83.  
  84. selector.Weapons[slot] = {}
  85.  
  86. if LocalPlayer():HasWeapons() then
  87. for k, v in pairs(LocalPlayer():GetWeapons()) do
  88. if v:IsValid() and v:GetSlot() == slot then
  89. table.insert(selector.Weapons[slot], v)
  90. end
  91. end
  92. end
  93. end
  94. end
  95.  
  96. function selector:SlotCount(slot)
  97. return table.Count(selector.Weapons[slot])
  98. end
  99.  
  100. function selector:GetSelectedWeapon(slot, weapon)
  101. return table.KeyFromValue(selector.Weapons[slot], weapon)
  102. end
  103.  
  104. function selector:SetSelected(slot, weapon)
  105. if LocalPlayer():InVehicle() then return end
  106. selector.SelectedSlot = slot
  107. selector.SelectedWeapon = weapon
  108.  
  109. selector.UpdateTime = CurTime()
  110. end
  111.  
  112. function selector:ShouldDraw()
  113. if selector.UpdateTime and selector.UpdateTime + 1 > CurTime() then
  114. return true
  115. end
  116.  
  117. return false
  118. end
  119.  
  120. function selector:Update()
  121. if selector:ShouldDraw() then
  122. selector.UpdateTime = CurTime()
  123. else
  124. selector:Initialize()
  125. selector.UpdateTime = CurTime()
  126. end
  127. end
  128.  
  129. function selector:FindNextSlot()
  130. if selector.SelectedSlot == selector.Slots then
  131. return selector.SelectedSlot
  132. end
  133.  
  134. for slot = selector.SelectedSlot, selector.Slots do
  135. if slot != selector.SelectedSlot then
  136. if selector:SlotCount(slot) > 0 then
  137. return slot
  138. end
  139. end
  140. end
  141.  
  142. return selector.SelectedSlot
  143. end
  144.  
  145. function selector:FindPreviousSlot()
  146. if selector.SelectedSlot == 0 then
  147. return selector.Slots
  148. end
  149. local slot = selector.SelectedSlot
  150. for i = 0, selector.Slots do
  151. if i < selector.SelectedSlot and i != selector.SelectedSlot then
  152. if selector:SlotCount(i) > 0 then
  153. slot = i
  154. end
  155. end
  156. end
  157.  
  158. return slot
  159. end
  160.  
  161. function selector:Next()
  162. if !LocalPlayer():HasWeapons() then return end
  163. if selector.SelectedWeapon == selector:SlotCount(selector.SelectedSlot) and selector.SelectedSlot == selector.Slots then
  164. selector.SelectedSlot = 0
  165. selector.SelectedWeapon = 1
  166. end
  167.  
  168. if selector.SelectedWeapon == selector:SlotCount(selector.SelectedSlot) or selector:SlotCount(selector.SelectedSlot) <= 0 then
  169. selector.SelectedSlot = selector:FindNextSlot()
  170. selector.SelectedWeapon = 1
  171. else
  172. selector.SelectedWeapon = selector.SelectedWeapon + 1
  173. end
  174. end
  175.  
  176. function selector:Previous()
  177. if !LocalPlayer():HasWeapons() then return end
  178. if selector.SelectedWeapon == 1 and selector.SelectedSlot == 0 then
  179. selector.SelectedSlot = selector.Slots
  180. selector.SelectedWeapon = selector:SlotCount(selector.SelectedSlot)
  181. elseif selector.SelectedWeapon == 1 or selector:SlotCount(selector.SelectedSlot) <= 0 then
  182. selector.SelectedSlot = selector:FindPreviousSlot()
  183. selector.SelectedWeapon = selector:SlotCount(selector.SelectedSlot)
  184. else
  185. selector.SelectedWeapon = selector.SelectedWeapon - 1
  186. end
  187. print("slot"..selector.SelectedSlot)
  188. print("weapon"..selector.SelectedWeapon)
  189. print("previous"..selector:FindPreviousSlot())
  190. end
  191.  
  192. hook.Add("PlayerBindPress", "SwitchSelected", function(ply, bind, pressed)
  193. if bind == "invnext" and pressed and !input.IsMouseDown(MOUSE_LEFT) then
  194. if !ply:InVehicle() then
  195. selector:Update()
  196. selector:Next()
  197. end
  198.  
  199. return true
  200. end
  201.  
  202. if bind == "invprev" and pressed and !input.IsMouseDown(MOUSE_LEFT) then
  203. if !ply:InVehicle() then
  204. selector:Update()
  205. selector:Previous()
  206. end
  207.  
  208. return true
  209. end
  210. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement