123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using ObjectScripts;
- using CSVReader;
- namespace UIScripts
- {
- public class Radar : MonoBehaviour
- {
- ///<summary>
- /// RadarDot Image
- ///</summary>
- [SerializeField] GameObject Dot;
- ///<summary>
- /// RadarCenter Image
- ///</summary>
- [SerializeField] GameObject Center;
- ///<summary>
- /// Eventhandler to get events
- ///</summary>
- [SerializeField] AbstractEventHandler EventHandler;
- ///<summary>
- /// Panel for Radar
- ///</summary>
- [SerializeField] GameObject Panel;
- ///<summary>
- /// Player gameobject to get the position
- ///</summary>
- [SerializeField] GameObject Player;
- ///<summary>
- /// scale of the map
- ///</summary>
- public float MapScale = 1;
- ///<summary>
- /// Transform of the player
- ///</summary>
- Transform PlayerTransform;
- ///<summary>
- /// Player position in x,z coordinates
- ///</summary>
- Vector2 PlayerPosition;
- ///<summary>
- /// Player rotation y in euler angle
- ///</summary>
- float EulerY;
- ///<summary>
- /// radius of the radar
- ///</summary>
- public float Radius = 100;
- ///<summary>
- ///
- ///</summary>
- List<AbstractEventScript> EventsInRadar;
- ///<summary>
- /// Dictionary to keep track of the each event dot
- ///</summary>
- Dictionary<uint, GameObject> Dots;
- ///<summary>
- /// number of known events
- ///</summary>
- int KnownCount = 0;
- // Start is called before the first frame update
- void Start()
- {
- EventsInRadar = new List<AbstractEventScript>();
- Dots = new Dictionary<uint, GameObject>();
- Instantiate(Center, Panel.transform);
- }
- void Update()
- {
- PlayerPosition = new Vector2(Player.transform.position.x, Player.transform.position.z);
- EulerY = Player.transform.eulerAngles.y;
- for (int i = KnownCount; i < EventHandler.EventList.Count; i++)
- {
- GameObject dot = Instantiate(Dot, Panel.transform);
- dot.SetActive(false);
- Dots.Add(EventHandler.EventList[i].ID, dot);
- KnownCount++;
- }
- foreach (AbstractEventScript eventScript in EventHandler.EventList)
- {
- DrawEventOnRadar(eventScript);
- }
- }
- ///<summary>
- /// true if event in radar
- ///</summary>
- private bool EventInRadar(AbstractEventScript e)
- {
- return Vector2.Distance(e.Position2D, PlayerPosition) < Radius;
- }
- ///<summary>
- /// Draw Event on Radar
- ///</summary>
- private void DrawEventOnRadar(AbstractEventScript e)
- {
- if (EventInRadar(e) && e.EventIsVisible)
- {
- Vector2 radarPos = (e.Position2D - PlayerPosition);
- float distToObject = Vector2.Distance(e.Position2D, PlayerPosition) * MapScale;
- float deltay = Mathf.Atan2(radarPos.x, radarPos.y) * Mathf.Rad2Deg - 270 - EulerY;
- radarPos.x = distToObject * Mathf.Cos(deltay * Mathf.Deg2Rad) * -1;
- radarPos.y = distToObject * Mathf.Sin(deltay * Mathf.Deg2Rad);
- GameObject dot = Dots[e.ID];
- dot.transform.localPosition = new Vector3(radarPos.x, radarPos.y, 0);
- dot.SetActive(true);
- }
- else
- {
- GameObject dot = Dots[e.ID];
- dot.SetActive(false);
- }
- }
- }
- }
|