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.

83 lines
2.1 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. CountText.text = Count.ToString();
  33. }
  34. #endregion Class Functionality
  35. #region LogicElementHolder Implementation
  36. /// <summary>
  37. /// When a logicElement needs to be added back to this holder
  38. /// </summary>
  39. /// <param name="element">Element which is being Added</param>
  40. public override void OnAdd(LogicElementUI element)
  41. {
  42. Destroy(element.gameObject);
  43. if (!InventoryData.infinit)
  44. Count++;
  45. UpdateUI();
  46. }
  47. /// <summary>
  48. /// When a LogicElement has started being dragged from this holder
  49. /// </summary>
  50. /// <param name="element">Element which is being dragged</param>
  51. public override void OnRemove(LogicElementUI element)
  52. {
  53. if (!InventoryData.infinit && Count > 0)
  54. Count--;
  55. else if (!InventoryData.infinit)
  56. Destroy(element.gameObject);
  57. LogicElementUI logicElement = Instantiate(LogicElementPrefab.gameObject, LogicElementLocation).GetComponent<LogicElementUI>();
  58. logicElement.Initialise(InventoryData.element, this);
  59. UpdateUI();
  60. }
  61. #endregion LogicElementHolder Implementation
  62. }