Radar.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using ObjectScripts;
  5. using CSVReader;
  6. namespace UIScripts
  7. {
  8. public class Radar : MonoBehaviour
  9. {
  10. ///<summary>
  11. /// RadarDot Image
  12. ///</summary>
  13. [SerializeField] GameObject Dot;
  14. ///<summary>
  15. /// RadarCenter Image
  16. ///</summary>
  17. [SerializeField] GameObject Center;
  18. ///<summary>
  19. /// Eventhandler to get events
  20. ///</summary>
  21. [SerializeField] AbstractEventHandler EventHandler;
  22. ///<summary>
  23. /// Panel for Radar
  24. ///</summary>
  25. [SerializeField] GameObject Panel;
  26. ///<summary>
  27. /// Player gameobject to get the position
  28. ///</summary>
  29. [SerializeField] GameObject Player;
  30. ///<summary>
  31. /// scale of the map
  32. ///</summary>
  33. public float MapScale = 1;
  34. ///<summary>
  35. /// Transform of the player
  36. ///</summary>
  37. Transform PlayerTransform;
  38. ///<summary>
  39. /// Player position in x,z coordinates
  40. ///</summary>
  41. Vector2 PlayerPosition;
  42. ///<summary>
  43. /// Player rotation y in euler angle
  44. ///</summary>
  45. float EulerY;
  46. ///<summary>
  47. /// radius of the radar
  48. ///</summary>
  49. public float Radius = 100;
  50. ///<summary>
  51. ///
  52. ///</summary>
  53. List<AbstractEventScript> EventsInRadar;
  54. ///<summary>
  55. /// Dictionary to keep track of the each event dot
  56. ///</summary>
  57. Dictionary<uint, GameObject> Dots;
  58. ///<summary>
  59. /// number of known events
  60. ///</summary>
  61. int KnownCount = 0;
  62. // Start is called before the first frame update
  63. void Start()
  64. {
  65. EventsInRadar = new List<AbstractEventScript>();
  66. Dots = new Dictionary<uint, GameObject>();
  67. Instantiate(Center, Panel.transform);
  68. }
  69. void Update()
  70. {
  71. PlayerPosition = new Vector2(Player.transform.position.x, Player.transform.position.z);
  72. EulerY = Player.transform.eulerAngles.y;
  73. for (int i = KnownCount; i < EventHandler.EventList.Count; i++)
  74. {
  75. GameObject dot = Instantiate(Dot, Panel.transform);
  76. dot.SetActive(false);
  77. Dots.Add(EventHandler.EventList[i].ID, dot);
  78. KnownCount++;
  79. }
  80. foreach (AbstractEventScript eventScript in EventHandler.EventList)
  81. {
  82. DrawEventOnRadar(eventScript);
  83. }
  84. }
  85. ///<summary>
  86. /// true if event in radar
  87. ///</summary>
  88. private bool EventInRadar(AbstractEventScript e)
  89. {
  90. return Vector2.Distance(e.Position2D, PlayerPosition) < Radius;
  91. }
  92. ///<summary>
  93. /// Draw Event on Radar
  94. ///</summary>
  95. private void DrawEventOnRadar(AbstractEventScript e)
  96. {
  97. if (EventInRadar(e) && e.EventIsVisible)
  98. {
  99. Vector2 radarPos = (e.Position2D - PlayerPosition);
  100. float distToObject = Vector2.Distance(e.Position2D, PlayerPosition) * MapScale;
  101. float deltay = Mathf.Atan2(radarPos.x, radarPos.y) * Mathf.Rad2Deg - 270 - EulerY;
  102. radarPos.x = distToObject * Mathf.Cos(deltay * Mathf.Deg2Rad) * -1;
  103. radarPos.y = distToObject * Mathf.Sin(deltay * Mathf.Deg2Rad);
  104. GameObject dot = Dots[e.ID];
  105. dot.transform.localPosition = new Vector3(radarPos.x, radarPos.y, 0);
  106. dot.SetActive(true);
  107. }
  108. else
  109. {
  110. GameObject dot = Dots[e.ID];
  111. dot.SetActive(false);
  112. }
  113. }
  114. }
  115. }