HotelTycoonBootstrap.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using Unity.AI.Navigation;
  2. using UnityEngine;
  3. using UnityEngine.UIElements;
  4. using UnityEngine.UI;
  5. public class HotelTycoonBootstrap : MonoBehaviour
  6. {
  7. [Header("Scene Setup")]
  8. [SerializeField] private bool autoSetupScene = true;
  9. [SerializeField] private bool createMainCamera = true;
  10. [SerializeField] private bool createUI = true;
  11. [SerializeField] private bool createNavMesh = true;
  12. [Header("Game Objects")]
  13. [SerializeField] private GameObject gameManagerPrefab;
  14. [SerializeField] private GameObject hotelManagerPrefab;
  15. [SerializeField] private GameObject staffManagerPrefab;
  16. [SerializeField] private GameObject facilityManagerPrefab;
  17. [SerializeField] private GameObject ratingSystemPrefab;
  18. private void Awake()
  19. {
  20. if (autoSetupScene)
  21. {
  22. SetupScene();
  23. }
  24. }
  25. private void SetupScene()
  26. {
  27. Debug.Log("Setting up Hotel Tycoon scene...");
  28. CreateEssentialManagers();
  29. if (createMainCamera)
  30. SetupMainCamera();
  31. if (createUI)
  32. SetupBasicUI();
  33. if (createNavMesh)
  34. SetupNavMeshSurfaces();
  35. CreateInitialRoomSetup();
  36. CreateRoomBuilder();
  37. Debug.Log("Hotel Tycoon scene setup complete!");
  38. }
  39. #region Manager Creation
  40. private void CreateEssentialManagers()
  41. {
  42. // Create GameManager
  43. if (GameManager.Instance == null)
  44. {
  45. CreateManager("GameManager", gameManagerPrefab, typeof(GameManager));
  46. }
  47. // Create HotelManager
  48. if (HotelManager.Instance == null)
  49. {
  50. CreateManager("HotelManager", hotelManagerPrefab, typeof(HotelManager));
  51. }
  52. // Create StaffManager
  53. if (StaffManager.Instance == null)
  54. {
  55. CreateManager("StaffManager", staffManagerPrefab, typeof(StaffManager));
  56. }
  57. // Create FacilityManager
  58. if (FacilityManager.Instance == null)
  59. {
  60. CreateManager("FacilityManager", facilityManagerPrefab, typeof(FacilityManager));
  61. }
  62. // Create Rating System
  63. if (HotelRatingSystem.Instance == null)
  64. {
  65. CreateManager("HotelRatingSystem", ratingSystemPrefab, typeof(HotelRatingSystem));
  66. }
  67. }
  68. private void CreateManager(string name, GameObject prefab, System.Type componentType)
  69. {
  70. GameObject manager;
  71. if (prefab != null)
  72. {
  73. manager = Instantiate(prefab);
  74. manager.name = name;
  75. }
  76. else
  77. {
  78. manager = new GameObject(name);
  79. manager.AddComponent(componentType);
  80. }
  81. DontDestroyOnLoad(manager);
  82. Debug.Log($"Created {name}");
  83. }
  84. #endregion
  85. #region Camera Setup
  86. private void SetupMainCamera()
  87. {
  88. Camera existingCamera = Camera.main;
  89. if (existingCamera != null)
  90. {
  91. // Add camera controller to existing camera
  92. if (existingCamera.GetComponent<CameraController>() == null)
  93. {
  94. existingCamera.gameObject.AddComponent<CameraController>();
  95. }
  96. }
  97. else
  98. {
  99. // Create new camera
  100. GameObject cameraObj = new GameObject("Main Camera");
  101. Camera camera = cameraObj.AddComponent<Camera>();
  102. cameraObj.AddComponent<AudioListener>();
  103. cameraObj.AddComponent<CameraController>();
  104. cameraObj.tag = "MainCamera";
  105. // Position camera for good hotel view
  106. cameraObj.transform.position = new Vector3(0, 15f, -20f);
  107. cameraObj.transform.rotation = Quaternion.Euler(45f, 0f, 0f);
  108. }
  109. Debug.Log("Main camera setup complete");
  110. }
  111. #endregion
  112. #region UI Setup
  113. private void SetupBasicUI()
  114. {
  115. // Create Canvas if it doesn't exist
  116. Canvas existingCanvas = FindObjectOfType<Canvas>();
  117. if (existingCanvas == null)
  118. {
  119. GameObject canvasObj = new GameObject("Canvas");
  120. Canvas canvas = canvasObj.AddComponent<Canvas>();
  121. canvas.renderMode = RenderMode.ScreenSpaceOverlay;
  122. canvasObj.AddComponent<CanvasScaler>();
  123. canvasObj.AddComponent<UnityEngine.UI.GraphicRaycaster>();
  124. // Create Event System if needed
  125. if (FindObjectOfType<UnityEngine.EventSystems.EventSystem>() == null)
  126. {
  127. GameObject eventSystemObj = new GameObject("EventSystem");
  128. eventSystemObj.AddComponent<UnityEngine.EventSystems.EventSystem>();
  129. eventSystemObj.AddComponent<UnityEngine.EventSystems.StandaloneInputModule>();
  130. }
  131. }
  132. // Create UI Controllers
  133. CreateUIController();
  134. CreateGuestInfoUI();
  135. Debug.Log("Basic UI setup complete");
  136. }
  137. private void CreateUIController()
  138. {
  139. if (FindObjectOfType<UIController>() == null)
  140. {
  141. GameObject uiControllerObj = new GameObject("UI Controller");
  142. uiControllerObj.AddComponent<UIController>();
  143. }
  144. }
  145. private void CreateGuestInfoUI()
  146. {
  147. if (FindObjectOfType<GuestInfoUI>() == null)
  148. {
  149. GameObject guestInfoObj = new GameObject("Guest Info UI");
  150. guestInfoObj.AddComponent<GuestInfoUI>();
  151. }
  152. }
  153. #endregion
  154. #region NavMesh Setup
  155. private void SetupNavMeshSurfaces()
  156. {
  157. // Create a ground plane for navigation
  158. GameObject ground = GameObject.Find("Ground");
  159. if (ground == null)
  160. {
  161. ground = GameObject.CreatePrimitive(PrimitiveType.Plane);
  162. ground.name = "Ground";
  163. // Only set tag if it exists, otherwise skip
  164. try
  165. {
  166. ground.tag = "Ground";
  167. }
  168. catch (UnityException)
  169. {
  170. Debug.LogWarning("Ground tag not defined in Tags and Layers. Create it in Project Settings > Tags and Layers if needed.");
  171. }
  172. ground.transform.position = Vector3.zero;
  173. ground.transform.localScale = new Vector3(20f, 1f, 20f);
  174. // Make it less visible but keep for navigation
  175. Renderer groundRenderer = ground.GetComponent<Renderer>();
  176. groundRenderer.material.color = new Color(0.8f, 0.8f, 0.8f, 0.5f);
  177. }
  178. // Note: For NavMesh to work properly, you need to:
  179. // 1. Install Unity NavMesh Components package (Window > Package Manager > Unity Registry > AI Navigation)
  180. // 2. Or manually bake NavMesh through Window > AI > Navigation
  181. Debug.Log("NavMesh setup: Go to Window > AI > Navigation and click 'Bake' to create NavMesh for AI agents.");
  182. Debug.Log("NavMesh surfaces setup complete - manual baking required");
  183. }
  184. #endregion
  185. #region Game Object Setup
  186. private void CreateInitialRoomSetup()
  187. {
  188. if (FindObjectOfType<InitialRoomSetup>() == null)
  189. {
  190. GameObject initialRoomObj = new GameObject("Initial Room Setup");
  191. initialRoomObj.AddComponent<InitialRoomSetup>();
  192. Debug.Log("Created Initial Room Setup");
  193. }
  194. }
  195. private void CreateRoomBuilder()
  196. {
  197. // Remove old RoomBuilder if it exists
  198. RoomBuilder oldBuilder = FindFirstObjectByType<RoomBuilder>();
  199. if (oldBuilder != null)
  200. {
  201. DestroyImmediate(oldBuilder.gameObject);
  202. }
  203. // Create new building system
  204. if (FindFirstObjectByType<NewRoomBuilder>() == null)
  205. {
  206. GameObject roomBuilderObj = new GameObject("New Room Builder");
  207. NewRoomBuilder newBuilder = roomBuilderObj.AddComponent<NewRoomBuilder>();
  208. // Create UI Controller for building interface
  209. GameObject uiObj = new GameObject("Building UI Controller");
  210. BuildingUIController uiController = uiObj.AddComponent<BuildingUIController>();
  211. // Add UIDocument component for the new UI
  212. UIDocument uiDocument = uiObj.AddComponent<UIDocument>();
  213. // Connect the UI to the builder
  214. uiController.roomBuilder = newBuilder;
  215. uiController.uiDocument = uiDocument;
  216. Debug.Log("Created New Room Builder with UI");
  217. }
  218. }
  219. #endregion
  220. #region Debug and Validation
  221. private void OnValidate()
  222. {
  223. // Validate that essential components are present
  224. ValidateScene();
  225. }
  226. private void ValidateScene()
  227. {
  228. if (autoSetupScene)
  229. {
  230. if (FindObjectOfType<GameManager>() == null && gameManagerPrefab == null)
  231. {
  232. Debug.LogWarning("No GameManager found and no prefab assigned!");
  233. }
  234. }
  235. }
  236. [ContextMenu("Setup Scene Manually")]
  237. public void SetupSceneManually()
  238. {
  239. SetupScene();
  240. }
  241. [ContextMenu("Validate Scene Setup")]
  242. public void ValidateSceneSetup()
  243. {
  244. Debug.Log("=== Hotel Tycoon Scene Validation ===");
  245. Debug.Log($"GameManager: {(GameManager.Instance != null ? "✓" : "✗")}");
  246. Debug.Log($"HotelManager: {(HotelManager.Instance != null ? "✓" : "✗")}");
  247. Debug.Log($"StaffManager: {(StaffManager.Instance != null ? "✓" : "✗")}");
  248. Debug.Log($"FacilityManager: {(FacilityManager.Instance != null ? "✓" : "✗")}");
  249. Debug.Log($"RatingSystem: {(HotelRatingSystem.Instance != null ? "✓" : "✗")}");
  250. Debug.Log($"Camera Controller: {(FindObjectOfType<CameraController>() != null ? "✓" : "✗")}");
  251. Debug.Log($"UI Controller: {(FindObjectOfType<UIController>() != null ? "✓" : "✗")}");
  252. Debug.Log($"Room Builder: {(FindObjectOfType<RoomBuilder>() != null ? "✓" : "✗")}");
  253. Debug.Log($"Initial Room Setup: {(FindObjectOfType<InitialRoomSetup>() != null ? "✓" : "✗")}");
  254. Debug.Log("=== Validation Complete ===");
  255. }
  256. #endregion
  257. }