Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- public class StorageSystem : MonoBehaviour
- {
- public static StorageSystem Instance { get; private set; }
- private Dictionary<Item, List<Item>> storageInventories = new Dictionary<Item, List<Item>>();
- private Dictionary<Item, Transform> storageHolders = new Dictionary<Item, Transform>();
- private void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- DontDestroyOnLoad(gameObject);
- }
- else
- {
- Destroy(gameObject);
- }
- }
- public void RegisterStorage(Item storageItem, Transform holder)
- {
- if (!storageHolders.ContainsKey(storageItem))
- {
- storageHolders.Add(storageItem, holder);
- }
- }
- public bool AddToStorage(Item storageItem, Item itemToStore)
- {
- if (!storageInventories.ContainsKey(storageItem))
- {
- storageInventories[storageItem] = new List<Item>();
- }
- if (storageInventories[storageItem].Count < storageItem.storageCapacity)
- {
- storageInventories[storageItem].Add(itemToStore);
- itemToStore.gameObject.SetActive(false);
- itemToStore.transform.SetParent(storageHolders[storageItem]);
- return true;
- }
- return false;
- }
- public void RemoveItemFromStorage(Item storageItem, Item itemToRemove)
- {
- if (storageInventories.TryGetValue(storageItem, out List<Item> items))
- {
- items.Remove(itemToRemove);
- itemToRemove.gameObject.SetActive(true);
- itemToRemove.transform.SetParent(null);
- }
- }
- public void OpenStorage(Item storageItem)
- {
- if (UIManager.Instance == null)
- {
- Debug.LogError("UIManager reference is null in StorageSystem");
- return;
- }
- if (storageItem == null)
- {
- Debug.LogError("Attempted to open storage for a null item");
- return;
- }
- UIManager.Instance.ShowInventoryPrompt(storageItem);
- }
- public List<Item> GetStorageInventory(Item storageItem)
- {
- if (storageInventories.TryGetValue(storageItem, out List<Item> items))
- {
- return items;
- }
- return new List<Item>();
- }
- public int GetAvailableSlots(Item storageItem)
- {
- if (!storageInventories.ContainsKey(storageItem))
- {
- return storageItem.storageCapacity;
- }
- return storageItem.storageCapacity - storageInventories[storageItem].Count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement