Advertisement
evelynshilosky

InventorySystem - Part 3 & 4

Feb 7th, 2025
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.78 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class InventorySystem : MonoBehaviour
  5. {
  6.     public static InventorySystem Instance { get; private set; }
  7.  
  8.     public int maxSlots = 15;
  9.     public List<Item> inventory = new List<Item>();
  10.     public Item leftHandItem;
  11.     public Item rightHandItem;
  12.     public Item backpack;
  13.  
  14.     private Dictionary<Item, List<Item>> storageInventories = new Dictionary<Item, List<Item>>();
  15.  
  16.     private void Awake()
  17.     {
  18.         if (Instance == null)
  19.         {
  20.             Instance = this;
  21.             DontDestroyOnLoad(gameObject);
  22.         }
  23.         else
  24.         {
  25.             Destroy(gameObject);
  26.         }
  27.     }
  28.  
  29.     public bool AddItem(Item item)
  30.     {
  31.         if (inventory.Count < maxSlots)
  32.         {
  33.             inventory.Add(item);
  34.             return true;
  35.         }
  36.         return false;
  37.     }
  38.  
  39.     public void RemoveItem(Item item)
  40.     {
  41.         inventory.Remove(item);
  42.     }
  43.  
  44.     public bool HasItem(Item item)
  45.     {
  46.         return inventory.Contains(item);
  47.     }
  48.  
  49.     public void EquipItem(Item item, bool isLeftHand, bool isTwoHanded)
  50.     {
  51.         if (isTwoHanded)
  52.         {
  53.             leftHandItem = item;
  54.             rightHandItem = item;
  55.         }
  56.         else if (isLeftHand)
  57.         {
  58.             leftHandItem = item;
  59.         }
  60.         else
  61.         {
  62.             rightHandItem = item;
  63.         }
  64.     }
  65.  
  66.     public void UnequipItem(bool isLeftHand)
  67.     {
  68.         if (leftHandItem != null && leftHandItem.isTwoHanded)
  69.         {
  70.             leftHandItem = null;
  71.             rightHandItem = null;
  72.         }
  73.         else if (isLeftHand)
  74.         {
  75.             leftHandItem = null;
  76.         }
  77.         else
  78.         {
  79.             rightHandItem = null;
  80.         }
  81.     }
  82.  
  83.     public void EquipBackpack(Item backpackItem)
  84.     {
  85.         if (backpack != null)
  86.         {
  87.             inventory.Add(backpack);
  88.         }
  89.         backpack = backpackItem;
  90.         inventory.Remove(backpackItem);
  91.     }
  92.  
  93.     public void UnequipBackpack()
  94.     {
  95.         if (backpack != null)
  96.         {
  97.             inventory.Add(backpack);
  98.             backpack = null;
  99.         }
  100.     }
  101.  
  102.     public void AddItemToStorage(Item storageItem, Item itemToStore)
  103.     {
  104.         if (!storageInventories.ContainsKey(storageItem))
  105.         {
  106.             storageInventories[storageItem] = new List<Item>();
  107.         }
  108.         storageInventories[storageItem].Add(itemToStore);
  109.     }
  110.  
  111.     public List<Item> GetStorageInventory(Item storageItem)
  112.     {
  113.         if (storageInventories.TryGetValue(storageItem, out List<Item> items))
  114.         {
  115.             return items;
  116.         }
  117.         return new List<Item>();
  118.     }
  119.  
  120.     public void RemoveItemFromStorage(Item storageItem, Item itemToRemove)
  121.     {
  122.         if (storageInventories.TryGetValue(storageItem, out List<Item> items))
  123.         {
  124.             items.Remove(itemToRemove);
  125.         }
  126.     }
  127.  
  128.     public bool TryStoreItem(Item storageItem, Item item)
  129.     {
  130.         if (storageInventories[storageItem].Count >= storageItem.storageCapacity)
  131.         {
  132.             UIManager.Instance.ShowError("Storage is full!");
  133.             return false;
  134.         }
  135.  
  136.         storageInventories[storageItem].Add(item);
  137.         item.transform.SetParent(UIManager.Instance.storageHolder);
  138.         item.gameObject.SetActive(false);
  139.         return true;
  140.     }
  141.  
  142.     public bool TryWithdrawItem(Item storageItem, Item item)
  143.     {
  144.         if (leftHandItem != null && rightHandItem != null)
  145.         {
  146.             UIManager.Instance.ShowError("Hands are full!");
  147.             return false;
  148.         }
  149.  
  150.         if (storageInventories[storageItem].Remove(item))
  151.         {
  152.             EquipItem(item, leftHandItem == null, false);
  153.             return true;
  154.         }
  155.         return false;
  156.     }
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement