aboutsummaryrefslogtreecommitdiff
path: root/gamemode/inventorysystem
diff options
context:
space:
mode:
Diffstat (limited to 'gamemode/inventorysystem')
-rw-r--r--gamemode/inventorysystem/exampleinventory.lua111
1 files changed, 111 insertions, 0 deletions
diff --git a/gamemode/inventorysystem/exampleinventory.lua b/gamemode/inventorysystem/exampleinventory.lua
new file mode 100644
index 0000000..b8c539f
--- /dev/null
+++ b/gamemode/inventorysystem/exampleinventory.lua
@@ -0,0 +1,111 @@
+---An example inventory.
+-- An inventory that can hold any kind of item. Inventories in Artery are pretty flexible, when you get started, decide on how you want to represent items in your inventory. Inventories use a "position table" to talk about where items are in the inventory. What the position table's structure is can be different for each type of inventory, but each inventory should not use more than one kind of position table. If you find yourself needing more than 1 position table, consider making 2 different inventories.
+--@classmod invtbl
+
+local inv = {}
+
+---Inventory name.
+-- All inventories must have a name, names must be unique per TYPE of inventory.
+inv.Name = "Example Inventory"
+
+inv.items = {}
+
+---Finds a spot for an item.
+-- Finds a place in this inventory to place an item. Returns nil if the item cannot fit.
+--@tparam invtbl self The instace we want to put the item into
+--@tparam itemtbl itemtbl The item we want to put into the inventory
+--@treturn table|nil Returns a position table if the item can fit, or nil if it cannot.
+function inv.FindPlaceFor(self,itemtbl)
+ return {#self.items + 1}
+end
+
+---Check if the item can fit in the given position.
+-- Finds if an item can be placed at a given position in this inventory
+--@tparam invtbl self The instance to put the item into
+--@tparam table position The position to put the item into
+--@tparam itemtbl item The item to try to put in
+--@treturn boolean True if the item can fit, false if it cannot
+function inv.CanFitIn(self,position,item)
+ return self.items[position[1]] == nil
+end
+
+---Puts an item into the inventory.
+-- Puts an item into the inventory at a given position
+--@tparam invtbl self The inventory instance
+--@tparam table position The position to put the item into
+--@tparam itemtbl item The item to put into the inventory
+function inv.Put(self,position,item)
+ self.items[position[1]] = item
+end
+
+---Checks if this inventory has an item.
+-- This method checks if an inventory has an item, this can be passed EITHER a string, item name, OR a function that takes a itemtbl as it's single parameter and returns True if the item is what is being looked for or false if it is not.
+--@tparam invtbl self The instance to look at
+--@tparam string|function ptr The string or compare function to look for
+--@treturn table|nil Returns the position of the item if the item was found, or nil if it was not
+function inv.Has(self,ptr)
+ for k,v in pairs(self.items) do
+ if type(ptr) == "function" then
+ if ptr(v) then
+ return {k}
+ end
+ elseif type(ptr) == "string" then
+ if v.Name == ptr then
+ return {k}
+ end
+ else
+ error("Passed a " .. type(ptr) .. " to example inventory's Has() function")
+ end
+ end
+end
+
+---Removes an item from the inventory.
+-- Removes an item by position, returns the item that was removed
+--@tparam invtbl self The inventory instance to remove from
+--@tparam table position The position to remove the item from
+--@treturn itemtbl The item that was removed
+--@raises error May error if the given position does not contain an item.
+function inv.Remove(self,position)
+ local item = self.items[position[1]]
+ self.items[position[1]] = nil
+ return item
+end
+
+---Gets the item at a given position.
+-- Returns the item at a given position in the inventory. May error if the given position does not have an item.
+--@tparam invtbl self The instance to get from
+--@tparam table position The position to look in
+--@treturn itemtbl The item at the given position
+--@raises error May error if the given position does not contain an item
+function inv.Get(self,position)
+ return self.items[position[1]]
+end
+
+---Serailizes the inventory.
+-- Returns a string that the inventory can be rebuilt from. This should include all items currently in the inventory. Take advantage of the fact that items must also have a Serailize() method.
+--@tparam invtbl self The instance of the inventory to serialize
+--@treturn string The string representation of this inventory
+function inv.Serailize(self)
+ local items = {}
+ for k,v in pairs(self.items) do
+ items[k] = {v.Name,v:Serialize()}
+ end
+ return util.TableToJSON(items)
+end
+
+---Deserializes an inventory.
+-- Takes the string returned by the Serialize() function and returns the inventory re-made
+--@tparam invtbl self The prototype table, you probably want to make a table.Copy() of this
+--@tparam string data The string returned by Serialize()
+--@treturn invtbl The table after it has been deserialized
+function inv.DeSerialize(self,str)
+ local cpy = table.Copy(self)
+ local json = util.JSONToTable(str)
+ for k,v in pairs(json) do
+ local item = itm.GetItemFromData(v[1],v[2])
+ cpy.items[k] = item
+ end
+ return cpy
+end
+
+RegisterInventory(inv)