SceneSetup.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. public class SceneSetup : MonoBehaviour
  4. {
  5. [Header("Scene Setup")]
  6. public bool setupSceneOnStart = true;
  7. public Vector3 townHallPosition = Vector3.zero;
  8. public int numberOfTrees = 5;
  9. public int numberOfStones = 3;
  10. public int numberOfFarms = 2;
  11. public float resourceSpawnRadius = 15f;
  12. [Header("Materials (Optional)")]
  13. public Material villagerMaterial;
  14. public Material townHallMaterial;
  15. public Material treeMaterial;
  16. public Material stoneMaterial;
  17. public Material farmMaterial;
  18. void Start()
  19. {
  20. if (setupSceneOnStart)
  21. {
  22. SetupScene();
  23. }
  24. }
  25. [ContextMenu("Setup Scene")]
  26. public void SetupScene()
  27. {
  28. CreateGround();
  29. CreateTownHall();
  30. CreateResourceNodes();
  31. SetupCamera();
  32. CreateGameManager();
  33. }
  34. void CreateGround()
  35. {
  36. GameObject ground = GameObject.Find("Ground");
  37. if (ground == null)
  38. {
  39. // Create ground plane
  40. ground = GameObject.CreatePrimitive(PrimitiveType.Plane);
  41. ground.name = "Ground";
  42. ground.transform.position = Vector3.zero;
  43. ground.transform.localScale = new Vector3(5f, 1f, 5f); // 50x50 units
  44. // Set material if available
  45. Renderer groundRenderer = ground.GetComponent<Renderer>();
  46. if (groundRenderer != null)
  47. {
  48. groundRenderer.material.color = new Color(0.3f, 0.5f, 0.3f); // Green ground
  49. }
  50. Debug.Log("Created ground plane");
  51. }
  52. }
  53. void CreateTownHall()
  54. {
  55. GameObject townHallGO = GameObject.FindWithTag("TownHall");
  56. if (townHallGO == null)
  57. {
  58. // Create TownHall
  59. townHallGO = GameObject.CreatePrimitive(PrimitiveType.Cube);
  60. townHallGO.name = "TownHall";
  61. townHallGO.tag = "TownHall";
  62. townHallGO.transform.position = townHallPosition;
  63. townHallGO.transform.localScale = new Vector3(3f, 2f, 3f);
  64. // Add TownHall component
  65. TownHall townHall = townHallGO.AddComponent<TownHall>();
  66. // Apply material if available
  67. if (townHallMaterial != null)
  68. {
  69. townHallGO.GetComponent<Renderer>().material = townHallMaterial;
  70. }
  71. else
  72. {
  73. townHallGO.GetComponent<Renderer>().material.color = Color.blue;
  74. }
  75. // Create selection indicator
  76. GameObject indicator = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
  77. indicator.transform.SetParent(townHallGO.transform);
  78. indicator.transform.localPosition = Vector3.down * 1.2f;
  79. indicator.transform.localScale = Vector3.one * 1.5f;
  80. indicator.GetComponent<Renderer>().material.color = Color.yellow;
  81. indicator.SetActive(false);
  82. townHall.selectionIndicator = indicator;
  83. Destroy(indicator.GetComponent<Collider>());
  84. Debug.Log("Created TownHall");
  85. }
  86. }
  87. void CreateResourceNodes()
  88. {
  89. // Create Trees
  90. for (int i = 0; i < numberOfTrees; i++)
  91. {
  92. Vector3 position = GetRandomPosition();
  93. CreateTree(position, i);
  94. }
  95. // Create Stones
  96. for (int i = 0; i < numberOfStones; i++)
  97. {
  98. Vector3 position = GetRandomPosition();
  99. CreateStone(position, i);
  100. }
  101. // Create Farms
  102. for (int i = 0; i < numberOfFarms; i++)
  103. {
  104. Vector3 position = GetRandomPosition();
  105. CreateFarm(position, i);
  106. }
  107. }
  108. void CreateTree(Vector3 position, int index)
  109. {
  110. GameObject tree = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
  111. tree.name = $"Tree_{index + 1}";
  112. tree.tag = "Tree";
  113. tree.transform.position = position;
  114. tree.transform.localScale = new Vector3(1f, 2f, 1f);
  115. // Add ResourceNode component
  116. ResourceNode node = tree.AddComponent<ResourceNode>();
  117. node.nodeType = ResourceNodeType.Tree;
  118. node.resourceType = ResourceType.Wood;
  119. node.maxResources = 50;
  120. node.currentResources = 50;
  121. node.regenerationRate = 2f;
  122. // Apply material
  123. if (treeMaterial != null)
  124. {
  125. tree.GetComponent<Renderer>().material = treeMaterial;
  126. }
  127. else
  128. {
  129. tree.GetComponent<Renderer>().material.color = Color.green;
  130. }
  131. // Create selection indicator
  132. CreateSelectionIndicator(tree, node);
  133. Debug.Log($"Created Tree at {position}");
  134. }
  135. void CreateStone(Vector3 position, int index)
  136. {
  137. GameObject stone = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  138. stone.name = $"Stone_{index + 1}";
  139. stone.tag = "Stone";
  140. stone.transform.position = position;
  141. stone.transform.localScale = Vector3.one * 1.5f;
  142. // Add ResourceNode component
  143. ResourceNode node = stone.AddComponent<ResourceNode>();
  144. node.nodeType = ResourceNodeType.Stone;
  145. node.resourceType = ResourceType.Stone;
  146. node.maxResources = 75;
  147. node.currentResources = 75;
  148. node.regenerationRate = 0.5f;
  149. // Apply material
  150. if (stoneMaterial != null)
  151. {
  152. stone.GetComponent<Renderer>().material = stoneMaterial;
  153. }
  154. else
  155. {
  156. stone.GetComponent<Renderer>().material.color = Color.gray;
  157. }
  158. // Create selection indicator
  159. CreateSelectionIndicator(stone, node);
  160. Debug.Log($"Created Stone at {position}");
  161. }
  162. void CreateFarm(Vector3 position, int index)
  163. {
  164. GameObject farm = GameObject.CreatePrimitive(PrimitiveType.Cube);
  165. farm.name = $"Farm_{index + 1}";
  166. farm.tag = "Farm";
  167. farm.transform.position = position;
  168. farm.transform.localScale = new Vector3(2f, 0.2f, 2f);
  169. // Add ResourceNode component
  170. ResourceNode node = farm.AddComponent<ResourceNode>();
  171. node.nodeType = ResourceNodeType.Farm;
  172. node.resourceType = ResourceType.Food;
  173. node.maxResources = 100;
  174. node.currentResources = 100;
  175. node.regenerationRate = 3f;
  176. // Apply material
  177. if (farmMaterial != null)
  178. {
  179. farm.GetComponent<Renderer>().material = farmMaterial;
  180. }
  181. else
  182. {
  183. farm.GetComponent<Renderer>().material.color = Color.yellow;
  184. }
  185. // Create selection indicator
  186. CreateSelectionIndicator(farm, node);
  187. Debug.Log($"Created Farm at {position}");
  188. }
  189. void CreateSelectionIndicator(GameObject parent, ResourceNode node)
  190. {
  191. GameObject indicator = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
  192. indicator.transform.SetParent(parent.transform);
  193. indicator.transform.localPosition = Vector3.down * 0.6f;
  194. indicator.transform.localScale = Vector3.one * 1.2f;
  195. indicator.GetComponent<Renderer>().material.color = Color.cyan;
  196. indicator.SetActive(false);
  197. node.selectionIndicator = indicator;
  198. Destroy(indicator.GetComponent<Collider>());
  199. }
  200. Vector3 GetRandomPosition()
  201. {
  202. Vector2 randomCircle = Random.insideUnitCircle * resourceSpawnRadius;
  203. Vector3 position = townHallPosition + new Vector3(randomCircle.x, 0, randomCircle.y);
  204. // Make sure we don't spawn too close to town hall
  205. if (Vector3.Distance(position, townHallPosition) < 3f)
  206. {
  207. return GetRandomPosition(); // Try again
  208. }
  209. return position;
  210. }
  211. void SetupCamera()
  212. {
  213. Camera mainCamera = Camera.main;
  214. if (mainCamera == null)
  215. {
  216. mainCamera = FindObjectOfType<Camera>();
  217. }
  218. if (mainCamera != null)
  219. {
  220. // Position camera above the scene
  221. mainCamera.transform.position = new Vector3(0, 15, -10);
  222. mainCamera.transform.rotation = Quaternion.Euler(45, 0, 0);
  223. // Add camera controller if it doesn't exist
  224. CameraController cameraController = mainCamera.GetComponent<CameraController>();
  225. if (cameraController == null)
  226. {
  227. cameraController = mainCamera.gameObject.AddComponent<CameraController>();
  228. }
  229. // Set up camera for orthographic view (better for RTS games)
  230. mainCamera.orthographic = true;
  231. mainCamera.orthographicSize = 15f;
  232. Debug.Log("Camera setup complete");
  233. }
  234. }
  235. void CreateGameManager()
  236. {
  237. // Check if GameManager already exists
  238. GameManager existingGameManager = FindObjectOfType<GameManager>();
  239. if (existingGameManager == null)
  240. {
  241. GameObject gameManagerGO = new GameObject("GameManager");
  242. // Add GameManager component
  243. GameManager gameManager = gameManagerGO.AddComponent<GameManager>();
  244. Debug.Log("Created GameManager");
  245. }
  246. // Create separate UI GameObject if it doesn't exist
  247. GameObject uiGameObject = GameObject.Find("UI");
  248. if (uiGameObject == null)
  249. {
  250. uiGameObject = new GameObject("UI");
  251. uiGameObject.transform.position = Vector3.zero;
  252. // Add UIDocument component
  253. UIDocument uiDoc = uiGameObject.AddComponent<UIDocument>();
  254. // Try to load the UXML file from Resources folder
  255. VisualTreeAsset gameUIAsset = Resources.Load<VisualTreeAsset>("GameUI");
  256. if (gameUIAsset != null)
  257. {
  258. uiDoc.visualTreeAsset = gameUIAsset;
  259. Debug.Log($"Assigned UXML asset '{gameUIAsset.name}' to UIDocument");
  260. }
  261. else
  262. {
  263. Debug.LogWarning("Could not find GameUI.uxml in Resources! Please assign it manually in the inspector.");
  264. }
  265. // Try to load panel settings
  266. PanelSettings panelSettings = Resources.Load<PanelSettings>("PanelSettings");
  267. if (panelSettings != null)
  268. {
  269. uiDoc.panelSettings = panelSettings;
  270. Debug.Log("Assigned PanelSettings to UIDocument");
  271. }
  272. // Add GameUI component
  273. uiGameObject.AddComponent<GameUI>();
  274. Debug.Log("Created UI GameObject with GameUI component");
  275. }
  276. // Create Building System GameObject
  277. GameObject buildingSystemGO = GameObject.Find("BuildingSystem");
  278. if (buildingSystemGO == null)
  279. {
  280. buildingSystemGO = new GameObject("BuildingSystem");
  281. buildingSystemGO.AddComponent<BuildingSystem>();
  282. buildingSystemGO.AddComponent<HousingManager>();
  283. Debug.Log("Created BuildingSystem and HousingManager");
  284. }
  285. }
  286. [ContextMenu("Clear Scene")]
  287. public void ClearScene()
  288. {
  289. // Clear existing objects (useful for testing)
  290. GameObject[] trees = GameObject.FindGameObjectsWithTag("Tree");
  291. GameObject[] stones = GameObject.FindGameObjectsWithTag("Stone");
  292. GameObject[] farms = GameObject.FindGameObjectsWithTag("Farm");
  293. GameObject[] villagers = GameObject.FindGameObjectsWithTag("Villager");
  294. GameObject townHall = GameObject.FindWithTag("TownHall");
  295. foreach (var obj in trees) DestroyImmediate(obj);
  296. foreach (var obj in stones) DestroyImmediate(obj);
  297. foreach (var obj in farms) DestroyImmediate(obj);
  298. foreach (var obj in villagers) DestroyImmediate(obj);
  299. if (townHall != null) DestroyImmediate(townHall);
  300. Debug.Log("Scene cleared");
  301. }
  302. }