Files
Perekyp/Assets/Scripts/Car/Part/CarPart.cs
T

426 lines
15 KiB
C#

using UnityEngine;
public enum PartState { Installed, InHand, Dropped }
public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
{
[Header("Данные")]
[SerializeField] private CarPartData partData;
[SerializeField] private float mass = 12f;
[Header("Позиционирование в руках")]
[SerializeField] private Vector3 inHandPositionOffset = Vector3.zero;
[SerializeField] private Vector3 inHandRotationOffset = Vector3.zero;
private CarPartAnimator partAnimator;
private PartState currentState = PartState.Installed;
private Transform originalParent;
private Vector3 originalLocalPos;
private Quaternion originalLocalRot;
private Vector3 originalLocalScale;
private WheelBolt[] wheelBolts;
private WheelCollider linkedWheelCollider;
private Transform currentHoldPoint;
private Vector3 targetInHandLocalPos;
private Quaternion targetInHandLocalRot;
// Родная сторона меша колеса (-1 = левое, +1 = правое)
private float nativeWheelSideSign = 0f;
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);
public void ResetUnbolt() => unboltProgress = 0f;
public CarPartData Data => partData;
public PartState State => currentState;
public bool HasAnimator => partAnimator != null;
public int TotalBoltsCount => wheelBolts != null ? wheelBolts.Length : 0;
public WheelCollider LinkedWheelCollider => linkedWheelCollider;
public WheelBrickSupport SpawnedBrickSupport { get; private set; }
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;
}
}
private void Awake()
{
partCollider = GetComponent<Collider>();
partAnimator = GetComponent<CarPartAnimator>();
wheelBolts = GetComponentsInChildren<WheelBolt>(true);
originalParent = transform.parent;
originalLocalPos = transform.localPosition;
originalLocalRot = transform.localRotation;
originalLocalScale = transform.localScale;
// Определяем родную сторону модели колеса по его имени или позиции
DetectNativeWheelSide();
VehicleInteraction car = GetComponentInParent<VehicleInteraction>();
if (car != null)
{
foreach (WheelCollider wc in car.GetComponentsInChildren<WheelCollider>(true))
{
if (wc.name == gameObject.name)
{
linkedWheelCollider = wc;
break;
}
}
if (linkedWheelCollider != null && partCollider != null)
{
if (partCollider is MeshCollider mc) mc.convex = true;
partCollider.isTrigger = true;
}
}
else
{
InitializeAsLoosePart();
}
}
private void DetectNativeWheelSide()
{
string n = gameObject.name.ToUpperInvariant();
if (n.Contains("_FL") || n.Contains("_RL") || n.Contains("_L"))
nativeWheelSideSign = -1f;
else if (n.Contains("_FR") || n.Contains("_RR") || n.Contains("_R"))
nativeWheelSideSign = 1f;
else if (Mathf.Abs(originalLocalPos.x) > 0.1f)
nativeWheelSideSign = Mathf.Sign(originalLocalPos.x);
else
nativeWheelSideSign = -1f;
}
public void InitializeAsLoosePart()
{
currentState = PartState.Dropped;
if (wheelBolts != null)
{
foreach (var b in wheelBolts)
{
if (b == null) continue;
b.SetTightenedImmediate(false);
Collider bc = b.GetComponent<Collider>();
if (bc != null) bc.enabled = false;
}
}
if (partCollider != null)
{
if (partCollider is MeshCollider mc) mc.convex = true;
partCollider.isTrigger = false;
partCollider.enabled = true;
}
SetupDroppedRigidbody();
}
private void SetupDroppedRigidbody()
{
if (rb == null) rb = GetComponent<Rigidbody>();
if (rb == null) rb = gameObject.AddComponent<Rigidbody>();
rb.mass = mass;
rb.interpolation = RigidbodyInterpolation.Interpolate;
rb.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
// Демпфирование полностью убирает мелкую дрожь колеса на асфальте
rb.linearDamping = 1.2f;
rb.angularDamping = 3.0f;
}
private void LateUpdate()
{
if (currentState == PartState.InHand && currentHoldPoint != null)
{
transform.localPosition = targetInHandLocalPos;
transform.localRotation = targetInHandLocalRot;
}
}
public string GetPrompt()
{
if (partData == null) return "Деталь (E)";
if (currentState == PartState.Installed)
{
if (wheelBolts != null && wheelBolts.Length > 0)
{
if (!CarJack.IsWheelLifted(transform))
return "Колесо под нагрузкой! Поднимите эту сторону домкратом";
if (TightenedBoltsCount > 0)
return $"Сначала открутите болты ({TightenedBoltsCount}/{wheelBolts.Length})";
return $"[E] Снять {partData.partName}";
}
if (unboltProgress > 0f)
return $"Откручивание болтов ({partData.partName})... {Mathf.RoundToInt(UnboltProgress01 * 100f)}%";
if (partAnimator != null)
{
string action = partAnimator.IsOpen ? "Закрыть" : "Открыть";
return $"[E] {action} {partData.partName} | [Удерж. R] Снять";
}
return $"[Удерж. E] Снять {partData.partName}";
}
if (currentState == PartState.Dropped)
return $"[E] Взять {partData.partName}";
return string.Empty;
}
public void Interact(GameObject interactor)
{
if (currentState == PartState.Installed)
{
if (partAnimator != null)
partAnimator.TogglePart();
else if (wheelBolts != null && wheelBolts.Length > 0 && TightenedBoltsCount == 0 && CarJack.IsWheelLifted(transform))
TakeToHands(interactor);
}
else if (currentState == PartState.Dropped)
{
TakeToHands(interactor);
}
}
public void SecondaryInteract(GameObject interactor) { }
public void ProcessUnbolting(GameObject interactor, float deltaTime)
{
if (currentState != PartState.Installed) return;
if (wheelBolts != null && wheelBolts.Length > 0)
{
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;
TakeToHands(interactor);
}
}
private void TakeToHands(GameObject interactor)
{
PlayerHands hands = interactor.GetComponentInChildren<PlayerHands>() ?? interactor.GetComponentInParent<PlayerHands>();
if (hands == null || hands.IsHolding) return;
if (currentState == PartState.Installed)
{
if (partAnimator != null) partAnimator.ResetToClosed();
CreateMountZone();
}
hands.PickUp(this);
}
public void OnPickedUp(Transform holdPoint)
{
currentState = PartState.InHand;
currentHoldPoint = holdPoint;
unboltProgress = 0f;
if (rb != null) Destroy(rb);
if (partCollider != null) partCollider.enabled = false;
if (wheelBolts != null)
{
foreach (var b in wheelBolts)
if (b != null && b.TryGetComponent<Collider>(out var bc)) bc.enabled = false;
}
if (linkedWheelCollider != null)
linkedWheelCollider.enabled = false;
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;
}
targetInHandLocalPos = transform.localPosition;
targetInHandLocalRot = transform.localRotation;
}
public void OnDropped(Vector3 throwForce)
{
currentState = PartState.Dropped;
unboltProgress = 0f;
if (currentHoldPoint != null)
transform.position = currentHoldPoint.position + currentHoldPoint.forward * 0.5f + Vector3.up * 0.1f;
currentHoldPoint = null;
transform.SetParent(null);
transform.localScale = originalLocalScale;
// Выключаем коллайдеры дочерних болтов, чтобы колесо на земле не дрожало!
if (wheelBolts != null)
{
foreach (var b in wheelBolts)
if (b != null && b.TryGetComponent<Collider>(out var bc)) bc.enabled = false;
}
if (partCollider != null)
{
if (partCollider is MeshCollider meshCol) meshCol.convex = true;
partCollider.isTrigger = false;
partCollider.enabled = true;
}
SetupDroppedRigidbody();
if (throwForce != Vector3.zero)
rb.AddForce(throwForce, ForceMode.Impulse);
}
public void Install()
{
currentState = PartState.Installed;
currentHoldPoint = null;
unboltProgress = 0f;
if (rb != null) Destroy(rb);
transform.SetParent(originalParent);
transform.localPosition = originalLocalPos;
transform.localRotation = originalLocalRot;
transform.localScale = originalLocalScale;
if (partCollider != null)
{
partCollider.enabled = true;
if (linkedWheelCollider != null)
partCollider.isTrigger = true;
}
// Сначала обновляем радиус WheelCollider (чтобы хорошее колесо вернуло полный радиус!), и только потом включаем его
CarWheelCondition cond = GetComponent<CarWheelCondition>();
if (cond != null) cond.UpdateWheelColliderPhysics();
if (linkedWheelCollider != null)
{
linkedWheelCollider.enabled = true;
linkedWheelCollider.motorTorque = 0f;
linkedWheelCollider.brakeTorque = 0f;
}
if (wheelBolts != null)
{
foreach (var b in wheelBolts)
{
if (b != null) b.RecalculateOutwardDirection();
}
}
}
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;
currentMountZone.transform.localScale = originalLocalScale;
CarPartMountZone zone = currentMountZone.AddComponent<CarPartMountZone>();
zone.Initialize(this);
}
public void MakeMissingAtStart(GameObject customBricksPrefab = null, bool enableBrickPhysics = true)
{
if (currentState != PartState.Installed) return;
CreateMountZone();
// Кирпичи ставим ТОЛЬКО если отсутствующая деталь — это колесо!
if (linkedWheelCollider != null)
{
linkedWheelCollider.enabled = false;
if (enableBrickPhysics)
SpawnedBrickSupport = WheelBrickSupport.CreateUnderWheel(this, customBricksPrefab);
}
currentState = PartState.Dropped;
gameObject.SetActive(false);
}
public void InstallIntoZone(
Transform newParent,
Vector3 slotLocalPos,
Quaternion slotLocalRot,
Vector3 slotLocalScale,
WheelCollider newWheelCol,
string slotWheelName)
{
originalParent = newParent;
originalLocalPos = slotLocalPos;
originalLocalScale = slotLocalScale;
linkedWheelCollider = newWheelCol;
gameObject.name = slotWheelName;
// Определяем сторону ступицы, куда ставится колесо (-1 = левая, +1 = правая)
float targetHubSideSign = slotLocalPos.x < 0f ? -1f : 1f;
if (Mathf.Abs(slotLocalPos.x) < 0.05f && newWheelCol != null)
targetHubSideSign = newWheelCol.transform.localPosition.x < 0f ? -1f : 1f;
// Разворачиваем на 180 градусов ТОЛЬКО если левую модель колеса ставят на правую сторону (или наоборот)
bool needsFlip = (nativeWheelSideSign * targetHubSideSign) < 0f;
originalLocalRot = needsFlip ? (slotLocalRot * Quaternion.Euler(0f, 180f, 0f)) : slotLocalRot;
Install();
if (wheelBolts != null)
{
foreach (var b in wheelBolts)
{
if (b == null) continue;
b.SetTightenedImmediate(false);
b.RecalculateOutwardDirection();
}
}
CarController controller = GetComponentInParent<CarController>();
if (controller != null && linkedWheelCollider != null)
{
controller.ReplaceWheelMesh(linkedWheelCollider, transform);
}
}
}