54 lines
1.4 KiB
C#
54 lines
1.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
namespace Interaction
|
|
{
|
|
public class InteractCountThresholdSystem : MonoBehaviour
|
|
{
|
|
[SerializeField] private int threshold = 1;
|
|
[SerializeField] private bool triggerOnlyOnce = true;
|
|
[SerializeField] private bool resetCountOnTrigger = false;
|
|
[SerializeField] private UnityEvent onThresholdReached;
|
|
|
|
[SerializeField] private int currentCount = 0;
|
|
[SerializeField] private bool hasTriggered = false;
|
|
|
|
public int CurrentCount => currentCount;
|
|
public int Threshold => threshold;
|
|
public bool HasTriggered => hasTriggered;
|
|
|
|
public void Add(int amount)
|
|
{
|
|
if (amount <= 0) return;
|
|
if (threshold <= 0) return;
|
|
if (triggerOnlyOnce && hasTriggered) return;
|
|
|
|
var before = currentCount;
|
|
currentCount += amount;
|
|
|
|
if (before < threshold && currentCount >= threshold)
|
|
{
|
|
hasTriggered = true;
|
|
onThresholdReached?.Invoke();
|
|
|
|
if (resetCountOnTrigger)
|
|
{
|
|
currentCount = 0;
|
|
hasTriggered = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ResetState()
|
|
{
|
|
currentCount = 0;
|
|
hasTriggered = false;
|
|
}
|
|
|
|
public void SetCount(int value)
|
|
{
|
|
currentCount = Mathf.Max(0, value);
|
|
}
|
|
}
|
|
}
|