ryzerrr's picture
Upload folder using huggingface_hub
6cae93f verified
Raw
History Blame Contribute Delete
3.98 kB
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>();
}
}
}