108 lines
3.3 KiB
C#
108 lines
3.3 KiB
C#
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.InputSystem;
|
||
|
|
|
||
|
|
public class PlayerInteraction : MonoBehaviour
|
||
|
|
{
|
||
|
|
[Header("Beállítások")]
|
||
|
|
public Camera playerCamera;
|
||
|
|
public float interactRange = 3f;
|
||
|
|
public Transform holdPosition; // Egy üres GameObject a kamera előtt, ahova a felvett tárgy ugrik
|
||
|
|
|
||
|
|
[Header("Állapot (Inspectorban látható)")]
|
||
|
|
[SerializeField] private Evidence currentlyHeldEvidence = null;
|
||
|
|
|
||
|
|
private InputMaster input;
|
||
|
|
private IInteractable currentTarget;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
input = new InputMaster();
|
||
|
|
input.Player.Fire.Enable();
|
||
|
|
input.Player.Fire.performed += ctx => OnInteract();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void OnEnable() => input.Enable();
|
||
|
|
private void OnDisable() => input.Disable();
|
||
|
|
|
||
|
|
private void Update()
|
||
|
|
{
|
||
|
|
// 1. LÁTÁS: Raycast a kamera közepéből
|
||
|
|
Ray ray = new Ray(playerCamera.transform.position, playerCamera.transform.forward);
|
||
|
|
RaycastHit hit;
|
||
|
|
|
||
|
|
if (Physics.Raycast(ray, out hit, interactRange))
|
||
|
|
{
|
||
|
|
// Megnézzük, van-e a célpontba vett tárgyon IInteractable (pl. HidingSpot vagy Evidence)
|
||
|
|
IInteractable interactable = hit.collider.GetComponent<IInteractable>();
|
||
|
|
|
||
|
|
if (interactable != null)
|
||
|
|
{
|
||
|
|
currentTarget = interactable;
|
||
|
|
|
||
|
|
// Itt tudod frissíteni a UI-t! Pl:
|
||
|
|
// UIManager.Instance.ShowCrosshairText(currentTarget.GetInteractionText(this));
|
||
|
|
} else
|
||
|
|
{
|
||
|
|
ClearTarget();
|
||
|
|
}
|
||
|
|
} else
|
||
|
|
{
|
||
|
|
ClearTarget();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void ClearTarget()
|
||
|
|
{
|
||
|
|
currentTarget = null;
|
||
|
|
// UIManager.Instance.HideCrosshairText();
|
||
|
|
}
|
||
|
|
|
||
|
|
// 2. CSELEKVÉS: Ezt hívja meg az Input System, ha kattintottál
|
||
|
|
private void OnInteract()
|
||
|
|
{
|
||
|
|
if (currentTarget != null)
|
||
|
|
{
|
||
|
|
// Átadjuk "saját magunkat" (this), hogy a rejtekhely tudja, minket kell vizsgálni
|
||
|
|
currentTarget.Interact(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// 3. INVENTORY RENDSZER (A Rejtekhelyek ezeket hívják meg)
|
||
|
|
|
||
|
|
// Lekéri, mit tartunk a kézben
|
||
|
|
public ItemType GetHeldItemType()
|
||
|
|
{
|
||
|
|
if (currentlyHeldEvidence != null)
|
||
|
|
return currentlyHeldEvidence.type;
|
||
|
|
|
||
|
|
return ItemType.None;
|
||
|
|
}
|
||
|
|
|
||
|
|
public Evidence GetHeldEvidence()
|
||
|
|
{
|
||
|
|
return currentlyHeldEvidence;
|
||
|
|
}
|
||
|
|
|
||
|
|
// A rejtekhely hívja meg, amikor elnyeli a tárgyat
|
||
|
|
public void RemoveHeldItem()
|
||
|
|
{
|
||
|
|
currentlyHeldEvidence = null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// A felvehető tárgyak (Evidence) hívják meg magukon, amikor rákattintasz
|
||
|
|
public void PickUpItem(Evidence newEvidence)
|
||
|
|
{
|
||
|
|
if (currentlyHeldEvidence != null) return; // Már tele van a kezünk
|
||
|
|
|
||
|
|
currentlyHeldEvidence = newEvidence;
|
||
|
|
|
||
|
|
// Vizuálisan a kezünkbe (kamera elé) tesszük a tárgyat
|
||
|
|
currentlyHeldEvidence.transform.SetParent(holdPosition);
|
||
|
|
currentlyHeldEvidence.transform.localPosition = Vector3.zero;
|
||
|
|
currentlyHeldEvidence.transform.localRotation = Quaternion.identity;
|
||
|
|
|
||
|
|
// Kikapcsoljuk a fizikáját, hogy ne essen le
|
||
|
|
if (currentlyHeldEvidence.TryGetComponent(out Rigidbody rb)) rb.isKinematic = true;
|
||
|
|
if (currentlyHeldEvidence.TryGetComponent(out Collider col)) col.enabled = false;
|
||
|
|
}
|
||
|
|
}
|