2026-11-19 23:24:44 +03:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class CarPartMountZone : MonoBehaviour, IInteractable
|
|
|
|
|
{
|
|
|
|
|
private CarPart targetPart;
|
|
|
|
|
private Collider zoneCollider;
|
|
|
|
|
private MeshRenderer ghostRenderer;
|
|
|
|
|
private PlayerHands cachedHands;
|
|
|
|
|
private Transform slotParent;
|
|
|
|
|
private Vector3 slotLocalPos;
|
|
|
|
|
private Quaternion slotLocalRot;
|
|
|
|
|
private Vector3 slotLocalScale;
|
|
|
|
|
private WheelCollider slotWheelCollider;
|
2026-11-20 05:08:26 +03:00
|
|
|
|
2026-11-19 23:24:44 +03:00
|
|
|
private string slotName;
|
|
|
|
|
private float mountDuration = 1.2f;
|
|
|
|
|
private float currentMountProgress = 0f;
|
|
|
|
|
private bool hasSeparateBolts = false;
|
|
|
|
|
private CarPartData targetData;
|
|
|
|
|
private Mesh targetMesh;
|
|
|
|
|
private bool acceptsByData;
|
2026-11-20 05:08:26 +03:00
|
|
|
private Bounds targetBoundsInParent;
|
|
|
|
|
private bool hasTargetBounds;
|
2026-11-19 23:24:44 +03:00
|
|
|
|
|
|
|
|
public CarPartData TargetData => targetData;
|
|
|
|
|
public CarPart TargetPart => targetPart;
|
|
|
|
|
public string SlotName => slotName;
|
|
|
|
|
public Vector3 SlotWorldPosition => slotParent != null ? slotParent.TransformPoint(slotLocalPos) : transform.position;
|
|
|
|
|
|
|
|
|
|
public string TargetTitle
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (targetPart != null) return targetPart.DisplayName;
|
|
|
|
|
if (targetData != null) return targetData.partName;
|
|
|
|
|
return slotName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2026-11-20 05:08:26 +03:00
|
|
|
|
2026-11-19 23:24:44 +03:00
|
|
|
targetPart = part;
|
|
|
|
|
targetData = part.Data;
|
|
|
|
|
gameObject.layer = part.gameObject.layer;
|
|
|
|
|
acceptsByData = part.GetComponent<EnginePart>() != null;
|
|
|
|
|
MeshFilter tmf = part.GetComponentInChildren<MeshFilter>(true);
|
|
|
|
|
targetMesh = tmf != null ? tmf.sharedMesh : null;
|
2026-11-20 05:08:26 +03:00
|
|
|
hasTargetBounds = TryComputeBoundsInSpace(part.transform, slotParent, out targetBoundsInParent);
|
2026-11-19 23:24:44 +03:00
|
|
|
|
|
|
|
|
cachedHands = FindAnyObjectByType<PlayerHands>();
|
|
|
|
|
hasSeparateBolts = part.GetComponentsInChildren<WheelBolt>(true).Length > 0;
|
|
|
|
|
|
|
|
|
|
if (part.Data != null && part.Data.boltDuration > 0f)
|
|
|
|
|
mountDuration = part.Data.boltDuration;
|
|
|
|
|
|
|
|
|
|
MeshFilter sourceFilter = part.GetComponentInChildren<MeshFilter>();
|
|
|
|
|
|
|
|
|
|
if (hasSeparateBolts)
|
|
|
|
|
{
|
|
|
|
|
SphereCollider sphere = gameObject.AddComponent<SphereCollider>();
|
|
|
|
|
if (sourceFilter != null && sourceFilter.sharedMesh != null)
|
|
|
|
|
sphere.center = sourceFilter.sharedMesh.bounds.center;
|
|
|
|
|
sphere.radius = 0.48f;
|
|
|
|
|
sphere.isTrigger = true;
|
|
|
|
|
zoneCollider = sphere;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sourceFilter != null && sourceFilter.sharedMesh != null)
|
|
|
|
|
{
|
|
|
|
|
MeshFilter mf = gameObject.AddComponent<MeshFilter>();
|
|
|
|
|
mf.sharedMesh = sourceFilter.sharedMesh;
|
|
|
|
|
|
|
|
|
|
ghostRenderer = gameObject.AddComponent<MeshRenderer>();
|
|
|
|
|
ghostRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
|
|
|
|
|
|
|
|
|
|
Material sharedGhostMat = part.Data != null ? part.Data.ghostMaterial : null;
|
|
|
|
|
if (sharedGhostMat != null)
|
|
|
|
|
{
|
|
|
|
|
ghostRenderer.sharedMaterial = sharedGhostMat;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Material fallbackMat = new Material(Shader.Find("Universal Render Pipeline/Unlit"));
|
|
|
|
|
fallbackMat.color = new Color(0.2f, 0.9f, 0.3f, 0.4f);
|
|
|
|
|
ghostRenderer.material = fallbackMat;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UpdateZoneVisibility();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
UpdateZoneVisibility();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateZoneVisibility()
|
|
|
|
|
{
|
|
|
|
|
if (cachedHands == null)
|
|
|
|
|
cachedHands = FindAnyObjectByType<PlayerHands>();
|
|
|
|
|
|
|
|
|
|
bool isHoldingTarget = IsHoldingCompatiblePart();
|
|
|
|
|
|
|
|
|
|
if (zoneCollider != null && zoneCollider.enabled != isHoldingTarget)
|
|
|
|
|
zoneCollider.enabled = isHoldingTarget;
|
|
|
|
|
|
|
|
|
|
if (ghostRenderer != null && ghostRenderer.enabled != isHoldingTarget)
|
|
|
|
|
ghostRenderer.enabled = isHoldingTarget;
|
|
|
|
|
|
|
|
|
|
if (!isHoldingTarget)
|
|
|
|
|
currentMountProgress = 0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetPrompt()
|
|
|
|
|
{
|
|
|
|
|
if (cachedHands == null || !IsHoldingCompatiblePart())
|
|
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
|
|
string partTitle = TargetTitle;
|
|
|
|
|
|
|
|
|
|
if (hasSeparateBolts)
|
|
|
|
|
{
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.E) || Input.GetMouseButtonDown(0))
|
|
|
|
|
{
|
|
|
|
|
CompleteInstall();
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
return $"[E / ЛКМ] Надеть {partTitle} на ступицу";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Input.GetKey(KeyCode.E))
|
|
|
|
|
{
|
|
|
|
|
currentMountProgress += Time.deltaTime;
|
|
|
|
|
if (currentMountProgress >= mountDuration)
|
|
|
|
|
{
|
|
|
|
|
CompleteInstall();
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int percent = Mathf.RoundToInt(Mathf.Clamp01(currentMountProgress / mountDuration) * 100f);
|
|
|
|
|
return $"Закручиваем болты: {partTitle}... {percent}%";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
currentMountProgress = 0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $"[Удерживать E] Установить {partTitle}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Interact(GameObject interactor)
|
|
|
|
|
{
|
|
|
|
|
if (hasSeparateBolts && cachedHands != null && IsHoldingCompatiblePart())
|
|
|
|
|
CompleteInstall();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CompleteInstall()
|
|
|
|
|
{
|
|
|
|
|
if (cachedHands == null || !IsHoldingCompatiblePart()) return;
|
|
|
|
|
|
|
|
|
|
CarPart partToInstall = cachedHands.CurrentPart;
|
|
|
|
|
cachedHands.ClearHandAfterInstall();
|
|
|
|
|
|
2026-11-20 05:08:26 +03:00
|
|
|
MeshFilter hmf = partToInstall.GetComponentInChildren<MeshFilter>(true);
|
|
|
|
|
Mesh heldMesh = hmf != null ? hmf.sharedMesh : null;
|
|
|
|
|
bool exactSameModel = partToInstall == targetPart || (targetMesh != null && heldMesh == targetMesh);
|
|
|
|
|
|
|
|
|
|
if (exactSameModel || !hasTargetBounds || hasSeparateBolts || slotParent == null)
|
|
|
|
|
{
|
|
|
|
|
partToInstall.InstallIntoZone(
|
|
|
|
|
slotParent,
|
|
|
|
|
slotLocalPos,
|
|
|
|
|
slotLocalRot,
|
|
|
|
|
slotLocalScale,
|
|
|
|
|
slotWheelCollider,
|
|
|
|
|
slotName
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Страховка на случай установки другой по форме модели: выравниваем по габаритам и дну слота
|
|
|
|
|
Vector3 worldScale = partToInstall.transform.lossyScale;
|
|
|
|
|
Transform carRoot = GetComponentInParent<VehicleInteraction>()?.transform ?? slotParent;
|
|
|
|
|
|
|
|
|
|
partToInstall.transform.SetParent(slotParent, true);
|
|
|
|
|
partToInstall.transform.rotation = carRoot.rotation;
|
|
|
|
|
Vector3 pScale = slotParent.lossyScale;
|
|
|
|
|
partToInstall.transform.localScale = new Vector3(
|
|
|
|
|
worldScale.x / Mathf.Max(Mathf.Abs(pScale.x), 0.0001f),
|
|
|
|
|
worldScale.y / Mathf.Max(Mathf.Abs(pScale.y), 0.0001f),
|
|
|
|
|
worldScale.z / Mathf.Max(Mathf.Abs(pScale.z), 0.0001f));
|
|
|
|
|
|
|
|
|
|
if (TryComputeBoundsInSpace(partToInstall.transform, slotParent, out Bounds curB))
|
|
|
|
|
{
|
|
|
|
|
float fitX = curB.size.x > 0.001f ? targetBoundsInParent.size.x / curB.size.x : 1f;
|
|
|
|
|
float fitY = curB.size.y > 0.001f ? targetBoundsInParent.size.y / curB.size.y : 1f;
|
|
|
|
|
float fitZ = curB.size.z > 0.001f ? targetBoundsInParent.size.z / curB.size.z : 1f;
|
|
|
|
|
float fit = Mathf.Min(1f, fitX, fitY, fitZ);
|
|
|
|
|
partToInstall.transform.localScale *= fit;
|
|
|
|
|
|
|
|
|
|
TryComputeBoundsInSpace(partToInstall.transform, slotParent, out curB);
|
|
|
|
|
Vector3 targetBottom = new Vector3(targetBoundsInParent.center.x, targetBoundsInParent.min.y, targetBoundsInParent.center.z);
|
|
|
|
|
Vector3 curBottom = new Vector3(curB.center.x, curB.min.y, curB.center.z);
|
|
|
|
|
partToInstall.transform.localPosition += (targetBottom - curBottom);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
partToInstall.InstallIntoZone(
|
|
|
|
|
slotParent,
|
|
|
|
|
partToInstall.transform.localPosition,
|
|
|
|
|
partToInstall.transform.localRotation,
|
|
|
|
|
partToInstall.transform.localScale,
|
|
|
|
|
slotWheelCollider,
|
|
|
|
|
slotName
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-11-19 23:24:44 +03:00
|
|
|
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
}
|
2026-11-20 05:08:26 +03:00
|
|
|
|
|
|
|
|
private static bool TryComputeBoundsInSpace(Transform root, Transform space, out Bounds result)
|
|
|
|
|
{
|
|
|
|
|
result = default;
|
|
|
|
|
if (root == null || space == null) return false;
|
|
|
|
|
bool has = false;
|
|
|
|
|
foreach (MeshFilter mf in root.GetComponentsInChildren<MeshFilter>(true))
|
|
|
|
|
{
|
|
|
|
|
if (mf.sharedMesh == null) continue;
|
|
|
|
|
Bounds mb = mf.sharedMesh.bounds;
|
|
|
|
|
Vector3 e = mb.extents;
|
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
|
{
|
|
|
|
|
Vector3 corner = mb.center + new Vector3(
|
|
|
|
|
i % 2 == 0 ? -e.x : e.x,
|
|
|
|
|
(i / 2) % 2 == 0 ? -e.y : e.y,
|
|
|
|
|
i / 4 == 0 ? -e.z : e.z);
|
|
|
|
|
Vector3 p = space.InverseTransformPoint(mf.transform.TransformPoint(corner));
|
|
|
|
|
if (!has) { result = new Bounds(p, Vector3.zero); has = true; }
|
|
|
|
|
else result.Encapsulate(p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return has;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 23:24:44 +03:00
|
|
|
private bool IsHoldingCompatiblePart()
|
|
|
|
|
{
|
|
|
|
|
if (cachedHands == null || !cachedHands.IsHolding || cachedHands.CurrentPart == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
CarPart held = cachedHands.CurrentPart;
|
|
|
|
|
if (held == targetPart) return true;
|
|
|
|
|
|
|
|
|
|
if ((hasSeparateBolts || acceptsByData) && held.Data != null && held.Data == targetData)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (targetMesh != null && held.Data != null && held.Data == targetData)
|
|
|
|
|
{
|
|
|
|
|
MeshFilter hmf = held.GetComponentInChildren<MeshFilter>(true);
|
|
|
|
|
if (hmf != null && hmf.sharedMesh == targetMesh) return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|