Target.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // MIT License
  2. // https://gitlab.com/ilnprj
  3. // Copyright (c) 2020 ilnprj
  4. using UnityEngine;
  5. using System.Collections.Generic;
  6. namespace RadarComponents
  7. {
  8. /// <summary>
  9. /// Class of the target is registered when the target appears on the scene in the manager, and removes it from there upon deactivation.
  10. /// The target can optionally update the player’s locator itself, if it has the ability to move
  11. /// </summary>
  12. public class Target : MonoBehaviour, ITarget
  13. {
  14. [SerializeField]
  15. private bool isTargetCanMove = default;
  16. [SerializeField]
  17. private Sprite defaultIcon = default;
  18. [SerializeField]
  19. private List<TypeIconTarget> targetIcons = new List<TypeIconTarget>();
  20. public List<TypeIconTarget> IconsTarget => targetIcons;
  21. [SerializeField]
  22. private string idTarget = default;
  23. public Transform TransformTarget => transform;
  24. public string IdTarget => idTarget;
  25. private ContainerTargetManager container;
  26. private Vector3 _pos;
  27. private Vector3 cachedPosition
  28. {
  29. set
  30. {
  31. if (value!= _pos)
  32. {
  33. _pos = value;
  34. PlayerLocator.onUpdateLocator();
  35. }
  36. }
  37. }
  38. public Sprite DefaultIcon => defaultIcon;
  39. private void OnEnable()
  40. {
  41. container = FindObjectOfType<ContainerTargetManager>();
  42. if (container.IsInited)
  43. OnTargetEnable();
  44. else
  45. {
  46. container.onInit += OnTargetEnable;
  47. }
  48. }
  49. private void FixedUpdate()
  50. {
  51. if (isTargetCanMove)
  52. {
  53. cachedPosition = transform.position;
  54. }
  55. }
  56. private void OnDisable()
  57. {
  58. OnTargetDisable();
  59. }
  60. /// <summary>
  61. /// Actions when activating a target on a scene
  62. /// </summary>
  63. public void OnTargetEnable()
  64. {
  65. container.TargetManager.AddTarget(this);
  66. }
  67. /// <summary>
  68. /// Actions when the target is deactivated on the scene(The method can also be called if the target can be active but not used)
  69. /// </summary>
  70. public void OnTargetDisable()
  71. {
  72. container.onInit -= OnTargetEnable;
  73. container.TargetManager.RemoveTarget(this);
  74. }
  75. }
  76. }