using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; public class UIController : MonoBehaviour { [Header("UI Panels")] [SerializeField] private GameObject buildingPanel; [SerializeField] private GameObject gameInfoPanel; [SerializeField] private GameObject roomSelectionPanel; [Header("Game Info Display")] [SerializeField] private Text speedText; [SerializeField] private Text gameStateText; [SerializeField] private Text instructionsText; [Header("Building Controls")] [SerializeField] private Button buildRoomButton; [SerializeField] private Button cancelBuildingButton; [SerializeField] private Dropdown roomTypeDropdown; [Header("Hotel Management")] [SerializeField] private Button openHotelButton; [SerializeField] private Text hotelRequirementsText; [Header("Error/Warning Display")] [SerializeField] private GameObject errorPanel; [SerializeField] private Text errorText; [SerializeField] private Button errorCloseButton; private RoomBuilder roomBuilder; private bool isUISetup = false; private void Start() { SetupUI(); SubscribeToEvents(); } private void OnDestroy() { UnsubscribeFromEvents(); } private void SetupUI() { // Find RoomBuilder in scene roomBuilder = FindObjectOfType(); // Setup button events if (buildRoomButton != null) buildRoomButton.onClick.AddListener(StartBuildingRoom); if (cancelBuildingButton != null) { cancelBuildingButton.onClick.AddListener(CancelBuilding); cancelBuildingButton.gameObject.SetActive(false); } if (openHotelButton != null) { openHotelButton.onClick.AddListener(TryOpenHotel); openHotelButton.interactable = false; } if (errorCloseButton != null) errorCloseButton.onClick.AddListener(CloseErrorPanel); if (errorPanel != null) errorPanel.SetActive(false); // Setup room type dropdown SetupRoomTypeDropdown(); isUISetup = true; UpdateUI(); } private void SetupRoomTypeDropdown() { if (roomTypeDropdown != null) { roomTypeDropdown.ClearOptions(); List roomTypes = new List(); foreach (RoomType roomType in System.Enum.GetValues(typeof(RoomType))) { roomTypes.Add(roomType.ToString()); } roomTypeDropdown.AddOptions(roomTypes); } } private void SubscribeToEvents() { if (GameManager.Instance != null) { GameManager.OnSpeedChanged += UpdateSpeedDisplay; GameManager.OnGameStateChanged += UpdateGameStateDisplay; } if (roomBuilder != null) { roomBuilder.OnRoomCompleted += OnRoomCompleted; roomBuilder.OnBuildingError += ShowError; } } private void UnsubscribeFromEvents() { if (GameManager.Instance != null) { GameManager.OnSpeedChanged -= UpdateSpeedDisplay; GameManager.OnGameStateChanged -= UpdateGameStateDisplay; } if (roomBuilder != null) { roomBuilder.OnRoomCompleted -= OnRoomCompleted; roomBuilder.OnBuildingError -= ShowError; } } private void Update() { if (isUISetup) { UpdateUI(); } } #region UI Updates private void UpdateUI() { UpdateInstructionsText(); UpdateHotelRequirements(); UpdateBuildingControls(); } private void UpdateInstructionsText() { if (instructionsText == null) return; if (roomBuilder != null && roomBuilder.IsBuilding()) { instructionsText.text = "Building Mode:\n" + "• Left click to place wall points\n" + "• Right click to complete room (if possible)\n" + "• Get close to start point to auto-complete\n" + "• After walls, click on a wall to place door\n" + "• ESC to cancel"; } else if (GameManager.Instance != null) { switch (GameManager.Instance.currentGameState) { case GameManager.GameState.Building: instructionsText.text = "Building Phase:\n" + "• Build rooms for your hotel\n" + "• Add entrance and reception to open\n" + "• Keys 1-4 control game speed\n" + "• Start building with the button below"; break; case GameManager.GameState.PreOpening: instructionsText.text = "Ready to Open:\n" + "• All requirements met!\n" + "• Click 'Open Hotel' to start operations\n" + "• Keys 1-4 control game speed"; break; case GameManager.GameState.Operating: instructionsText.text = "Hotel Operating:\n" + "• Guests are now arriving\n" + "• Manage your hotel operations\n" + "• Keys 1-4 control game speed"; break; } } } private void UpdateHotelRequirements() { if (hotelRequirementsText == null || GameManager.Instance == null) return; string requirements = "Hotel Requirements:\n"; requirements += $"• Entrance: {(GameManager.Instance.hasEntrance ? "✓" : "✗")}\n"; requirements += $"• Reception: {(GameManager.Instance.hasReception ? "✓" : "✗")}\n"; if (GameManager.Instance.canOpenHotel) { requirements += "\nReady to open!"; } hotelRequirementsText.text = requirements; // Update open hotel button if (openHotelButton != null) { openHotelButton.interactable = GameManager.Instance.canOpenHotel && GameManager.Instance.currentGameState == GameManager.GameState.PreOpening; } } private void UpdateBuildingControls() { if (buildRoomButton == null || cancelBuildingButton == null) return; bool isBuilding = roomBuilder != null && roomBuilder.IsBuilding(); buildRoomButton.gameObject.SetActive(!isBuilding); cancelBuildingButton.gameObject.SetActive(isBuilding); // Disable building if hotel is operating if (GameManager.Instance != null && GameManager.Instance.IsHotelOperating()) { buildRoomButton.interactable = false; } } private void UpdateSpeedDisplay(GameManager.GameSpeed speed) { if (speedText != null) { speedText.text = $"Speed: {speed} ({Time.timeScale}x)"; } } private void UpdateGameStateDisplay(GameManager.GameState state) { if (gameStateText != null) { gameStateText.text = $"State: {state}"; } } #endregion #region Button Events private void StartBuildingRoom() { Debug.Log("Starting room building mode"); // RoomBuilder will handle the building logic through mouse input } private void CancelBuilding() { if (roomBuilder != null) { // RoomBuilder handles cancellation through ESC key, // but we can trigger it programmatically too Debug.Log("Cancelling building mode via UI"); } } private void TryOpenHotel() { if (GameManager.Instance != null) { GameManager.Instance.OpenHotel(); } } private void OnRoomCompleted(Room room) { Debug.Log($"Room completed: {room.roomType}"); // Check if this room satisfies hotel requirements RoomType selectedType = (RoomType)roomTypeDropdown.value; room.roomType = selectedType; switch (selectedType) { case RoomType.Reception: if (GameManager.Instance != null) GameManager.Instance.SetReception(true); break; case RoomType.Lobby: if (GameManager.Instance != null) GameManager.Instance.SetEntrance(true); break; } ShowMessage($"Room completed: {selectedType}"); } private void ShowError(string errorMessage) { if (errorPanel != null && errorText != null) { errorText.text = errorMessage; errorPanel.SetActive(true); Debug.LogWarning($"Building Error: {errorMessage}"); } } private void ShowMessage(string message) { Debug.Log(message); // You can create a message panel similar to error panel for positive feedback } private void CloseErrorPanel() { if (errorPanel != null) { errorPanel.SetActive(false); } } #endregion }