aboutsummaryrefslogtreecommitdiff
path: root/gamemode/core/npc
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-04-02 20:05:56 -0400
committerAlexander Pickering <alexandermpickering@gmail.com>2017-04-02 20:05:56 -0400
commit191ba416c8b611ea4901cead138789a357c56134 (patch)
treeb30ae3473c16028a14d3ed0c80633f360cc1c914 /gamemode/core/npc
parenta22cbeddc5f8fb61e87a30aa14ba354de5cf4431 (diff)
downloadartery-191ba416c8b611ea4901cead138789a357c56134.tar.gz
artery-191ba416c8b611ea4901cead138789a357c56134.tar.bz2
artery-191ba416c8b611ea4901cead138789a357c56134.zip
I finally had some time to work on this... dependency added on bobbleheadbob's zone addon
Diffstat (limited to 'gamemode/core/npc')
-rw-r--r--gamemode/core/npc/cl_shop.lua141
-rw-r--r--gamemode/core/npc/sv_shop.lua58
2 files changed, 199 insertions, 0 deletions
diff --git a/gamemode/core/npc/cl_shop.lua b/gamemode/core/npc/cl_shop.lua
new file mode 100644
index 0000000..fa076ce
--- /dev/null
+++ b/gamemode/core/npc/cl_shop.lua
@@ -0,0 +1,141 @@
+local inv = nrequire("client/cl_inventory.lua")
+local itm = nrequire("core/inventory/item.lua")
+print("in cl_shop inv is", inv)
+local w,h = ScrW(),ScrH()
+local function DrawShopItemOnDPanel(dp,itemtbl,cost)
+ --An item is a string, and int cost
+
+ local shape = itemtbl.Shape
+ local twidth,theight = 0,#shape
+ for k = 1,#shape do
+ twidth = math.max(twidth,#shape[k])
+ end
+
+ local slotsize = math.Round(w / 32)
+ local backgrid = vgui.Create( "DGrid", dp )
+ backgrid:SetPos( 10, 30 )
+ backgrid:SetCols( twidth )
+ backgrid:SetColWide( theight )
+ backgrid:Dock(LEFT)
+
+ local shopicon = vgui.Create( "DImageButton", dp )
+ shopicon:SetSize(slotsize * twidth, slotsize * theight)
+ shopicon:SetPos(0,0)
+ shopicon:SetText(itemtbl.Name)
+ if itemtbl.Tooltip then
+ shopicon:SetTooltip(itemtbl.Tooltip)
+ end
+ if itemtbl.Paint then
+ shopicon.Paint = itemtbl.Paint
+ end
+ if itemtbl.DoOnPanel then
+ itemtbl.DoOnPanel(shopicon)
+ end
+ shopicon.Paint = function(self, wi, hi)
+ surface.SetDrawColor( 0, 0, 0, 255 )
+ surface.DrawOutlinedRect( 0, 0, wi, hi)
+ end
+ shopicon.DoClick = function()
+ print("You cliked me!")
+ end
+ shopicon.Item = itemtbl
+ shopicon.Cost = cost
+ shopicon.ondropped = function(back,j,i,item)
+ print("ondropped was called!")
+ print("back",back,"j",j,"i",i,"item",item)
+ PrintTable(item)
+ net.Start("buyitem")
+ net.WriteString(item.Name)
+ net.WriteUInt(back,8)
+ net.WriteUInt(j,8)
+ net.WriteUInt(i,8)
+ net.SendToServer()
+ end
+
+ print("Displaying shape:")
+ PrintTable(shape)
+ for k = 1, twidth do
+ for i = 1, theight do
+ if not shape[k][i] then
+ print("Found false spot:",k,i)
+ local emptyslot = vgui.Create("DPanel", dp)
+ emptyslot:SetSize(slotsize,slotsize)
+ emptyslot:SetPos(slotsize * (i - 1) + 2, slotsize * (k - 1) + 2)
+ end
+ end
+ end
+
+ local buybutton = vgui.Create("DButton",dp)
+ buybutton:Dock(RIGHT)
+ buybutton:SetText("Buy\n(" .. cost .. ")")
+ buybutton.DoClick = function()
+ net.Start("art_buyitem")
+ net.WriteString(itemtbl.Name)
+ net.SendToServer()
+ end
+
+end
+
+local slotsize = math.Round(w / 32)
+
+local function DrawShopOnDPanel(dp,items)
+ --This gets pretty involved, lets try to not make it a clusterfuck.
+ dp.Paint = function(self, wi, hi) draw.RoundedBox(4, 0,0,wi,hi,Color(100,0,0)) end
+ print("dp",dp)
+ local scrollpanel = vgui.Create( "DScrollPanel",dp )
+ print("scollpanel",scrollpanel)
+ scrollpanel.Paint = function(self, wi, hi) draw.RoundedBox(4, 0,0,wi,hi,Color(0,0,100)) end
+ scrollpanel:Dock(FILL)
+ for k,v in pairs(items) do
+ local itemtbl = itm.GetItemByName(v[1])
+ local invpanel = vgui.Create( "DPanel", scollpanel)
+ invpanel.Paint = function(self, wi, hi)
+ draw.RoundedBox(4, 1,1,wi-4,hi-4,Color(50,50,50))
+ draw.RoundedBox(4, 2,2,wi-5,hi-5,Color(100,100,100))
+ end
+ print("invpanel",invpanel)
+ DrawShopItemOnDPanel(invpanel,itemtbl,v[2])
+ scrollpanel:AddItem(invpanel)
+ invpanel:Dock(TOP)
+ local x,_ = invpanel:GetSize()
+ print("item is",v)
+ PrintTable(v)
+ invpanel:SetSize(x,slotsize * (#itemtbl.Shape) + 4)
+ invpanel:Dock(TOP)
+
+ end
+
+end
+
+local shopwindow,shoppanel
+
+local function createshopwindow()
+ print("Createing shopwindow")
+ shopwindow = vgui.Create( "DFrame" )
+ shopwindow:SetPos( w - (w / 4), 0 )
+ shopwindow:SetSize( w / 4, h )
+ shopwindow:SetTitle( "Unset shop" )
+ shopwindow:SetDraggable( true )
+ shopwindow.OnClose = function(self)
+ self:SetVisible(false)
+ print("After onclose, shopwindow was",shopwindow)
+ end
+ shopwindow:SetVisible(false)
+
+ shoppanel = vgui.Create( "DPanel",shopwindow)
+ shoppanel:SetPos( 10, 30 ) -- Set the position of the panel
+ shoppanel:Dock(FILL)
+end
+createshopwindow()
+
+net.Receive("art_openshop",function()
+ print("shopwindows was ",shopwindow)
+ if not shopwindow:IsValid() then createshopwindow() end
+ assert(shopwindow,"Shopwindow was not created, even after re-createing!")
+ print("inv was", inv)
+ inv.ShowInventory()
+ shopwindow:SetVisible(true)
+ local stock = net.ReadTable()
+ DrawShopOnDPanel(shoppanel,stock)
+ shopwindow:MakePopup()
+end)
diff --git a/gamemode/core/npc/sv_shop.lua b/gamemode/core/npc/sv_shop.lua
index 2e54f7e..2825996 100644
--- a/gamemode/core/npc/sv_shop.lua
+++ b/gamemode/core/npc/sv_shop.lua
@@ -2,8 +2,28 @@
Create a shop npc
]]
+local itm = nrequire("core/inventory/item.lua")
+
local shop = {}
+for k,v in pairs({
+ "art_openshop",
+ "art_buyitem",
+ "art_sellitem",
+}) do
+ util.AddNetworkString(v)
+end
+
+function shop.OpenShop(tbl, ply)
+ print("Called openshop!")
+ print("tbl was")
+ PrintTable(tbl)
+ if CLIENT then return end
+ net.Start("art_openshop")
+ net.WriteTable(tbl)
+ net.Send(ply)
+end
+
function shop.CreateShop(npc)
print("Createing shop npc")
local npcent = ents.Create("npc_shop")
@@ -14,4 +34,42 @@ function shop.CreateShop(npc)
print("Called spawn")
end
+net.Receive("art_buyitem",function(len,ply)
+ local itemname = net.ReadString()
+
+ --Find the shop near the player
+ local es = ents.FindInSphere(ply:GetPos(),500)
+ local shop
+ for k,v in pairs(es) do
+ if IsValid(v) and v:GetClass() == "npc_shop" then
+ shop = v
+ break
+ end
+ end
+ print("Shop was", shop)
+ print("Items:", shop.shopitems)
+ PrintTable(shop.shopitems)
+
+ --Find the price of the item we want to buy
+ local price
+ for k,v in pairs(shop.shopitems) do
+ if v[1] == itemname then
+ price = v[2]
+ break
+ end
+ end
+
+ --Make sure we have enough credits to buy it
+ if ply:GetCredits() < price then
+ print(ply, " didn't have enough credits to buy a ", itemname, "(" .. price .. ")")
+ else
+ xpcall(function()
+ ply:GiveItem(itm.GetItemByName(itemname))
+ ply:SetCredits(ply:GetCredits() - price)
+ end,function(err)
+ print("Coulden't fit a " , itemname, " into ", ply, "'s inventory: ", err)
+ end)
+ end
+end)
+
return shop