SettlementInteractionUI.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. using System.Collections;
  4. /// <summary>
  5. /// Simple UI controller for settlement interaction prompts
  6. /// Displays when near a settlement and shows interaction instructions
  7. /// </summary>
  8. public class SettlementInteractionUI : MonoBehaviour
  9. {
  10. [Header("UI References")]
  11. [Tooltip("UI Document for settlement interaction prompt")]
  12. public UIDocument uiDocument;
  13. [Header("UI Element Names")]
  14. [Tooltip("Name of the interaction prompt container in UXML")]
  15. public string promptContainerName = "SettlementPrompt";
  16. [Tooltip("Name of the settlement name label in UXML")]
  17. public string settlementNameLabelName = "SettlementName";
  18. [Tooltip("Name of the interaction instruction label in UXML")]
  19. public string instructionLabelName = "InteractionInstruction";
  20. // UI Elements
  21. private VisualElement promptContainer;
  22. private Label settlementNameLabel;
  23. private Label instructionLabel;
  24. // References
  25. private SettlementInteractionManager interactionManager;
  26. private bool eventsSubscribed = false;
  27. void Start()
  28. {
  29. InitializeUI();
  30. SetupEventListeners();
  31. // Check for nearby settlements on start
  32. StartCoroutine(CheckForNearbySettlementOnStart());
  33. }
  34. void OnEnable()
  35. {
  36. // When returning from a settlement, check if we're still near one
  37. if (interactionManager != null)
  38. {
  39. StartCoroutine(CheckForNearbySettlementOnEnable());
  40. }
  41. }
  42. private IEnumerator CheckForNearbySettlementOnStart()
  43. {
  44. // Wait a frame to ensure other systems are ready
  45. yield return new WaitForEndOfFrame();
  46. CheckForNearbySettlement();
  47. }
  48. private IEnumerator CheckForNearbySettlementOnEnable()
  49. {
  50. // Wait a frame to ensure other systems are ready
  51. yield return new WaitForEndOfFrame();
  52. CheckForNearbySettlement();
  53. }
  54. private void CheckForNearbySettlement()
  55. {
  56. // If manager wasn't found during setup, try to find it again
  57. if (interactionManager == null)
  58. {
  59. interactionManager = FindFirstObjectByType<SettlementInteractionManager>();
  60. if (interactionManager != null && !eventsSubscribed)
  61. {
  62. // Subscribe to events now that we found the manager
  63. interactionManager.OnSettlementDetected += ShowSettlementPrompt;
  64. interactionManager.OnSettlementLeft += HideSettlementPrompt;
  65. interactionManager.OnSettlementEntered += OnSettlementEntered;
  66. eventsSubscribed = true;
  67. }
  68. }
  69. if (interactionManager != null)
  70. {
  71. var currentSettlement = interactionManager.GetCurrentNearbySettlement();
  72. if (currentSettlement != null)
  73. {
  74. ShowSettlementPrompt(currentSettlement);
  75. }
  76. else
  77. {
  78. HideSettlementPrompt();
  79. }
  80. }
  81. else
  82. {
  83. Debug.LogWarning("SettlementInteractionUI: Still cannot find SettlementInteractionManager");
  84. }
  85. }
  86. /// <summary>
  87. /// Initialize UI elements
  88. /// </summary>
  89. private void InitializeUI()
  90. {
  91. if (uiDocument == null)
  92. {
  93. uiDocument = GetComponent<UIDocument>();
  94. }
  95. if (uiDocument == null)
  96. {
  97. Debug.LogError("SettlementInteractionUI: No UIDocument found!");
  98. return;
  99. }
  100. var root = uiDocument.rootVisualElement;
  101. // Find UI elements
  102. promptContainer = root.Q<VisualElement>(promptContainerName);
  103. settlementNameLabel = root.Q<Label>(settlementNameLabelName);
  104. instructionLabel = root.Q<Label>(instructionLabelName);
  105. // Initially hide the prompt
  106. if (promptContainer != null)
  107. {
  108. promptContainer.style.display = DisplayStyle.None;
  109. }
  110. }
  111. /// <summary>
  112. /// Setup event listeners for settlement interaction
  113. /// </summary>
  114. private void SetupEventListeners()
  115. {
  116. interactionManager = FindFirstObjectByType<SettlementInteractionManager>();
  117. if (interactionManager != null && !eventsSubscribed)
  118. {
  119. interactionManager.OnSettlementDetected += ShowSettlementPrompt;
  120. interactionManager.OnSettlementLeft += HideSettlementPrompt;
  121. interactionManager.OnSettlementEntered += OnSettlementEntered;
  122. eventsSubscribed = true;
  123. }
  124. else
  125. {
  126. Debug.LogWarning("SettlementInteractionUI: SettlementInteractionManager not found! Will retry in CheckForNearbySettlement");
  127. }
  128. }
  129. /// <summary>
  130. /// Show settlement interaction prompt
  131. /// </summary>
  132. private void ShowSettlementPrompt(Settlement settlement)
  133. {
  134. if (promptContainer == null)
  135. {
  136. Debug.LogWarning("SettlementInteractionUI: Cannot show prompt - promptContainer is null");
  137. return;
  138. }
  139. if (settlement == null)
  140. {
  141. Debug.LogWarning("SettlementInteractionUI: Cannot show prompt - settlement is null");
  142. return;
  143. }
  144. // Update settlement name
  145. if (settlementNameLabel != null)
  146. {
  147. settlementNameLabel.text = settlement.name;
  148. }
  149. else
  150. {
  151. Debug.LogWarning("SettlementInteractionUI: settlementNameLabel is null");
  152. }
  153. // Update interaction instruction
  154. if (instructionLabel != null)
  155. {
  156. string settlementType = settlement.Type == SettlementType.Town ? "Town" : "Village";
  157. string keyName = interactionManager?.enterSettlementKey.ToString() ?? "E";
  158. instructionLabel.text = $"Press {keyName} to enter {settlementType}";
  159. }
  160. else
  161. {
  162. Debug.LogWarning("SettlementInteractionUI: instructionLabel is null");
  163. }
  164. // Show the prompt
  165. promptContainer.style.display = DisplayStyle.Flex;
  166. }
  167. /// <summary>
  168. /// Hide settlement interaction prompt
  169. /// </summary>
  170. private void HideSettlementPrompt()
  171. {
  172. if (promptContainer != null)
  173. {
  174. promptContainer.style.display = DisplayStyle.None;
  175. }
  176. }
  177. /// <summary>
  178. /// Handle settlement entry (cleanup if needed)
  179. /// </summary>
  180. private void OnSettlementEntered(Settlement settlement)
  181. {
  182. // UI will be destroyed when scene changes, no cleanup needed
  183. }
  184. void OnDestroy()
  185. {
  186. // Cleanup event listeners
  187. if (interactionManager != null && eventsSubscribed)
  188. {
  189. interactionManager.OnSettlementDetected -= ShowSettlementPrompt;
  190. interactionManager.OnSettlementLeft -= HideSettlementPrompt;
  191. interactionManager.OnSettlementEntered -= OnSettlementEntered;
  192. eventsSubscribed = false;
  193. }
  194. }
  195. /// <summary>
  196. /// Force UI to show (for testing or manual control)
  197. /// </summary>
  198. public void ForceShowUI()
  199. {
  200. if (promptContainer != null)
  201. {
  202. promptContainer.style.display = DisplayStyle.Flex;
  203. // Update with current nearby settlement if available
  204. if (interactionManager != null)
  205. {
  206. var currentSettlement = interactionManager.GetCurrentNearbySettlement();
  207. if (currentSettlement != null)
  208. {
  209. ShowSettlementPrompt(currentSettlement);
  210. return;
  211. }
  212. }
  213. // Fallback: show with generic text
  214. if (settlementNameLabel != null)
  215. {
  216. settlementNameLabel.text = "Nearby Settlement";
  217. }
  218. if (instructionLabel != null)
  219. {
  220. instructionLabel.text = "Press E to enter";
  221. }
  222. }
  223. }
  224. /// <summary>
  225. /// Force UI to hide (for testing or manual control)
  226. /// </summary>
  227. public void ForceHideUI()
  228. {
  229. if (promptContainer != null)
  230. {
  231. promptContainer.style.display = DisplayStyle.None;
  232. }
  233. }
  234. /// <summary>
  235. /// Check if UI is currently visible
  236. /// </summary>
  237. public bool IsUIVisible()
  238. {
  239. return promptContainer != null && promptContainer.style.display == DisplayStyle.Flex;
  240. }
  241. }