using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; [DisallowMultipleComponent] public sealed class MapRadarController : MonoBehaviour { [Header("Radar Settings")] [SerializeField, Min(0f)] private float cooldown = 5f; [SerializeField] private bool useKeyboardInput = true; [Header("Radar State (Runtime)")] [SerializeField] private float cooldownRemaining; private readonly List radarMarks = new(); private MapCoordinateSystem coordinateSystem; private MapTargetController targetController; private MapTarget radarMarkPrefab; private float nextActivationTime; private bool isConfigured; public float CooldownRemaining => cooldownRemaining; public bool IsReady => cooldownRemaining <= 0f; private void OnValidate() { cooldown = Mathf.Max(0f, cooldown); } public void Configure( MapCoordinateSystem newCoordinateSystem, MapTargetController newTargetController, MapTarget newRadarMarkPrefab) { coordinateSystem = newCoordinateSystem; targetController = newTargetController; radarMarkPrefab = newRadarMarkPrefab; isConfigured = coordinateSystem != null && coordinateSystem.HasValidCorners() && targetController != null && radarMarkPrefab != null; if (targetController != null) targetController.SetTargetVisualsVisible(false); } private void Update() { cooldownRemaining = Mathf.Max(0f, nextActivationTime - Time.time); if (!useKeyboardInput) return; Keyboard keyboard = Keyboard.current; if (keyboard != null && keyboard.oKey.wasPressedThisFrame) ActivateRadar(); } public bool ActivateRadar() { if (!isConfigured || Time.time < nextActivationTime) return false; PlaceRadarMarks(); nextActivationTime = Time.time + Mathf.Max(0f, cooldown); cooldownRemaining = Mathf.Max(0f, nextActivationTime - Time.time); return true; } private void PlaceRadarMarks() { int targetCount = targetController.Targets.Count; EnsureRadarMarkCount(targetCount); for (int index = 0; index < radarMarks.Count; index++) { bool hasTarget = index < targetCount && targetController.Targets[index] != null; GameObject radarMark = radarMarks[index]; radarMark.SetActive(hasTarget); if (!hasTarget) continue; MapTarget target = targetController.Targets[index]; radarMark.name = $"Radar Mark - {target.name}"; radarMark.transform.position = coordinateSystem.CoordinatesToWorldPosition(target.MapCoordinates); } } private void EnsureRadarMarkCount(int requiredCount) { while (radarMarks.Count < requiredCount) { GameObject radarMark = Instantiate(radarMarkPrefab.gameObject, transform); PrepareRadarMark(radarMark); radarMarks.Add(radarMark); } } private static void PrepareRadarMark(GameObject radarMark) { MapTarget targetMovement = radarMark.GetComponent(); if (targetMovement != null) targetMovement.enabled = false; foreach (Collider targetCollider in radarMark.GetComponentsInChildren(true)) targetCollider.enabled = false; foreach (Renderer targetRenderer in radarMark.GetComponentsInChildren(true)) targetRenderer.enabled = true; } }