150 lines
5.3 KiB
C#
150 lines
5.3 KiB
C#
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// Посадка/высадка игрока (клавиша F), управление машиной и внешняя камера CarOrbitCamera.
|
|
/// Двигатель заводится при первом нажатии газа (если исправны АКБ и свечи).
|
|
/// </summary>
|
|
public class VehicleInteraction : MonoBehaviour, IInteractable
|
|
{
|
|
[Header("Setup")]
|
|
[SerializeField] private Transform exitPoint;
|
|
[SerializeField] private UIManager uiManager;
|
|
|
|
/// <summary>Машина, в которой сейчас сидит игрок (null — игрок пешком).</summary>
|
|
public static VehicleInteraction Current { get; private set; }
|
|
|
|
private CarOrbitCamera globalCarCamera;
|
|
private CarController carController;
|
|
private GameObject playerInstance;
|
|
[HideInInspector] public bool isPlayerInside;
|
|
private int enteredFrame = -1;
|
|
|
|
public CarController Controller => carController;
|
|
public Transform ExitPoint => exitPoint;
|
|
|
|
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
|
private static void ResetStatics() => Current = null;
|
|
|
|
private void Awake()
|
|
{
|
|
carController = GetComponent<CarController>();
|
|
if (carController == null) carController = GetComponentInChildren<CarController>();
|
|
if (carController == null) carController = GetComponentInParent<CarController>();
|
|
if (uiManager == null) uiManager = FindAnyObjectByType<UIManager>();
|
|
globalCarCamera = FindAnyObjectByType<CarOrbitCamera>(FindObjectsInactive.Include);
|
|
|
|
if (exitPoint == null)
|
|
{
|
|
foreach (Transform t in GetComponentsInChildren<Transform>(true))
|
|
{
|
|
string n = t.name.Replace(" ", "").ToLowerInvariant();
|
|
if (n == "exitpoint") { exitPoint = t; break; }
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Interact(GameObject interactor)
|
|
{
|
|
if (!isPlayerInside) EnterVehicle(interactor);
|
|
}
|
|
|
|
public string GetPrompt() => "[F] Сесть в машину";
|
|
|
|
private void EnterVehicle(GameObject player)
|
|
{
|
|
if (player == null) return;
|
|
|
|
// Нельзя садиться за руль с деталью или канистрой в руках — кладем её на землю
|
|
PlayerHands hands = player.GetComponentInChildren<PlayerHands>();
|
|
if (hands != null) hands.DropEverything();
|
|
|
|
isPlayerInside = true;
|
|
enteredFrame = Time.frameCount;
|
|
Current = this;
|
|
playerInstance = player;
|
|
playerInstance.SetActive(false);
|
|
|
|
if (globalCarCamera == null) globalCarCamera = FindAnyObjectByType<CarOrbitCamera>(FindObjectsInactive.Include);
|
|
if (globalCarCamera != null)
|
|
{
|
|
globalCarCamera.SetTarget(transform);
|
|
globalCarCamera.gameObject.SetActive(true);
|
|
}
|
|
|
|
if (carController != null) carController.SetDriver(true);
|
|
|
|
if (uiManager != null)
|
|
{
|
|
uiManager.HidePrompt();
|
|
uiManager.ToggleDashboard(true);
|
|
}
|
|
|
|
GameHUD.Notify("[W] — завести и ехать, [Space] — ручник, [L] — фары, [F] — выйти", GameUI.Muted);
|
|
}
|
|
|
|
public void ExitVehicle()
|
|
{
|
|
if (!isPlayerInside) return;
|
|
isPlayerInside = false;
|
|
if (Current == this) Current = null;
|
|
if (carController != null) carController.SetDriver(false);
|
|
|
|
if (globalCarCamera != null)
|
|
{
|
|
globalCarCamera.SetTarget(null);
|
|
globalCarCamera.gameObject.SetActive(false);
|
|
}
|
|
|
|
if (uiManager != null) uiManager.ToggleDashboard(false);
|
|
|
|
if (playerInstance == null) return;
|
|
|
|
CharacterController charController = playerInstance.GetComponent<CharacterController>();
|
|
if (charController != null) charController.enabled = false;
|
|
|
|
Vector3 pos = exitPoint != null
|
|
? exitPoint.position
|
|
: transform.position - transform.right * 1.8f + Vector3.up * 0.5f;
|
|
if (Physics.Raycast(pos + Vector3.up * 1.5f, Vector3.down, out RaycastHit hit, 4f, ~0, QueryTriggerInteraction.Ignore)
|
|
&& !hit.collider.transform.IsChildOf(transform))
|
|
pos.y = hit.point.y + 1.0f;
|
|
|
|
playerInstance.transform.position = pos;
|
|
playerInstance.transform.rotation = Quaternion.Euler(0f, transform.eulerAngles.y, 0f);
|
|
|
|
if (charController != null) charController.enabled = true;
|
|
|
|
playerInstance.SetActive(true);
|
|
playerInstance = null;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!isPlayerInside) return;
|
|
|
|
if (GameInputLock.IsLocked)
|
|
{
|
|
if (carController != null) carController.ProcessInput(0f, 0f, true);
|
|
return;
|
|
}
|
|
|
|
if (carController != null)
|
|
{
|
|
float vertical = Input.GetAxis("Vertical");
|
|
float horizontal = Input.GetAxis("Horizontal");
|
|
bool isBraking = Input.GetKey(KeyCode.Space);
|
|
|
|
carController.ProcessInput(vertical, horizontal, isBraking);
|
|
if (uiManager != null) uiManager.UpdateSpeed(carController.SpeedKmH);
|
|
}
|
|
|
|
if (Time.frameCount != enteredFrame && (Input.GetKeyDown(KeyCode.F) || Input.GetKeyDown(KeyCode.E)))
|
|
ExitVehicle();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (isPlayerInside) ExitVehicle();
|
|
}
|
|
}
|