From f2c110ee739a4c1a8cbc7aba597671181d5c835e Mon Sep 17 00:00:00 2001 From: AlmTech Software Date: Tue, 7 Jan 2020 19:55:29 +0100 Subject: [PATCH] Added unequip option to items --- .../engine/core/client/cl_character_net.lua | 15 ++++ .../engine/derma/lib/cl_menu_iteminfo.lua | 77 +++++++++++++------ gamemode/engine/derma/menus/menu_charinfo.lua | 16 ++-- gamemode/engine/lib/server/sv_networking.lua | 3 + gamemode/engine/vars/sh_vars.lua | 7 +- 5 files changed, 87 insertions(+), 31 deletions(-) diff --git a/gamemode/engine/core/client/cl_character_net.lua b/gamemode/engine/core/client/cl_character_net.lua index 06f4157..3cd34ae 100644 --- a/gamemode/engine/core/client/cl_character_net.lua +++ b/gamemode/engine/core/client/cl_character_net.lua @@ -112,4 +112,19 @@ function Quantum.Client.InventoryNet.EquipItem( index ) end end +end + +function Quantum.Client.InventoryNet.UnequipItem( equipindex ) + -- local item = Quantum.Client.Inventory[index] + -- local itemTbl = Quantum.Item.Get( item[1] ) + -- if( itemTbl != nil && item[2] > 0 ) then + -- if( itemTbl.equipslot != nil ) then + + net.Start( "quantum_item_action" ) + Quantum.WriteIntcode( Quantum.IntCode.UNEQUIP_ITEM ) + net.WriteInt( equipindex, Quantum.calculateNeededBits( Quantum.Inventory.Size ) ) + net.SendToServer() + + -- end + -- end end \ No newline at end of file diff --git a/gamemode/engine/derma/lib/cl_menu_iteminfo.lua b/gamemode/engine/derma/lib/cl_menu_iteminfo.lua index 7f7285f..55c403f 100644 --- a/gamemode/engine/derma/lib/cl_menu_iteminfo.lua +++ b/gamemode/engine/derma/lib/cl_menu_iteminfo.lua @@ -189,32 +189,65 @@ function iteminfo.giveoptions( p, page ) if( item.equipslot != nil ) then -- Equip - op.equip = vgui.Create( "DButton", options ) - op.equip:SetText( "Equip (" .. Quantum.EquipSlotsNames[item.equipslot] .. ")" ) - op.equip:SetFont( "q_item_option_button" ) - op.equip:SizeToContents() - op.equip.w, op.equip.h = op.equip:GetSize() - op.equip:SetPos( xbasepos, ypos ) - op.equip.x, op.equip.y = op.equip:GetPos() - op.equip.Paint = function( self ) - theme.iteminfobutton( self ) - end - op.equip.DoClick = function( self ) - surface.PlaySound( "UI/buttonclick.wav" ) - options.Close() - - if( page.equippanels[item.equipslot] != nil ) then - page.equippanels[item.equipslot].SetItem( item.id ) -- set its item + if( page.equippanels[item.equipslot].itemindex != index ) then + + op.equip = vgui.Create( "DButton", options ) + op.equip:SetText( "Equip (" .. Quantum.EquipSlotsNames[item.equipslot] .. ")" ) + op.equip:SetFont( "q_item_option_button" ) + op.equip:SizeToContents() + op.equip.w, op.equip.h = op.equip:GetSize() + op.equip:SetPos( xbasepos, ypos ) + op.equip.x, op.equip.y = op.equip:GetPos() + op.equip.Paint = function( self ) + theme.iteminfobutton( self ) + end + op.equip.DoClick = function( self ) + surface.PlaySound( "UI/buttonclick.wav" ) + options.Close() + + if( page.equippanels[item.equipslot] != nil ) then + page.equippanels[item.equipslot].SetItem( index ) -- set its item + end + + if( page.markedItemPanel[item.equipslot] != nil ) then + page.markedItemPanel[item.equipslot]:SetVisible( false ) -- unmark the old panel + end + p.mark:SetVisible( true ) -- mark the new one + page.markedItemPanel[item.equipslot] = p.mark -- and save it to the table for future use + + ---- EQUIP NET ---- + Quantum.Client.InventoryNet.EquipItem( index ) end - if( page.markedItemPanel[item.equipslot] != nil ) then - page.markedItemPanel[item.equipslot]:SetVisible( false ) -- unmark the old panel + else + + op.equip = vgui.Create( "DButton", options ) + op.equip:SetText( "Unequip (" .. Quantum.EquipSlotsNames[item.equipslot] .. ")" ) + op.equip:SetFont( "q_item_option_button" ) + op.equip:SizeToContents() + op.equip.w, op.equip.h = op.equip:GetSize() + op.equip:SetPos( xbasepos, ypos ) + op.equip.x, op.equip.y = op.equip:GetPos() + op.equip.Paint = function( self ) + theme.iteminfobutton( self ) + end + op.equip.DoClick = function( self ) + surface.PlaySound( "UI/buttonclick.wav" ) + options.Close() + + if( page.equippanels[item.equipslot] != nil ) then + page.equippanels[item.equipslot].SetItem( nil ) -- remove the item from the display + end + + if( page.markedItemPanel[item.equipslot] != nil ) then + page.markedItemPanel[item.equipslot]:SetVisible( false ) -- unmark the panel + end + page.markedItemPanel[item.equipslot] = nil -- remove it from the table so its spot is free + + ---- EQUIP NET ---- + Quantum.Client.InventoryNet.UnequipItem( item.equipslot ) end - p.mark:SetVisible( true ) -- mark the new one - page.markedItemPanel[item.equipslot] = p.mark -- and save it to the table for future use - ---- EQUIP NET ---- - Quantum.Client.InventoryNet.EquipItem( index ) end ypos = ypos + op.equip.h + yspacing diff --git a/gamemode/engine/derma/menus/menu_charinfo.lua b/gamemode/engine/derma/menus/menu_charinfo.lua index 3dc9dae..71dea9c 100644 --- a/gamemode/engine/derma/menus/menu_charinfo.lua +++ b/gamemode/engine/derma/menus/menu_charinfo.lua @@ -64,12 +64,16 @@ local function createEquipSlotPanel( equiptype, x, y, scale, parent ) theme.itempanel( self, self.itemcolor, true ) end - function p.SetItem( itemid ) - --local itemid = itempanel.item.id - local itemTbl = Quantum.Item.Get( itemid ) - if( itemTbl != nil ) then - --itempanel:Remove() + function p.SetItem( itemindex, itemid ) + local item = Quantum.Client.Inventory[itemindex] + + + if( item != nil ) then + itemid = itemid || item[1] + local itemTbl = Quantum.Item.Get( itemid ) + p.itemid = itemid -- give it its equipped item + p.itemindex = itemindex p.itemcolor = itemTbl.rarity.color -- give it its color p.item = itemTbl @@ -121,7 +125,7 @@ end local function getItemInSlot( pos ) local inv = Quantum.Client.Inventory || {} if( inv[pos] != nil ) then - return inv[pos][1] + return pos, inv[pos][1] else return end diff --git a/gamemode/engine/lib/server/sv_networking.lua b/gamemode/engine/lib/server/sv_networking.lua index 6cf3993..7e3c2dc 100644 --- a/gamemode/engine/lib/server/sv_networking.lua +++ b/gamemode/engine/lib/server/sv_networking.lua @@ -174,6 +174,9 @@ local intcodeFunctions = { end, [Quantum.IntCode.EQUIP_ITEM] = function( pl, index, itemid, amount ) Quantum.Server.Inventory.EquipItem( pl, index ) + end, + [Quantum.IntCode.UNEQUIP_ITEM] = function( pl, equipslot, itemid, amount ) + Quantum.Server.Inventory.UnEquipItem( pl, equipslot ) end } diff --git a/gamemode/engine/vars/sh_vars.lua b/gamemode/engine/vars/sh_vars.lua index a0b8ddc..852482d 100644 --- a/gamemode/engine/vars/sh_vars.lua +++ b/gamemode/engine/vars/sh_vars.lua @@ -41,9 +41,10 @@ Quantum.IntCode = { USE_ITEM = 2, EAT_ITEM = 3, EQUIP_ITEM = 4, - DESTROY_ITEM = 5, -- to be added - UPDATE = 6, - BIT_SIZE = 4 + UNEQUIP_ITEM = 5, + DESTROY_ITEM = 6, -- to be added + UPDATE = 7, + BIT_SIZE = 5 } function Quantum.calculateNeededBits( n ) return math.ceil( math.log( n, 2 ) ) end