Advertisement
evelynshilosky

StorageSystem - Part 1

Jan 21st, 2025
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 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 InventorySystem inventorySystem;
  10.     private UIManager uiManager;
  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.         inventorySystem = InventorySystem.Instance;
  28.         uiManager = UIManager.Instance;
  29.         if (uiManager == null)
  30.         {
  31.             Debug.LogError("Failed to find UIManager instance");
  32.         }
  33.     }
  34.  
  35.     public void OpenStorage(Item storageItem)
  36.     {
  37.         if (uiManager == null)
  38.         {
  39.             Debug.LogError("UIManager reference is null in StorageSystem");
  40.             return;
  41.         }
  42.  
  43.         if (storageItem == null)
  44.         {
  45.             Debug.LogError("Attempted to open storage for a null item");
  46.             return;
  47.         }
  48.  
  49.         uiManager.ShowInventoryPrompt(storageItem);
  50.     }
  51.  
  52.     public void AddItemToStorage(Item storageItem, Item itemToStore)
  53.     {
  54.         if (!storageInventories.ContainsKey(storageItem))
  55.         {
  56.             storageInventories[storageItem] = new List<Item>();
  57.         }
  58.         storageInventories[storageItem].Add(itemToStore);
  59.     }
  60.  
  61.     public List<Item> GetStorageInventory(Item storageItem)
  62.     {
  63.         if (storageInventories.TryGetValue(storageItem, out List<Item> items))
  64.         {
  65.             return items;
  66.         }
  67.         return new List<Item>();
  68.     }
  69.  
  70.     public void RemoveItemFromStorage(Item storageItem, Item itemToRemove)
  71.     {
  72.         if (storageInventories.TryGetValue(storageItem, out List<Item> items))
  73.         {
  74.             items.Remove(itemToRemove);
  75.         }
  76.     }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement