Files
Northbound/Assets/Scripts/InteractableModalManager.cs

161 lines
4.8 KiB
C#

using UnityEngine;
using Unity.Netcode;
namespace Northbound
{
public class InteractableModalManager : MonoBehaviour
{
[Header("UI Reference (Optional - auto-finds if null)")]
public InteractableModal interactableModal;
[Header("References")]
[SerializeField] private PlayerInteraction playerInteraction;
private IInteractable _currentInteractable;
private bool _isEnabled = false;
private void Awake()
{
Debug.Log($"[InteractableModalManager] Awake called on {gameObject.name}");
if (playerInteraction == null)
{
playerInteraction = GetComponent<PlayerInteraction>();
Debug.Log($"[InteractableModalManager] PlayerInteraction: {(playerInteraction != null ? "Found" : "Not Found")}");
}
FindModalComponent();
}
private void Start()
{
Debug.Log($"[InteractableModalManager] Start called on {gameObject.name}");
FindModalComponent();
if (interactableModal != null)
{
interactableModal.HideModal();
}
else
{
Debug.LogError("[InteractableModalManager] InteractableModal is still null in Start!");
}
}
private void FindModalComponent()
{
if (interactableModal != null)
{
Debug.Log($"[InteractableModalManager] InteractableModal already assigned: {interactableModal.gameObject.name}");
return;
}
InteractableModal[] modals = GetComponentsInChildren<InteractableModal>(true);
Debug.Log($"[InteractableModalManager] Found {modals.Length} InteractableModal components in player children");
if (modals.Length > 0)
{
interactableModal = modals[0];
Debug.Log($"[InteractableModalManager] Using InteractableModal on: {interactableModal.gameObject.name}");
}
else
{
Debug.LogWarning("[InteractableModalManager] InteractableModal not found in player children!");
}
}
private void Update()
{
if (playerInteraction == null)
{
Debug.LogWarning("[InteractableModalManager] Update: playerInteraction is null");
return;
}
bool isLocalPlayer = IsLocalPlayer();
if (!isLocalPlayer)
{
if (_isEnabled)
{
HideModal();
_isEnabled = false;
}
return;
}
_isEnabled = true;
IInteractable detectedInteractable = GetDetectedInteractable();
if (detectedInteractable != _currentInteractable)
{
_currentInteractable = detectedInteractable;
if (_currentInteractable != null)
{
Debug.Log($"[InteractableModalManager] Show modal for interactable");
ShowModal(_currentInteractable);
}
else
{
Debug.Log($"[InteractableModalManager] Hide modal");
HideModal();
}
}
else if (_currentInteractable != null)
{
UpdateModalContent();
}
}
private bool IsLocalPlayer()
{
if (playerInteraction == null)
return false;
var networkObject = playerInteraction.GetComponent<Unity.Netcode.NetworkObject>();
if (networkObject != null)
{
return networkObject.IsOwner;
}
return false;
}
private IInteractable GetDetectedInteractable()
{
var field = playerInteraction.GetType().GetField("_currentInteractable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (field != null)
{
return field.GetValue(playerInteraction) as IInteractable;
}
return null;
}
private void ShowModal(IInteractable interactable)
{
if (interactableModal != null)
{
interactableModal.ShowModal(interactable);
}
}
private void HideModal()
{
if (interactableModal != null)
{
interactableModal.HideModal();
}
}
private void UpdateModalContent()
{
if (interactableModal != null)
{
interactableModal.UpdateModalContent();
}
}
}
}