Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. namespace FRM.Inventory
  2. {
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6.  
  7. public class Inventory : IInventory
  8. {
  9. [SerializeField] private InventoryTemplate template;
  10. [SerializeField] private IItem[] inventoryItems;
  11.  
  12. public InventoryTemplate Template
  13. {
  14. set { template = value; }
  15. }
  16.  
  17. public IItem[] Items
  18. {
  19. get { return inventoryItems; }
  20. set { inventoryItems = value; }
  21. }
  22.  
  23. public int Rows { get { return template.Rows; } }
  24. public int Columns { get { return template.Columns; } }
  25. public int Cells { get { return template.Cells; } }
  26. public int HotbarCells { get { return template.HotbarCells; } }
  27. public bool UseHotbar { get { return template.UseHotbar; } }
  28.  
  29. public Inventory(InventoryTemplate inventoryTemplate)
  30. {
  31. template = inventoryTemplate;
  32. inventoryItems = new IItem[template.Cells];
  33. }
  34.  
  35. // returns null if it transferred fully, if not then it returns the item and the rest of the quantity
  36. public IItem AddItem(IItem item, int toSlot = -1, int splitQuantity = -1)
  37. {
  38. // temporarily store the original item quantity
  39. int originalQuantity = item.Quantity;
  40.  
  41. // if we use item splitting, we need to limit split quantity to the original item quantity just in case
  42. if (splitQuantity != -1 && splitQuantity != item.Quantity)
  43. item.Quantity = Mathf.Clamp(splitQuantity, 1, originalQuantity);
  44.  
  45. int startIndex = 0;
  46. if (template.UseHotbar && !item.Info.HotbarPriority)
  47. startIndex = template.HotbarCells;
  48.  
  49. // if its slot specific, go for that slot
  50. if (toSlot != -1)
  51. item.Quantity = AddItemToSlot(item, toSlot);
  52.  
  53. // first search for the same item and check if you can stack it (if it's stackable)
  54. if (item.Quantity != 0 && item.Info.MaxStack > 1)
  55. {
  56. for (int i = 0; i < inventoryItems.Length; i++)
  57. {
  58. if (inventoryItems[i] == null || inventoryItems[i].Info != item.Info)
  59. continue;
  60.  
  61. item.Quantity = AddItemToSlot(item, i);
  62.  
  63. if (item.Quantity == 0 || inventoryItems[i].Dragged)
  64. break;
  65. }
  66. }
  67.  
  68. // if there is anything left or if its not stackable, try to store the rest it in empty spaces
  69. int count = inventoryItems.Length;
  70. if (item.Quantity > 0)
  71. {
  72. for (int i = startIndex; i < count; i++)
  73. {
  74. item.Quantity = AddItemToSlot(item, i);
  75. if (item.Quantity == 0 || inventoryItems[i].Dragged)
  76. break;
  77. if (i == inventoryItems.Length - 1 && !item.Info.HotbarPriority)
  78. {
  79. i = -1;
  80. count = template.HotbarCells;
  81. }
  82. }
  83. }
  84.  
  85. // if there is any leftover of the split quantity transferred, we solve it here
  86. if (splitQuantity != -1 && splitQuantity != originalQuantity)
  87. item.Quantity = originalQuantity - splitQuantity + item.Quantity;
  88.  
  89. // if we got rid of the item fully, make it null
  90. if (item.Quantity == 0)
  91. item = null;
  92.  
  93. return item;
  94. }
  95.  
  96. public void AddItemToSlotOverride(IItem item, int slotIndex)
  97. {
  98. if (item != null)
  99. inventoryItems[slotIndex] = CoreInventory.Instance.CreateItem(item.Stats);
  100. else
  101. inventoryItems[slotIndex] = null;
  102. }
  103.  
  104. public void RemoveItem(int slot)
  105. {
  106. inventoryItems[slot] = null;
  107. }
  108.  
  109. // should return 0 if all is good(no leftover), returns leftover if there was no space for the whole item amount
  110. private int AddItemToSlot(IItem item, int slotIndex = -1)
  111. {
  112. // if slot is null, place it here fully and set quantity to zero
  113. if (inventoryItems[slotIndex] == null)
  114. {
  115. // we need to make a new data object for here in case we got leftover
  116. inventoryItems[slotIndex] = CoreInventory.Instance.CreateItem(item.Stats);
  117. item.Quantity = 0;
  118. }
  119. else if (inventoryItems[slotIndex] != null && inventoryItems[slotIndex].Info == item.Info && !inventoryItems[slotIndex].Dragged)
  120. {
  121. // if its the same item, place as much as you can here, but if it's not the dragged one
  122. item.Quantity = inventoryItems[slotIndex].AddToStack(item.Quantity);
  123. }
  124.  
  125. // return how much of the item is left
  126. return item.Quantity;
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement