SceneSetup.cs 10 KB

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