Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Inventory : MonoBehaviour
  6. {
  7. int inventorySize = 69;
  8.  
  9. public short[,] PlayerInventory = new short[69, 2];
  10.  
  11. public static Inventory instance;
  12.  
  13. public GameObject inventoryDisplay;
  14.  
  15. public delegate void OnItemChanged();
  16. public OnItemChanged onItemChangedCallback;
  17.  
  18. void Awake()
  19. {
  20. if (instance != null)
  21. return;
  22.  
  23. instance = this;
  24. }
  25.  
  26. void Start()
  27. {
  28. CleanInventory();
  29. }
  30.  
  31. public void AddItem(short id, short amount)
  32. {
  33. bool hasItem = false;
  34.  
  35. for (int i = 0; i < inventorySize; i++)
  36. {
  37. if (PlayerInventory[i, 0] == id)
  38. {
  39. PlayerInventory[i, 0] = id;
  40. PlayerInventory[i, 1] += amount;
  41. Debug.Log("Added item ID: " + PlayerInventory[i, 0] + " (" + PlayerInventory[i, 1] + " total).");
  42. hasItem = true;
  43. break;
  44. }
  45. }
  46. if (!hasItem)
  47. {
  48. for (int i = 0; i < inventorySize; i++)
  49. {
  50. if (PlayerInventory[i, 0] == -1)
  51. {
  52. PlayerInventory[i, 0] = id;
  53. PlayerInventory[i, 1] += amount;
  54. Debug.Log("Added item ID: " + PlayerInventory[i, 0] + " (" + PlayerInventory[i, 1] + " total).");
  55. break;
  56. }
  57. }
  58. }
  59.  
  60. if (onItemChangedCallback != null)
  61. onItemChangedCallback.Invoke();
  62. }
  63.  
  64. public void RemoveItem(int slot, short amount)
  65. {
  66. //Check if they're trying to remove too many items
  67. if(amount > PlayerInventory[slot, 1] || PlayerInventory[slot, 0] == -1)
  68. {
  69. Debug.Log("Nothing to remove.");
  70. return;
  71. }
  72. //Check if it is their last item
  73. else if(PlayerInventory[slot, 1] == 1)
  74. {
  75. Debug.Log("Removed " + amount + " item ID " +
  76. PlayerInventory[slot, 0] + " from inventory. (" + (PlayerInventory[slot, 1] - (short)1) + " remaining.)");
  77. PlayerInventory[slot, 0] = -1;
  78. PlayerInventory[slot, 1] = 0;
  79. }
  80. else
  81. {
  82. Debug.Log("Removed " + amount + " item ID " +
  83. PlayerInventory[slot, 0] + " from inventory. (" + (PlayerInventory[slot, 1] - (short)1) + " remaining.)");
  84. PlayerInventory[slot, 1] -= amount;
  85. }
  86.  
  87. if (onItemChangedCallback != null)
  88. onItemChangedCallback.Invoke();
  89. }
  90.  
  91. public void MoveItem(int slotA, int slotB)
  92. {
  93. //Temp values
  94. short slotItemA = PlayerInventory[slotA, 0];
  95. short slotAmountA = PlayerInventory[slotA, 1];
  96.  
  97. short slotItemB = PlayerInventory[slotB, 0];
  98. short slotAmountB = PlayerInventory[slotB, 1];
  99.  
  100. //Swap the items
  101. PlayerInventory[slotA, 0] = slotItemB;
  102. PlayerInventory[slotA, 1] = slotAmountB;
  103.  
  104. PlayerInventory[slotB, 0] = slotItemA;
  105. PlayerInventory[slotB, 1] = slotAmountA;
  106.  
  107.  
  108. Debug.Log("Swapped slot " + slotA + " with slot " + slotB + ".");
  109.  
  110. if (onItemChangedCallback != null)
  111. onItemChangedCallback.Invoke();
  112. }
  113.  
  114. public void PrintInventory()
  115. {
  116. for (int i = 0; i < inventorySize; i++)
  117. {
  118. Debug.Log("Slot " + i + ": item ID: " + PlayerInventory[i, 0] + " (" + PlayerInventory[i, 1] + ").");
  119. }
  120.  
  121. if (onItemChangedCallback != null)
  122. onItemChangedCallback.Invoke();
  123. }
  124.  
  125. public void CleanInventory()
  126. {
  127. for (int i = 0; i < inventorySize; i++)
  128. {
  129. //Set item ID to -1
  130. PlayerInventory[i, 0] = -1;
  131.  
  132. //Set the item amount to 0
  133. PlayerInventory[i, 1] = 0;
  134. }
  135.  
  136. if(onItemChangedCallback != null)
  137. onItemChangedCallback.Invoke();
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement