You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
3.4 KiB
C#
101 lines
3.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Rendering.PostProcessing;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
|
|
public class CarSelectionManager : MonoBehaviour
|
|
{
|
|
[Header("Car Selection")]
|
|
public CarData[] allCarData;
|
|
public GameObject[] allCarPrefabs;
|
|
public Transform playerSpawnPoint;
|
|
public List<Transform> aiSpawnPoints;
|
|
|
|
[Header("Camera")]
|
|
public CameraController cameraController;
|
|
|
|
[Header("Name Tags")]
|
|
public GameObject nameTagPrefab; // World space canvas with TextMeshProUGUI
|
|
|
|
private PostProcessVolume cachedVolume;
|
|
|
|
// Random AI names
|
|
private List<string> availableAINames = new List<string> { "Nitro", "Shadow", "Blaze", "Turbo", "Skid", "Flash", "Dash", "Zed" };
|
|
|
|
|
|
private void Start()
|
|
{
|
|
cachedVolume = Camera.main.GetComponent<PostProcessVolume>();
|
|
|
|
int selectedIndex = GameConstants.CarSelected;
|
|
|
|
if (selectedIndex < 0 || selectedIndex >= allCarPrefabs.Length || selectedIndex >= allCarData.Length)
|
|
{
|
|
Debug.LogError("Selected car index is out of bounds!");
|
|
return;
|
|
}
|
|
|
|
// === PLAYER CAR ===
|
|
GameObject playerCar = Instantiate(
|
|
allCarPrefabs[selectedIndex],
|
|
playerSpawnPoint.position,
|
|
playerSpawnPoint.rotation,
|
|
playerSpawnPoint
|
|
);
|
|
|
|
playerCar.transform.localPosition = Vector3.zero;
|
|
playerCar.transform.localRotation = Quaternion.identity;
|
|
|
|
VehicleController playerController = playerCar.GetComponent<VehicleController>();
|
|
if (playerController != null)
|
|
{
|
|
playerController.InitializeWithData(allCarData[selectedIndex]);
|
|
playerController.isAIControlled = false;
|
|
|
|
cameraController.target = playerController.transform;
|
|
cameraController.InitializeTarget();
|
|
}
|
|
|
|
NOSController playerNOS = playerCar.GetComponent<NOSController>();
|
|
if (playerNOS != null && cachedVolume != null)
|
|
{
|
|
playerNOS.SetPostProcessVolume(cachedVolume);
|
|
}
|
|
|
|
CreateNameTag(playerCar.transform, GameConstants.PlayerName);
|
|
|
|
// === AI CARS ===
|
|
int aiCount = Mathf.Min(3, aiSpawnPoints.Count);
|
|
for (int i = 0; i < aiCount; i++)
|
|
{
|
|
int aiPrefabIndex = (selectedIndex + i + 1) % allCarPrefabs.Length;
|
|
Transform spawn = aiSpawnPoints[i];
|
|
|
|
GameObject aiCar = Instantiate(
|
|
allCarPrefabs[aiPrefabIndex],
|
|
spawn.position,
|
|
spawn.rotation
|
|
);
|
|
|
|
VehicleController aiController = aiCar.GetComponent<VehicleController>();
|
|
if (aiController != null)
|
|
{
|
|
aiController.InitializeWithData(allCarData[selectedIndex]); // same stats as player
|
|
aiController.isAIControlled = true;
|
|
}
|
|
string aiName = availableAINames[Random.Range(0, availableAINames.Count)];
|
|
availableAINames.Remove(aiName);
|
|
CreateNameTag(aiCar.transform, aiName);
|
|
|
|
}
|
|
}
|
|
|
|
private void CreateNameTag(Transform carTransform, string nameText)
|
|
{
|
|
GameObject nameTag = Instantiate(nameTagPrefab, carTransform);
|
|
nameTag.transform.localPosition = new Vector3(0, 2.5f, 0); // Adjust height above car
|
|
TextMeshProUGUI text = nameTag.GetComponentInChildren<TextMeshProUGUI>();
|
|
if (text != null) text.text = nameText;
|
|
}
|
|
}
|