Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / Managers / InventoryManager.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class InventoryManager : MonoBehaviour
  5. {
  6. [Header("Inventory Settings: ")]
  7. [SerializeField] private int inventoryCapacity;
  8. [SerializeField] private int quickInventoryCapacity = 5;
  9. [SerializeField] private Inventory inventory;
  10. [SerializeField] private Inventory quickInventory;
  11. public Inventory Inventory => inventory;
  12. public Inventory QuickInventory => quickInventory;
  13. //Testing purposes
  14. [Header("Item Stack Prefabs")]
  15. [SerializeField] private Item item;
  16. public GameObject itemStack;
  17. void Awake()
  18. {
  19. inventory = new Inventory(inventoryCapacity, gameObject);
  20. quickInventory = new Inventory(quickInventoryCapacity, gameObject);
  21. }
  22. // Start is called before the first frame update
  23. void Start()
  24. {
  25. }
  26. // Update is called once per frame
  27. void Update()
  28. {
  29. //Perform inventory slot interaction
  30. QuickInventoryActions();
  31. if (Input.GetKeyDown(KeyCode.F))
  32. {
  33. AddItem(item, 1);
  34. Debug.Log($"Added Item: {item.ItemName}");
  35. }
  36. }
  37. private void QuickInventoryActions()
  38. {
  39. if (Input.GetKeyDown(InputManager.GetKey(InputAction.use1)))
  40. {
  41. UseInventoryItems(0);
  42. }
  43. else if (Input.GetKeyDown(InputManager.GetKey(InputAction.use2)))
  44. {
  45. UseInventoryItems(1);
  46. }
  47. else if (Input.GetKeyDown(InputManager.GetKey(InputAction.use3)))
  48. {
  49. UseInventoryItems(2);
  50. }
  51. else if (Input.GetKeyDown(InputManager.GetKey(InputAction.use4)))
  52. {
  53. UseInventoryItems(3);
  54. }
  55. else if (Input.GetKeyDown(InputManager.GetKey(InputAction.use5)))
  56. {
  57. UseInventoryItems(4);
  58. }
  59. }
  60. private void UseInventoryItems(int index)
  61. {
  62. InventorySlot slot = quickInventory.GetSlot(index);
  63. if (slot.IsEmpty()) return;
  64. Item item = slot.Item;
  65. if (item == null) return;
  66. if (transform.parent.TryGetComponent(out PlayerController controller))
  67. {
  68. switch (item)
  69. {
  70. case StatPotion potion:
  71. Debug.Log("Try to apply effect");
  72. //Apply Potion Effects
  73. potion.Visit(controller);
  74. slot.Owner.RemoveItemAmount(index, 1);
  75. return;
  76. }
  77. }
  78. else
  79. {
  80. Debug.LogError($"PlayerController is NULL!");
  81. }
  82. }
  83. //Handles the item store on the inventories
  84. public bool AddItem(Item item, int amount)
  85. {
  86. //Tries to add the item on the quick inventory
  87. if (quickInventory.AddItem(item, amount))
  88. {
  89. return true;
  90. }
  91. //Tries to add the item to the internal inventory if the quickInventory is full
  92. else if(inventory.AddItem(item, amount))
  93. {
  94. return true;
  95. }
  96. else
  97. {
  98. return false;
  99. }
  100. }
  101. public void SwapInventorySlot(InventorySlot slotA, InventorySlot slotB)
  102. {
  103. if (slotA.Owner != slotB.Owner)
  104. {
  105. Inventory.SwapInventoriesSlot(slotA, slotB);
  106. }
  107. else
  108. {
  109. //Get the inventory of the slot
  110. Inventory inventory = slotA.Owner;
  111. inventory.SwapItemSlot(slotA.SlotIndex, slotB.SlotIndex);
  112. }
  113. }
  114. public void DropItem(int index, int amount)
  115. {
  116. //Item item = GetItem(index);
  117. //GameObject go = gameObject.itemStack;
  118. //if (go != null)
  119. //{
  120. // Vector3 targePos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, 0);
  121. // GameObject obj = Instantiate(go, targePos, Quaternion.identity);
  122. // ItemStack stack = obj.GetComponent<ItemStack>();
  123. // stack.ItemStackInit(item, amount);
  124. // RemoveItem(index, amount);
  125. // return;
  126. //}
  127. //else
  128. //{
  129. // Debug.LogError($"Item stack is Null");
  130. //}
  131. }
  132. }