InventoryManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using Assets.HeroEditor4D.InventorySystem.Scripts.Data;
  6. using Assets.HeroEditor4D.InventorySystem.Scripts.Elements;
  7. using UnityEngine;
  8. public class InventoryManager : MonoBehaviour
  9. {
  10. public playerNetwork pnet;
  11. public InventorySlot[] inventorySlots;
  12. public InventoryItemsCollection lootsData;
  13. public GameObject ItemInventoryPrefab;
  14. public List<item> startingItems;
  15. public bool resetInventory = false;
  16. private void OnValidate()
  17. {
  18. if (resetInventory)
  19. {
  20. resetInventory = false;
  21. foreach (InventorySlot slot in inventorySlots)
  22. {
  23. slot.Clear();
  24. }
  25. }
  26. }
  27. public item GetItemByType(string type)
  28. {
  29. foreach (item i in lootsData.items)
  30. {
  31. if (i.type == type)
  32. {
  33. return i;
  34. }
  35. }
  36. Debug.Log("Could not find anything for " + type);
  37. return null;
  38. }
  39. void Start()
  40. {
  41. foreach (item i in startingItems)
  42. {
  43. AddInvItem(i);
  44. }
  45. }
  46. public Dictionary<int, string> GetEntries()
  47. {
  48. Dictionary<int, string> entries = new Dictionary<int, string>();
  49. for (int i = 0; i < inventorySlots.Length; i++)
  50. {
  51. ItemInventory itemInSlot = inventorySlots[i].GetComponentInChildren<ItemInventory>();
  52. if (itemInSlot != null && itemInSlot.item != null)
  53. {
  54. entries.Add(i, itemInSlot.item.type);
  55. }
  56. }
  57. return entries;
  58. }
  59. public int GetStock(string type)
  60. {
  61. int count = 0;
  62. for (int i = 0; i < inventorySlots.Length; i++)
  63. {
  64. ItemInventory itemInSlot = inventorySlots[i].GetComponentInChildren<ItemInventory>();
  65. if (itemInSlot != null && itemInSlot.item != null)
  66. {
  67. if (itemInSlot.item.type == type)
  68. {
  69. count++;
  70. }
  71. }
  72. }
  73. return count;
  74. }
  75. public void RemoveItem(string type)
  76. {
  77. for (int i = 0; i < inventorySlots.Length; i++)
  78. {
  79. ItemInventory itemInSlot = inventorySlots[i].GetComponentInChildren<ItemInventory>();
  80. if (itemInSlot != null && itemInSlot.item != null)
  81. {
  82. if (itemInSlot.item.type == type)
  83. {
  84. Destroy(itemInSlot.gameObject);
  85. return;
  86. }
  87. }
  88. }
  89. }
  90. public void SetInventory(Dictionary<int, string> data)
  91. {
  92. Clear();
  93. foreach (KeyValuePair<int, string> entry in data)
  94. {
  95. SpawnNewItem(GetItemByType(entry.Value), inventorySlots[entry.Key]);
  96. }
  97. }
  98. public bool UseItem(item i)
  99. {
  100. if (pnet.health >= 100)
  101. {
  102. return false;
  103. }
  104. pnet.SetHealth(pnet.health + i.healthIncrease);
  105. pnet.SavePlayerData();
  106. return true;
  107. }
  108. public item selectedItem;
  109. public void SelectItem(item itemI)
  110. {
  111. selectedItem = itemI;
  112. }
  113. public void DropItem(item i)
  114. {
  115. pnet.DropPickup(i.type);
  116. pnet.SavePlayerData();
  117. }
  118. public void AddInvItem(string type)
  119. {
  120. AddInvItem(GetItemByType(type));
  121. }
  122. public void AddInvItem(item item)
  123. {
  124. //find an empty slot
  125. for (int i = inventorySlots.Length - 1; i >= 0; i--)
  126. {
  127. InventorySlot slot = inventorySlots[i];
  128. ItemInventory itemInSlot = slot.GetComponentInChildren<ItemInventory>();
  129. if (itemInSlot == null)
  130. {
  131. SpawnNewItem(item, slot);
  132. return;
  133. }
  134. //implement check for slot full
  135. }
  136. Debug.Log("Slots are full");
  137. }
  138. public void Clear()
  139. {
  140. for (int i = 0; i < inventorySlots.Length; i++)
  141. {
  142. if (inventorySlots[i].transform.childCount > 0)
  143. {
  144. Destroy(inventorySlots[i].transform.GetChild(0).gameObject);
  145. }
  146. }
  147. }
  148. void SpawnNewItem(item item, InventorySlot slot)
  149. {
  150. GameObject newItemAdd = Instantiate(ItemInventoryPrefab, slot.transform);
  151. ItemInventory inventoryItemm = newItemAdd.GetComponent<ItemInventory>();
  152. inventoryItemm.Set(item, this);
  153. pnet.SavePlayerData();
  154. }
  155. }
  156. [System.Serializable]
  157. public class InventoryEntry
  158. {
  159. public int slot;
  160. public string item;
  161. }