using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ObjectScripts;
using CSVReader;
namespace UIScripts
{
public class Radar : MonoBehaviour
{
///
/// RadarDot Image
///
[SerializeField] GameObject Dot;
///
/// RadarCenter Image
///
[SerializeField] GameObject Center;
///
/// Eventhandler to get events
///
[SerializeField] AbstractEventHandler EventHandler;
///
/// Panel for Radar
///
[SerializeField] GameObject Panel;
///
/// Player gameobject to get the position
///
[SerializeField] GameObject Player;
///
/// scale of the map
///
public float MapScale = 1;
///
/// Transform of the player
///
Transform PlayerTransform;
///
/// Player position in x,z coordinates
///
Vector2 PlayerPosition;
///
/// Player rotation y in euler angle
///
float EulerY;
///
/// radius of the radar
///
public float Radius = 100;
///
///
///
List EventsInRadar;
///
/// Dictionary to keep track of the each event dot
///
Dictionary Dots;
///
/// number of known events
///
int KnownCount = 0;
// Start is called before the first frame update
void Start()
{
EventsInRadar = new List();
Dots = new Dictionary();
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);
}
}
///
/// true if event in radar
///
private bool EventInRadar(AbstractEventScript e)
{
return Vector2.Distance(e.Position2D, PlayerPosition) < Radius;
}
///
/// Draw Event on Radar
///
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);
}
}
}
}