diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2017-01-04 23:27:36 -0500 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2017-01-04 23:27:36 -0500 |
| commit | 4879eb1d78520ce0ac9b0bb0ef5244cf65ad7c99 (patch) | |
| tree | ac47724191a8959c19b2408d4da384d64b6098ec /gamemode/core/inventory/item.lua | |
| parent | 2c4329e2b6e19182a441f79a5c3010011f8ae767 (diff) | |
| download | artery-4879eb1d78520ce0ac9b0bb0ef5244cf65ad7c99.tar.gz artery-4879eb1d78520ce0ac9b0bb0ef5244cf65ad7c99.tar.bz2 artery-4879eb1d78520ce0ac9b0bb0ef5244cf65ad7c99.zip | |
Started refactoring item and inventory system
Diffstat (limited to 'gamemode/core/inventory/item.lua')
| -rw-r--r-- | gamemode/core/inventory/item.lua | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/gamemode/core/inventory/item.lua b/gamemode/core/inventory/item.lua new file mode 100644 index 0000000..ef6ec1d --- /dev/null +++ b/gamemode/core/inventory/item.lua @@ -0,0 +1,67 @@ +--[[ + An itemsystem + public functions: + RegisterItem(table_item) ::nil + Registers a new item + GetItemByName(string_name) ::table_item + Returns the master copy of an item by name + DeriveItem(table_item,string_name) ::nil + Sets the table to a copy of an item. Copying happens in order, so if two items we derive from have a "DoThing" method, and "DoThing" is not defined in the item, the second derived item's "DoThing" method gets called on .DoThing() + Item: + item.Name ::string + Items must have unique names + item:Serialize() ::string + Turn any instace specific data of this item into a string, should be able to recreate an exact copy of this item in DeSerialize with this data + item:DeSerialize(string_data) ::nil + Recreate an item. If this item has any instance specific data, it should return a table.Copy(self) with the appropriate fields set. + The above must be defined for every item + Items may also have methods from one or more interfaces registered with RegisterInterface +]] +local log = nrequire("log.lua") + +local itm = {} +local required_fields = { + "Name","Serialize","DeSerialize" +} + +local items = {} --Master table of all item prototypes +function itm.RegisterItem(tbl) + for k,v in pairs(required_fields) do + assert(tbl[v] ~= nil, string.format("Attempted to register item without field %q",v)) + end + assert(items[tbl.Name] == nil, string.format("Attempted to register 2 items with the same name %q",tbl.Name)) + log.debug("Registered item: " .. tbl.Name) +end + +function itm.GetItemByName(name) + assert(items[name] ~= nil,string.format("Attempted to get item with invalid name %q",name)) + return items[name] +end + +function itm.GetItemFromData(name,data) + assert(items[name] ~= nil,string.format("Attempted to get item with invalid name %q",name)) + return items[name]:DeSerialize(data) +end + +--Must be called in a coroutine. +function itm.DeriveItem(tbl,name) + while items[name] == nil do + coroutine.yield() + end + --Create a flywieght copy + local ret = tbl + local mt = { + __index = function(tbl,key) + return items[name][key] + end + } + setmetatable(ret,mt) +end + +concommand.Add("art_printitems",function() + for k,v in pairs(items) do + print(k) + end +end) + +return itm |
