Guest User

Untitled

a guest
Jan 19th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5.  
  6. public class Inventory : MonoBehaviour {
  7.  
  8.     public static Inventory Instance;
  9.  
  10.     public int maxItems;
  11.  
  12.     public List<BaseItem> items;
  13.     public List<Item> itemsToAddOnStart;
  14.     public int ItemsCount
  15.     {
  16.         get
  17.         {
  18.             return items.Count;
  19.         }
  20.     }
  21.  
  22.     private void Start()
  23.     {
  24.         Instance = this;
  25.  
  26.         for (int i = 0; i < itemsToAddOnStart.Count; i++)
  27.         {
  28.             itemsToAddOnStart[i].AddOnStart();
  29.         }
  30.     }
  31.  
  32.     public void AddItem(BaseItem item)
  33.     {
  34.         if (IsContains(item))
  35.         {
  36.             AddToItem(item);
  37.         }
  38.         else if(items.Count < maxItems)
  39.         {
  40.             items.Add(item);
  41.         }
  42.  
  43.         if(InventoryWindow.Instance != null)
  44.             InventoryWindow.Instance.Init();
  45.     }
  46.  
  47.     public bool IsContains(BaseItem item)
  48.     {
  49.         foreach (BaseItem i in items)
  50.         {
  51.             if (i.type == item.type && i.itemName == item.itemName)
  52.             {
  53.                 return true;
  54.             }
  55.         }
  56.  
  57.         return false;
  58.     }
  59.  
  60.     public void RemoveItem(BaseItem item)
  61.     {
  62.         int ind = items.IndexOf(item);
  63.         if (items[ind].count > 1)
  64.         {
  65.             items[ind].count -= 1;
  66.         }
  67.         else
  68.         {
  69.             items.Remove(item);
  70.         }
  71.  
  72.         if(InventoryWindow.Instance != null)
  73.             InventoryWindow.Instance.Init();
  74.     }
  75.  
  76.     public void RemoveItem(BaseItem item, int count)
  77.     {
  78.         int ind = items.IndexOf(item);
  79.         if (items[ind].count > count)
  80.         {
  81.             items[ind].count -= count;
  82.         }
  83.         else if (items[ind].count == count)
  84.         {
  85.             items.Remove(item);
  86.         }
  87.  
  88.         if(InventoryWindow.Instance != null)
  89.             InventoryWindow.Instance.Init();
  90.     }
  91.  
  92.     public BaseItem GetItemAtIndex(int index)
  93.     {
  94.         return items[index];
  95.     }
  96.  
  97.     private void AddToItem(BaseItem item)
  98.     {
  99.         foreach (BaseItem i in items)
  100.         {
  101.             if (i.type == item.type && i.itemName == item.itemName)
  102.             {
  103.                 i.count += item.count;
  104.             }
  105.         }
  106.  
  107.         if (InventoryWindow.Instance != null)
  108.             InventoryWindow.Instance.Init();
  109.     }
  110.  
  111.     public BaseItem GetItemWithName(string itemName)
  112.     {
  113.         BaseItem item = items.Find(i => i.itemName == itemName);
  114.         return item;
  115.     }
  116.  
  117.     public void UseItemWithName(string itemName, int count, Action<bool,int> onSuccess)
  118.     {
  119.         BaseItem item = GetItemWithName(itemName);
  120.         if (item != null)
  121.         {
  122.             if (item.count >= count)
  123.             {
  124.                 RemoveItem(item, count);
  125.                 onSuccess(true, count);
  126.             }
  127.             else
  128.             {
  129.                 RemoveItem(item, item.count);
  130.                 onSuccess(true, item.count);
  131.             }
  132.         }
  133.         onSuccess(false,0);
  134.     }
  135. }
  136.  
  137.  
  138. public enum ItemType
  139. {
  140.     keys,
  141.     weapons,
  142.     medics,
  143.     books,
  144.     ammo,
  145.     others
  146. }
Add Comment
Please, Sign In to add comment