MiniMapTargetView.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // MIT License
  2. // https://gitlab.com/ilnprj
  3. // Copyright (c) 2020 ilnprj
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace RadarComponents
  7. {
  8. /// <summary>
  9. /// Script that display target on the radar
  10. /// </summary>
  11. public class MiniMapTargetView : BaseTargetView
  12. {
  13. private RectTransform rectPositionView;
  14. private Image imageTargetView;
  15. private Image _background;
  16. private Image backgroundRadar
  17. {
  18. get
  19. {
  20. if (_background == null)
  21. {
  22. _background = rootTransform.GetComponent<Image>();
  23. return _background;
  24. }
  25. return _background;
  26. }
  27. }
  28. private float radarWidth;
  29. private float radarHeight;
  30. private float targetHeight;
  31. private float targetWidth;
  32. private MiniMapRadar radarContainer;
  33. protected override void Awake()
  34. {
  35. base.Awake();
  36. rectPositionView = GetComponent<RectTransform>();
  37. imageTargetView = GetComponent<Image>();
  38. imageTargetView.preserveAspect = true;
  39. radarContainer = GetComponentInParent<MiniMapRadar>();
  40. }
  41. private void Start()
  42. {
  43. radarWidth = backgroundRadar.rectTransform.rect.width;
  44. radarHeight = backgroundRadar.rectTransform.rect.height;
  45. targetHeight = radarHeight * radarContainer.TargetViewSize / 100;
  46. targetWidth = radarWidth * radarContainer.TargetViewSize / 100;
  47. UpdateViewTarget();
  48. }
  49. public override void UpdateViewTarget()
  50. {
  51. Vector3 playerPos = playerTransform.position;
  52. Vector3 targetPos = CurrentTarget.TransformTarget.position;
  53. Vector3 normalisedTargetPosiiton = NormalisedPosition(playerPos, targetPos);
  54. Vector2 targetPosition = CalculateBlipPosition(normalisedTargetPosiiton);
  55. if (radarContainer.TargetsFadeOut)
  56. {
  57. Vector3 dif = targetPos - playerPos;
  58. float distance = dif.magnitude;
  59. imageTargetView.enabled = distance <= radarContainer.RadarViewDistance;
  60. }
  61. if (!radarContainer.CircleRadar)
  62. {
  63. targetPosition.x = CheckBorder(targetPosition.x, backgroundRadar.rectTransform.rect.width);
  64. targetPosition.y = CheckBorder(targetPosition.y, backgroundRadar.rectTransform.rect.height);
  65. }
  66. else
  67. {
  68. ClampPositionToCircle(new Vector2(60f,60f), 60f, ref targetPosition);
  69. }
  70. UpdateResultPosition(targetPosition);
  71. }
  72. /// <summary>
  73. /// Method using only for Circle Radar
  74. /// </summary>
  75. public void ClampPositionToCircle(Vector2 center, float radius, ref Vector2 position)
  76. {
  77. // Calculate the offset vector from the center of the circle to our position
  78. Vector2 offset = position - center;
  79. // Calculate the linear distance of this offset vector
  80. float distance = offset.magnitude;
  81. if (radius < distance)
  82. {
  83. // If the distance is more than our radius we need to clamp
  84. // Calculate the direction to our position
  85. Vector2 direction = offset / distance;
  86. // Calculate our new position using the direction to our old position and our radius
  87. position = center + direction * radius;
  88. }
  89. }
  90. /// <summary>
  91. /// Controls the location of the target within. Use only for Square radar
  92. /// </summary>
  93. private float CheckBorder(float position, float border)
  94. {
  95. if (position + targetWidth > border)
  96. {
  97. position = border - targetWidth;
  98. }
  99. if (position < 0)
  100. {
  101. position = 0;
  102. }
  103. return position;
  104. }
  105. /// <summary>
  106. /// Final set position target in radar
  107. /// </summary>
  108. /// <param name="position"></param>
  109. private void UpdateResultPosition(Vector2 position)
  110. {
  111. rectPositionView.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, position.x, targetWidth);
  112. rectPositionView.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, position.y, targetHeight);
  113. UpdateExtensions();
  114. }
  115. private Vector3 NormalisedPosition(Vector3 playerPos, Vector3 targetPos)
  116. {
  117. float normalisedyTargetX = (targetPos.x - playerPos.x) / radarContainer.RadarViewDistance;
  118. float normalisedyTargetZ = (targetPos.z - playerPos.z) / radarContainer.RadarViewDistance;
  119. return new Vector3(normalisedyTargetX, 0, normalisedyTargetZ);
  120. }
  121. private Vector2 CalculateBlipPosition(Vector3 targetPos)
  122. {
  123. float angleToTarget = Mathf.Atan2(targetPos.x, targetPos.z) * Mathf.Rad2Deg;
  124. float anglePlayer = playerTransform.eulerAngles.y;
  125. // Angle
  126. float angleRadarDegrees = angleToTarget - anglePlayer - 90;
  127. // Calculate
  128. float normalisedDistanceToTarget = targetPos.magnitude;
  129. float angleRadians = angleRadarDegrees * Mathf.Deg2Rad;
  130. float tarX = normalisedDistanceToTarget * Mathf.Cos(angleRadians);
  131. float tarY = normalisedDistanceToTarget * Mathf.Sin(angleRadians);
  132. // Scale
  133. tarX *= radarWidth / 2;
  134. tarY *= radarHeight / 2;
  135. // Offset
  136. tarX += radarWidth / radarContainer.OffsetVector.x;
  137. tarY += radarHeight / radarContainer.OffsetVector.y;
  138. return new Vector2(tarX, tarY);
  139. }
  140. }
  141. }