SceneSetup.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. // Farms will be built by players, not pre-generated
  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 CreateForests()
  88. {
  89. int numForestClusters = Random.Range(2, 5); // 2-4 forest clusters
  90. for (int cluster = 0; cluster < numForestClusters; cluster++)
  91. {
  92. // Pick a random center point for this forest cluster
  93. Vector3 clusterCenter = GetRandomPosition();
  94. // Create 3-7 trees in this cluster
  95. int treesInCluster = Random.Range(3, 8);
  96. for (int i = 0; i < treesInCluster; i++)
  97. {
  98. // Position trees within cluster radius
  99. Vector3 offset = Random.insideUnitSphere * 8f; // 8 unit radius
  100. offset.y = 0; // Keep on ground level
  101. Vector3 treePosition = clusterCenter + offset;
  102. // Make sure it's within bounds
  103. treePosition.x = Mathf.Clamp(treePosition.x, -resourceSpawnRadius + 2, resourceSpawnRadius - 2);
  104. treePosition.z = Mathf.Clamp(treePosition.z, -resourceSpawnRadius + 2, resourceSpawnRadius - 2);
  105. CreateTree(treePosition, cluster * 10 + i);
  106. }
  107. }
  108. // Add some scattered individual trees (about 30% of cluster trees)
  109. int scatteredTrees = (numForestClusters * 5) / 3; // Roughly 30% extra
  110. for (int i = 0; i < scatteredTrees; i++)
  111. {
  112. Vector3 position = GetRandomPosition();
  113. CreateTree(position, 1000 + i); // Use high IDs to avoid conflicts
  114. }
  115. Debug.Log($"Created {numForestClusters} forest clusters with scattered trees");
  116. }
  117. void CreateResourceNodes()
  118. {
  119. // Create Trees in forest-like clusters
  120. CreateForests();
  121. // Create Stones
  122. for (int i = 0; i < numberOfStones; i++)
  123. {
  124. Vector3 position = GetRandomPosition();
  125. CreateStone(position, i);
  126. }
  127. // No longer create pre-made farms - farmhouses will be built by players
  128. }
  129. void CreateTree(Vector3 position, int index)
  130. {
  131. GameObject tree = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
  132. tree.name = $"Tree_{index + 1}";
  133. tree.tag = "Tree";
  134. tree.transform.position = position;
  135. tree.transform.localScale = new Vector3(1f, 2f, 1f);
  136. // Add ResourceNode component
  137. ResourceNode node = tree.AddComponent<ResourceNode>();
  138. node.nodeType = ResourceNodeType.Tree;
  139. node.resourceType = ResourceType.Wood;
  140. node.maxResources = 50;
  141. node.currentResources = 50;
  142. node.regenerationRate = 2f;
  143. // Apply material
  144. if (treeMaterial != null)
  145. {
  146. tree.GetComponent<Renderer>().material = treeMaterial;
  147. }
  148. else
  149. {
  150. tree.GetComponent<Renderer>().material.color = Color.green;
  151. }
  152. // Create selection indicator
  153. CreateSelectionIndicator(tree, node);
  154. Debug.Log($"Created Tree at {position}");
  155. }
  156. void CreateStone(Vector3 position, int index)
  157. {
  158. GameObject stone = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  159. stone.name = $"Stone_{index + 1}";
  160. stone.tag = "Stone";
  161. stone.transform.position = position;
  162. stone.transform.localScale = Vector3.one * 1.5f;
  163. // Add ResourceNode component
  164. ResourceNode node = stone.AddComponent<ResourceNode>();
  165. node.nodeType = ResourceNodeType.Stone;
  166. node.resourceType = ResourceType.Stone;
  167. node.maxResources = 75;
  168. node.currentResources = 75;
  169. node.regenerationRate = 0.5f;
  170. // Apply material
  171. if (stoneMaterial != null)
  172. {
  173. stone.GetComponent<Renderer>().material = stoneMaterial;
  174. }
  175. else
  176. {
  177. stone.GetComponent<Renderer>().material.color = Color.gray;
  178. }
  179. // Create selection indicator
  180. CreateSelectionIndicator(stone, node);
  181. Debug.Log($"Created Stone at {position}");
  182. }
  183. void CreateFarm(Vector3 position, int index)
  184. {
  185. GameObject farm = GameObject.CreatePrimitive(PrimitiveType.Cube);
  186. farm.name = $"Farm_{index + 1}";
  187. farm.tag = "Farm";
  188. farm.transform.position = position;
  189. farm.transform.localScale = new Vector3(2f, 0.2f, 2f);
  190. // Add ResourceNode component
  191. ResourceNode node = farm.AddComponent<ResourceNode>();
  192. node.nodeType = ResourceNodeType.Farm;
  193. node.resourceType = ResourceType.Food;
  194. node.maxResources = 100;
  195. node.currentResources = 100;
  196. node.regenerationRate = 3f;
  197. // Apply material
  198. if (farmMaterial != null)
  199. {
  200. farm.GetComponent<Renderer>().material = farmMaterial;
  201. }
  202. else
  203. {
  204. farm.GetComponent<Renderer>().material.color = Color.yellow;
  205. }
  206. // Create selection indicator
  207. CreateSelectionIndicator(farm, node);
  208. Debug.Log($"Created Farm at {position}");
  209. }
  210. void CreateSelectionIndicator(GameObject parent, ResourceNode node)
  211. {
  212. GameObject indicator = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
  213. indicator.transform.SetParent(parent.transform);
  214. indicator.transform.localPosition = Vector3.down * 0.6f;
  215. indicator.transform.localScale = Vector3.one * 1.2f;
  216. indicator.GetComponent<Renderer>().material.color = Color.cyan;
  217. indicator.SetActive(false);
  218. node.selectionIndicator = indicator;
  219. Destroy(indicator.GetComponent<Collider>());
  220. }
  221. Vector3 GetRandomPosition()
  222. {
  223. Vector2 randomCircle = Random.insideUnitCircle * resourceSpawnRadius;
  224. Vector3 position = townHallPosition + new Vector3(randomCircle.x, 0, randomCircle.y);
  225. // Make sure we don't spawn too close to town hall
  226. if (Vector3.Distance(position, townHallPosition) < 3f)
  227. {
  228. return GetRandomPosition(); // Try again
  229. }
  230. return position;
  231. }
  232. void SetupCamera()
  233. {
  234. Camera mainCamera = Camera.main;
  235. if (mainCamera == null)
  236. {
  237. mainCamera = FindFirstObjectByType<Camera>();
  238. }
  239. if (mainCamera != null)
  240. {
  241. // Position camera above the scene
  242. mainCamera.transform.position = new Vector3(0, 15, -10);
  243. mainCamera.transform.rotation = Quaternion.Euler(45, 0, 0);
  244. // Add camera controller if it doesn't exist
  245. CameraController cameraController = mainCamera.GetComponent<CameraController>();
  246. if (cameraController == null)
  247. {
  248. cameraController = mainCamera.gameObject.AddComponent<CameraController>();
  249. }
  250. // Set up camera for orthographic view (better for RTS games)
  251. mainCamera.orthographic = true;
  252. mainCamera.orthographicSize = 15f;
  253. Debug.Log("Camera setup complete");
  254. }
  255. }
  256. void CreateGameManager()
  257. {
  258. // Check if GameManager already exists
  259. GameManager existingGameManager = FindFirstObjectByType<GameManager>();
  260. if (existingGameManager == null)
  261. {
  262. GameObject gameManagerGO = new GameObject("GameManager");
  263. // Add GameManager component
  264. GameManager gameManager = gameManagerGO.AddComponent<GameManager>();
  265. Debug.Log("Created GameManager");
  266. }
  267. // Create separate UI GameObject if it doesn't exist
  268. GameObject uiGameObject = GameObject.Find("UI");
  269. if (uiGameObject == null)
  270. {
  271. uiGameObject = new GameObject("UI");
  272. uiGameObject.transform.position = Vector3.zero;
  273. // Add UIDocument component
  274. UIDocument uiDoc = uiGameObject.AddComponent<UIDocument>();
  275. // Try to load the UXML file from Resources folder
  276. VisualTreeAsset gameUIAsset = Resources.Load<VisualTreeAsset>("GameUI");
  277. if (gameUIAsset != null)
  278. {
  279. uiDoc.visualTreeAsset = gameUIAsset;
  280. Debug.Log($"Assigned UXML asset '{gameUIAsset.name}' to UIDocument");
  281. }
  282. else
  283. {
  284. Debug.LogWarning("Could not find GameUI.uxml in Resources! Please assign it manually in the inspector.");
  285. }
  286. // Try to load panel settings
  287. PanelSettings panelSettings = Resources.Load<PanelSettings>("PanelSettings");
  288. if (panelSettings != null)
  289. {
  290. uiDoc.panelSettings = panelSettings;
  291. Debug.Log("Assigned PanelSettings to UIDocument");
  292. }
  293. // Add GameUI component
  294. uiGameObject.AddComponent<GameUI>();
  295. Debug.Log("Created UI GameObject with GameUI component");
  296. }
  297. // Create Building System GameObject
  298. GameObject buildingSystemGO = GameObject.Find("BuildingSystem");
  299. if (buildingSystemGO == null)
  300. {
  301. buildingSystemGO = new GameObject("BuildingSystem");
  302. buildingSystemGO.AddComponent<BuildingSystem>();
  303. buildingSystemGO.AddComponent<HousingManager>();
  304. Debug.Log("Created BuildingSystem and HousingManager");
  305. }
  306. }
  307. [ContextMenu("Clear Scene")]
  308. public void ClearScene()
  309. {
  310. // Clear existing objects (useful for testing)
  311. GameObject[] trees = GameObject.FindGameObjectsWithTag("Tree");
  312. GameObject[] stones = GameObject.FindGameObjectsWithTag("Stone");
  313. GameObject[] farms = GameObject.FindGameObjectsWithTag("Farm");
  314. GameObject[] villagers = GameObject.FindGameObjectsWithTag("Villager");
  315. GameObject townHall = GameObject.FindWithTag("TownHall");
  316. foreach (var obj in trees) DestroyImmediate(obj);
  317. foreach (var obj in stones) DestroyImmediate(obj);
  318. foreach (var obj in farms) DestroyImmediate(obj);
  319. foreach (var obj in villagers) DestroyImmediate(obj);
  320. if (townHall != null) DestroyImmediate(townHall);
  321. Debug.Log("Scene cleared");
  322. }
  323. }