evelynshilosky

InventorySystem - Part 6.1

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