PlayerLocator.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // MIT License
  2. // https://gitlab.com/ilnprj
  3. // Copyright (c) 2020 ilnprj
  4. using System;
  5. using UnityEngine;
  6. namespace RadarComponents
  7. {
  8. /// <summary>
  9. /// A class that will report when it is possible to update any means of the radar.
  10. /// </summary>
  11. public class PlayerLocator : MonoBehaviour
  12. {
  13. [SerializeField]
  14. private Camera cameraPlayer = default;
  15. public Camera CameraPlayer
  16. {
  17. get
  18. {
  19. return cameraPlayer;
  20. }
  21. }
  22. public static bool IsInited
  23. {
  24. get
  25. {
  26. return isInited;
  27. }
  28. private set
  29. {
  30. isInited = value;
  31. if (value)
  32. {
  33. onInit();
  34. }
  35. }
  36. }
  37. public static Action onUpdateLocator = delegate { };
  38. public static event Action onInit = delegate { };
  39. private static bool isInited = false;
  40. private Vector3 _position;
  41. private Quaternion _rotation;
  42. private Vector3 lastPosition
  43. {
  44. set
  45. {
  46. if (_position != value)
  47. {
  48. _position = value;
  49. onUpdateLocator();
  50. }
  51. }
  52. }
  53. private Quaternion lastRotation
  54. {
  55. set
  56. {
  57. if (_rotation != value)
  58. {
  59. _rotation = value;
  60. onUpdateLocator();
  61. }
  62. }
  63. }
  64. private void Awake()
  65. {
  66. IsInited = true;
  67. }
  68. private void FixedUpdate()
  69. {
  70. lastPosition = transform.position;
  71. lastRotation = transform.rotation;
  72. }
  73. /// <summary>
  74. /// Update the locator forcibly from the outside(In case the object (target) moves by itself.
  75. /// </summary>
  76. public void UpdateLocator()
  77. {
  78. onUpdateLocator();
  79. }
  80. }
  81. }