Advertisement
evelynshilosky

StorageSystem - Part 3 & 4

Feb 7th, 2025
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 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.  
  11.     private void Awake()
  12.     {
  13.         if (Instance == null)
  14.         {
  15.             Instance = this;
  16.             DontDestroyOnLoad(gameObject);
  17.         }
  18.         else
  19.         {
  20.             Destroy(gameObject);
  21.         }
  22.     }
  23.  
  24.     public void RegisterStorage(Item storageItem, Transform holder)
  25.     {
  26.         if (!storageHolders.ContainsKey(storageItem))
  27.         {
  28.             storageHolders.Add(storageItem, holder);
  29.         }
  30.     }
  31.  
  32.     public bool AddToStorage(Item storageItem, Item itemToStore)
  33.     {
  34.         if (!storageInventories.ContainsKey(storageItem))
  35.         {
  36.             storageInventories[storageItem] = new List<Item>();
  37.         }
  38.  
  39.         if (storageInventories[storageItem].Count < storageItem.storageCapacity)
  40.         {
  41.             storageInventories[storageItem].Add(itemToStore);
  42.             itemToStore.gameObject.SetActive(false);
  43.             itemToStore.transform.SetParent(storageHolders[storageItem]);
  44.             return true;
  45.         }
  46.  
  47.         return false;
  48.     }
  49.  
  50.     public void RemoveItemFromStorage(Item storageItem, Item itemToRemove)
  51.     {
  52.         if (storageInventories.TryGetValue(storageItem, out List<Item> items))
  53.         {
  54.             items.Remove(itemToRemove);
  55.             itemToRemove.gameObject.SetActive(true);
  56.             itemToRemove.transform.SetParent(null);
  57.         }
  58.     }
  59.  
  60.     public void OpenStorage(Item storageItem)
  61.     {
  62.         if (UIManager.Instance == null)
  63.         {
  64.             Debug.LogError("UIManager reference is null in StorageSystem");
  65.             return;
  66.         }
  67.  
  68.         if (storageItem == null)
  69.         {
  70.             Debug.LogError("Attempted to open storage for a null item");
  71.             return;
  72.         }
  73.  
  74.         UIManager.Instance.ShowInventoryPrompt(storageItem);
  75.     }
  76.  
  77.     public List<Item> GetStorageInventory(Item storageItem)
  78.     {
  79.         if (storageInventories.TryGetValue(storageItem, out List<Item> items))
  80.         {
  81.             return items;
  82.         }
  83.         return new List<Item>();
  84.     }
  85.  
  86.     public int GetAvailableSlots(Item storageItem)
  87.     {
  88.         if (!storageInventories.ContainsKey(storageItem))
  89.         {
  90.             return storageItem.storageCapacity;
  91.         }
  92.  
  93.         return storageItem.storageCapacity - storageInventories[storageItem].Count;
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement