Files
BABA_YAGA/Assets/Scripts/Player/Generic/Triggers/vSimpleTriggerWithInput.cs
2026-06-04 12:42:00 +07:00

128 lines
3.6 KiB
C#

using Invector;
using Invector.vCharacterController;
using UnityEngine;
using UnityEngine.Events;
using OnlyScove.Scripts;
[vClassHeader("Simple Trigger Input")]
public class vSimpleTriggerWithInput : vSimpleTrigger
{
public InputType inputType = InputType.GetButtonDown;
[HideInInspector]
public InputReader inputReader;
public enum InputType
{
GetButtonDown,
GetDoubleButton,
GetButtonTimer
};
[vHelpBox("Time you have to hold the button *Only for GetButtonTimer*")]
public float buttonTimer = 3f;
[vHelpBox("Add delay to start the input count *Only for GetButtonTimer*")]
public float inputDelay = 0.1f;
[vHelpBox("Time to press the button twice *Only for GetDoubleButton*")]
public float doubleButtomTime = 0.25f;
public float _currentInputDelay;
public float currentButtonTimer;
public UnityEvent OnPressButton;
public UnityEvent OnCancelButtonTimer;
public OnUpdateValue OnUpdateButtonTimer;
void Update()
{
if (!other)
{
_currentInputDelay = inputDelay;
return;
}
if (inputReader == null)
{
inputReader = other.GetComponentInParent<InputReader>();
if (inputReader == null) inputReader = other.GetComponent<InputReader>();
}
if (inputReader == null) return;
// GetButtonDown
if (inputType == InputType.GetButtonDown)
{
if (inputReader.ConsumeInteract())
{
OnPressButton.Invoke();
}
}
// GetDoubleButton (Note: New Input System handles double tap via Interactions,
// but here we can implement a simple version or just skip if not used often)
else if (inputType == InputType.GetDoubleButton)
{
// For now, mapping to single press or custom logic if needed.
if (inputReader.ConsumeInteract())
{
OnPressButton.Invoke();
}
}
// GetButtonTimer (Hold Button)
else if (inputType == InputType.GetButtonTimer)
{
if (_currentInputDelay <= 0)
{
var up = !inputReader.IsInteractHeld;
var t = currentButtonTimer;
if (inputReader.IsInteractHeld)
{
currentButtonTimer += Time.deltaTime;
t = currentButtonTimer / buttonTimer;
UpdateButtonTimer(t);
if (currentButtonTimer >= buttonTimer)
{
_currentInputDelay = inputDelay;
currentButtonTimer = 0;
OnPressButton.Invoke();
}
}
// reset the buttonTimer if you release the button before finishing
if (up && currentButtonTimer > 0)
{
currentButtonTimer = 0;
CancelButtonTimer();
}
}
else
{
_currentInputDelay -= Time.deltaTime;
}
}
}
public void UpdateButtonTimer(float value)
{
if (value != currentButtonTimer)
{
currentButtonTimer = value;
OnUpdateButtonTimer.Invoke(value);
}
}
private void CancelButtonTimer()
{
OnCancelButtonTimer.Invoke();
_currentInputDelay = inputDelay;
UpdateButtonTimer(0);
}
[System.Serializable]
public class OnUpdateValue : UnityEvent<float>
{
}
}