| using UnityEngine; |
|
|
| namespace UnityAgent |
| { |
| |
| |
| |
| |
| public class GameManager : MonoBehaviour |
| { |
| [Header("Player")] |
| public GameObject playerPrefab; |
| public Vector3 spawnPosition = new Vector3(0f, 2f, 0f); |
|
|
| [Header("City")] |
| public int cityGridX = 8; |
| public int cityGridZ = 8; |
|
|
| private void Start() |
| { |
| SpawnPlayer(); |
| SpawnCity(); |
| SpawnLights(); |
| SetupCamera(); |
| SetupAudio(); |
| SetupUI(); |
| } |
|
|
| private void SpawnPlayer() |
| { |
| GameObject player = playerPrefab != null |
| ? Instantiate(playerPrefab, spawnPosition, Quaternion.identity) |
| : CreatePrimitivePlayer(); |
| player.tag = "Player"; |
| player.name = "Player"; |
|
|
| var pc = player.GetComponent<PlayerController>(); |
| if (pc == null) pc = player.AddComponent<PlayerController>(); |
| var rb = player.GetComponent<Rigidbody>(); |
| if (rb == null) rb = player.AddComponent<Rigidbody>(); |
| var col = player.GetComponent<CapsuleCollider>(); |
| if (col == null) col = player.AddComponent<CapsuleCollider>(); |
| col.height = 2f; col.radius = 0.4f; col.center = new Vector3(0f, 1f, 0f); |
| rb.constraints = RigidbodyConstraints.FreezeRotation; |
| } |
|
|
| private GameObject CreatePrimitivePlayer() |
| { |
| GameObject go = GameObject.CreatePrimitive(PrimitiveType.Capsule); |
| go.transform.position = spawnPosition; |
| return go; |
| } |
|
|
| private void SpawnCity() |
| { |
| GameObject city = new GameObject("City"); |
| var gen = city.AddComponent<CityGenerator>(); |
| gen.gridX = cityGridX; |
| gen.gridZ = cityGridZ; |
|
|
| GameObject roads = new GameObject("Roads"); |
| roads.transform.SetParent(city.transform, false); |
| var net = roads.AddComponent<RoadNetwork>(); |
| net.gridSize = Mathf.Max(cityGridX, cityGridZ); |
| } |
|
|
| private void SpawnLights() |
| { |
| GameObject sunGo = new GameObject("Sun"); |
| var sun = sunGo.AddComponent<Light>(); |
| sun.type = LightType.Directional; |
| sun.intensity = 1.2f; |
| sun.shadows = LightShadows.Soft; |
|
|
| var dnc = sunGo.AddComponent<DayNightCycle>(); |
| dnc.sun = sun; |
| |
| StartCoroutine(BindStreetLights(dnc)); |
| } |
|
|
| private System.Collections.IEnumerator BindStreetLights(DayNightCycle dnc) |
| { |
| yield return null; |
| var cg = FindObjectOfType<CityGenerator>(); |
| if (cg != null) dnc.streetLights = cg.StreetLights; |
| } |
|
|
| private void SetupCamera() |
| { |
| Camera cam = Camera.main; |
| if (cam == null) |
| { |
| var go = new GameObject("Main Camera"); |
| cam = go.AddComponent<Camera>(); |
| go.tag = "MainCamera"; |
| go.AddComponent<AudioListener>(); |
| } |
| var tpc = cam.GetComponent<ThirdPersonCamera>(); |
| if (tpc == null) tpc = cam.gameObject.AddComponent<ThirdPersonCamera>(); |
| var player = GameObject.FindGameObjectWithTag("Player"); |
| tpc.target = player != null ? player.transform : null; |
| } |
|
|
| private void SetupAudio() |
| { |
| var go = new GameObject("AudioManager"); |
| go.AddComponent<AudioManager>(); |
| } |
|
|
| private void SetupUI() |
| { |
| var go = new GameObject("UIManager"); |
| go.AddComponent<UIManager>(); |
| } |
| } |
| } |
|
|