Advertisement
Guest User

Untitled

a guest
Jan 13th, 2015
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.37 KB | None | 0 0
  1.  
  2. Backpack = {
  3.     list = {}
  4. }
  5.  
  6. setmetatable(Backpack, {
  7.     __call = function (self, ...)
  8.         return self:create(...)
  9.     end
  10. })
  11.  
  12. function Backpack:create(player, slots)
  13.     if Backpack.list[player] then
  14.         return Backpack.list[player]
  15.     end
  16.  
  17.     local new = {
  18.         items = {},
  19.         slots = slots or 20,
  20.         owner = player
  21.     }
  22.     setmetatable(new, {__index = Backpack})
  23.     Backpack.list[player] = new
  24.  
  25.     return new
  26. end
  27.  
  28. function Backpack:getSlots()
  29.     return self.slots
  30. end
  31.  
  32. function Backpack:getEmptySlot()
  33.     for i = 1, self.slots do
  34.         if not self.items[i] then
  35.             return i
  36.         end
  37.     end
  38.     return false
  39. end
  40.  
  41. function Backpack:getItems(withAmount)
  42.     local items = {}
  43.  
  44.     for index, slot in pairs(self.items) do
  45.         if not withAmount then
  46.             items[#items + 1] = slot.item
  47.         else
  48.             items[#items + 1] = slot
  49.         end
  50.     end
  51.  
  52.     return items
  53. end
  54.  
  55. function Backpack:hasItem(item)
  56.     for index, slot in pairs(self.items) do
  57.         if slot.item == item then
  58.             return true
  59.         end
  60.     end
  61.     return false
  62. end
  63.  
  64. function Backpack:getOwner()
  65.     return self.owner
  66. end
  67.  
  68. function Backpack:addItem(item, amount)
  69.     if not self:hasItem(item) then
  70.         local slot = self:getEmptySlot()
  71.        
  72.         if slot then
  73.             self.items[slot] = {item = item, amount = amount}
  74.             return true
  75.         else
  76.             return false
  77.         end
  78.     else
  79.         for index, slot in pairs(self.items) do
  80.             if slot.item == item then
  81.                 slot.amount = slot.amount + amount
  82.                 return true
  83.             end
  84.         end
  85.         return false
  86.     end
  87. end
  88.  
  89. function Backpack:removeItem(item, amount)
  90.     if not self:hasItem(item) then
  91.         return false
  92.     end
  93.  
  94.     for index, slot in pairs(self.items) do
  95.         if slot.item == item then
  96.             if not amount or (slot.amount - amount) <= 0 then
  97.                 self.items[index] = nil
  98.             else
  99.                 slot.amount = slot.amount - amount
  100.             end
  101.             return true
  102.         end
  103.     end
  104.  
  105.     return false
  106. end
  107.  
  108. function Backpack:getWeight()
  109.     local weight = 0
  110.  
  111.     for index, slot in pairs(self.items) do
  112.         weight = weight + (slot.item:getWeight() * slot.amount)
  113.     end
  114.  
  115.     return weight
  116. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement