| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- using UnityEngine;
- using UnityEngine.UIElements;
- public class SceneSetup : MonoBehaviour
- {
- [Header("Scene Setup")]
- public bool setupSceneOnStart = true;
- public Vector3 townHallPosition = Vector3.zero;
- public int numberOfTrees = 5;
- public int numberOfStones = 3;
- // Farms will be built by players, not pre-generated
- public float resourceSpawnRadius = 15f;
- [Header("Materials (Optional)")]
- public Material villagerMaterial;
- public Material townHallMaterial;
- public Material treeMaterial;
- public Material stoneMaterial;
- public Material farmMaterial;
- void Start()
- {
- if (setupSceneOnStart)
- {
- SetupScene();
- }
- }
- [ContextMenu("Setup Scene")]
- public void SetupScene()
- {
- CreateGround();
- CreateTownHall();
- CreateResourceNodes();
- SetupCamera();
- CreateGameManager();
- }
- void CreateGround()
- {
- GameObject ground = GameObject.Find("Ground");
- if (ground == null)
- {
- // Create ground plane
- ground = GameObject.CreatePrimitive(PrimitiveType.Plane);
- ground.name = "Ground";
- ground.transform.position = Vector3.zero;
- ground.transform.localScale = new Vector3(5f, 1f, 5f); // 50x50 units
- // Set material if available
- Renderer groundRenderer = ground.GetComponent<Renderer>();
- if (groundRenderer != null)
- {
- groundRenderer.material.color = new Color(0.3f, 0.5f, 0.3f); // Green ground
- }
- Debug.Log("Created ground plane");
- }
- }
- void CreateTownHall()
- {
- GameObject townHallGO = GameObject.FindWithTag("TownHall");
- if (townHallGO == null)
- {
- // Create TownHall
- townHallGO = GameObject.CreatePrimitive(PrimitiveType.Cube);
- townHallGO.name = "TownHall";
- townHallGO.tag = "TownHall";
- townHallGO.transform.position = townHallPosition;
- townHallGO.transform.localScale = new Vector3(3f, 2f, 3f);
- // Add TownHall component
- TownHall townHall = townHallGO.AddComponent<TownHall>();
- // Apply material if available
- if (townHallMaterial != null)
- {
- townHallGO.GetComponent<Renderer>().material = townHallMaterial;
- }
- else
- {
- townHallGO.GetComponent<Renderer>().material.color = Color.blue;
- }
- // Create selection indicator
- GameObject indicator = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
- indicator.transform.SetParent(townHallGO.transform);
- indicator.transform.localPosition = Vector3.down * 1.2f;
- indicator.transform.localScale = Vector3.one * 1.5f;
- indicator.GetComponent<Renderer>().material.color = Color.yellow;
- indicator.SetActive(false);
- townHall.selectionIndicator = indicator;
- Destroy(indicator.GetComponent<Collider>());
- Debug.Log("Created TownHall");
- }
- }
- void CreateForests()
- {
- int numForestClusters = Random.Range(2, 5); // 2-4 forest clusters
- for (int cluster = 0; cluster < numForestClusters; cluster++)
- {
- // Pick a random center point for this forest cluster
- Vector3 clusterCenter = GetRandomPosition();
- // Create 3-7 trees in this cluster
- int treesInCluster = Random.Range(3, 8);
- for (int i = 0; i < treesInCluster; i++)
- {
- // Position trees within cluster radius
- Vector3 offset = Random.insideUnitSphere * 8f; // 8 unit radius
- offset.y = 0; // Keep on ground level
- Vector3 treePosition = clusterCenter + offset;
- // Make sure it's within bounds
- treePosition.x = Mathf.Clamp(treePosition.x, -resourceSpawnRadius + 2, resourceSpawnRadius - 2);
- treePosition.z = Mathf.Clamp(treePosition.z, -resourceSpawnRadius + 2, resourceSpawnRadius - 2);
- CreateTree(treePosition, cluster * 10 + i);
- }
- }
- // Add some scattered individual trees (about 30% of cluster trees)
- int scatteredTrees = (numForestClusters * 5) / 3; // Roughly 30% extra
- for (int i = 0; i < scatteredTrees; i++)
- {
- Vector3 position = GetRandomPosition();
- CreateTree(position, 1000 + i); // Use high IDs to avoid conflicts
- }
- Debug.Log($"Created {numForestClusters} forest clusters with scattered trees");
- }
- void CreateResourceNodes()
- {
- // Create Trees in forest-like clusters
- CreateForests();
- // Create Stones
- for (int i = 0; i < numberOfStones; i++)
- {
- Vector3 position = GetRandomPosition();
- CreateStone(position, i);
- }
- // No longer create pre-made farms - farmhouses will be built by players
- }
- void CreateTree(Vector3 position, int index)
- {
- GameObject tree = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
- tree.name = $"Tree_{index + 1}";
- tree.tag = "Tree";
- tree.transform.position = position;
- tree.transform.localScale = new Vector3(1f, 2f, 1f);
- // Add ResourceNode component
- ResourceNode node = tree.AddComponent<ResourceNode>();
- node.nodeType = ResourceNodeType.Tree;
- node.resourceType = ResourceType.Wood;
- node.maxResources = 50;
- node.currentResources = 50;
- node.regenerationRate = 2f;
- // Apply material
- if (treeMaterial != null)
- {
- tree.GetComponent<Renderer>().material = treeMaterial;
- }
- else
- {
- tree.GetComponent<Renderer>().material.color = Color.green;
- }
- // Create selection indicator
- CreateSelectionIndicator(tree, node);
- Debug.Log($"Created Tree at {position}");
- }
- void CreateStone(Vector3 position, int index)
- {
- GameObject stone = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- stone.name = $"Stone_{index + 1}";
- stone.tag = "Stone";
- stone.transform.position = position;
- stone.transform.localScale = Vector3.one * 1.5f;
- // Add ResourceNode component
- ResourceNode node = stone.AddComponent<ResourceNode>();
- node.nodeType = ResourceNodeType.Stone;
- node.resourceType = ResourceType.Stone;
- node.maxResources = 75;
- node.currentResources = 75;
- node.regenerationRate = 0.5f;
- // Apply material
- if (stoneMaterial != null)
- {
- stone.GetComponent<Renderer>().material = stoneMaterial;
- }
- else
- {
- stone.GetComponent<Renderer>().material.color = Color.gray;
- }
- // Create selection indicator
- CreateSelectionIndicator(stone, node);
- Debug.Log($"Created Stone at {position}");
- }
- void CreateFarm(Vector3 position, int index)
- {
- GameObject farm = GameObject.CreatePrimitive(PrimitiveType.Cube);
- farm.name = $"Farm_{index + 1}";
- farm.tag = "Farm";
- farm.transform.position = position;
- farm.transform.localScale = new Vector3(2f, 0.2f, 2f);
- // Add ResourceNode component
- ResourceNode node = farm.AddComponent<ResourceNode>();
- node.nodeType = ResourceNodeType.Farm;
- node.resourceType = ResourceType.Food;
- node.maxResources = 100;
- node.currentResources = 100;
- node.regenerationRate = 3f;
- // Apply material
- if (farmMaterial != null)
- {
- farm.GetComponent<Renderer>().material = farmMaterial;
- }
- else
- {
- farm.GetComponent<Renderer>().material.color = Color.yellow;
- }
- // Create selection indicator
- CreateSelectionIndicator(farm, node);
- Debug.Log($"Created Farm at {position}");
- }
- void CreateSelectionIndicator(GameObject parent, ResourceNode node)
- {
- GameObject indicator = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
- indicator.transform.SetParent(parent.transform);
- indicator.transform.localPosition = Vector3.down * 0.6f;
- indicator.transform.localScale = Vector3.one * 1.2f;
- indicator.GetComponent<Renderer>().material.color = Color.cyan;
- indicator.SetActive(false);
- node.selectionIndicator = indicator;
- Destroy(indicator.GetComponent<Collider>());
- }
- Vector3 GetRandomPosition()
- {
- Vector2 randomCircle = Random.insideUnitCircle * resourceSpawnRadius;
- Vector3 position = townHallPosition + new Vector3(randomCircle.x, 0, randomCircle.y);
- // Make sure we don't spawn too close to town hall
- if (Vector3.Distance(position, townHallPosition) < 3f)
- {
- return GetRandomPosition(); // Try again
- }
- return position;
- }
- void SetupCamera()
- {
- Camera mainCamera = Camera.main;
- if (mainCamera == null)
- {
- mainCamera = FindFirstObjectByType<Camera>();
- }
- if (mainCamera != null)
- {
- // Position camera above the scene
- mainCamera.transform.position = new Vector3(0, 15, -10);
- mainCamera.transform.rotation = Quaternion.Euler(45, 0, 0);
- // Add camera controller if it doesn't exist
- CameraController cameraController = mainCamera.GetComponent<CameraController>();
- if (cameraController == null)
- {
- cameraController = mainCamera.gameObject.AddComponent<CameraController>();
- }
- // Set up camera for orthographic view (better for RTS games)
- mainCamera.orthographic = true;
- mainCamera.orthographicSize = 15f;
- Debug.Log("Camera setup complete");
- }
- }
- void CreateGameManager()
- {
- // Check if GameManager already exists
- GameManager existingGameManager = FindFirstObjectByType<GameManager>();
- if (existingGameManager == null)
- {
- GameObject gameManagerGO = new GameObject("GameManager");
- // Add GameManager component
- GameManager gameManager = gameManagerGO.AddComponent<GameManager>();
- Debug.Log("Created GameManager");
- }
- // Create separate UI GameObject if it doesn't exist
- GameObject uiGameObject = GameObject.Find("UI");
- if (uiGameObject == null)
- {
- uiGameObject = new GameObject("UI");
- uiGameObject.transform.position = Vector3.zero;
- // Add UIDocument component
- UIDocument uiDoc = uiGameObject.AddComponent<UIDocument>();
- // Try to load the UXML file from Resources folder
- VisualTreeAsset gameUIAsset = Resources.Load<VisualTreeAsset>("GameUI");
- if (gameUIAsset != null)
- {
- uiDoc.visualTreeAsset = gameUIAsset;
- Debug.Log($"Assigned UXML asset '{gameUIAsset.name}' to UIDocument");
- }
- else
- {
- Debug.LogWarning("Could not find GameUI.uxml in Resources! Please assign it manually in the inspector.");
- }
- // Try to load panel settings
- PanelSettings panelSettings = Resources.Load<PanelSettings>("PanelSettings");
- if (panelSettings != null)
- {
- uiDoc.panelSettings = panelSettings;
- Debug.Log("Assigned PanelSettings to UIDocument");
- }
- // Add GameUI component
- uiGameObject.AddComponent<GameUI>();
- Debug.Log("Created UI GameObject with GameUI component");
- }
- // Create Building System GameObject
- GameObject buildingSystemGO = GameObject.Find("BuildingSystem");
- if (buildingSystemGO == null)
- {
- buildingSystemGO = new GameObject("BuildingSystem");
- buildingSystemGO.AddComponent<BuildingSystem>();
- buildingSystemGO.AddComponent<HousingManager>();
- Debug.Log("Created BuildingSystem and HousingManager");
- }
- }
- [ContextMenu("Clear Scene")]
- public void ClearScene()
- {
- // Clear existing objects (useful for testing)
- GameObject[] trees = GameObject.FindGameObjectsWithTag("Tree");
- GameObject[] stones = GameObject.FindGameObjectsWithTag("Stone");
- GameObject[] farms = GameObject.FindGameObjectsWithTag("Farm");
- GameObject[] villagers = GameObject.FindGameObjectsWithTag("Villager");
- GameObject townHall = GameObject.FindWithTag("TownHall");
- foreach (var obj in trees) DestroyImmediate(obj);
- foreach (var obj in stones) DestroyImmediate(obj);
- foreach (var obj in farms) DestroyImmediate(obj);
- foreach (var obj in villagers) DestroyImmediate(obj);
- if (townHall != null) DestroyImmediate(townHall);
- Debug.Log("Scene cleared");
- }
- }
|