Initial Unity project commit
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user