SettlementInteractionUI.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. /// <summary>
  4. /// Simple UI controller for settlement interaction prompts
  5. /// Displays when near a settlement and shows interaction instructions
  6. /// </summary>
  7. public class SettlementInteractionUI : MonoBehaviour
  8. {
  9. [Header("UI References")]
  10. [Tooltip("UI Document for settlement interaction prompt")]
  11. public UIDocument uiDocument;
  12. [Header("UI Element Names")]
  13. [Tooltip("Name of the interaction prompt container in UXML")]
  14. public string promptContainerName = "SettlementPrompt";
  15. [Tooltip("Name of the settlement name label in UXML")]
  16. public string settlementNameLabelName = "SettlementName";
  17. [Tooltip("Name of the interaction instruction label in UXML")]
  18. public string instructionLabelName = "InteractionInstruction";
  19. // UI Elements
  20. private VisualElement promptContainer;
  21. private Label settlementNameLabel;
  22. private Label instructionLabel;
  23. // References
  24. private SettlementInteractionManager interactionManager;
  25. void Start()
  26. {
  27. InitializeUI();
  28. SetupEventListeners();
  29. }
  30. /// <summary>
  31. /// Initialize UI elements
  32. /// </summary>
  33. private void InitializeUI()
  34. {
  35. if (uiDocument == null)
  36. {
  37. uiDocument = GetComponent<UIDocument>();
  38. }
  39. if (uiDocument == null)
  40. {
  41. Debug.LogError("SettlementInteractionUI: No UIDocument found!");
  42. return;
  43. }
  44. var root = uiDocument.rootVisualElement;
  45. // Find UI elements
  46. promptContainer = root.Q<VisualElement>(promptContainerName);
  47. settlementNameLabel = root.Q<Label>(settlementNameLabelName);
  48. instructionLabel = root.Q<Label>(instructionLabelName);
  49. // Initially hide the prompt
  50. if (promptContainer != null)
  51. {
  52. promptContainer.style.display = DisplayStyle.None;
  53. }
  54. Debug.Log("SettlementInteractionUI initialized");
  55. }
  56. /// <summary>
  57. /// Setup event listeners for settlement interaction
  58. /// </summary>
  59. private void SetupEventListeners()
  60. {
  61. interactionManager = FindFirstObjectByType<SettlementInteractionManager>();
  62. if (interactionManager != null)
  63. {
  64. interactionManager.OnSettlementDetected += ShowSettlementPrompt;
  65. interactionManager.OnSettlementLeft += HideSettlementPrompt;
  66. interactionManager.OnSettlementEntered += OnSettlementEntered;
  67. Debug.Log("SettlementInteractionUI connected to SettlementInteractionManager");
  68. }
  69. else
  70. {
  71. Debug.LogWarning("SettlementInteractionUI: SettlementInteractionManager not found!");
  72. }
  73. }
  74. /// <summary>
  75. /// Show settlement interaction prompt
  76. /// </summary>
  77. private void ShowSettlementPrompt(Settlement settlement)
  78. {
  79. if (promptContainer == null) return;
  80. // Update settlement name
  81. if (settlementNameLabel != null)
  82. {
  83. settlementNameLabel.text = settlement.name;
  84. }
  85. // Update interaction instruction
  86. if (instructionLabel != null)
  87. {
  88. string settlementType = settlement.Type == SettlementType.Town ? "Town" : "Village";
  89. string keyName = interactionManager?.enterSettlementKey.ToString() ?? "E";
  90. instructionLabel.text = $"Press {keyName} to enter {settlementType}";
  91. }
  92. // Show the prompt
  93. promptContainer.style.display = DisplayStyle.Flex;
  94. Debug.Log($"Showing prompt for: {settlement.name}");
  95. }
  96. /// <summary>
  97. /// Hide settlement interaction prompt
  98. /// </summary>
  99. private void HideSettlementPrompt()
  100. {
  101. if (promptContainer != null)
  102. {
  103. promptContainer.style.display = DisplayStyle.None;
  104. }
  105. Debug.Log("Hiding settlement prompt");
  106. }
  107. /// <summary>
  108. /// Handle settlement entry (cleanup if needed)
  109. /// </summary>
  110. private void OnSettlementEntered(Settlement settlement)
  111. {
  112. Debug.Log($"Entering settlement: {settlement.name}");
  113. // UI will be destroyed when scene changes, no cleanup needed
  114. }
  115. void OnDestroy()
  116. {
  117. // Cleanup event listeners
  118. if (interactionManager != null)
  119. {
  120. interactionManager.OnSettlementDetected -= ShowSettlementPrompt;
  121. interactionManager.OnSettlementLeft -= HideSettlementPrompt;
  122. interactionManager.OnSettlementEntered -= OnSettlementEntered;
  123. }
  124. }
  125. /// <summary>
  126. /// Force UI to show (for testing or manual control)
  127. /// </summary>
  128. public void ForceShowUI()
  129. {
  130. if (promptContainer != null)
  131. {
  132. promptContainer.style.display = DisplayStyle.Flex;
  133. // Update with current nearby settlement if available
  134. if (interactionManager != null)
  135. {
  136. var currentSettlement = interactionManager.GetCurrentNearbySettlement();
  137. if (currentSettlement != null)
  138. {
  139. ShowSettlementPrompt(currentSettlement);
  140. return;
  141. }
  142. }
  143. // Fallback: show with generic text
  144. if (settlementNameLabel != null)
  145. {
  146. settlementNameLabel.text = "Nearby Settlement";
  147. }
  148. if (instructionLabel != null)
  149. {
  150. instructionLabel.text = "Press E to enter";
  151. }
  152. Debug.Log("[SettlementUI] Force showing UI");
  153. }
  154. }
  155. /// <summary>
  156. /// Force UI to hide (for testing or manual control)
  157. /// </summary>
  158. public void ForceHideUI()
  159. {
  160. if (promptContainer != null)
  161. {
  162. promptContainer.style.display = DisplayStyle.None;
  163. Debug.Log("[SettlementUI] Force hiding UI");
  164. }
  165. }
  166. /// <summary>
  167. /// Check if UI is currently visible
  168. /// </summary>
  169. public bool IsUIVisible()
  170. {
  171. return promptContainer != null && promptContainer.style.display == DisplayStyle.Flex;
  172. }
  173. }