Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. using UnityEngine;
  2. using VHS;
  3.  
  4. public class InventoryUI : MonoBehaviour
  5. {
  6.  
  7. Inventory inventory;
  8.  
  9. public bool isItemAdded;
  10.  
  11. public static InventoryUI inventoryui;
  12.  
  13. public GameObject Player;
  14.  
  15. public GameObject inventoryUI;
  16.  
  17. public Transform slotHolder;
  18. public InventorySlot[] slots;
  19.  
  20. [HideInInspector]
  21. public bool isOpened;
  22.  
  23. void Start()
  24. {
  25. isOpened = false;
  26.  
  27. inventoryui = this;
  28.  
  29. inventory = Inventory.instance;
  30. inventory.onItemChangedCallback += UpdateUI;
  31.  
  32. slots = slotHolder.GetComponentsInChildren<InventorySlot>();
  33. }
  34.  
  35. void Update()
  36. {
  37. if (Input.GetKeyDown(KeyCode.Tab))
  38. {
  39. inventoryUI.SetActive(!inventoryUI.activeSelf);
  40. isOpened = !isOpened;
  41. }
  42.  
  43. if (isOpened)
  44. {
  45. Player.GetComponent<FirstPersonController>().enabled = false;
  46. Player.GetComponent<CharacterController>().enabled = false;
  47. Player.GetComponentInChildren<CameraController>().enabled = false;
  48. Cursor.lockState = CursorLockMode.None;
  49. Cursor.visible = true;
  50. }
  51. else
  52. {
  53. Player.GetComponent<FirstPersonController>().enabled = true;
  54. Player.GetComponent<CharacterController>().enabled = true;
  55. Player.GetComponentInChildren<CameraController>().enabled = true;
  56. Cursor.lockState = CursorLockMode.Locked;
  57. Cursor.visible = false;
  58. }
  59. }
  60.  
  61. void UpdateUI()
  62. {
  63. checkForInventoryItems();
  64. for (int i = 0; i < slots.Length; i++)
  65. {
  66. if (isExists(slots[i].item) && slots[i].item.maxStackAmount > 1 && isThisItem(i) && int.Parse(slots[i].itemAmount.text) < slots[i].item.maxStackAmount)
  67. {
  68. int amount = int.Parse(slots[i].itemAmount.text);
  69. if (slots[i].item != null)
  70. {
  71. slots[i].itemAmount.text = (amount + 1).ToString();
  72. Inventory.instance.latestPickup = null;
  73. }
  74. }
  75. else
  76. {
  77. for (int c = 0; c < inventory.items.Count; c++)
  78. {
  79. if (!isExists(inventory.items[c]) || (isExists(inventory.items[c]) && inventory.items[c].nextStack() && needToAdd(inventory.items[c])))
  80. {
  81. slots[findNextEmptySlot()].AddItem(inventory.items[c]);
  82. Inventory.instance.latestPickup = null;
  83. return;
  84. }
  85. }
  86. }
  87. }
  88. }
  89.  
  90. bool needToAdd(Item item)
  91. {
  92. for (int i = 0; i < slots.Length; i++)
  93. {
  94. if(slots[i].item != null)
  95. {
  96. if (int.Parse(slots[i].itemAmount.text) < slots[i].item.maxStackAmount && slots[i].item == item)
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102.  
  103. int findNextEmptySlot()
  104. {
  105. for (int i = 0; i < slots.Length; i++)
  106. {
  107. if (slots[i].item == null)
  108. return i;
  109. }
  110. return 0;
  111. }
  112.  
  113. public void checkForInventoryItems()
  114. {
  115. for (int i = 0; i < slots.Length; i++)
  116. {
  117. if (slots[i].item != null && !inventory.items.Contains(slots[i].item))
  118. slots[i].RemoveItem();
  119. }
  120. }
  121.  
  122. public bool isExists(Item item)
  123. {
  124. for (int i = 0; i < slots.Length; i++)
  125. {
  126. if (slots[i].item != null)
  127. {
  128. if (slots[i].item == item)
  129. {
  130. return true;
  131. }
  132. }
  133. }
  134. return false;
  135. }
  136.  
  137. bool isThisItem(int index)
  138. {
  139. if (slots.Length > 0 && slots[index].item != null)
  140. {
  141. if (slots[index].item.ItemName.Equals(Inventory.instance.latestPickup))
  142. {
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement