Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- 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>>();
- private void Awake()
- {
- if (Instance == null)
- {
- Instance = this;
- DontDestroyOnLoad(gameObject);
- }
- else
- {
- Destroy(gameObject);
- }
- }
- 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);
- }
- backpack = backpackItem;
- inventory.Remove(backpackItem);
- }
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement