UIController.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections.Generic;
  4. public class UIController : MonoBehaviour
  5. {
  6. [Header("UI Panels")]
  7. [SerializeField] private GameObject buildingPanel;
  8. [SerializeField] private GameObject gameInfoPanel;
  9. [SerializeField] private GameObject roomSelectionPanel;
  10. [Header("Game Info Display")]
  11. [SerializeField] private Text speedText;
  12. [SerializeField] private Text gameStateText;
  13. [SerializeField] private Text instructionsText;
  14. [Header("Building Controls")]
  15. [SerializeField] private Button buildRoomButton;
  16. [SerializeField] private Button cancelBuildingButton;
  17. [SerializeField] private Dropdown roomTypeDropdown;
  18. [Header("Hotel Management")]
  19. [SerializeField] private Button openHotelButton;
  20. [SerializeField] private Text hotelRequirementsText;
  21. [Header("Error/Warning Display")]
  22. [SerializeField] private GameObject errorPanel;
  23. [SerializeField] private Text errorText;
  24. [SerializeField] private Button errorCloseButton;
  25. private RoomBuilder roomBuilder;
  26. private bool isUISetup = false;
  27. private void Start()
  28. {
  29. SetupUI();
  30. SubscribeToEvents();
  31. }
  32. private void OnDestroy()
  33. {
  34. UnsubscribeFromEvents();
  35. }
  36. private void SetupUI()
  37. {
  38. // Find RoomBuilder in scene
  39. roomBuilder = FindObjectOfType<RoomBuilder>();
  40. // Setup button events
  41. if (buildRoomButton != null)
  42. buildRoomButton.onClick.AddListener(StartBuildingRoom);
  43. if (cancelBuildingButton != null)
  44. {
  45. cancelBuildingButton.onClick.AddListener(CancelBuilding);
  46. cancelBuildingButton.gameObject.SetActive(false);
  47. }
  48. if (openHotelButton != null)
  49. {
  50. openHotelButton.onClick.AddListener(TryOpenHotel);
  51. openHotelButton.interactable = false;
  52. }
  53. if (errorCloseButton != null)
  54. errorCloseButton.onClick.AddListener(CloseErrorPanel);
  55. if (errorPanel != null)
  56. errorPanel.SetActive(false);
  57. // Setup room type dropdown
  58. SetupRoomTypeDropdown();
  59. isUISetup = true;
  60. UpdateUI();
  61. }
  62. private void SetupRoomTypeDropdown()
  63. {
  64. if (roomTypeDropdown != null)
  65. {
  66. roomTypeDropdown.ClearOptions();
  67. List<string> roomTypes = new List<string>();
  68. foreach (RoomType roomType in System.Enum.GetValues(typeof(RoomType)))
  69. {
  70. roomTypes.Add(roomType.ToString());
  71. }
  72. roomTypeDropdown.AddOptions(roomTypes);
  73. }
  74. }
  75. private void SubscribeToEvents()
  76. {
  77. if (GameManager.Instance != null)
  78. {
  79. GameManager.OnSpeedChanged += UpdateSpeedDisplay;
  80. GameManager.OnGameStateChanged += UpdateGameStateDisplay;
  81. }
  82. if (roomBuilder != null)
  83. {
  84. roomBuilder.OnRoomCompleted += OnRoomCompleted;
  85. roomBuilder.OnBuildingError += ShowError;
  86. }
  87. }
  88. private void UnsubscribeFromEvents()
  89. {
  90. if (GameManager.Instance != null)
  91. {
  92. GameManager.OnSpeedChanged -= UpdateSpeedDisplay;
  93. GameManager.OnGameStateChanged -= UpdateGameStateDisplay;
  94. }
  95. if (roomBuilder != null)
  96. {
  97. roomBuilder.OnRoomCompleted -= OnRoomCompleted;
  98. roomBuilder.OnBuildingError -= ShowError;
  99. }
  100. }
  101. private void Update()
  102. {
  103. if (isUISetup)
  104. {
  105. UpdateUI();
  106. }
  107. }
  108. #region UI Updates
  109. private void UpdateUI()
  110. {
  111. UpdateInstructionsText();
  112. UpdateHotelRequirements();
  113. UpdateBuildingControls();
  114. }
  115. private void UpdateInstructionsText()
  116. {
  117. if (instructionsText == null) return;
  118. if (roomBuilder != null && roomBuilder.IsBuilding())
  119. {
  120. instructionsText.text = "Building Mode:\n" +
  121. "• Left click to place wall points\n" +
  122. "• Right click to complete room (if possible)\n" +
  123. "• Get close to start point to auto-complete\n" +
  124. "• After walls, click on a wall to place door\n" +
  125. "• ESC to cancel";
  126. }
  127. else if (GameManager.Instance != null)
  128. {
  129. switch (GameManager.Instance.currentGameState)
  130. {
  131. case GameManager.GameState.Building:
  132. instructionsText.text = "Building Phase:\n" +
  133. "• Build rooms for your hotel\n" +
  134. "• Add entrance and reception to open\n" +
  135. "• Keys 1-4 control game speed\n" +
  136. "• Start building with the button below";
  137. break;
  138. case GameManager.GameState.PreOpening:
  139. instructionsText.text = "Ready to Open:\n" +
  140. "• All requirements met!\n" +
  141. "• Click 'Open Hotel' to start operations\n" +
  142. "• Keys 1-4 control game speed";
  143. break;
  144. case GameManager.GameState.Operating:
  145. instructionsText.text = "Hotel Operating:\n" +
  146. "• Guests are now arriving\n" +
  147. "• Manage your hotel operations\n" +
  148. "• Keys 1-4 control game speed";
  149. break;
  150. }
  151. }
  152. }
  153. private void UpdateHotelRequirements()
  154. {
  155. if (hotelRequirementsText == null || GameManager.Instance == null) return;
  156. string requirements = "Hotel Requirements:\n";
  157. requirements += $"• Entrance: {(GameManager.Instance.hasEntrance ? "✓" : "✗")}\n";
  158. requirements += $"• Reception: {(GameManager.Instance.hasReception ? "✓" : "✗")}\n";
  159. if (GameManager.Instance.canOpenHotel)
  160. {
  161. requirements += "\n<color=green>Ready to open!</color>";
  162. }
  163. hotelRequirementsText.text = requirements;
  164. // Update open hotel button
  165. if (openHotelButton != null)
  166. {
  167. openHotelButton.interactable = GameManager.Instance.canOpenHotel &&
  168. GameManager.Instance.currentGameState == GameManager.GameState.PreOpening;
  169. }
  170. }
  171. private void UpdateBuildingControls()
  172. {
  173. if (buildRoomButton == null || cancelBuildingButton == null) return;
  174. bool isBuilding = roomBuilder != null && roomBuilder.IsBuilding();
  175. buildRoomButton.gameObject.SetActive(!isBuilding);
  176. cancelBuildingButton.gameObject.SetActive(isBuilding);
  177. // Disable building if hotel is operating
  178. if (GameManager.Instance != null && GameManager.Instance.IsHotelOperating())
  179. {
  180. buildRoomButton.interactable = false;
  181. }
  182. }
  183. private void UpdateSpeedDisplay(GameManager.GameSpeed speed)
  184. {
  185. if (speedText != null)
  186. {
  187. speedText.text = $"Speed: {speed} ({Time.timeScale}x)";
  188. }
  189. }
  190. private void UpdateGameStateDisplay(GameManager.GameState state)
  191. {
  192. if (gameStateText != null)
  193. {
  194. gameStateText.text = $"State: {state}";
  195. }
  196. }
  197. #endregion
  198. #region Button Events
  199. private void StartBuildingRoom()
  200. {
  201. Debug.Log("Starting room building mode");
  202. // RoomBuilder will handle the building logic through mouse input
  203. }
  204. private void CancelBuilding()
  205. {
  206. if (roomBuilder != null)
  207. {
  208. // RoomBuilder handles cancellation through ESC key,
  209. // but we can trigger it programmatically too
  210. Debug.Log("Cancelling building mode via UI");
  211. }
  212. }
  213. private void TryOpenHotel()
  214. {
  215. if (GameManager.Instance != null)
  216. {
  217. GameManager.Instance.OpenHotel();
  218. }
  219. }
  220. private void OnRoomCompleted(Room room)
  221. {
  222. Debug.Log($"Room completed: {room.roomType}");
  223. // Check if this room satisfies hotel requirements
  224. RoomType selectedType = (RoomType)roomTypeDropdown.value;
  225. room.roomType = selectedType;
  226. switch (selectedType)
  227. {
  228. case RoomType.Reception:
  229. if (GameManager.Instance != null)
  230. GameManager.Instance.SetReception(true);
  231. break;
  232. case RoomType.Lobby:
  233. if (GameManager.Instance != null)
  234. GameManager.Instance.SetEntrance(true);
  235. break;
  236. }
  237. ShowMessage($"Room completed: {selectedType}");
  238. }
  239. private void ShowError(string errorMessage)
  240. {
  241. if (errorPanel != null && errorText != null)
  242. {
  243. errorText.text = errorMessage;
  244. errorPanel.SetActive(true);
  245. Debug.LogWarning($"Building Error: {errorMessage}");
  246. }
  247. }
  248. private void ShowMessage(string message)
  249. {
  250. Debug.Log(message);
  251. // You can create a message panel similar to error panel for positive feedback
  252. }
  253. private void CloseErrorPanel()
  254. {
  255. if (errorPanel != null)
  256. {
  257. errorPanel.SetActive(false);
  258. }
  259. }
  260. #endregion
  261. }