From fca3ad0654a136ee209bb36e68303c2e3ce8d7b1 Mon Sep 17 00:00:00 2001 From: AlmTech Software Date: Sat, 4 Jan 2020 16:34:10 +0100 Subject: [PATCH] Added item consuming, using etc --- .../engine/core/client/cl_character_net.lua | 34 +++++++++++++++++ .../engine/derma/lib/cl_menu_iteminfo.lua | 38 ++++++++++++++++--- gamemode/engine/derma/menus/menu_charinfo.lua | 15 ++------ gamemode/engine/lib/server/sv_inventory.lua | 30 +++++++++++++++ gamemode/engine/lib/server/sv_networking.lua | 8 +++- gamemode/engine/lib/server/sv_notify.lua | 7 ++++ gamemode/settings/sh_items.lua | 9 ++++- gamemode/settings/sv_settings.lua | 2 +- 8 files changed, 123 insertions(+), 20 deletions(-) diff --git a/gamemode/engine/core/client/cl_character_net.lua b/gamemode/engine/core/client/cl_character_net.lua index 171c757..8bd9ed2 100644 --- a/gamemode/engine/core/client/cl_character_net.lua +++ b/gamemode/engine/core/client/cl_character_net.lua @@ -49,3 +49,37 @@ function Quantum.Client.InventoryNet.DropItem( itemid, index, amount ) net.WriteInt( amount, Quantum.calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) net.SendToServer() end + +function Quantum.Client.InventoryNet.UseItem( index ) + local item = Quantum.Client.Inventory[index] + local itemTbl = Quantum.Item.Get( item[1] ) + if( itemTbl != nil && item[2] > 0 ) then + if( itemTbl.usefunction != nil ) then + + Quantum.Client.InventoryNet.SetItem( index, item[1], item[2] - 1 ) -- remove one from the inventory on the client + + net.Start( "quantum_item_action" ) + Quantum.WriteIntcode( Quantum.IntCode.USE_ITEM ) + net.WriteInt( index, Quantum.calculateNeededBits( Quantum.Inventory.Size ) ) + net.SendToServer() + + end + end +end + +function Quantum.Client.InventoryNet.EatItem( index ) + local item = Quantum.Client.Inventory[index] + local itemTbl = Quantum.Item.Get( item[1] ) + if( itemTbl != nil && item[2] > 0 ) then + if( itemTbl.consumefunction != nil ) then + + Quantum.Client.InventoryNet.SetItem( index, item[1], item[2] - 1 ) -- remove one from the inventory on the client + + net.Start( "quantum_item_action" ) + Quantum.WriteIntcode( Quantum.IntCode.EAT_ITEM ) + net.WriteInt( index, 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 cbaa4f3..e546435 100644 --- a/gamemode/engine/derma/lib/cl_menu_iteminfo.lua +++ b/gamemode/engine/derma/lib/cl_menu_iteminfo.lua @@ -182,8 +182,10 @@ function iteminfo.giveoptions( p, page ) ---- EQUIP NET HERE ---- end ypos = ypos + op.equip.h + yspacing + + end - elseif( item.usefunction != nil ) then + if( item.usefunction != nil ) then -- USE op.use = vgui.Create( "DButton", options ) op.use:SetText( "Use Item" ) @@ -198,15 +200,42 @@ function iteminfo.giveoptions( p, page ) op.use.DoClick = function( self ) surface.PlaySound( "UI/buttonclick.wav" ) - p:GetParent().RemoveItem() + p:GetParent().SetItemAmount( amount - 1 ) options.Close() - ---- USE NET HERE ---- + ---- USE NET ---- + Quantum.Client.InventoryNet.UseItem( index ) end ypos = ypos + op.use.h + yspacing + end + + if( item.consumefunction ) then -- EAT + + op.eat = vgui.Create( "DButton", options ) + op.eat:SetText( "Consume" ) + op.eat:SetFont( "q_item_option_button" ) + op.eat:SizeToContents() + op.eat.w, op.eat.h = op.eat:GetSize() + op.eat:SetPos( xbasepos, ypos ) + op.eat.x, op.eat.y = op.eat:GetPos() + op.eat.Paint = function( self ) + theme.iteminfobutton( self ) + end + op.eat.DoClick = function( self ) + surface.PlaySound( "UI/buttonclick.wav" ) - elseif( !item.soulbound ) then -- Drop + p:GetParent().SetItemAmount( amount - 1 ) + + options.Close() + + ---- EAT NET ---- + Quantum.Client.InventoryNet.EatItem( index ) + end + ypos = ypos + op.eat.h + yspacing + end + + if( !item.soulbound ) then -- Drop op.drop = vgui.Create( "DButton", options ) op.drop:SetText( "Drop" ) op.drop:SetFont( "q_item_option_button" ) @@ -231,7 +260,6 @@ function iteminfo.giveoptions( p, page ) end end ypos = ypos + op.drop.h + yspacing - end op.close = vgui.Create( "DButton", options ) diff --git a/gamemode/engine/derma/menus/menu_charinfo.lua b/gamemode/engine/derma/menus/menu_charinfo.lua index dc09552..bbfdd4c 100644 --- a/gamemode/engine/derma/menus/menu_charinfo.lua +++ b/gamemode/engine/derma/menus/menu_charinfo.lua @@ -200,6 +200,10 @@ function menu.open( dt ) end end + itempanels[ii].GetItemAmount = function() + return itempanels[ii].item.amount + end + ---- Create the model icon for the item panel ---- if( itempanels[ii].item != nil && itempanels[ii].item.model != nil ) then itempanels[ii].icon = vgui.Create( "DModelPanel", itempanels[ii] ) @@ -283,17 +287,6 @@ function menu.open( dt ) end hook.Add("ScoreboardShow", "Quantum_Menu_CharInfo_Open", function() - -- if( InventoryStartTime == nil ) then - -- InventoryStartTime = CurTime() - -- end - - -- if( InventoryStartTime + 1 <= CurTime() ) then - -- menu.open() - -- InventoryStartTime = nil - -- end -- open the menu - - -- menu.open() - return false end) diff --git a/gamemode/engine/lib/server/sv_inventory.lua b/gamemode/engine/lib/server/sv_inventory.lua index 365c3ba..c3d7032 100644 --- a/gamemode/engine/lib/server/sv_inventory.lua +++ b/gamemode/engine/lib/server/sv_inventory.lua @@ -224,4 +224,34 @@ function Quantum.Server.Inventory.DropItem( pl, index, amount ) -- Quantum.Serve else Quantum.Error( "Player " .. tostring( pl ) .. " tried to drop a something from index=" .. tostring(index) .. " where there exists no item." ) end +end + +function Quantum.Server.Inventory.UseItem( pl, index ) + local char = Quantum.Server.Char.GetCurrentCharacter( pl ) + local inv = Quantum.Server.Char.GetInventory( char ) + + local item = inv[index] + + if( item != nil || #item > 0 ) then + local itemTbl = Quantum.Item.Get( item[1] ) + if( itemTbl.usefunction != nil ) then + Quantum.Server.Inventory.SetSlotItem( pl, char, index, item[1], item[2] - 1 ) + itemTbl.usefunction(pl) -- call the function + end + end +end + +function Quantum.Server.Inventory.EatItem( pl, index ) + local char = Quantum.Server.Char.GetCurrentCharacter( pl ) + local inv = Quantum.Server.Char.GetInventory( char ) + + local item = inv[index] + + if( item != nil || #item > 0 ) then + local itemTbl = Quantum.Item.Get( item[1] ) + if( itemTbl.consumefunction != nil ) then + Quantum.Server.Inventory.SetSlotItem( pl, char, index, item[1], item[2] - 1 ) + itemTbl.consumefunction( pl ) + end + end end \ No newline at end of file diff --git a/gamemode/engine/lib/server/sv_networking.lua b/gamemode/engine/lib/server/sv_networking.lua index e23a102..b763b04 100644 --- a/gamemode/engine/lib/server/sv_networking.lua +++ b/gamemode/engine/lib/server/sv_networking.lua @@ -150,13 +150,19 @@ local intcodeFunctions = { end, [Quantum.IntCode.DROP_ITEM] = function( pl, index, itemid, amount ) Quantum.Server.Inventory.DropItem( pl, index, amount ) + end, + [Quantum.IntCode.USE_ITEM] = function( pl, index, itemid, amount ) + Quantum.Server.Inventory.UseItem( pl, index ) + end, + [Quantum.IntCode.EAT_ITEM] = function( pl, index, itemid, amount ) + Quantum.Server.Inventory.EatItem( pl, index ) end } net.Receive( "quantum_item_action", function( len, pl ) local intcode = net.ReadInt( Quantum.IntCode.BIT_SIZE ) local index = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.Size ) ) - local itemid = net.ReadString() + local itemid = net.ReadString() local amount = net.ReadInt( Quantum.calculateNeededBits( Quantum.Inventory.MaxStackSize ) ) intcodeFunctions[intcode]( pl, index, itemid, amount ) diff --git a/gamemode/engine/lib/server/sv_notify.lua b/gamemode/engine/lib/server/sv_notify.lua index 240c366..a827213 100644 --- a/gamemode/engine/lib/server/sv_notify.lua +++ b/gamemode/engine/lib/server/sv_notify.lua @@ -31,5 +31,12 @@ function Quantum.Notify.Deny( pl, text ) local luaArgs = makeColorAString( Color( 245, 20, 20 ) ) .. ",'" .. tostring( text ) .. "'" local luaFunc = "chat.AddText(" .. luaArgs .. ")" + pl:SendLua( luaFunc ) +end + +function Quantum.Notify.Info( pl, text ) + local luaArgs = makeColorAString( Color( 245, 245, 245 ) ) .. ",'" .. tostring( text ) .. "'" + local luaFunc = "chat.AddText(" .. luaArgs .. ")" + pl:SendLua( luaFunc ) end \ No newline at end of file diff --git a/gamemode/settings/sh_items.lua b/gamemode/settings/sh_items.lua index b14abbd..5a4f81d 100644 --- a/gamemode/settings/sh_items.lua +++ b/gamemode/settings/sh_items.lua @@ -25,8 +25,8 @@ Quantum.Item.Create( "test2", { equipable = false, rarity = Quantum.Rarity.Trash, consumefunction = function( user ) + Quantum.Notify.Info( user, "You consumed trash and therefore died!" ) user:Kill() - user:PrintChat( "You consumed trash and died!" ) end } ) @@ -47,5 +47,10 @@ Quantum.Item.Create( "potatoe", { stack = 1, soulbound = false, equipable = false, - rarity = Quantum.Rarity.Legendary + rarity = Quantum.Rarity.Legendary, + consumefunction = function( user ) + Quantum.Notify.Info( user, "You consumed a legendary potatoe! You now have 1000 health for 10 seconds!" ) + user:SetHealth( 1000 ) + timer.Simple( 10, function() user:SetHealth( user:GetMaxHealth() ) end) + end } ) \ No newline at end of file diff --git a/gamemode/settings/sv_settings.lua b/gamemode/settings/sv_settings.lua index a1ff02b..dcf7644 100644 --- a/gamemode/settings/sv_settings.lua +++ b/gamemode/settings/sv_settings.lua @@ -15,7 +15,7 @@ Quantum.Server.Settings.MaxHealth = 100 Quantum.Server.Settings.StarterMoney = 0 -Quantum.Server.Settings.ItemDespawnTimer = 3000 +Quantum.Server.Settings.ItemDespawnTimer = 300 Quantum.Server.Settings.ItemPickupSound = "physics/cardboard/cardboard_box_impact_hard2.wav"