all repos — RPG @ e473e7dcfb2acd82870d5f2ccbbd3d9539e07c45

Fully functional 3D turn based role playing game coded in C# and base Unity.

Assets/Scripts/Utility/Inventory/Inventory.cs (view raw)

  1using System.Collections;
  2using System.Collections.Generic;
  3using UnityEngine;
  4
  5public class Inventory
  6{
  7    private ItemInfo nothing = new ItemInfo(new Nothing(), -1);
  8
  9    private Dictionary<string, ItemInfo> content;
 10    
 11
 12    //constructor
 13    public Inventory()
 14    {
 15        content = new Dictionary<string, ItemInfo>();
 16        cleanContent();
 17    }
 18
 19    public Inventory(Inventory inventory)
 20    {
 21        foreach(ItemInfo item in new List<ItemInfo>(inventory.getContents()))
 22        {
 23            Debug.Log("adding " + item.item.name);
 24            generateItem(item);
 25            Debug.Log("added " + item.item.name);
 26        }
 27    }
 28
 29    public Inventory(IEnumerable<ItemInfo> list)
 30    {
 31        foreach (ItemInfo item in list)
 32        {
 33            generateItem(item);
 34        }
 35    }
 36
 37    private void cleanContent()
 38    {
 39        List<string> delete = new List<string>();
 40        foreach (KeyValuePair<string, ItemInfo> kv in content)
 41        {
 42            if (kv.Value.quantity == 0)
 43            {
 44                delete.Add(kv.Key);
 45            }
 46        }
 47
 48        foreach(string s in delete)
 49        {
 50            content.Remove(s);
 51        }
 52    }
 53
 54    public List<ItemInfo> getContents()
 55    {
 56        cleanContent();
 57        List<ItemInfo> tmp = new List<ItemInfo>();
 58        foreach (KeyValuePair<string, ItemInfo> kv in content)
 59        {
 60            if(kv.Value.quantity > 0)
 61            {
 62                tmp.Add(kv.Value);
 63            }
 64        }
 65
 66        if (tmp.Count == 0)
 67        {
 68            tmp.Add(nothing);
 69        }
 70
 71        return tmp;
 72    }
 73
 74    public int countItems(string key)
 75    {
 76        ItemInfo tmp;
 77        if (content == null)
 78        {
 79            content = new Dictionary<string, ItemInfo>();
 80            return 0;
 81        }
 82        if(content.TryGetValue(key, out tmp))
 83        {
 84            return tmp.quantity;
 85        } else
 86        {
 87            return 0;
 88        }
 89    }
 90    
 91    public void generateItem(ItemInfo ii)
 92    {
 93        if(countItems(ii.item.name) <= 0)
 94        {
 95            content.Add(ii.item.name, new ItemInfo(ii.item, ii.quantity));
 96        } else
 97        {
 98            content[ii.item.name].quantity += ii.quantity;
 99        }
100        cleanContent();
101    }
102    public void destroyItem(string key, int quantity)
103    {
104        if (countItems(key) < 1)
105        {
106            return;
107        }
108        else
109        {
110            if(content[key].quantity < quantity)
111            {
112                content[key].quantity = 0;
113            } else
114            {
115                content[key].quantity -= quantity;
116            }
117        }
118        cleanContent();
119    }
120    public ItemInfo readItem(string key)
121    {
122        ItemInfo tmp;
123        if (content.TryGetValue(key, out tmp))
124        {
125            return tmp;
126        }
127        else
128        {
129            return null;
130        }
131    }
132
133    public void transferItem(string name, Inventory other)
134    {
135        if (other.countItems(name) < 1)
136        {
137            return;
138        } else
139        {
140            generateItem(other.readItem(name));
141            other.destroyItem(name, other.readItem(name).quantity);
142        }
143        cleanContent();
144    }
145
146    public void addInventory(Inventory inv)
147    {
148        cleanContent();
149        foreach (ItemInfo info in inv.getContents())
150        {
151            if(!(info.item is Nothing))
152            {
153                transferItem(info.item.name, inv);
154            }
155        }
156    }
157}