evelynshilosky

InventorySystem - Part 6.3.4

May 2nd, 2025
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.63 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 void AddBlanketToBackpack(Item backpack)
  32.     {
  33.         if (!storageInventories.ContainsKey(backpack)) return;
  34.  
  35.         foreach (var item in storageInventories[backpack])
  36.         {
  37.             if (item.isBlanket) return; // Blanket already exists
  38.         }
  39.  
  40.         GameObject blanketObject = UIManager.Instance.blanketPrefab;
  41.         if (blanketObject != null)
  42.         {
  43.             Item blanketItem = Instantiate(blanketObject).GetComponent<Item>();
  44.             storageInventories[backpack].Add(blanketItem);
  45.         }
  46.     }
  47.  
  48.     public void RemoveBlanketFromBackpack(Item backpack)
  49.     {
  50.         if (!storageInventories.ContainsKey(backpack)) return;
  51.  
  52.         Item blanketToRemove = null;
  53.         foreach (var item in storageInventories[backpack])
  54.         {
  55.             if (item.isBlanket)
  56.             {
  57.                 blanketToRemove = item;
  58.                 break;
  59.             }
  60.         }
  61.  
  62.         if (blanketToRemove != null)
  63.         {
  64.             storageInventories[backpack].Remove(blanketToRemove);
  65.             Destroy(blanketToRemove.gameObject);
  66.         }
  67.     }
  68.  
  69.     public void HandleBackpackInOtherStorage(Item backpack, Item storageContainer, bool isAdding)
  70.     {
  71.         if (!storageInventories.ContainsKey(storageContainer)) return;
  72.  
  73.         if (isAdding)
  74.         {
  75.             AddBlanketToBackpack(backpack); // Add blanket when backpack is stored in another container
  76.         }
  77.         else
  78.         {
  79.             RemoveBlanketFromBackpack(backpack); // Remove blanket when backpack is removed from another container
  80.         }
  81.     }
  82.  
  83.     public bool AddItem(Item item)
  84.     {
  85.         if (inventory.Count < maxSlots)
  86.         {
  87.             inventory.Add(item);
  88.             return true;
  89.         }
  90.         return false;
  91.     }
  92.  
  93.     public void RemoveItem(Item item)
  94.     {
  95.         inventory.Remove(item);
  96.     }
  97.  
  98.     public bool HasItem(Item item)
  99.     {
  100.         return inventory.Contains(item);
  101.     }
  102.  
  103.     public void EquipItem(Item item, bool isLeftHand, bool isTwoHanded)
  104.     {
  105.         if (isTwoHanded)
  106.         {
  107.             leftHandItem = item;
  108.             rightHandItem = item;
  109.         }
  110.         else if (isLeftHand)
  111.         {
  112.             leftHandItem = item;
  113.         }
  114.         else
  115.         {
  116.             rightHandItem = item;
  117.         }
  118.     }
  119.  
  120.     public void UnequipItem(bool isLeftHand)
  121.     {
  122.         if (leftHandItem != null && leftHandItem.isTwoHanded)
  123.         {
  124.             leftHandItem = null;
  125.             rightHandItem = null;
  126.         }
  127.         else if (isLeftHand)
  128.         {
  129.             leftHandItem = null;
  130.         }
  131.         else
  132.         {
  133.             rightHandItem = null;
  134.         }
  135.     }
  136.  
  137.     public void EquipBackpack(Item backpackItem)
  138.     {
  139.         if (backpack != null)
  140.         {
  141.             inventory.Add(backpack); // Add the previous backpack back to the inventory
  142.         }
  143.  
  144.         backpack = backpackItem; // Equip the new backpack
  145.         inventory.Remove(backpackItem); // Remove it from the hand-held items
  146.  
  147.         StorageSystem.Instance.InitializeStorage(backpack); // Initialize its storage functionality
  148.     }
  149.  
  150.     public void UnequipBackpack()
  151.     {
  152.         if (backpack != null)
  153.         {
  154.             inventory.Add(backpack);
  155.             backpack = null;
  156.         }
  157.     }
  158.  
  159.     public void AddItemToStorage(Item storageItem, Item itemToStore)
  160.     {
  161.         if (!storageInventories.ContainsKey(storageItem))
  162.         {
  163.             storageInventories[storageItem] = new List<Item>();
  164.         }
  165.         storageInventories[storageItem].Add(itemToStore);
  166.     }
  167.  
  168.     public List<Item> GetStorageInventory(Item storageItem)
  169.     {
  170.         if (storageInventories.TryGetValue(storageItem, out List<Item> items))
  171.         {
  172.             return items;
  173.         }
  174.         return new List<Item>();
  175.     }
  176.  
  177.     public void RemoveItemFromStorage(Item storageItem, Item itemToRemove)
  178.     {
  179.         if (storageInventories.TryGetValue(storageItem, out List<Item> items))
  180.         {
  181.             items.Remove(itemToRemove);
  182.         }
  183.     }
  184.  
  185.     public bool TryStoreItem(Item storageItem, Item item)
  186.     {
  187.         if (storageInventories[storageItem].Count >= storageItem.storageCapacity)
  188.         {
  189.             UIManager.Instance.ShowError("Storage is full!");
  190.             return false;
  191.         }
  192.  
  193.         storageInventories[storageItem].Add(item);
  194.         item.transform.SetParent(UIManager.Instance.storageHolder);
  195.         item.gameObject.SetActive(false);
  196.         return true;
  197.     }
  198.  
  199.     public bool TryWithdrawItem(Item storageItem, Item item)
  200.     {
  201.         if (leftHandItem != null && rightHandItem != null)
  202.         {
  203.             UIManager.Instance.ShowError("Hands are full!");
  204.             return false;
  205.         }
  206.  
  207.         if (storageInventories[storageItem].Remove(item))
  208.         {
  209.             EquipItem(item, leftHandItem == null, false);
  210.             return true;
  211.         }
  212.         return false;
  213.     }
  214. }
  215.  
Advertisement
Add Comment
Please, Sign In to add comment