63 lines
2.3 KiB
C#
63 lines
2.3 KiB
C#
using RaceFields.CameraSystem;
|
|
using RaceFields.Damage.VehicleDamage;
|
|
using RaceFields.UI.HUD;
|
|
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);
|
|
AssignDamageHud(vehicle);
|
|
}
|
|
|
|
private static void AssignCameraTarget(Transform vehicle)
|
|
{
|
|
BuggyFollowCamera followCamera = FindAnyObjectByType<BuggyFollowCamera>();
|
|
if (followCamera != null)
|
|
followCamera.SetTarget(vehicle, true);
|
|
|
|
RearViewMirrorCamera rearViewCamera = FindAnyObjectByType<RearViewMirrorCamera>();
|
|
if (rearViewCamera != null)
|
|
rearViewCamera.SetTarget(vehicle, true);
|
|
}
|
|
|
|
private static void AssignDamageHud(GameObject vehicle)
|
|
{
|
|
VehicleDamageController damageController = vehicle.GetComponentInChildren<VehicleDamageController>();
|
|
if (damageController == null)
|
|
return;
|
|
|
|
VehicleDamageHud damageHud = FindAnyObjectByType<VehicleDamageHud>();
|
|
if (damageHud != null)
|
|
damageHud.Bind(damageController);
|
|
}
|
|
|
|
private static void DestroyExistingVehicleControllers()
|
|
{
|
|
ArcadeBuggyController[] existingVehicles = FindObjectsByType<ArcadeBuggyController>(FindObjectsInactive.Exclude);
|
|
for (int i = 0; i < existingVehicles.Length; i++)
|
|
{
|
|
if (existingVehicles[i] != null)
|
|
Destroy(existingVehicles[i].gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|