using UnityEngine;
using UnityEngine.UIElements;
using System.Collections;
///
/// Simple UI controller for settlement interaction prompts
/// Displays when near a settlement and shows interaction instructions
///
public class SettlementInteractionUI : MonoBehaviour
{
[Header("UI References")]
[Tooltip("UI Document for settlement interaction prompt")]
public UIDocument uiDocument;
[Header("UI Element Names")]
[Tooltip("Name of the interaction prompt container in UXML")]
public string promptContainerName = "SettlementPrompt";
[Tooltip("Name of the settlement name label in UXML")]
public string settlementNameLabelName = "SettlementName";
[Tooltip("Name of the interaction instruction label in UXML")]
public string instructionLabelName = "InteractionInstruction";
// UI Elements
private VisualElement promptContainer;
private Label settlementNameLabel;
private Label instructionLabel;
// References
private SettlementInteractionManager interactionManager;
private bool eventsSubscribed = false;
void Start()
{
InitializeUI();
SetupEventListeners();
// Check for nearby settlements on start
StartCoroutine(CheckForNearbySettlementOnStart());
}
void OnEnable()
{
// When returning from a settlement, check if we're still near one
if (interactionManager != null)
{
StartCoroutine(CheckForNearbySettlementOnEnable());
}
}
private IEnumerator CheckForNearbySettlementOnStart()
{
// Wait a frame to ensure other systems are ready
yield return new WaitForEndOfFrame();
CheckForNearbySettlement();
}
private IEnumerator CheckForNearbySettlementOnEnable()
{
// Wait a frame to ensure other systems are ready
yield return new WaitForEndOfFrame();
CheckForNearbySettlement();
}
private void CheckForNearbySettlement()
{
// If manager wasn't found during setup, try to find it again
if (interactionManager == null)
{
interactionManager = FindFirstObjectByType();
if (interactionManager != null && !eventsSubscribed)
{
// Subscribe to events now that we found the manager
interactionManager.OnSettlementDetected += ShowSettlementPrompt;
interactionManager.OnSettlementLeft += HideSettlementPrompt;
interactionManager.OnSettlementEntered += OnSettlementEntered;
eventsSubscribed = true;
}
}
if (interactionManager != null)
{
var currentSettlement = interactionManager.GetCurrentNearbySettlement();
if (currentSettlement != null)
{
ShowSettlementPrompt(currentSettlement);
}
else
{
HideSettlementPrompt();
}
}
else
{
Debug.LogWarning("SettlementInteractionUI: Still cannot find SettlementInteractionManager");
}
}
///
/// Initialize UI elements
///
private void InitializeUI()
{
if (uiDocument == null)
{
uiDocument = GetComponent();
}
if (uiDocument == null)
{
Debug.LogError("SettlementInteractionUI: No UIDocument found!");
return;
}
var root = uiDocument.rootVisualElement;
// Find UI elements
promptContainer = root.Q(promptContainerName);
settlementNameLabel = root.Q