45 lines
1.6 KiB
C#
45 lines
1.6 KiB
C#
using RaceFields.CameraSystem;
|
|
using RaceFields.Vehicles;
|
|
using UnityEngine;
|
|
|
|
namespace RaceFields.GameFlow
|
|
{
|
|
public sealed class SelectedVehicleSpawner : MonoBehaviour
|
|
{
|
|
[SerializeField] private Transform spawnPoint;
|
|
[SerializeField] private GameObject fallbackVehiclePrefab;
|
|
[SerializeField] private bool destroyExistingVehicles = true;
|
|
|
|
private void Start()
|
|
{
|
|
GameObject prefab = GameSelectionState.VehiclePrefab != null ? GameSelectionState.VehiclePrefab : fallbackVehiclePrefab;
|
|
if (prefab == null)
|
|
return;
|
|
|
|
if (destroyExistingVehicles)
|
|
DestroyExistingVehicleControllers();
|
|
|
|
Transform targetSpawn = spawnPoint != null ? spawnPoint : transform;
|
|
GameObject vehicle = Instantiate(prefab, targetSpawn.position, targetSpawn.rotation);
|
|
AssignCameraTarget(vehicle.transform);
|
|
}
|
|
|
|
private static void AssignCameraTarget(Transform vehicle)
|
|
{
|
|
BuggyFollowCamera followCamera = FindFirstObjectByType<BuggyFollowCamera>();
|
|
if (followCamera != null)
|
|
followCamera.SetTarget(vehicle, true);
|
|
}
|
|
|
|
private static void DestroyExistingVehicleControllers()
|
|
{
|
|
ArcadeBuggyController[] existingVehicles = FindObjectsByType<ArcadeBuggyController>(FindObjectsSortMode.None);
|
|
for (int i = 0; i < existingVehicles.Length; i++)
|
|
{
|
|
if (existingVehicles[i] != null)
|
|
Destroy(existingVehicles[i].gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|