using UnityEngine; using Unheimlich.Gameplay.Player; using Unheimlich.UI.DetectiveBoard; namespace Unheimlich.NonEuclidean { public class DetectiveBoardPortalHider : MonoBehaviour { [SerializeField] private float _checkInterval = 0.1f; [SerializeField] private float _hideDistance = 5f; [SerializeField] private bool _enableDebugLogs = true; private Canvas _detectiveBoardCanvas; private NonEuclideanManager _manager; private float _checkTimer; private void Start() { _manager = NonEuclideanManager.Instance; if (DetectiveBoardCanvas.Instance != null) { _detectiveBoardCanvas = DetectiveBoardCanvas.Instance.GetComponent(); } if (_enableDebugLogs) { Debug.Log($"[DetectiveBoardPortalHider] Initialized - Manager: {_manager != null}, Canvas: {_detectiveBoardCanvas != null}"); } } private void Update() { if (_detectiveBoardCanvas == null || _manager == null || PlayerController.Instance == null) { if (_enableDebugLogs && Time.frameCount % 300 == 0) { Debug.LogWarning($"[DetectiveBoardPortalHider] Missing references - Canvas: {_detectiveBoardCanvas != null}, Manager: {_manager != null}, Player: {PlayerController.Instance != null}"); } return; } _checkTimer += Time.deltaTime; if (_checkTimer >= _checkInterval) { _checkTimer = 0f; CheckAndToggleCanvas(); } } private void CheckAndToggleCanvas() { Vector3 playerPos = PlayerController.Instance.transform.position; NonEuclideanRoom currentRoom = _manager.ActiveRoom; if (currentRoom == null) { if (_enableDebugLogs) Debug.LogWarning("[DetectiveBoardPortalHider] No active room"); return; } NonEuclideanRoom boardRoom = GetDetectiveBoardRoom(); if (boardRoom == null) { if (_enableDebugLogs && Time.frameCount % 300 == 0) Debug.LogWarning("[DetectiveBoardPortalHider] Could not find board room"); _detectiveBoardCanvas.enabled = true; return; } bool isInBoardRoom = (currentRoom == boardRoom); if (_enableDebugLogs) Debug.Log($"[DetectiveBoardPortalHider] Current room: {currentRoom.name}, Board room: {boardRoom.name}, In board room: {isInBoardRoom}, Portals count: {currentRoom.Portals.Length}"); foreach (var portal in currentRoom.Portals) { float distance = Vector3.Distance(playerPos, portal.transform.position); bool shouldHide = false; if (isInBoardRoom) { shouldHide = (distance < _hideDistance); if (_enableDebugLogs && shouldHide) Debug.Log($"[DetectiveBoardPortalHider] In board room, close to portal '{portal.name}' (distance: {distance:F2}m) - hiding canvas"); } else if (portal.TargetRoom == boardRoom) { shouldHide = (distance < _hideDistance); if (_enableDebugLogs && shouldHide) Debug.Log($"[DetectiveBoardPortalHider] Close to portal '{portal.name}' leading to board room (distance: {distance:F2}m) - hiding canvas"); } if (shouldHide) { _detectiveBoardCanvas.enabled = false; return; } } if (_enableDebugLogs) Debug.Log("[DetectiveBoardPortalHider] Not close to any relevant portal - enabling canvas"); _detectiveBoardCanvas.enabled = true; } private NonEuclideanRoom GetDetectiveBoardRoom() { if (_detectiveBoardCanvas == null) return null; Transform current = _detectiveBoardCanvas.transform; while (current != null) { NonEuclideanRoom room = current.GetComponent(); if (room != null) { if (_enableDebugLogs && Time.frameCount % 300 == 0) Debug.Log($"[DetectiveBoardPortalHider] Found board room: {room.name}"); return room; } current = current.parent; } return null; } } }