248 lines
9.6 KiB
C#
248 lines
9.6 KiB
C#
using UnityEngine;
|
|
|
|
public class PlayerInteractor : MonoBehaviour
|
|
{
|
|
[Header("Настройки луча")]
|
|
[SerializeField] private float interactRange = 3.2f;
|
|
[SerializeField] private LayerMask interactableLayer;
|
|
|
|
[Header("Клавиши управления")]
|
|
[SerializeField] private KeyCode primaryKey = KeyCode.E;
|
|
[SerializeField] private KeyCode secondaryKey = KeyCode.R;
|
|
[SerializeField] private KeyCode enterVehicleKey = KeyCode.F;
|
|
|
|
[Header("Ссылки")]
|
|
[SerializeField] private UIManager uiManager;
|
|
|
|
private CarPart lastHoveredPart;
|
|
private PlayerHands cachedHands;
|
|
|
|
private void Awake()
|
|
{
|
|
if (uiManager == null) uiManager = FindAnyObjectByType<UIManager>();
|
|
cachedHands = FindAnyObjectByType<PlayerHands>();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
ResetProgressTargets();
|
|
if (uiManager != null) uiManager.HidePrompt();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (CarJack.ActiveJackInHand != null) return;
|
|
|
|
Ray ray = new Ray(transform.position, transform.forward);
|
|
GameObject playerRoot = transform.root.gameObject;
|
|
|
|
// 0. Если в руках деталь — проверяем все зоны установки по лучу (чтобы кузов не перекрывал зону колеса!)
|
|
if (cachedHands == null) cachedHands = FindAnyObjectByType<PlayerHands>();
|
|
if (cachedHands != null && cachedHands.IsHolding)
|
|
{
|
|
RaycastHit[] allHits = Physics.RaycastAll(ray, interactRange, interactableLayer, QueryTriggerInteraction.Collide);
|
|
foreach (var h in allHits)
|
|
{
|
|
CarPartMountZone zone = h.collider.GetComponentInParent<CarPartMountZone>();
|
|
if (zone != null)
|
|
{
|
|
ResetProgressTargets();
|
|
string zPrompt = zone.GetPrompt();
|
|
if (!string.IsNullOrEmpty(zPrompt))
|
|
uiManager?.ShowPrompt(zPrompt);
|
|
else
|
|
uiManager?.HidePrompt();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (Physics.Raycast(ray, out RaycastHit hit, interactRange, interactableLayer, QueryTriggerInteraction.Collide))
|
|
{
|
|
// 1. ТОЧНЫЙ ПОИСК БЛИЖАЙШЕГО К ПРИЦЕЛУ БОЛТА
|
|
WheelBolt bestBolt = FindClosestBoltToRay(ray, hit.point);
|
|
if (bestBolt != null)
|
|
{
|
|
ResetProgressTargets();
|
|
string boltPrompt = bestBolt.GetPrompt();
|
|
if (!string.IsNullOrEmpty(boltPrompt))
|
|
uiManager?.ShowPrompt(boltPrompt);
|
|
else
|
|
uiManager?.HidePrompt();
|
|
return;
|
|
}
|
|
|
|
// 2. ПРИОРИТЕТ ДОМКРАТУ ПОД ПОРОГОМ
|
|
CarJack jack = hit.collider.GetComponentInParent<CarJack>();
|
|
if (jack == null)
|
|
{
|
|
Collider[] nearJackCols = Physics.OverlapSphere(hit.point, 0.22f, interactableLayer, QueryTriggerInteraction.Collide);
|
|
foreach (var c in nearJackCols)
|
|
{
|
|
jack = c.GetComponentInParent<CarJack>();
|
|
if (jack != null) break;
|
|
}
|
|
}
|
|
|
|
if (jack != null)
|
|
{
|
|
ResetProgressTargets();
|
|
uiManager?.ShowPrompt(jack.GetPrompt());
|
|
if (Input.GetKeyDown(primaryKey))
|
|
jack.Interact(playerRoot);
|
|
return;
|
|
}
|
|
|
|
WheelBrickSupport bricks = hit.collider.GetComponentInParent<WheelBrickSupport>();
|
|
if (bricks == null)
|
|
{
|
|
Collider[] nearBrickCols = Physics.OverlapSphere(hit.point, 0.22f, interactableLayer, QueryTriggerInteraction.Collide);
|
|
foreach (var c in nearBrickCols)
|
|
{
|
|
bricks = c.GetComponentInParent<WheelBrickSupport>();
|
|
if (bricks != null) break;
|
|
}
|
|
}
|
|
|
|
if (bricks != null)
|
|
{
|
|
ResetProgressTargets();
|
|
uiManager?.ShowPrompt(bricks.GetPrompt());
|
|
if (Input.GetKeyDown(primaryKey))
|
|
bricks.Interact(playerRoot);
|
|
return;
|
|
}
|
|
|
|
// 3. ОБЫЧНЫЕ ДЕТАЛИ И АВТОМОБИЛЬ
|
|
IInteractable interactable = hit.collider.GetComponentInParent<IInteractable>();
|
|
VehicleInteraction vehicle = hit.collider.GetComponentInParent<VehicleInteraction>();
|
|
CarPart part = hit.collider.GetComponentInParent<CarPart>();
|
|
CarPartMountZone mountZone = hit.collider.GetComponentInParent<CarPartMountZone>();
|
|
|
|
if (part != lastHoveredPart)
|
|
{
|
|
ResetProgressTargets();
|
|
lastHoveredPart = part;
|
|
}
|
|
|
|
if (interactable != null || vehicle != null)
|
|
{
|
|
if (Input.GetKeyDown(enterVehicleKey) && vehicle != null)
|
|
{
|
|
vehicle.Interact(playerRoot);
|
|
return;
|
|
}
|
|
|
|
if (mountZone == null && part != null)
|
|
{
|
|
if (part.State == PartState.Dropped)
|
|
{
|
|
if (Input.GetKeyDown(primaryKey))
|
|
part.Interact(playerRoot);
|
|
}
|
|
else if (part.State == PartState.Installed)
|
|
{
|
|
bool hasAnimator = part.GetComponent<CarPartAnimator>() != null;
|
|
if (hasAnimator)
|
|
{
|
|
if (Input.GetKeyDown(primaryKey))
|
|
part.Interact(playerRoot);
|
|
|
|
if (Input.GetKey(secondaryKey))
|
|
part.ProcessUnbolting(playerRoot, Time.deltaTime);
|
|
else
|
|
part.ResetUnbolt();
|
|
}
|
|
else
|
|
{
|
|
if (Input.GetKey(primaryKey))
|
|
part.ProcessUnbolting(playerRoot, Time.deltaTime);
|
|
else
|
|
part.ResetUnbolt();
|
|
}
|
|
}
|
|
}
|
|
else if (mountZone == null && interactable != null && Input.GetKeyDown(primaryKey))
|
|
{
|
|
interactable.Interact(playerRoot);
|
|
}
|
|
|
|
string finalPrompt = BuildPrompt(vehicle, interactable);
|
|
if (!string.IsNullOrEmpty(finalPrompt))
|
|
uiManager?.ShowPrompt(finalPrompt);
|
|
else
|
|
uiManager?.HidePrompt();
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
ResetProgressTargets();
|
|
uiManager?.HidePrompt();
|
|
}
|
|
|
|
private WheelBolt FindClosestBoltToRay(Ray ray, Vector3 hitPoint)
|
|
{
|
|
WheelBolt closest = null;
|
|
float minRayDist = 0.085f; // Радиус захвата точки прицела (8.5 см вокруг луча)
|
|
|
|
// 1. Ищем колесо в радиусе попадания луча и проверяем ВСЕ его дочерние болты напрямую
|
|
// (работает даже если коллайдер шины перекрывает ступицу!)
|
|
Collider[] nearCols = Physics.OverlapSphere(hitPoint, 0.35f, interactableLayer, QueryTriggerInteraction.Collide);
|
|
foreach (var c in nearCols)
|
|
{
|
|
CarPart part = c.GetComponentInParent<CarPart>();
|
|
if (part != null && part.TotalBoltsCount > 0 && part.State == PartState.Installed)
|
|
{
|
|
foreach (WheelBolt b in part.GetComponentsInChildren<WheelBolt>(true))
|
|
{
|
|
if (b == null || !b.IsAvailableForAim) continue;
|
|
|
|
Vector3 boltPos = b.WorldBoltCenter;
|
|
float distToRay = Vector3.Cross(ray.direction, boltPos - ray.origin).magnitude;
|
|
if (distToRay < minRayDist)
|
|
{
|
|
minRayDist = distToRay;
|
|
closest = b;
|
|
}
|
|
}
|
|
}
|
|
|
|
// На случай если луч попал прямо в коллайдер самого болта
|
|
WheelBolt directBolt = c.GetComponent<WheelBolt>();
|
|
if (directBolt != null && directBolt.IsAvailableForAim)
|
|
{
|
|
float distToRay = Vector3.Cross(ray.direction, directBolt.WorldBoltCenter - ray.origin).magnitude;
|
|
if (distToRay < minRayDist)
|
|
{
|
|
minRayDist = distToRay;
|
|
closest = directBolt;
|
|
}
|
|
}
|
|
}
|
|
|
|
return closest;
|
|
}
|
|
|
|
private string BuildPrompt(VehicleInteraction vehicle, IInteractable interactable)
|
|
{
|
|
string partPrompt = (interactable != null && !(interactable is VehicleInteraction))
|
|
? interactable.GetPrompt()
|
|
: string.Empty;
|
|
|
|
bool canEnter = vehicle != null && !vehicle.isPlayerInside;
|
|
|
|
if (canEnter && !string.IsNullOrEmpty(partPrompt))
|
|
return $"[F] Сесть | {partPrompt}";
|
|
if (canEnter)
|
|
return "[F] Сесть в машину";
|
|
|
|
return partPrompt;
|
|
}
|
|
|
|
private void ResetProgressTargets()
|
|
{
|
|
if (lastHoveredPart != null) lastHoveredPart.ResetUnbolt();
|
|
lastHoveredPart = null;
|
|
}
|
|
} |