SettlementInteractionUI.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. Debug.Log("SettlementInteractionUI: Late connection to SettlementInteractionManager successful");
  68. }
  69. }
  70. if (interactionManager != null)
  71. {
  72. var currentSettlement = interactionManager.GetCurrentNearbySettlement();
  73. if (currentSettlement != null)
  74. {
  75. Debug.Log($"SettlementInteractionUI: Found nearby settlement on enable: {currentSettlement.name}");
  76. ShowSettlementPrompt(currentSettlement);
  77. }
  78. else
  79. {
  80. Debug.Log("SettlementInteractionUI: No nearby settlement found on enable");
  81. HideSettlementPrompt();
  82. }
  83. }
  84. else
  85. {
  86. Debug.LogWarning("SettlementInteractionUI: Still cannot find SettlementInteractionManager");
  87. }
  88. }
  89. /// <summary>
  90. /// Initialize UI elements
  91. /// </summary>
  92. private void InitializeUI()
  93. {
  94. if (uiDocument == null)
  95. {
  96. uiDocument = GetComponent<UIDocument>();
  97. }
  98. if (uiDocument == null)
  99. {
  100. Debug.LogError("SettlementInteractionUI: No UIDocument found!");
  101. return;
  102. }
  103. var root = uiDocument.rootVisualElement;
  104. // Find UI elements
  105. promptContainer = root.Q<VisualElement>(promptContainerName);
  106. settlementNameLabel = root.Q<Label>(settlementNameLabelName);
  107. instructionLabel = root.Q<Label>(instructionLabelName);
  108. // Initially hide the prompt
  109. if (promptContainer != null)
  110. {
  111. promptContainer.style.display = DisplayStyle.None;
  112. }
  113. Debug.Log("SettlementInteractionUI initialized");
  114. }
  115. /// <summary>
  116. /// Setup event listeners for settlement interaction
  117. /// </summary>
  118. private void SetupEventListeners()
  119. {
  120. interactionManager = FindFirstObjectByType<SettlementInteractionManager>();
  121. if (interactionManager != null && !eventsSubscribed)
  122. {
  123. interactionManager.OnSettlementDetected += ShowSettlementPrompt;
  124. interactionManager.OnSettlementLeft += HideSettlementPrompt;
  125. interactionManager.OnSettlementEntered += OnSettlementEntered;
  126. eventsSubscribed = true;
  127. Debug.Log("SettlementInteractionUI connected to SettlementInteractionManager");
  128. }
  129. else
  130. {
  131. Debug.LogWarning("SettlementInteractionUI: SettlementInteractionManager not found! Will retry in CheckForNearbySettlement");
  132. }
  133. }
  134. /// <summary>
  135. /// Show settlement interaction prompt
  136. /// </summary>
  137. private void ShowSettlementPrompt(Settlement settlement)
  138. {
  139. if (promptContainer == null)
  140. {
  141. Debug.LogWarning("SettlementInteractionUI: Cannot show prompt - promptContainer is null");
  142. return;
  143. }
  144. if (settlement == null)
  145. {
  146. Debug.LogWarning("SettlementInteractionUI: Cannot show prompt - settlement is null");
  147. return;
  148. }
  149. // Update settlement name
  150. if (settlementNameLabel != null)
  151. {
  152. settlementNameLabel.text = settlement.name;
  153. Debug.Log($"SettlementInteractionUI: Set settlement name to: '{settlement.name}'");
  154. }
  155. else
  156. {
  157. Debug.LogWarning("SettlementInteractionUI: settlementNameLabel is null");
  158. }
  159. // Update interaction instruction
  160. if (instructionLabel != null)
  161. {
  162. string settlementType = settlement.Type == SettlementType.Town ? "Town" : "Village";
  163. string keyName = interactionManager?.enterSettlementKey.ToString() ?? "E";
  164. instructionLabel.text = $"Press {keyName} to enter {settlementType}";
  165. Debug.Log($"SettlementInteractionUI: Set instruction to: '{instructionLabel.text}'");
  166. }
  167. else
  168. {
  169. Debug.LogWarning("SettlementInteractionUI: instructionLabel is null");
  170. }
  171. // Show the prompt
  172. promptContainer.style.display = DisplayStyle.Flex;
  173. Debug.Log($"SettlementInteractionUI: Showing prompt for: {settlement.name}");
  174. }
  175. /// <summary>
  176. /// Hide settlement interaction prompt
  177. /// </summary>
  178. private void HideSettlementPrompt()
  179. {
  180. if (promptContainer != null)
  181. {
  182. promptContainer.style.display = DisplayStyle.None;
  183. }
  184. Debug.Log("Hiding settlement prompt");
  185. }
  186. /// <summary>
  187. /// Handle settlement entry (cleanup if needed)
  188. /// </summary>
  189. private void OnSettlementEntered(Settlement settlement)
  190. {
  191. Debug.Log($"Entering settlement: {settlement.name}");
  192. // UI will be destroyed when scene changes, no cleanup needed
  193. }
  194. void OnDestroy()
  195. {
  196. // Cleanup event listeners
  197. if (interactionManager != null && eventsSubscribed)
  198. {
  199. interactionManager.OnSettlementDetected -= ShowSettlementPrompt;
  200. interactionManager.OnSettlementLeft -= HideSettlementPrompt;
  201. interactionManager.OnSettlementEntered -= OnSettlementEntered;
  202. eventsSubscribed = false;
  203. }
  204. }
  205. /// <summary>
  206. /// Force UI to show (for testing or manual control)
  207. /// </summary>
  208. public void ForceShowUI()
  209. {
  210. if (promptContainer != null)
  211. {
  212. promptContainer.style.display = DisplayStyle.Flex;
  213. // Update with current nearby settlement if available
  214. if (interactionManager != null)
  215. {
  216. var currentSettlement = interactionManager.GetCurrentNearbySettlement();
  217. if (currentSettlement != null)
  218. {
  219. ShowSettlementPrompt(currentSettlement);
  220. return;
  221. }
  222. }
  223. // Fallback: show with generic text
  224. if (settlementNameLabel != null)
  225. {
  226. settlementNameLabel.text = "Nearby Settlement";
  227. }
  228. if (instructionLabel != null)
  229. {
  230. instructionLabel.text = "Press E to enter";
  231. }
  232. Debug.Log("[SettlementUI] Force showing UI");
  233. }
  234. }
  235. /// <summary>
  236. /// Force UI to hide (for testing or manual control)
  237. /// </summary>
  238. public void ForceHideUI()
  239. {
  240. if (promptContainer != null)
  241. {
  242. promptContainer.style.display = DisplayStyle.None;
  243. Debug.Log("[SettlementUI] Force hiding UI");
  244. }
  245. }
  246. /// <summary>
  247. /// Check if UI is currently visible
  248. /// </summary>
  249. public bool IsUIVisible()
  250. {
  251. return promptContainer != null && promptContainer.style.display == DisplayStyle.Flex;
  252. }
  253. }