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.

100 lines
2.5 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 Class Functionality
  22. public void Initialise(Inventory.Data data)
  23. {
  24. InventoryData = data;
  25. if (CountText1 == null)
  26. CountText1 = GetComponentInChildren<Text>();
  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. CountText1.text = Count.ToString();
  36. else
  37. CountText1.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 when an element in this holder is double clicked
  68. /// </summary>
  69. /// <param name="element">element which was double clicked</param>
  70. public override void OnDoubleClick(LogicElementUI element)
  71. {
  72. }
  73. #endregion LogicElementHolder Implementation
  74. }