You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.8 KiB

5 years ago
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. public class BagItem : LogicElementHolder
  4. {
  5. #region Inspector Elements
  6. [SerializeField]
  7. private Transform LogicElementLocation;
  8. [SerializeField]
  9. private LogicElementUI LogicElementPrefab;
  10. #endregion Inspector Elements
  11. #region Private Variables
  12. private int Count;
  13. protected Inventory.Data InventoryData;
  14. //private TextMeshProUGUI CountText;
  15. private Text CountText1;
  16. #endregion Private Variables
  17. #region Class Functionality
  18. public void Initialise(Inventory.Data data)
  19. {
  20. InventoryData = data;
  21. if (CountText1 == null)
  22. CountText1 = GetComponentInChildren<Text>();
  23. Count = data.Count;
  24. LogicElementUI logicElement = Instantiate(LogicElementPrefab.gameObject, LogicElementLocation).GetComponent<LogicElementUI>();
  25. logicElement.Initialise(data.element, this,true);
  26. UpdateUI();
  27. }
  28. public void UpdateUI()
  29. {
  30. if (!InventoryData.isInfinit)
  31. CountText1.text = Count.ToString();
  32. else
  33. CountText1.text = "∞";
  34. }
  35. #endregion Class Functionality
  36. #region LogicElementHolder Implementation
  37. /// <summary>
  38. /// When a logicElement needs to be added back to this holder
  39. /// </summary>
  40. /// <param name="element">Element which is being Added</param>
  41. public override void OnAdd(LogicElementUI element)
  42. {
  43. Destroy(element.gameObject);
  44. if (!InventoryData.isInfinit)
  45. Count++;
  46. UpdateUI();
  47. }
  48. /// <summary>
  49. /// When a LogicElement has started being dragged from this holder
  50. /// </summary>
  51. /// <param name="element">Element which is being dragged</param>
  52. public override void OnRemove(LogicElementUI element)
  53. {
  54. if (!InventoryData.isInfinit && Count > 0)
  55. Count--;
  56. else if (!InventoryData.isInfinit)
  57. Destroy(element.gameObject);
  58. LogicElementUI logicElement = Instantiate(LogicElementPrefab.gameObject, LogicElementLocation).GetComponent<LogicElementUI>();
  59. logicElement.Initialise(InventoryData.element, this,true);
  60. UpdateUI();
  61. }
  62. /// <summary>
  63. /// Called to check if this holder can hold the provided element;
  64. /// </summary>
  65. /// <param name="element">element to check if it can hold</param>
  66. /// <returns>returns true if this can hold the element </returns>
  67. public override bool canHold(LogicBlock element)
  68. {
  69. return element.isSameType(InventoryData.element);
  70. }
  71. /// <summary>
  72. /// Called when an element in this holder is double clicked
  73. /// </summary>
  74. /// <param name="element">element which was double clicked</param>
  75. public override void OnDoubleClick(LogicElementUI element)
  76. {
  77. }
  78. #endregion LogicElementHolder Implementation
  79. }