Perekyp/Assets/Scripts/Car/Part/CarPartMountZone.cs

82 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
public class CarPartMountZone : MonoBehaviour, IInteractable
{
private CarPart targetPart;
private Collider zoneCollider;
public void Initialize(CarPart part)
{
targetPart = part;
gameObject.layer = part.gameObject.layer;
// В точности копируем параметры оригинального коллайдера детали
Collider sourceCollider = part.GetComponent<Collider>();
if (sourceCollider is BoxCollider sourceBox)
{
BoxCollider box = gameObject.AddComponent<BoxCollider>();
box.center = sourceBox.center;
box.size = sourceBox.size;
box.isTrigger = true;
zoneCollider = box;
}
else if (sourceCollider is MeshCollider sourceMesh)
{
MeshCollider meshCol = gameObject.AddComponent<MeshCollider>();
meshCol.sharedMesh = sourceMesh.sharedMesh;
meshCol.convex = true;
meshCol.isTrigger = true;
zoneCollider = meshCol;
}
else
{
// Универсальный запасной вариант
SphereCollider sphere = gameObject.AddComponent<SphereCollider>();
sphere.radius = 0.6f;
sphere.isTrigger = true;
zoneCollider = sphere;
}
}
public string GetPrompt()
{
PlayerHands hands = FindAnyObjectByType<PlayerHands>();
// Показываем надпись ТОЛЬКО если игрок держит именно эту деталь
if (hands != null && hands.IsHolding && hands.CurrentPart == targetPart)
{
return $"Установить {targetPart.Data.partName} (E)";
}
return string.Empty;
}
public void Interact(GameObject interactor)
{
PlayerHands hands = interactor.GetComponent<PlayerHands>();
if (hands == null) hands = interactor.GetComponentInParent<PlayerHands>();
if (hands != null && hands.IsHolding && hands.CurrentPart == targetPart)
{
hands.ClearHandAfterInstall();
targetPart.Install();
Destroy(gameObject); // Убираем зону после установки
}
}
// Рисует зеленый контур зоны в окне Scene во время игры
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
if (zoneCollider is BoxCollider box)
{
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.DrawWireCube(box.center, box.size);
}
else
{
Gizmos.DrawWireSphere(transform.position, 0.5f);
}
}
}