Advertisement
evelynshilosky

StorageSystem - Part 5

Feb 25th, 2025
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class StorageSystem : MonoBehaviour
  5. {
  6.     public static StorageSystem Instance { get; private set; }
  7.  
  8.     private Dictionary<Item, List<Item>> storageInventories = new Dictionary<Item, List<Item>>();
  9.     private Dictionary<Item, Transform> storageHolders = new Dictionary<Item, Transform>();
  10.     private Dictionary<Item, Dictionary<Item, int>> storageSlotPositions = new Dictionary<Item, Dictionary<Item, int>>();
  11.  
  12.     private void Awake()
  13.     {
  14.         if (Instance == null)
  15.         {
  16.             Instance = this;
  17.             DontDestroyOnLoad(gameObject);
  18.         }
  19.         else
  20.         {
  21.             Destroy(gameObject);
  22.         }
  23.     }
  24.  
  25.     public void RegisterStorage(Item storageItem, Transform holder)
  26.     {
  27.         if (!storageHolders.ContainsKey(storageItem))
  28.         {
  29.             storageHolders.Add(storageItem, holder);
  30.             storageSlotPositions[storageItem] = new Dictionary<Item, int>();
  31.         }
  32.     }
  33.  
  34.     public bool AddToStorage(Item storageItem, Item itemToStore, int slotIndex = -1)
  35.     {
  36.         if (!storageInventories.ContainsKey(storageItem))
  37.         {
  38.             storageInventories[storageItem] = new List<Item>();
  39.         }
  40.  
  41.         if (storageInventories[storageItem].Count < storageItem.storageCapacity)
  42.         {
  43.             storageInventories[storageItem].Add(itemToStore);
  44.             itemToStore.gameObject.SetActive(false);
  45.             itemToStore.transform.SetParent(storageHolders[storageItem]);
  46.  
  47.             if (slotIndex >= 0)
  48.             {
  49.                 storageSlotPositions[storageItem][itemToStore] = slotIndex;
  50.             }
  51.             return true;
  52.         }
  53.  
  54.         return false;
  55.     }
  56.  
  57.     public void RemoveItemFromStorage(Item storageItem, Item itemToRemove)
  58.     {
  59.         if (storageInventories.TryGetValue(storageItem, out List<Item> items))
  60.         {
  61.             items.Remove(itemToRemove);
  62.             itemToRemove.gameObject.SetActive(true);
  63.             itemToRemove.transform.SetParent(null);
  64.  
  65.             if (storageSlotPositions.ContainsKey(storageItem))
  66.             {
  67.                 storageSlotPositions[storageItem].Remove(itemToRemove);
  68.             }
  69.         }
  70.     }
  71.  
  72.     public void OpenStorage(Item storageItem)
  73.     {
  74.         if (UIManager.Instance == null)
  75.         {
  76.             Debug.LogError("UIManager reference is null in StorageSystem");
  77.             return;
  78.         }
  79.  
  80.         if (storageItem == null)
  81.         {
  82.             Debug.LogError("Attempted to open storage for a null item");
  83.             return;
  84.         }
  85.  
  86.         UIManager.Instance.ShowInventoryPrompt(storageItem);
  87.     }
  88.  
  89.     public List<Item> GetStorageInventory(Item storageItem)
  90.     {
  91.         if (storageInventories.TryGetValue(storageItem, out List<Item> items))
  92.         {
  93.             return items;
  94.         }
  95.         return new List<Item>();
  96.     }
  97.  
  98.     public int GetAvailableSlots(Item storageItem)
  99.     {
  100.         if (!storageInventories.ContainsKey(storageItem))
  101.         {
  102.             return storageItem.storageCapacity;
  103.         }
  104.  
  105.         return storageItem.storageCapacity - storageInventories[storageItem].Count;
  106.     }
  107.  
  108.     public void UpdateItemPosition(Item storageItem, Item item, int newSlotIndex)
  109.     {
  110.         if (storageSlotPositions.ContainsKey(storageItem))
  111.         {
  112.             storageSlotPositions[storageItem][item] = newSlotIndex;
  113.         }
  114.     }
  115.  
  116.     public int GetItemSlotIndex(Item storageItem, Item item)
  117.     {
  118.         if (storageSlotPositions.ContainsKey(storageItem) && storageSlotPositions[storageItem].ContainsKey(item))
  119.         {
  120.             return storageSlotPositions[storageItem][item];
  121.         }
  122.         return -1;
  123.     }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement