BaseTargetView.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // MIT License
  2. // https://gitlab.com/ilnprj
  3. // Copyright (c) 2020 ilnprj
  4. using System;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using System.Collections.Generic;
  8. namespace RadarComponents
  9. {
  10. /// <summary>
  11. /// Base functional for Target View
  12. /// </summary>
  13. public abstract class BaseTargetView : MonoBehaviour
  14. {
  15. [SerializeField]
  16. private string idIconTarget = default;
  17. [SerializeField]
  18. protected Image iconTarget = default;
  19. [SerializeField]
  20. protected List<AbstractExtensionTarget> extensionsForView = new List<AbstractExtensionTarget>();
  21. protected Transform targetTransform;
  22. protected Transform playerTransform;
  23. protected RectTransform rootTransform;
  24. protected PlayerLocator locator;
  25. public ITarget CurrentTarget { get; private set; }
  26. /// <summary>
  27. /// Init View
  28. /// </summary>
  29. public void InitTargetView(ITarget target, Transform inputPlayer, RectTransform inputRootTransform)
  30. {
  31. rootTransform = inputRootTransform;
  32. if (iconTarget != null)
  33. {
  34. try {
  35. iconTarget.sprite = target.IconsTarget.Find(x => x.IdType == idIconTarget).IconTarget;
  36. }
  37. catch(Exception e)
  38. {
  39. Debug.LogWarning(e.Message);
  40. if (iconTarget.sprite == null)
  41. {
  42. iconTarget.sprite = target.DefaultIcon;
  43. }
  44. }
  45. }
  46. targetTransform = target.TransformTarget;
  47. playerTransform = inputPlayer;
  48. CurrentTarget = target;
  49. }
  50. protected virtual void OnEnable()
  51. {
  52. PlayerLocator.onUpdateLocator += UpdateViewTarget;
  53. }
  54. protected virtual void Awake()
  55. {
  56. locator = FindObjectOfType<PlayerLocator>();
  57. }
  58. protected virtual void OnDisable()
  59. {
  60. PlayerLocator.onUpdateLocator -= UpdateViewTarget;
  61. }
  62. /// <summary>
  63. /// What should an abstract class do when updating data about a player’s goal or location
  64. /// </summary>
  65. public abstract void UpdateViewTarget();
  66. protected void UpdateExtensions()
  67. {
  68. foreach (var item in extensionsForView)
  69. {
  70. item.UpdateExtensionView(playerTransform, CurrentTarget);
  71. }
  72. }
  73. }
  74. }