92 lines
2.1 KiB
C#
92 lines
2.1 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class EdgeIndicatorManager : MonoBehaviour
|
|
{
|
|
public static EdgeIndicatorManager Instance;
|
|
|
|
[Header("References")]
|
|
public MinimapManager minimapManager;
|
|
public RectTransform edgeContainer;
|
|
public EdgeIndicator indicatorPrefab;
|
|
|
|
[Header("Settings")]
|
|
public float edgePadding = 18f;
|
|
public float refreshInterval = 1f;
|
|
|
|
private readonly Dictionary<EdgeTarget, EdgeIndicator> indicators =
|
|
new Dictionary<EdgeTarget, EdgeIndicator>();
|
|
|
|
float timer;
|
|
|
|
void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
if (minimapManager == null)
|
|
minimapManager = FindObjectOfType<MinimapManager>();
|
|
|
|
RefreshTargets();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
timer += Time.deltaTime;
|
|
|
|
if (timer >= refreshInterval)
|
|
{
|
|
timer = 0;
|
|
RefreshTargets();
|
|
}
|
|
}
|
|
|
|
void RefreshTargets()
|
|
{
|
|
EdgeTarget[] targets = FindObjectsOfType<EdgeTarget>();
|
|
|
|
HashSet<EdgeTarget> currentTargets = new HashSet<EdgeTarget>(targets);
|
|
|
|
//-------------------------------------
|
|
// Remove destroyed targets
|
|
//-------------------------------------
|
|
|
|
List<EdgeTarget> remove = new List<EdgeTarget>();
|
|
|
|
foreach (var pair in indicators)
|
|
{
|
|
if (pair.Key == null || !currentTargets.Contains(pair.Key))
|
|
{
|
|
if (pair.Value != null)
|
|
Destroy(pair.Value.gameObject);
|
|
|
|
remove.Add(pair.Key);
|
|
}
|
|
}
|
|
|
|
foreach (var t in remove)
|
|
indicators.Remove(t);
|
|
|
|
//-------------------------------------
|
|
// Add new targets
|
|
//-------------------------------------
|
|
|
|
foreach (EdgeTarget target in targets)
|
|
{
|
|
if (indicators.ContainsKey(target))
|
|
continue;
|
|
|
|
EdgeIndicator arrow =
|
|
Instantiate(indicatorPrefab, edgeContainer);
|
|
|
|
arrow.Initialize(
|
|
target,
|
|
edgeContainer,
|
|
edgePadding);
|
|
|
|
indicators.Add(target, arrow);
|
|
}
|
|
}
|
|
} |