File size: 3,981 Bytes
40c0886
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using UnityEngine;

namespace UnityAgent
{
    /// <summary>
    /// Bootstrap that wires up the generated systems on scene load. The
    /// scene only needs an empty GameObject with this component attached.
    /// </summary>
    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;
            // Bind street lights from the city generator after one frame.
            StartCoroutine(BindStreetLights(dnc));
        }

        private System.Collections.IEnumerator BindStreetLights(DayNightCycle dnc)
        {
            yield return null; // allow CityGenerator.Start() to run
            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>();
        }
    }
}