Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if !CLIENT then return end
- local meta = FindMetaTable("Player")
- function meta:HasWeapons()
- if table.Count(self:GetWeapons()) > 0 then
- return true
- end
- return false
- end
- GUISELECTOR = {}
- local selector = {}
- selector.Weapons = {}
- selector.Slots = 5
- selector.SelectedWeapon = 0
- selector.SelectedSlot = 0
- selector.UpdateTime = 0
- selector.Width = ScrW() * 0.05
- selector.Height = 30
- selector.SlotWidth = 30
- selector.SlotHeight = 30
- hook.Add("HUDPaint", "Draw", function()
- selector:Sort()
- end)
- function selector:Initialize()
- if !LocalPlayer():HasWeapons() then return end
- selector.Slots = selector:GetMaxSlot()
- selector.SelectedSlot = selector:GetSlot(LocalPlayer():GetActiveWeapon())
- selector.SelectedWeapon = selector:GetSlotWeapon(LocalPlayer():GetActiveWeapon(), selector.SelectedSlot)
- selector:Sort()
- if !timer.Exists("UpdateWeapon") then
- timer.Create("UpdateWeapon", 0.3, 0, function()
- selector:Sort()
- end)
- end
- end
- function selector:GetSlot(weapon)
- if weapon:IsValid() then
- for k, v in pairs(selector.Weapons) do
- if selector.Weapons[table.KeyFromValue(v, weapon)] then
- return k
- end
- end
- end
- return 0
- end
- function selector:GetSlotWeapon(weapon, slot)
- return table.KeyFromValue(selector.Weapons[(slot or selector:GetSlot(LocalPlayer():GetActiveWeapon()))], weapon)
- end
- function selector:GetMaxSlot()
- if !LocalPlayer():HasWeapons() then return end
- local number = 0
- for k, v in pairs(LocalPlayer():GetWeapons()) do
- if v:GetSlot() > number then
- number = v:GetSlot()
- end
- end
- return number
- end
- function selector:Sort()
- if !selector.Weapons then
- selector.Weapons = {}
- end
- for slot = 0, selector.Slots do
- if selector.Weapons[slot] then
- table.Empty(selector.Weapons[slot])
- end
- selector.Weapons[slot] = {}
- if LocalPlayer():HasWeapons() then
- for k, v in pairs(LocalPlayer():GetWeapons()) do
- if v:IsValid() and v:GetSlot() == slot then
- table.insert(selector.Weapons[slot], v)
- end
- end
- end
- end
- end
- function selector:SlotCount(slot)
- return table.Count(selector.Weapons[slot])
- end
- function selector:GetSelectedWeapon(slot, weapon)
- return table.KeyFromValue(selector.Weapons[slot], weapon)
- end
- function selector:SetSelected(slot, weapon)
- if LocalPlayer():InVehicle() then return end
- selector.SelectedSlot = slot
- selector.SelectedWeapon = weapon
- selector.UpdateTime = CurTime()
- end
- function selector:ShouldDraw()
- if selector.UpdateTime and selector.UpdateTime + 1 > CurTime() then
- return true
- end
- return false
- end
- function selector:Update()
- if selector:ShouldDraw() then
- selector.UpdateTime = CurTime()
- else
- selector:Initialize()
- selector.UpdateTime = CurTime()
- end
- end
- function selector:FindNextSlot()
- if selector.SelectedSlot == selector.Slots then
- return selector.SelectedSlot
- end
- for slot = selector.SelectedSlot, selector.Slots do
- if slot != selector.SelectedSlot then
- if selector:SlotCount(slot) > 0 then
- return slot
- end
- end
- end
- return selector.SelectedSlot
- end
- function selector:FindPreviousSlot()
- if selector.SelectedSlot == 0 then
- return selector.Slots
- end
- local slot = selector.SelectedSlot
- for i = 0, selector.Slots do
- if i < selector.SelectedSlot and i != selector.SelectedSlot then
- if selector:SlotCount(i) > 0 then
- slot = i
- end
- end
- end
- return slot
- end
- function selector:Next()
- if !LocalPlayer():HasWeapons() then return end
- if selector.SelectedWeapon == selector:SlotCount(selector.SelectedSlot) and selector.SelectedSlot == selector.Slots then
- selector.SelectedSlot = 0
- selector.SelectedWeapon = 1
- end
- if selector.SelectedWeapon == selector:SlotCount(selector.SelectedSlot) or selector:SlotCount(selector.SelectedSlot) <= 0 then
- selector.SelectedSlot = selector:FindNextSlot()
- selector.SelectedWeapon = 1
- else
- selector.SelectedWeapon = selector.SelectedWeapon + 1
- end
- end
- function selector:Previous()
- if !LocalPlayer():HasWeapons() then return end
- if selector.SelectedWeapon == 1 and selector.SelectedSlot == 0 then
- selector.SelectedSlot = selector.Slots
- selector.SelectedWeapon = selector:SlotCount(selector.SelectedSlot)
- elseif selector.SelectedWeapon == 1 or selector:SlotCount(selector.SelectedSlot) <= 0 then
- selector.SelectedSlot = selector:FindPreviousSlot()
- selector.SelectedWeapon = selector:SlotCount(selector.SelectedSlot)
- else
- selector.SelectedWeapon = selector.SelectedWeapon - 1
- end
- print("slot"..selector.SelectedSlot)
- print("weapon"..selector.SelectedWeapon)
- print("previous"..selector:FindPreviousSlot())
- end
- hook.Add("PlayerBindPress", "SwitchSelected", function(ply, bind, pressed)
- if bind == "invnext" and pressed and !input.IsMouseDown(MOUSE_LEFT) then
- if !ply:InVehicle() then
- selector:Update()
- selector:Next()
- end
- return true
- end
- if bind == "invprev" and pressed and !input.IsMouseDown(MOUSE_LEFT) then
- if !ply:InVehicle() then
- selector:Update()
- selector:Previous()
- end
- return true
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement