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.

86 lines
2.2 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 LogicElementUI LogicElementPrefab;
  11. [SerializeField]
  12. private Transform LogicElementLocation;
  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 Class Functionality
  20. public void Initialise(Inventory.Data data)
  21. {
  22. InventoryData = data;
  23. if (CountText == null)
  24. CountText = GetComponentInChildren<TextMeshProUGUI>();
  25. Count = data.Count;
  26. LogicElementUI logicElement = Instantiate(LogicElementPrefab.gameObject, LogicElementLocation).GetComponent<LogicElementUI>();
  27. logicElement.Initialise(data.element, this);
  28. UpdateUI();
  29. }
  30. public void UpdateUI()
  31. {
  32. if (!InventoryData.isInfinit)
  33. CountText.text = Count.ToString();
  34. else
  35. CountText.text = "∞";
  36. }
  37. #endregion Class Functionality
  38. #region LogicElementHolder Implementation
  39. /// <summary>
  40. /// When a logicElement needs to be added back to this holder
  41. /// </summary>
  42. /// <param name="element">Element which is being Added</param>
  43. public override void OnAdd(LogicElementUI element)
  44. {
  45. Destroy(element.gameObject);
  46. if (!InventoryData.isInfinit)
  47. Count++;
  48. UpdateUI();
  49. }
  50. /// <summary>
  51. /// When a LogicElement has started being dragged from this holder
  52. /// </summary>
  53. /// <param name="element">Element which is being dragged</param>
  54. public override void OnRemove(LogicElementUI element)
  55. {
  56. if (!InventoryData.isInfinit && Count > 0)
  57. Count--;
  58. else if (!InventoryData.isInfinit)
  59. Destroy(element.gameObject);
  60. LogicElementUI logicElement = Instantiate(LogicElementPrefab.gameObject, LogicElementLocation).GetComponent<LogicElementUI>();
  61. logicElement.Initialise(InventoryData.element, this);
  62. UpdateUI();
  63. }
  64. #endregion LogicElementHolder Implementation
  65. }