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.

112 lines
2.9 KiB

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