Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using VHS;
- public class InventoryUI : MonoBehaviour
- {
- Inventory inventory;
- public bool isItemAdded;
- public static InventoryUI inventoryui;
- public GameObject Player;
- public GameObject inventoryUI;
- public Transform slotHolder;
- public InventorySlot[] slots;
- [HideInInspector]
- public bool isOpened;
- void Start()
- {
- isOpened = false;
- inventoryui = this;
- inventory = Inventory.instance;
- inventory.onItemChangedCallback += UpdateUI;
- slots = slotHolder.GetComponentsInChildren<InventorySlot>();
- }
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.Tab))
- {
- inventoryUI.SetActive(!inventoryUI.activeSelf);
- isOpened = !isOpened;
- }
- if (isOpened)
- {
- Player.GetComponent<FirstPersonController>().enabled = false;
- Player.GetComponent<CharacterController>().enabled = false;
- Player.GetComponentInChildren<CameraController>().enabled = false;
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- }
- else
- {
- Player.GetComponent<FirstPersonController>().enabled = true;
- Player.GetComponent<CharacterController>().enabled = true;
- Player.GetComponentInChildren<CameraController>().enabled = true;
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- }
- }
- void UpdateUI()
- {
- checkForInventoryItems();
- for (int i = 0; i < slots.Length; i++)
- {
- if (isExists(slots[i].item) && slots[i].item.maxStackAmount > 1 && isThisItem(i) && int.Parse(slots[i].itemAmount.text) < slots[i].item.maxStackAmount)
- {
- int amount = int.Parse(slots[i].itemAmount.text);
- if (slots[i].item != null)
- {
- slots[i].itemAmount.text = (amount + 1).ToString();
- Inventory.instance.latestPickup = null;
- }
- }
- else
- {
- for (int c = 0; c < inventory.items.Count; c++)
- {
- if (!isExists(inventory.items[c]) || (isExists(inventory.items[c]) && inventory.items[c].nextStack() && needToAdd(inventory.items[c])))
- {
- slots[findNextEmptySlot()].AddItem(inventory.items[c]);
- Inventory.instance.latestPickup = null;
- return;
- }
- }
- }
- }
- }
- bool needToAdd(Item item)
- {
- for (int i = 0; i < slots.Length; i++)
- {
- if(slots[i].item != null)
- {
- if (int.Parse(slots[i].itemAmount.text) < slots[i].item.maxStackAmount && slots[i].item == item)
- return false;
- }
- }
- return true;
- }
- int findNextEmptySlot()
- {
- for (int i = 0; i < slots.Length; i++)
- {
- if (slots[i].item == null)
- return i;
- }
- return 0;
- }
- public void checkForInventoryItems()
- {
- for (int i = 0; i < slots.Length; i++)
- {
- if (slots[i].item != null && !inventory.items.Contains(slots[i].item))
- slots[i].RemoveItem();
- }
- }
- public bool isExists(Item item)
- {
- for (int i = 0; i < slots.Length; i++)
- {
- if (slots[i].item != null)
- {
- if (slots[i].item == item)
- {
- return true;
- }
- }
- }
- return false;
- }
- bool isThisItem(int index)
- {
- if (slots.Length > 0 && slots[index].item != null)
- {
- if (slots[index].item.ItemName.Equals(Inventory.instance.latestPickup))
- {
- return true;
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement