2026-09-30 03:22:34 +03:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
public enum PartState { Installed, InHand, Dropped }
|
|
|
|
|
|
|
2026-10-05 05:00:16 +03:00
|
|
|
|
public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
|
2026-09-30 03:22:34 +03:00
|
|
|
|
{
|
|
|
|
|
|
[Header("Данные")]
|
|
|
|
|
|
[SerializeField] private CarPartData partData;
|
|
|
|
|
|
[SerializeField] private float mass = 12f;
|
2026-10-20 17:54:31 +03:00
|
|
|
|
|
2026-09-30 03:22:34 +03:00
|
|
|
|
[Header("Позиционирование в руках")]
|
|
|
|
|
|
[SerializeField] private Vector3 inHandPositionOffset = Vector3.zero;
|
|
|
|
|
|
[SerializeField] private Vector3 inHandRotationOffset = Vector3.zero;
|
2026-10-20 17:54:31 +03:00
|
|
|
|
|
2026-10-05 05:00:16 +03:00
|
|
|
|
private CarPartAnimator partAnimator;
|
2026-09-30 03:22:34 +03:00
|
|
|
|
private PartState currentState = PartState.Installed;
|
|
|
|
|
|
private Transform originalParent;
|
|
|
|
|
|
private Vector3 originalLocalPos;
|
|
|
|
|
|
private Quaternion originalLocalRot;
|
|
|
|
|
|
private Vector3 originalLocalScale;
|
2026-10-16 02:45:36 +03:00
|
|
|
|
private WheelBolt[] wheelBolts;
|
2026-09-30 03:22:34 +03:00
|
|
|
|
|
2026-10-16 02:45:36 +03:00
|
|
|
|
private WheelCollider linkedWheelCollider;
|
|
|
|
|
|
private Transform currentHoldPoint;
|
|
|
|
|
|
private Vector3 targetInHandLocalPos;
|
|
|
|
|
|
private Quaternion targetInHandLocalRot;
|
2026-09-30 03:22:34 +03:00
|
|
|
|
|
2026-10-20 17:54:31 +03:00
|
|
|
|
private Rigidbody rb;
|
|
|
|
|
|
private Collider partCollider;
|
|
|
|
|
|
private GameObject currentMountZone;
|
|
|
|
|
|
|
|
|
|
|
|
private float unboltDuration => (partData != null && partData.boltDuration > 0f) ? partData.boltDuration : 1.2f;
|
|
|
|
|
|
private float unboltProgress = 0f;
|
|
|
|
|
|
|
|
|
|
|
|
public float UnboltProgress01 => Mathf.Clamp01(unboltProgress / unboltDuration);
|
2026-10-16 02:45:36 +03:00
|
|
|
|
public void ResetUnbolt() => unboltProgress = 0f;
|
2026-09-30 03:22:34 +03:00
|
|
|
|
public CarPartData Data => partData;
|
|
|
|
|
|
public PartState State => currentState;
|
2026-10-20 17:54:31 +03:00
|
|
|
|
public bool HasAnimator => partAnimator != null;
|
2026-10-16 02:45:36 +03:00
|
|
|
|
public int TotalBoltsCount => wheelBolts != null ? wheelBolts.Length : 0;
|
|
|
|
|
|
|
2026-10-20 17:54:31 +03:00
|
|
|
|
public int TightenedBoltsCount
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (wheelBolts == null || wheelBolts.Length == 0) return 0;
|
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
foreach (var b in wheelBolts)
|
|
|
|
|
|
if (b != null && b.IsTightened) count++;
|
|
|
|
|
|
return count;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-30 03:22:34 +03:00
|
|
|
|
private void Awake()
|
|
|
|
|
|
{
|
|
|
|
|
|
partCollider = GetComponent<Collider>();
|
2026-10-05 05:00:16 +03:00
|
|
|
|
partAnimator = GetComponent<CarPartAnimator>();
|
2026-10-16 02:45:36 +03:00
|
|
|
|
wheelBolts = GetComponentsInChildren<WheelBolt>(true);
|
2026-10-20 17:54:31 +03:00
|
|
|
|
|
2026-09-30 03:22:34 +03:00
|
|
|
|
originalParent = transform.parent;
|
|
|
|
|
|
originalLocalPos = transform.localPosition;
|
|
|
|
|
|
originalLocalRot = transform.localRotation;
|
|
|
|
|
|
originalLocalScale = transform.localScale;
|
2026-10-16 02:45:36 +03:00
|
|
|
|
|
|
|
|
|
|
VehicleInteraction car = GetComponentInParent<VehicleInteraction>();
|
|
|
|
|
|
if (car != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (WheelCollider wc in car.GetComponentsInChildren<WheelCollider>(true))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (wc.name == gameObject.name)
|
|
|
|
|
|
{
|
|
|
|
|
|
linkedWheelCollider = wc;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-10-20 17:54:31 +03:00
|
|
|
|
|
|
|
|
|
|
// ВАЖНО: Если это колесо, на машине его MeshCollider ДОЛЖЕН быть триггером!
|
|
|
|
|
|
// Иначе он трется об асфальт и крылья машины, мешая WheelCollider'у ехать!
|
|
|
|
|
|
if (linkedWheelCollider != null && partCollider != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (partCollider is MeshCollider mc) mc.convex = true;
|
|
|
|
|
|
partCollider.isTrigger = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void LateUpdate()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (currentState == PartState.InHand && currentHoldPoint != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
transform.localPosition = targetInHandLocalPos;
|
|
|
|
|
|
transform.localRotation = targetInHandLocalRot;
|
|
|
|
|
|
}
|
2026-09-30 03:22:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public string GetPrompt()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (partData == null) return "Деталь (E)";
|
|
|
|
|
|
|
2026-10-05 05:00:16 +03:00
|
|
|
|
if (currentState == PartState.Installed)
|
2026-09-30 03:22:34 +03:00
|
|
|
|
{
|
2026-10-20 17:54:31 +03:00
|
|
|
|
if (wheelBolts != null && wheelBolts.Length > 0)
|
2026-10-16 02:45:36 +03:00
|
|
|
|
{
|
2026-10-20 17:54:31 +03:00
|
|
|
|
if (!CarJack.IsWheelLifted(transform))
|
|
|
|
|
|
return "Колесо под нагрузкой! Поднимите авто домкратом";
|
|
|
|
|
|
|
|
|
|
|
|
if (TightenedBoltsCount > 0)
|
|
|
|
|
|
return $"Сначала открутите болты ({TightenedBoltsCount}/{wheelBolts.Length})";
|
|
|
|
|
|
|
|
|
|
|
|
return $"[E] Снять {partData.partName}";
|
2026-10-16 02:45:36 +03:00
|
|
|
|
}
|
2026-10-20 17:54:31 +03:00
|
|
|
|
|
2026-10-16 02:45:36 +03:00
|
|
|
|
if (unboltProgress > 0f)
|
|
|
|
|
|
return $"Откручивание болтов ({partData.partName})... {Mathf.RoundToInt(UnboltProgress01 * 100f)}%";
|
2026-10-20 17:54:31 +03:00
|
|
|
|
|
2026-10-05 05:00:16 +03:00
|
|
|
|
if (partAnimator != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
string action = partAnimator.IsOpen ? "Закрыть" : "Открыть";
|
2026-10-20 17:54:31 +03:00
|
|
|
|
return $"[E] {action} {partData.partName} | [Удерж. R] Снять";
|
2026-10-05 05:00:16 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-20 17:54:31 +03:00
|
|
|
|
return $"[Удерж. E] Снять {partData.partName}";
|
2026-10-05 05:00:16 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (currentState == PartState.Dropped)
|
2026-10-20 17:54:31 +03:00
|
|
|
|
return $"[E] Взять {partData.partName}";
|
|
|
|
|
|
|
2026-10-05 05:00:16 +03:00
|
|
|
|
return string.Empty;
|
2026-09-30 03:22:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Interact(GameObject interactor)
|
2026-10-05 05:00:16 +03:00
|
|
|
|
{
|
|
|
|
|
|
if (currentState == PartState.Installed)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (partAnimator != null)
|
|
|
|
|
|
partAnimator.TogglePart();
|
2026-10-20 17:54:31 +03:00
|
|
|
|
else if (wheelBolts != null && wheelBolts.Length > 0 && TightenedBoltsCount == 0 && CarJack.IsWheelLifted(transform))
|
2026-10-05 05:00:16 +03:00
|
|
|
|
TakeToHands(interactor);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (currentState == PartState.Dropped)
|
|
|
|
|
|
{
|
|
|
|
|
|
TakeToHands(interactor);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-20 17:54:31 +03:00
|
|
|
|
public void SecondaryInteract(GameObject interactor) { }
|
|
|
|
|
|
|
|
|
|
|
|
public void ProcessUnbolting(GameObject interactor, float deltaTime)
|
2026-10-05 05:00:16 +03:00
|
|
|
|
{
|
2026-10-20 17:54:31 +03:00
|
|
|
|
if (currentState != PartState.Installed) return;
|
|
|
|
|
|
|
|
|
|
|
|
if (wheelBolts != null && wheelBolts.Length > 0)
|
2026-10-05 05:00:16 +03:00
|
|
|
|
{
|
2026-10-20 17:54:31 +03:00
|
|
|
|
if (!CarJack.IsWheelLifted(transform) || TightenedBoltsCount > 0) return;
|
|
|
|
|
|
TakeToHands(interactor);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PlayerToolManager toolMgr = interactor.GetComponentInChildren<PlayerToolManager>();
|
|
|
|
|
|
if (toolMgr != null && toolMgr.currentTool != ToolType.Hands) return;
|
|
|
|
|
|
|
|
|
|
|
|
unboltProgress += deltaTime;
|
|
|
|
|
|
if (unboltProgress >= unboltDuration)
|
|
|
|
|
|
{
|
|
|
|
|
|
unboltProgress = 0f;
|
2026-10-05 05:00:16 +03:00
|
|
|
|
TakeToHands(interactor);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void TakeToHands(GameObject interactor)
|
2026-09-30 03:22:34 +03:00
|
|
|
|
{
|
2026-10-20 17:54:31 +03:00
|
|
|
|
PlayerHands hands = interactor.GetComponentInChildren<PlayerHands>() ?? interactor.GetComponentInParent<PlayerHands>();
|
|
|
|
|
|
if (hands == null || hands.IsHolding) return;
|
2026-09-30 03:22:34 +03:00
|
|
|
|
|
|
|
|
|
|
if (currentState == PartState.Installed)
|
|
|
|
|
|
{
|
2026-10-05 05:00:16 +03:00
|
|
|
|
if (partAnimator != null) partAnimator.ResetToClosed();
|
2026-09-30 03:22:34 +03:00
|
|
|
|
CreateMountZone();
|
|
|
|
|
|
}
|
2026-10-05 05:00:16 +03:00
|
|
|
|
|
|
|
|
|
|
hands.PickUp(this);
|
2026-09-30 03:22:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void OnPickedUp(Transform holdPoint)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentState = PartState.InHand;
|
2026-10-16 02:45:36 +03:00
|
|
|
|
currentHoldPoint = holdPoint;
|
2026-10-20 17:54:31 +03:00
|
|
|
|
unboltProgress = 0f;
|
2026-09-30 03:22:34 +03:00
|
|
|
|
|
|
|
|
|
|
if (rb != null) Destroy(rb);
|
2026-10-16 02:45:36 +03:00
|
|
|
|
if (partCollider != null) partCollider.enabled = false;
|
|
|
|
|
|
|
|
|
|
|
|
if (linkedWheelCollider != null)
|
|
|
|
|
|
linkedWheelCollider.enabled = false;
|
2026-09-30 03:22:34 +03:00
|
|
|
|
|
|
|
|
|
|
transform.SetParent(holdPoint);
|
|
|
|
|
|
transform.localScale = originalLocalScale;
|
|
|
|
|
|
transform.localRotation = Quaternion.Euler(inHandRotationOffset);
|
|
|
|
|
|
transform.localPosition = Vector3.zero;
|
|
|
|
|
|
|
|
|
|
|
|
Renderer meshRenderer = GetComponentInChildren<Renderer>();
|
|
|
|
|
|
if (meshRenderer != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Vector3 visualCenterWorld = meshRenderer.bounds.center;
|
|
|
|
|
|
Vector3 offsetToCenter = holdPoint.position - visualCenterWorld;
|
|
|
|
|
|
transform.position += offsetToCenter;
|
|
|
|
|
|
transform.localPosition += inHandPositionOffset;
|
|
|
|
|
|
}
|
2026-10-16 02:45:36 +03:00
|
|
|
|
|
|
|
|
|
|
targetInHandLocalPos = transform.localPosition;
|
|
|
|
|
|
targetInHandLocalRot = transform.localRotation;
|
2026-09-30 03:22:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void OnDropped(Vector3 throwForce)
|
|
|
|
|
|
{
|
|
|
|
|
|
currentState = PartState.Dropped;
|
2026-10-20 17:54:31 +03:00
|
|
|
|
unboltProgress = 0f;
|
2026-09-30 03:22:34 +03:00
|
|
|
|
|
2026-10-16 02:45:36 +03:00
|
|
|
|
if (currentHoldPoint != null)
|
2026-10-20 17:54:31 +03:00
|
|
|
|
transform.position = currentHoldPoint.position + currentHoldPoint.forward * 0.45f;
|
2026-10-16 02:45:36 +03:00
|
|
|
|
currentHoldPoint = null;
|
|
|
|
|
|
|
2026-09-30 03:22:34 +03:00
|
|
|
|
transform.SetParent(null);
|
|
|
|
|
|
transform.localScale = originalLocalScale;
|
|
|
|
|
|
|
2026-10-16 02:45:36 +03:00
|
|
|
|
if (partCollider != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (partCollider is MeshCollider meshCol) meshCol.convex = true;
|
2026-10-20 17:54:31 +03:00
|
|
|
|
partCollider.isTrigger = false; // На земле деталь твердая
|
|
|
|
|
|
partCollider.enabled = true;
|
2026-10-16 02:45:36 +03:00
|
|
|
|
}
|
2026-09-30 03:22:34 +03:00
|
|
|
|
|
|
|
|
|
|
rb = gameObject.AddComponent<Rigidbody>();
|
|
|
|
|
|
rb.mass = mass;
|
|
|
|
|
|
rb.interpolation = RigidbodyInterpolation.Interpolate;
|
|
|
|
|
|
|
|
|
|
|
|
if (throwForce != Vector3.zero)
|
|
|
|
|
|
rb.AddForce(throwForce, ForceMode.Impulse);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Install()
|
|
|
|
|
|
{
|
|
|
|
|
|
currentState = PartState.Installed;
|
2026-10-16 02:45:36 +03:00
|
|
|
|
currentHoldPoint = null;
|
2026-10-20 17:54:31 +03:00
|
|
|
|
unboltProgress = 0f;
|
2026-09-30 03:22:34 +03:00
|
|
|
|
|
|
|
|
|
|
if (rb != null) Destroy(rb);
|
|
|
|
|
|
|
|
|
|
|
|
transform.SetParent(originalParent);
|
|
|
|
|
|
transform.localPosition = originalLocalPos;
|
|
|
|
|
|
transform.localRotation = originalLocalRot;
|
|
|
|
|
|
transform.localScale = originalLocalScale;
|
|
|
|
|
|
|
2026-10-20 17:54:31 +03:00
|
|
|
|
if (partCollider != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
partCollider.enabled = true;
|
|
|
|
|
|
// Установленное колесо делаем триггером, чтобы оно не мешало физике езды!
|
|
|
|
|
|
if (linkedWheelCollider != null)
|
|
|
|
|
|
partCollider.isTrigger = true;
|
|
|
|
|
|
}
|
2026-10-16 02:45:36 +03:00
|
|
|
|
|
|
|
|
|
|
if (linkedWheelCollider != null)
|
2026-10-20 17:54:31 +03:00
|
|
|
|
{
|
2026-10-16 02:45:36 +03:00
|
|
|
|
linkedWheelCollider.enabled = true;
|
2026-10-20 17:54:31 +03:00
|
|
|
|
linkedWheelCollider.motorTorque = 0f;
|
|
|
|
|
|
linkedWheelCollider.brakeTorque = 0f;
|
|
|
|
|
|
}
|
2026-09-30 03:22:34 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CreateMountZone()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (currentMountZone != null) return;
|
|
|
|
|
|
|
|
|
|
|
|
currentMountZone = new GameObject($"MountZone_{gameObject.name}");
|
|
|
|
|
|
currentMountZone.transform.SetParent(originalParent);
|
|
|
|
|
|
currentMountZone.transform.localPosition = originalLocalPos;
|
|
|
|
|
|
currentMountZone.transform.localRotation = originalLocalRot;
|
2026-10-20 17:54:31 +03:00
|
|
|
|
currentMountZone.transform.localScale = originalLocalScale;
|
2026-09-30 03:22:34 +03:00
|
|
|
|
|
|
|
|
|
|
CarPartMountZone zone = currentMountZone.AddComponent<CarPartMountZone>();
|
|
|
|
|
|
zone.Initialize(this);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|