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.

114 lines
3.0 KiB

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