| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- 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<RoomBuilder>();
- // 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<string> roomTypes = new List<string>();
- 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 += "\n<color=green>Ready to open!</color>";
- }
- 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
- }
|