InventoryManager.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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, int amount = 1)
  76. {
  77. int leftToRemove = amount;
  78. for (int i = 0; i < inventorySlots.Length; i++)
  79. {
  80. ItemInventory itemInSlot = inventorySlots[i].GetComponentInChildren<ItemInventory>();
  81. if (itemInSlot != null && itemInSlot.item != null)
  82. {
  83. if (itemInSlot.item.type == type)
  84. {
  85. Destroy(itemInSlot.gameObject);
  86. leftToRemove--;
  87. if(leftToRemove <= 0){
  88. return;
  89. }
  90. }
  91. }
  92. }
  93. }
  94. public void SetInventory(Dictionary<int, string> data)
  95. {
  96. Clear();
  97. foreach (KeyValuePair<int, string> entry in data)
  98. {
  99. SpawnNewItem(GetItemByType(entry.Value), inventorySlots[entry.Key]);
  100. }
  101. }
  102. public bool UseItem(item i)
  103. {
  104. if(i.bowId != -1){
  105. pnet.characterMan.EquipBow(i.bowId);return false;
  106. }
  107. if(i.armorId != -1){
  108. pnet.characterMan.EquipArmor(i.armorId);return false;
  109. }
  110. if(i.helmetId != -1){
  111. pnet.characterMan.EquipHelmet(i.helmetId);return false;
  112. }
  113. if(i.swordId != -1){
  114. pnet.characterMan.EquipSword(i.swordId);return false;
  115. }
  116. if(i.wandId != -1){
  117. pnet.characterMan.EquipWand(i.wandId);return false;
  118. }
  119. if (pnet.health >= 100)
  120. {
  121. return false;
  122. }
  123. pnet.SetHealth(pnet.health + i.healthIncrease);
  124. pnet.SavePlayerData();
  125. return true;
  126. }
  127. public item selectedItem;
  128. public void SelectItem(item itemI)
  129. {
  130. selectedItem = itemI;
  131. }
  132. public void DropItem(item i)
  133. {
  134. pnet.DropPickup(i.type);
  135. pnet.SavePlayerData();
  136. }
  137. public void AddInvItem(string type)
  138. {
  139. AddInvItem(GetItemByType(type));
  140. }
  141. public void AddInvItem(item item)
  142. {
  143. //find an empty slot
  144. for (int i = inventorySlots.Length - 1; i >= 0; i--)
  145. {
  146. InventorySlot slot = inventorySlots[i];
  147. ItemInventory itemInSlot = slot.GetComponentInChildren<ItemInventory>();
  148. if (itemInSlot == null)
  149. {
  150. SpawnNewItem(item, slot);
  151. return;
  152. }
  153. //implement check for slot full
  154. }
  155. Debug.Log("Slots are full");
  156. }
  157. public void Clear()
  158. {
  159. for (int i = 0; i < inventorySlots.Length; i++)
  160. {
  161. if (inventorySlots[i].transform.childCount > 0)
  162. {
  163. Destroy(inventorySlots[i].transform.GetChild(0).gameObject);
  164. }
  165. }
  166. }
  167. void SpawnNewItem(item item, InventorySlot slot)
  168. {
  169. GameObject newItemAdd = Instantiate(ItemInventoryPrefab, slot.transform);
  170. ItemInventory inventoryItemm = newItemAdd.GetComponent<ItemInventory>();
  171. inventoryItemm.Set(item, this);
  172. pnet.SavePlayerData();
  173. }
  174. }
  175. [System.Serializable]
  176. public class InventoryEntry
  177. {
  178. public int slot;
  179. public string item;
  180. }