Files
BABA_YAGA/Assets/Scripts/Player/Generic/Triggers/vSimpleTriggerWithInput.cs

128 lines
3.6 KiB
C#
Raw Normal View History

2026-05-30 09:16:35 +07:00
using Invector;
using Invector.vCharacterController;
using UnityEngine;
using UnityEngine.Events;
2026-06-04 12:42:00 +07:00
using OnlyScove.Scripts;
2026-05-30 09:16:35 +07:00
[vClassHeader("Simple Trigger Input")]
public class vSimpleTriggerWithInput : vSimpleTrigger
{
public InputType inputType = InputType.GetButtonDown;
2026-06-04 12:42:00 +07:00
[HideInInspector]
public InputReader inputReader;
2026-05-30 09:16:35 +07:00
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;
}
2026-06-04 12:42:00 +07:00
if (inputReader == null)
{
inputReader = other.GetComponentInParent<InputReader>();
if (inputReader == null) inputReader = other.GetComponent<InputReader>();
}
if (inputReader == null) return;
2026-05-30 09:16:35 +07:00
// GetButtonDown
if (inputType == InputType.GetButtonDown)
{
2026-06-04 12:42:00 +07:00
if (inputReader.ConsumeInteract())
2026-05-30 09:16:35 +07:00
{
OnPressButton.Invoke();
}
}
2026-06-04 12:42:00 +07:00
// 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)
2026-05-30 09:16:35 +07:00
else if (inputType == InputType.GetDoubleButton)
{
2026-06-04 12:42:00 +07:00
// For now, mapping to single press or custom logic if needed.
if (inputReader.ConsumeInteract())
2026-05-30 09:16:35 +07:00
{
OnPressButton.Invoke();
}
}
// GetButtonTimer (Hold Button)
else if (inputType == InputType.GetButtonTimer)
{
if (_currentInputDelay <= 0)
{
2026-06-04 12:42:00 +07:00
var up = !inputReader.IsInteractHeld;
var t = currentButtonTimer;
2026-05-30 09:16:35 +07:00
2026-06-04 12:42:00 +07:00
if (inputReader.IsInteractHeld)
2026-05-30 09:16:35 +07:00
{
2026-06-04 12:42:00 +07:00
currentButtonTimer += Time.deltaTime;
t = currentButtonTimer / buttonTimer;
2026-05-30 09:16:35 +07:00
UpdateButtonTimer(t);
2026-06-04 12:42:00 +07:00
if (currentButtonTimer >= buttonTimer)
{
_currentInputDelay = inputDelay;
currentButtonTimer = 0;
OnPressButton.Invoke();
}
2026-05-30 09:16:35 +07:00
}
// reset the buttonTimer if you release the button before finishing
2026-06-04 12:42:00 +07:00
if (up && currentButtonTimer > 0)
{
currentButtonTimer = 0;
2026-05-30 09:16:35 +07:00
CancelButtonTimer();
2026-06-04 12:42:00 +07:00
}
2026-05-30 09:16:35 +07:00
}
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>
{
}
}