Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- public class InventorySystem : MonoBehaviour
- {
- public static InventorySystem Instance { get; private set; }
- public int maxSlots = 15;
- public List<Item> inventory = new List<Item>();
- public Item leftHandItem;
- public Item rightHandItem;
- public Item backpack;
- private Dictionary<Item, List<Item>> storageInventories = new Dictionary<Item, List<Item>>();
- public bool IsBackpackEquipped => backpack != null;
- private void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- DontDestroyOnLoad(gameObject);
- }
- else
- {
- Destroy(gameObject);
- }
- }
- public void AddBlanketToBackpack(Item backpack)
- {
- if (!storageInventories.ContainsKey(backpack)) return;
- foreach (var item in storageInventories[backpack])
- {
- if (item.isBlanket) return; // Blanket already exists
- }
- GameObject blanketObject = UIManager.Instance.blanketPrefab;
- if (blanketObject != null)
- {
- Item blanketItem = Instantiate(blanketObject).GetComponent<Item>();
- storageInventories[backpack].Add(blanketItem);
- }
- }
- public void RemoveBlanketFromBackpack(Item backpack)
- {
- if (!storageInventories.ContainsKey(backpack)) return;
- Item blanketToRemove = null;
- foreach (var item in storageInventories[backpack])
- {
- if (item.isBlanket)
- {
- blanketToRemove = item;
- break;
- }
- }
- if (blanketToRemove != null)
- {
- storageInventories[backpack].Remove(blanketToRemove);
- Destroy(blanketToRemove.gameObject);
- }
- }
- public void HandleBackpackInOtherStorage(Item backpack, Item storageContainer, bool isAdding)
- {
- if (!storageInventories.ContainsKey(storageContainer)) return;
- if (isAdding)
- {
- AddBlanketToBackpack(backpack); // Add blanket when backpack is stored in another container
- }
- else
- {
- RemoveBlanketFromBackpack(backpack); // Remove blanket when backpack is removed from another container
- }
- }
- public bool AddItem(Item item)
- {
- if (inventory.Count < maxSlots)
- {
- inventory.Add(item);
- return true;
- }
- return false;
- }
- public void RemoveItem(Item item)
- {
- inventory.Remove(item);
- }
- public bool HasItem(Item item)
- {
- return inventory.Contains(item);
- }
- public void EquipItem(Item item, bool isLeftHand, bool isTwoHanded)
- {
- if (isTwoHanded)
- {
- leftHandItem = item;
- rightHandItem = item;
- }
- else if (isLeftHand)
- {
- leftHandItem = item;
- }
- else
- {
- rightHandItem = item;
- }
- }
- public void UnequipItem(bool isLeftHand)
- {
- if (leftHandItem != null && leftHandItem.isTwoHanded)
- {
- leftHandItem = null;
- rightHandItem = null;
- }
- else if (isLeftHand)
- {
- leftHandItem = null;
- }
- else
- {
- rightHandItem = null;
- }
- }
- public void EquipBackpack(Item backpackItem)
- {
- if (backpack != null)
- {
- inventory.Add(backpack); // Add the previous backpack back to the inventory
- }
- backpack = backpackItem; // Equip the new backpack
- inventory.Remove(backpackItem); // Remove it from the hand-held items
- StorageSystem.Instance.InitializeStorage(backpack); // Initialize its storage functionality
- }
- public void UnequipBackpack()
- {
- if (backpack != null)
- {
- inventory.Add(backpack);
- backpack = null;
- }
- }
- public void AddItemToStorage(Item storageItem, Item itemToStore)
- {
- if (!storageInventories.ContainsKey(storageItem))
- {
- storageInventories[storageItem] = new List<Item>();
- }
- storageInventories[storageItem].Add(itemToStore);
- }
- public List<Item> GetStorageInventory(Item storageItem)
- {
- if (storageInventories.TryGetValue(storageItem, out List<Item> items))
- {
- return items;
- }
- return new List<Item>();
- }
- public void RemoveItemFromStorage(Item storageItem, Item itemToRemove)
- {
- if (storageInventories.TryGetValue(storageItem, out List<Item> items))
- {
- items.Remove(itemToRemove);
- }
- }
- public bool TryStoreItem(Item storageItem, Item item)
- {
- if (storageInventories[storageItem].Count >= storageItem.storageCapacity)
- {
- UIManager.Instance.ShowError("Storage is full!");
- return false;
- }
- storageInventories[storageItem].Add(item);
- item.transform.SetParent(UIManager.Instance.storageHolder);
- item.gameObject.SetActive(false);
- return true;
- }
- public bool TryWithdrawItem(Item storageItem, Item item)
- {
- if (leftHandItem != null && rightHandItem != null)
- {
- UIManager.Instance.ShowError("Hands are full!");
- return false;
- }
- if (storageInventories[storageItem].Remove(item))
- {
- EquipItem(item, leftHandItem == null, false);
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment