Advertisement
evelynshilosky

StorageSystem - Part 6.1

Mar 27th, 2025
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.45 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.     private void Start()
  26.     {
  27.         if (InventorySystem.Instance.backpack != null)
  28.         {
  29.             InitializeStorage(InventorySystem.Instance.backpack);
  30.         }
  31.     }
  32.  
  33.     public void InitializeStorage(Item storageItem)
  34.     {
  35.         if (!storageInventories.ContainsKey(storageItem))
  36.         {
  37.             storageInventories[storageItem] = new List<Item>();
  38.             storageHolders[storageItem] = storageItem.transform;
  39.             storageSlotPositions[storageItem] = new Dictionary<Item, int>();
  40.         }
  41.     }
  42.  
  43.     public void RegisterStorage(Item storageItem, Transform holder)
  44.     {
  45.         if (!storageHolders.ContainsKey(storageItem))
  46.         {
  47.             storageHolders.Add(storageItem, holder);
  48.             storageSlotPositions[storageItem] = new Dictionary<Item, int>();
  49.         }
  50.     }
  51.  
  52.     public bool AddToStorage(Item storageItem, Item itemToStore, int slotIndex = -1)
  53.     {
  54.         if (storageItem == null)
  55.         {
  56.             Debug.LogError("Attempted to add item to null storage");
  57.             return false;
  58.         }
  59.  
  60.         if (!storageInventories.ContainsKey(storageItem))
  61.         {
  62.             InitializeStorage(storageItem);
  63.         }
  64.  
  65.         if (storageInventories[storageItem].Count < storageItem.storageCapacity)
  66.         {
  67.             storageInventories[storageItem].Add(itemToStore);
  68.             itemToStore.gameObject.SetActive(false);
  69.  
  70.             if (storageHolders.ContainsKey(storageItem))
  71.             {
  72.                 itemToStore.transform.SetParent(storageHolders[storageItem]);
  73.             }
  74.  
  75.             if (slotIndex >= 0)
  76.             {
  77.                 storageSlotPositions[storageItem][itemToStore] = slotIndex;
  78.             }
  79.             return true;
  80.         }
  81.         return false;
  82.     }
  83.  
  84.     public void RemoveItemFromStorage(Item storageItem, Item itemToRemove)
  85.     {
  86.         if (storageInventories.TryGetValue(storageItem, out List<Item> items))
  87.         {
  88.             items.Remove(itemToRemove);
  89.             itemToRemove.gameObject.SetActive(true);
  90.             itemToRemove.transform.SetParent(null);
  91.  
  92.             if (storageSlotPositions.ContainsKey(storageItem))
  93.             {
  94.                 storageSlotPositions[storageItem].Remove(itemToRemove);
  95.             }
  96.         }
  97.     }
  98.  
  99.     public void OpenStorage(Item storageItem)
  100.     {
  101.         if (UIManager.Instance == null)
  102.         {
  103.             Debug.LogError("UIManager reference is null in StorageSystem");
  104.             return;
  105.         }
  106.  
  107.         if (storageItem == null)
  108.         {
  109.             Debug.LogError("Attempted to open storage for a null item");
  110.             return;
  111.         }
  112.  
  113.         UIManager.Instance.ShowInventoryPrompt(storageItem);
  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 int GetAvailableSlots(Item storageItem)
  126.     {
  127.         if (!storageInventories.ContainsKey(storageItem))
  128.         {
  129.             return storageItem.storageCapacity;
  130.         }
  131.         return storageItem.storageCapacity - storageInventories[storageItem].Count;
  132.     }
  133.  
  134.     public void UpdateItemPosition(Item storageItem, Item item, int newSlotIndex)
  135.     {
  136.         if (storageSlotPositions.ContainsKey(storageItem))
  137.         {
  138.             storageSlotPositions[storageItem][item] = newSlotIndex;
  139.         }
  140.     }
  141.  
  142.     public int GetItemSlotIndex(Item storageItem, Item item)
  143.     {
  144.         if (storageSlotPositions.ContainsKey(storageItem) && storageSlotPositions[storageItem].ContainsKey(item))
  145.         {
  146.             return storageSlotPositions[storageItem][item];
  147.         }
  148.         return -1;
  149.     }
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement