using UnityEngine;
public class PickUpSystem : MonoBehaviour
{
[Header("Ссылки и дальность")]
public Transform holdParent;
public GameObject uiPrompt;
public float range = 3f, throwForce = 400f;
[HideInInspector] public bool hasKey = false; // Карман для ключа
[HideInInspector] public bool hasGasCan = false; // Карман для канистры
private GameObject heldObj;
private Collider playerCollider;
void Start()
{
playerCollider = GetComponent<Collider>();
if (uiPrompt != null) uiPrompt.SetActive(false);
}
void Update()
{
// БРОСОК: Если в правой руке есть фонарик, его можно бросить на G
if (heldObj != null && Input.GetKeyDown(KeyCode.G))
{
ThrowObject();
}
// ВЗАИМОДЕЙСТВИЕ: Луч из глаз Олега работает ВСЕГДА
if (Camera.main == null) return;
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
if (Physics.Raycast(ray, out RaycastHit hit, range))
{
if (hit.transform.TryGetComponent(out Interactable interactableObj))
{
// Если мы смотрим на предмет, который УЖЕ держим в руках, подсказку не показываем
if (hit.transform.gameObject == heldObj)
{
if (uiPrompt != null) uiPrompt.SetActive(false);
return;
}
if (uiPrompt != null) uiPrompt.SetActive(true); // Включаем [E]
if (Input.GetKeyDown(KeyCode.E))
{
interactableObj.Activate(gameObject);
}
}
else { if (uiPrompt != null) uiPrompt.SetActive(false); }
}
else { if (uiPrompt != null) uiPrompt.SetActive(false); }
}
public void GrabObject(GameObject pickUpObj)
{
heldObj = pickUpObj;
if (heldObj.TryGetComponent(out Collider objCollider) && playerCollider != null)
Physics.IgnoreCollision(playerCollider, objCollider, true);
if (heldObj.TryGetComponent(out Rigidbody rb)) { rb.isKinematic = true; rb.useGravity = false; }
heldObj.transform.parent = holdParent;
heldObj.transform.localPosition = Vector3.zero;
heldObj.transform.localRotation = Quaternion.identity;
if (heldObj.TryGetComponent(out PhysicalFlashlight f)) f.isGrabbed = true;
if (uiPrompt != null) uiPrompt.SetActive(false);
}
void ThrowObject()
{
if (heldObj.TryGetComponent(out Rigidbody rb)) { rb.isKinematic = false; rb.useGravity = true; rb.AddForce(Camera.main.transform.forward * throwForce); }
if (heldObj.TryGetComponent(out Collider objCollider) && playerCollider != null) Physics.IgnoreCollision(playerCollider, objCollider, false);
heldObj.transform.parent = null;
if (heldObj.TryGetComponent(out PhysicalFlashlight f)) f.isGrabbed = false;
heldObj = null;
}
}