InventoryManager.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. foreach(item i in startingItems){
  41. AddInvItem(i);
  42. }
  43. }
  44. public Dictionary<int, string> GetEntries(){
  45. Dictionary<int, string> entries = new Dictionary<int, string>();
  46. for(int i=0; i < inventorySlots.Length; i++){
  47. ItemInventory itemInSlot = inventorySlots[i].GetComponentInChildren<ItemInventory>();
  48. if(itemInSlot!= null && itemInSlot.item != null){
  49. entries.Add(i, itemInSlot.item.type);
  50. }
  51. }
  52. return entries;
  53. }
  54. public void SetInventory(Dictionary<int, string> data)
  55. {
  56. Clear();
  57. foreach(KeyValuePair<int, string> entry in data)
  58. {
  59. SpawnNewItem(GetItemByType(entry.Value), inventorySlots[entry.Key]);
  60. }
  61. }
  62. public bool UseItem(item i)
  63. {
  64. if(pnet.health >= 100)
  65. {
  66. return false;
  67. }
  68. pnet.SetHealth(pnet.health+i.healthIncrease);
  69. pnet.SavePlayerData();
  70. return true;
  71. }
  72. public item selectedItem;
  73. public void SelectItem(item itemI){
  74. selectedItem = itemI;
  75. }
  76. public void DropItem(item i)
  77. {
  78. pnet.DropPickup(i.type);
  79. pnet.SavePlayerData();
  80. }
  81. public void AddInvItem(string type)
  82. {
  83. AddInvItem(GetItemByType(type));
  84. }
  85. public void AddInvItem (item item){
  86. //find an empty slot
  87. for(int i = inventorySlots.Length-1; i >=0 ; i--){
  88. InventorySlot slot = inventorySlots[i];
  89. ItemInventory itemInSlot = slot.GetComponentInChildren<ItemInventory>();
  90. if(itemInSlot == null){
  91. SpawnNewItem(item , slot);
  92. return;
  93. }
  94. //implement check for slot full
  95. }
  96. Debug.Log("Slots are full");
  97. }
  98. public void Clear(){
  99. for(int i=0; i < inventorySlots.Length; i++){
  100. if(inventorySlots[i].transform.childCount > 0){
  101. Destroy(inventorySlots[i].transform.GetChild(0).gameObject);
  102. }
  103. }
  104. }
  105. void SpawnNewItem(item item , InventorySlot slot ){
  106. GameObject newItemAdd = Instantiate(ItemInventoryPrefab , slot.transform);
  107. ItemInventory inventoryItemm = newItemAdd.GetComponent<ItemInventory>();
  108. inventoryItemm.Set(item,this);
  109. pnet.SavePlayerData();
  110. }
  111. }
  112. [System.Serializable]
  113. public class InventoryEntry{
  114. public int slot;
  115. public string item;
  116. }