| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- using UnityEngine;
- using System;
- public class GameManager : MonoBehaviour
- {
- #region Singleton
- public static GameManager Instance { get; private set; }
- private void Awake()
- {
- if (Instance != null && Instance != this)
- {
- Destroy(this.gameObject);
- return;
- }
- Instance = this;
- DontDestroyOnLoad(this.gameObject);
- }
- #endregion
- [Header("Game Speed Settings")]
- [SerializeField] private float normalSpeed = 1f;
- [SerializeField] private float fastSpeed = 2f;
- [SerializeField] private float ultraSpeed = 4f;
- [Header("Game State")]
- public GameState currentGameState = GameState.Building;
- [Header("Hotel Requirements")]
- public bool hasEntrance = false;
- public bool hasReception = false;
- public bool canOpenHotel = false;
- [Header("UI References")]
- [SerializeField] private UnityEngine.UI.Text speedText;
- [SerializeField] private UnityEngine.UI.Text gameStateText;
- public enum GameSpeed
- {
- Paused = 0,
- Normal = 1,
- Fast = 2,
- Ultra = 3
- }
- public enum GameState
- {
- Building,
- PreOpening,
- Operating,
- Paused
- }
- private GameSpeed currentSpeed = GameSpeed.Normal;
- // Events
- public static event Action<GameSpeed> OnSpeedChanged;
- public static event Action<GameState> OnGameStateChanged;
- public static event Action OnHotelOpened;
- void Start()
- {
- SetGameSpeed(GameSpeed.Normal);
- UpdateGameState();
- }
- void Update()
- {
- HandleSpeedInput();
- CheckHotelRequirements();
- UpdateUI();
- }
- #region Speed Control
- private void HandleSpeedInput()
- {
- if (Input.GetKeyDown(KeyCode.Alpha1))
- {
- SetGameSpeed(GameSpeed.Paused);
- }
- else if (Input.GetKeyDown(KeyCode.Alpha2))
- {
- SetGameSpeed(GameSpeed.Normal);
- }
- else if (Input.GetKeyDown(KeyCode.Alpha3))
- {
- SetGameSpeed(GameSpeed.Fast);
- }
- else if (Input.GetKeyDown(KeyCode.Alpha4))
- {
- SetGameSpeed(GameSpeed.Ultra);
- }
- }
- public void SetGameSpeed(GameSpeed speed)
- {
- currentSpeed = speed;
- switch (speed)
- {
- case GameSpeed.Paused:
- Time.timeScale = 0f;
- break;
- case GameSpeed.Normal:
- Time.timeScale = normalSpeed;
- break;
- case GameSpeed.Fast:
- Time.timeScale = fastSpeed;
- break;
- case GameSpeed.Ultra:
- Time.timeScale = ultraSpeed;
- break;
- }
- OnSpeedChanged?.Invoke(speed);
- Debug.Log($"Game speed changed to: {speed} (TimeScale: {Time.timeScale})");
- }
- public GameSpeed GetCurrentSpeed()
- {
- return currentSpeed;
- }
- #endregion
- #region Game State Management
- private void CheckHotelRequirements()
- {
- bool previousCanOpen = canOpenHotel;
- canOpenHotel = hasEntrance && hasReception;
- if (canOpenHotel && !previousCanOpen && currentGameState == GameState.Building)
- {
- currentGameState = GameState.PreOpening;
- OnGameStateChanged?.Invoke(currentGameState);
- }
- }
- public void OpenHotel()
- {
- if (canOpenHotel)
- {
- currentGameState = GameState.Operating;
- OnGameStateChanged?.Invoke(currentGameState);
- OnHotelOpened?.Invoke();
- Debug.Log("Hotel is now open for business!");
- }
- else
- {
- Debug.LogWarning("Cannot open hotel: Missing requirements (Entrance: " + hasEntrance + ", Reception: " + hasReception + ")");
- }
- }
- public void SetEntrance(bool value)
- {
- hasEntrance = value;
- Debug.Log("Hotel entrance: " + (value ? "Added" : "Removed"));
- }
- public void SetReception(bool value)
- {
- hasReception = value;
- Debug.Log("Hotel reception: " + (value ? "Added" : "Removed"));
- }
- private void UpdateGameState()
- {
- OnGameStateChanged?.Invoke(currentGameState);
- }
- #endregion
- #region UI Updates
- private void UpdateUI()
- {
- if (speedText != null)
- {
- speedText.text = $"Speed: {currentSpeed} ({Time.timeScale}x)";
- }
- if (gameStateText != null)
- {
- string stateInfo = $"State: {currentGameState}";
- if (currentGameState == GameState.Building || currentGameState == GameState.PreOpening)
- {
- stateInfo += $"\nEntrance: {(hasEntrance ? "✓" : "✗")}";
- stateInfo += $"\nReception: {(hasReception ? "✓" : "✗")}";
- if (canOpenHotel)
- {
- stateInfo += "\n[Ready to Open!]";
- }
- }
- gameStateText.text = stateInfo;
- }
- }
- #endregion
- #region Utility Methods
- public bool IsGamePaused()
- {
- return currentSpeed == GameSpeed.Paused || currentGameState == GameState.Paused;
- }
- public bool IsHotelOperating()
- {
- return currentGameState == GameState.Operating;
- }
- public bool IsBuilding()
- {
- return currentGameState == GameState.Building;
- }
- #endregion
- }
|