91 lines
2.2 KiB
C#
91 lines
2.2 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace Northbound
|
|
{
|
|
public class InteractableModal : MonoBehaviour
|
|
{
|
|
[Header("UI References")]
|
|
[SerializeField] private GameObject modalPanel;
|
|
[SerializeField] private TextMeshProUGUI keyText;
|
|
[SerializeField] private TextMeshProUGUI interactText;
|
|
|
|
[Header("Settings")]
|
|
[SerializeField] private string defaultKey = "E";
|
|
|
|
private IInteractable _currentInteractable;
|
|
private Canvas _parentCanvas;
|
|
|
|
private void Awake()
|
|
{
|
|
_parentCanvas = GetComponentInParent<Canvas>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (modalPanel != null)
|
|
{
|
|
modalPanel.SetActive(false);
|
|
}
|
|
}
|
|
|
|
public void ShowModal(IInteractable interactable)
|
|
{
|
|
if (interactable == null)
|
|
{
|
|
HideModal();
|
|
return;
|
|
}
|
|
|
|
_currentInteractable = interactable;
|
|
|
|
if (modalPanel != null)
|
|
{
|
|
modalPanel.SetActive(true);
|
|
UpdateModalContent();
|
|
}
|
|
}
|
|
|
|
public void HideModal()
|
|
{
|
|
_currentInteractable = null;
|
|
|
|
if (modalPanel != null)
|
|
{
|
|
modalPanel.SetActive(false);
|
|
}
|
|
}
|
|
|
|
public void UpdateModalContent()
|
|
{
|
|
if (_currentInteractable == null)
|
|
return;
|
|
|
|
if (keyText != null)
|
|
{
|
|
keyText.text = defaultKey;
|
|
}
|
|
|
|
if (interactText != null)
|
|
{
|
|
string prompt = _currentInteractable.GetInteractionPrompt();
|
|
interactText.text = ExtractInteractText(prompt);
|
|
}
|
|
}
|
|
|
|
private string ExtractInteractText(string prompt)
|
|
{
|
|
if (string.IsNullOrEmpty(prompt))
|
|
return string.Empty;
|
|
|
|
int startIndex = prompt.IndexOf(']');
|
|
if (startIndex >= 0 && startIndex + 1 < prompt.Length)
|
|
{
|
|
return prompt.Substring(startIndex + 1).Trim();
|
|
}
|
|
|
|
return prompt;
|
|
}
|
|
}
|
|
}
|