Система со сменой колес дорабавтываю. Есть задатки системы объявлений и встречи с продавцом

This commit is contained in:
bybohka committed 2026-10-23 02:59:47 +03:00
1 parent 2ce853999d
commit 27ad7eac11
25 files changed
+37130 -34404

No files matched your search

+154 -11
View File
@@ -25,6 +25,9 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
private Vector3 targetInHandLocalPos;
private Quaternion targetInHandLocalRot;
// Родная сторона меша колеса (-1 = левое, +1 = правое)
private float nativeWheelSideSign = 0f;
private Rigidbody rb;
private Collider partCollider;
private GameObject currentMountZone;
@@ -38,6 +41,8 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
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
{
@@ -62,6 +67,9 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
originalLocalRot = transform.localRotation;
originalLocalScale = transform.localScale;
// Определяем родную сторону модели колеса по его имени или позиции
DetectNativeWheelSide();
VehicleInteraction car = GetComponentInParent<VehicleInteraction>();
if (car != null)
{
@@ -73,15 +81,68 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
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;
}
}
// ВАЖНО: Если это колесо, на машине его MeshCollider ДОЛЖЕН быть триггером!
// Иначе он трется об асфальт и крылья машины, мешая WheelCollider'у ехать!
if (linkedWheelCollider != null && partCollider != null)
if (partCollider != null)
{
if (partCollider is MeshCollider mc) mc.convex = true;
partCollider.isTrigger = 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()
@@ -102,7 +163,7 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
if (wheelBolts != null && wheelBolts.Length > 0)
{
if (!CarJack.IsWheelLifted(transform))
return "Колесо под нагрузкой! Поднимите авто домкратом";
return "Колесо под нагрузкой! Поднимите эту сторону домкратом";
if (TightenedBoltsCount > 0)
return $"Сначала открутите болты ({TightenedBoltsCount}/{wheelBolts.Length})";
@@ -190,6 +251,12 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
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;
@@ -217,22 +284,27 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
unboltProgress = 0f;
if (currentHoldPoint != null)
transform.position = currentHoldPoint.position + currentHoldPoint.forward * 0.45f;
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.isTrigger = false;
partCollider.enabled = true;
}
rb = gameObject.AddComponent<Rigidbody>();
rb.mass = mass;
rb.interpolation = RigidbodyInterpolation.Interpolate;
SetupDroppedRigidbody();
if (throwForce != Vector3.zero)
rb.AddForce(throwForce, ForceMode.Impulse);
@@ -254,17 +326,28 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
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()
@@ -280,4 +363,64 @@ public class CarPart : MonoBehaviour, IInteractable, ISecondaryInteractable
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);
}
}
}
+44 -6
View File
@@ -6,13 +6,26 @@ public class CarPartMountZone : MonoBehaviour, IInteractable
private Collider zoneCollider;
private MeshRenderer ghostRenderer;
private PlayerHands cachedHands;
private Transform slotParent;
private Vector3 slotLocalPos;
private Quaternion slotLocalRot;
private Vector3 slotLocalScale;
private WheelCollider slotWheelCollider;
private string slotName;
private float mountDuration = 1.2f;
private float currentMountProgress = 0f;
private bool hasSeparateBolts = false;
public void Initialize(CarPart part)
{
slotParent = part.transform.parent;
slotLocalPos = part.transform.localPosition;
slotLocalRot = part.transform.localRotation;
slotLocalScale = part.transform.localScale;
slotWheelCollider = part.LinkedWheelCollider;
slotName = part.gameObject.name;
targetPart = part;
gameObject.layer = part.gameObject.layer;
@@ -96,7 +109,7 @@ public class CarPartMountZone : MonoBehaviour, IInteractable
if (cachedHands == null)
cachedHands = FindAnyObjectByType<PlayerHands>();
bool isHoldingTarget = cachedHands != null && cachedHands.IsHolding && cachedHands.CurrentPart == targetPart;
bool isHoldingTarget = IsHoldingCompatiblePart();
if (zoneCollider != null && zoneCollider.enabled != isHoldingTarget)
zoneCollider.enabled = isHoldingTarget;
@@ -111,7 +124,7 @@ public class CarPartMountZone : MonoBehaviour, IInteractable
public string GetPrompt()
{
if (targetPart == null) return string.Empty;
if (cachedHands == null || !cachedHands.IsHolding || cachedHands.CurrentPart != targetPart)
if (cachedHands == null || !IsHoldingCompatiblePart())
return string.Empty;
string partTitle = targetPart.Data != null ? targetPart.Data.partName : "Деталь";
@@ -149,7 +162,7 @@ public class CarPartMountZone : MonoBehaviour, IInteractable
public void Interact(GameObject interactor)
{
if (hasSeparateBolts && cachedHands != null && cachedHands.IsHolding && cachedHands.CurrentPart == targetPart)
if (hasSeparateBolts && cachedHands != null && IsHoldingCompatiblePart())
{
CompleteInstall();
}
@@ -157,10 +170,35 @@ public class CarPartMountZone : MonoBehaviour, IInteractable
private void CompleteInstall()
{
if (cachedHands == null || targetPart == null) return;
if (cachedHands == null || !IsHoldingCompatiblePart()) return;
CarPart partToInstall = cachedHands.CurrentPart;
cachedHands.ClearHandAfterInstall();
targetPart.Install();
partToInstall.InstallIntoZone(
slotParent,
slotLocalPos,
slotLocalRot,
slotLocalScale,
slotWheelCollider,
slotName
);
Destroy(gameObject);
}
private bool IsHoldingCompatiblePart()
{
if (cachedHands == null || !cachedHands.IsHolding || cachedHands.CurrentPart == null)
return false;
CarPart held = cachedHands.CurrentPart;
if (held == targetPart) return true;
// Разрешаем установить любое колесо-дубликат с тем же ScriptableObject CarPartData (например, "Wheel")
if (hasSeparateBolts && held.Data != null && targetPart != null && held.Data == targetPart.Data)
return true;
return false;
}
}