feat (interaction): Add PressurePlateButton and WallInteractButton classes with interaction logic
This commit is contained in:
109
Assets/Code/Scripts/Interaction/WallInteractButton.cs
Normal file
109
Assets/Code/Scripts/Interaction/WallInteractButton.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
[RequireComponent(typeof(Collider))]
|
||||
public class WallInteractButton : MonoBehaviour
|
||||
{
|
||||
[Header("Interaction")]
|
||||
[Tooltip("Optional Input Action. If empty, fallback key will be used.")]
|
||||
[SerializeField] private InputActionReference interactAction;
|
||||
|
||||
[Tooltip("Used only if no Input Action is assigned.")]
|
||||
[SerializeField] private Key fallbackKey = Key.E;
|
||||
|
||||
[SerializeField] private bool oneShot = false;
|
||||
|
||||
[Header("Prompt")]
|
||||
[SerializeField] private TMP_Text promptText;
|
||||
[SerializeField] private string promptMessage = "Press E to interact";
|
||||
|
||||
[Header("Events")]
|
||||
public UnityEvent OnInteract;
|
||||
|
||||
private bool m_playerInRange;
|
||||
private bool m_hasInteracted;
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
Collider col = GetComponent<Collider>();
|
||||
col.isTrigger = true;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (interactAction != null)
|
||||
{
|
||||
interactAction.action.Enable();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (interactAction != null)
|
||||
{
|
||||
interactAction.action.Disable();
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
UpdatePrompt(false);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!m_playerInRange)
|
||||
return;
|
||||
|
||||
if (oneShot && m_hasInteracted)
|
||||
return;
|
||||
|
||||
if (WasInteractPressed())
|
||||
{
|
||||
m_hasInteracted = true;
|
||||
OnInteract?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (!IsPlayer(other))
|
||||
return;
|
||||
|
||||
m_playerInRange = true;
|
||||
UpdatePrompt(true);
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (!IsPlayer(other))
|
||||
return;
|
||||
|
||||
m_playerInRange = false;
|
||||
UpdatePrompt(false);
|
||||
}
|
||||
|
||||
private bool WasInteractPressed()
|
||||
{
|
||||
if (interactAction != null)
|
||||
return interactAction.action.WasPressedThisFrame() || interactAction.action.WasPerformedThisFrame();
|
||||
|
||||
return Keyboard.current != null && Keyboard.current[fallbackKey].wasPressedThisFrame;
|
||||
}
|
||||
|
||||
private bool IsPlayer(Collider other)
|
||||
{
|
||||
return other.CompareTag("Player") || other.GetComponentInParent<PlayerMovement>() != null;
|
||||
}
|
||||
|
||||
private void UpdatePrompt(bool visible)
|
||||
{
|
||||
if (promptText == null)
|
||||
return;
|
||||
|
||||
promptText.text = promptMessage;
|
||||
promptText.gameObject.SetActive(visible);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user