GameManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using UnityEngine;
  2. using System;
  3. public class GameManager : MonoBehaviour
  4. {
  5. #region Singleton
  6. public static GameManager Instance { get; private set; }
  7. private void Awake()
  8. {
  9. if (Instance != null && Instance != this)
  10. {
  11. Destroy(this.gameObject);
  12. return;
  13. }
  14. Instance = this;
  15. DontDestroyOnLoad(this.gameObject);
  16. }
  17. #endregion
  18. [Header("Game Speed Settings")]
  19. [SerializeField] private float normalSpeed = 1f;
  20. [SerializeField] private float fastSpeed = 2f;
  21. [SerializeField] private float ultraSpeed = 4f;
  22. [Header("Game State")]
  23. public GameState currentGameState = GameState.Building;
  24. [Header("Hotel Requirements")]
  25. public bool hasEntrance = false;
  26. public bool hasReception = false;
  27. public bool canOpenHotel = false;
  28. [Header("UI References")]
  29. [SerializeField] private UnityEngine.UI.Text speedText;
  30. [SerializeField] private UnityEngine.UI.Text gameStateText;
  31. public enum GameSpeed
  32. {
  33. Paused = 0,
  34. Normal = 1,
  35. Fast = 2,
  36. Ultra = 3
  37. }
  38. public enum GameState
  39. {
  40. Building,
  41. PreOpening,
  42. Operating,
  43. Paused
  44. }
  45. private GameSpeed currentSpeed = GameSpeed.Normal;
  46. // Events
  47. public static event Action<GameSpeed> OnSpeedChanged;
  48. public static event Action<GameState> OnGameStateChanged;
  49. public static event Action OnHotelOpened;
  50. void Start()
  51. {
  52. SetGameSpeed(GameSpeed.Normal);
  53. UpdateGameState();
  54. }
  55. void Update()
  56. {
  57. HandleSpeedInput();
  58. CheckHotelRequirements();
  59. UpdateUI();
  60. }
  61. #region Speed Control
  62. private void HandleSpeedInput()
  63. {
  64. if (Input.GetKeyDown(KeyCode.Alpha1))
  65. {
  66. SetGameSpeed(GameSpeed.Paused);
  67. }
  68. else if (Input.GetKeyDown(KeyCode.Alpha2))
  69. {
  70. SetGameSpeed(GameSpeed.Normal);
  71. }
  72. else if (Input.GetKeyDown(KeyCode.Alpha3))
  73. {
  74. SetGameSpeed(GameSpeed.Fast);
  75. }
  76. else if (Input.GetKeyDown(KeyCode.Alpha4))
  77. {
  78. SetGameSpeed(GameSpeed.Ultra);
  79. }
  80. }
  81. public void SetGameSpeed(GameSpeed speed)
  82. {
  83. currentSpeed = speed;
  84. switch (speed)
  85. {
  86. case GameSpeed.Paused:
  87. Time.timeScale = 0f;
  88. break;
  89. case GameSpeed.Normal:
  90. Time.timeScale = normalSpeed;
  91. break;
  92. case GameSpeed.Fast:
  93. Time.timeScale = fastSpeed;
  94. break;
  95. case GameSpeed.Ultra:
  96. Time.timeScale = ultraSpeed;
  97. break;
  98. }
  99. OnSpeedChanged?.Invoke(speed);
  100. Debug.Log($"Game speed changed to: {speed} (TimeScale: {Time.timeScale})");
  101. }
  102. public GameSpeed GetCurrentSpeed()
  103. {
  104. return currentSpeed;
  105. }
  106. #endregion
  107. #region Game State Management
  108. private void CheckHotelRequirements()
  109. {
  110. bool previousCanOpen = canOpenHotel;
  111. canOpenHotel = hasEntrance && hasReception;
  112. if (canOpenHotel && !previousCanOpen && currentGameState == GameState.Building)
  113. {
  114. currentGameState = GameState.PreOpening;
  115. OnGameStateChanged?.Invoke(currentGameState);
  116. }
  117. }
  118. public void OpenHotel()
  119. {
  120. if (canOpenHotel)
  121. {
  122. currentGameState = GameState.Operating;
  123. OnGameStateChanged?.Invoke(currentGameState);
  124. OnHotelOpened?.Invoke();
  125. Debug.Log("Hotel is now open for business!");
  126. }
  127. else
  128. {
  129. Debug.LogWarning("Cannot open hotel: Missing requirements (Entrance: " + hasEntrance + ", Reception: " + hasReception + ")");
  130. }
  131. }
  132. public void SetEntrance(bool value)
  133. {
  134. hasEntrance = value;
  135. Debug.Log("Hotel entrance: " + (value ? "Added" : "Removed"));
  136. }
  137. public void SetReception(bool value)
  138. {
  139. hasReception = value;
  140. Debug.Log("Hotel reception: " + (value ? "Added" : "Removed"));
  141. }
  142. private void UpdateGameState()
  143. {
  144. OnGameStateChanged?.Invoke(currentGameState);
  145. }
  146. #endregion
  147. #region UI Updates
  148. private void UpdateUI()
  149. {
  150. if (speedText != null)
  151. {
  152. speedText.text = $"Speed: {currentSpeed} ({Time.timeScale}x)";
  153. }
  154. if (gameStateText != null)
  155. {
  156. string stateInfo = $"State: {currentGameState}";
  157. if (currentGameState == GameState.Building || currentGameState == GameState.PreOpening)
  158. {
  159. stateInfo += $"\nEntrance: {(hasEntrance ? "✓" : "✗")}";
  160. stateInfo += $"\nReception: {(hasReception ? "✓" : "✗")}";
  161. if (canOpenHotel)
  162. {
  163. stateInfo += "\n[Ready to Open!]";
  164. }
  165. }
  166. gameStateText.text = stateInfo;
  167. }
  168. }
  169. #endregion
  170. #region Utility Methods
  171. public bool IsGamePaused()
  172. {
  173. return currentSpeed == GameSpeed.Paused || currentGameState == GameState.Paused;
  174. }
  175. public bool IsHotelOperating()
  176. {
  177. return currentGameState == GameState.Operating;
  178. }
  179. public bool IsBuilding()
  180. {
  181. return currentGameState == GameState.Building;
  182. }
  183. #endregion
  184. }