| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using UnityEngine;
- using UnityEngine.UIElements;
- /// <summary>
- /// Simple UI controller for settlement interaction prompts
- /// Displays when near a settlement and shows interaction instructions
- /// </summary>
- 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;
- void Start()
- {
- InitializeUI();
- SetupEventListeners();
- }
- /// <summary>
- /// Initialize UI elements
- /// </summary>
- private void InitializeUI()
- {
- if (uiDocument == null)
- {
- uiDocument = GetComponent<UIDocument>();
- }
- if (uiDocument == null)
- {
- Debug.LogError("SettlementInteractionUI: No UIDocument found!");
- return;
- }
- var root = uiDocument.rootVisualElement;
- // Find UI elements
- promptContainer = root.Q<VisualElement>(promptContainerName);
- settlementNameLabel = root.Q<Label>(settlementNameLabelName);
- instructionLabel = root.Q<Label>(instructionLabelName);
- // Initially hide the prompt
- if (promptContainer != null)
- {
- promptContainer.style.display = DisplayStyle.None;
- }
- Debug.Log("SettlementInteractionUI initialized");
- }
- /// <summary>
- /// Setup event listeners for settlement interaction
- /// </summary>
- private void SetupEventListeners()
- {
- interactionManager = FindFirstObjectByType<SettlementInteractionManager>();
- if (interactionManager != null)
- {
- interactionManager.OnSettlementDetected += ShowSettlementPrompt;
- interactionManager.OnSettlementLeft += HideSettlementPrompt;
- interactionManager.OnSettlementEntered += OnSettlementEntered;
- Debug.Log("SettlementInteractionUI connected to SettlementInteractionManager");
- }
- else
- {
- Debug.LogWarning("SettlementInteractionUI: SettlementInteractionManager not found!");
- }
- }
- /// <summary>
- /// Show settlement interaction prompt
- /// </summary>
- private void ShowSettlementPrompt(Settlement settlement)
- {
- if (promptContainer == null) return;
- // Update settlement name
- if (settlementNameLabel != null)
- {
- settlementNameLabel.text = settlement.name;
- }
- // Update interaction instruction
- if (instructionLabel != null)
- {
- string settlementType = settlement.Type == SettlementType.Town ? "Town" : "Village";
- string keyName = interactionManager?.enterSettlementKey.ToString() ?? "E";
- instructionLabel.text = $"Press {keyName} to enter {settlementType}";
- }
- // Show the prompt
- promptContainer.style.display = DisplayStyle.Flex;
- Debug.Log($"Showing prompt for: {settlement.name}");
- }
- /// <summary>
- /// Hide settlement interaction prompt
- /// </summary>
- private void HideSettlementPrompt()
- {
- if (promptContainer != null)
- {
- promptContainer.style.display = DisplayStyle.None;
- }
- Debug.Log("Hiding settlement prompt");
- }
- /// <summary>
- /// Handle settlement entry (cleanup if needed)
- /// </summary>
- private void OnSettlementEntered(Settlement settlement)
- {
- Debug.Log($"Entering settlement: {settlement.name}");
- // UI will be destroyed when scene changes, no cleanup needed
- }
- void OnDestroy()
- {
- // Cleanup event listeners
- if (interactionManager != null)
- {
- interactionManager.OnSettlementDetected -= ShowSettlementPrompt;
- interactionManager.OnSettlementLeft -= HideSettlementPrompt;
- interactionManager.OnSettlementEntered -= OnSettlementEntered;
- }
- }
- /// <summary>
- /// Force UI to show (for testing or manual control)
- /// </summary>
- public void ForceShowUI()
- {
- if (promptContainer != null)
- {
- promptContainer.style.display = DisplayStyle.Flex;
- // Update with current nearby settlement if available
- if (interactionManager != null)
- {
- var currentSettlement = interactionManager.GetCurrentNearbySettlement();
- if (currentSettlement != null)
- {
- ShowSettlementPrompt(currentSettlement);
- return;
- }
- }
- // Fallback: show with generic text
- if (settlementNameLabel != null)
- {
- settlementNameLabel.text = "Nearby Settlement";
- }
- if (instructionLabel != null)
- {
- instructionLabel.text = "Press E to enter";
- }
- Debug.Log("[SettlementUI] Force showing UI");
- }
- }
- /// <summary>
- /// Force UI to hide (for testing or manual control)
- /// </summary>
- public void ForceHideUI()
- {
- if (promptContainer != null)
- {
- promptContainer.style.display = DisplayStyle.None;
- Debug.Log("[SettlementUI] Force hiding UI");
- }
- }
- /// <summary>
- /// Check if UI is currently visible
- /// </summary>
- public bool IsUIVisible()
- {
- return promptContainer != null && promptContainer.style.display == DisplayStyle.Flex;
- }
- }
|