namespace Zinnia.Haptics { using System; using System.Collections; using UnityEngine; using Zinnia.Extension; /// /// Processes a given repeatedly for a given duration and with a pause interval between each process. /// public class TimedHapticProcess : HapticProcess { [Tooltip("The process to utilize.")] [SerializeField] private HapticProcess hapticProcess; /// /// The process to utilize. /// public HapticProcess HapticProcess { get { return hapticProcess; } set { hapticProcess = value; } } [Tooltip("The amount of time to keep repeating the process for.")] [SerializeField] private float duration = 1f; /// /// The amount of time to keep repeating the process for. /// public float Duration { get { return duration; } set { duration = value; } } [Tooltip("The amount of time to pause after each process iteration.")] [SerializeField] private float interval = 0.1f; /// /// The amount of time to pause after each process iteration. /// public float Interval { get { return interval; } set { interval = value; if (this.IsMemberChangeAllowed()) { OnAfterIntervalChange(); } } } /// /// A reference to the started routine. /// protected Coroutine hapticRoutine; /// /// Delays the by seconds. /// protected WaitForSeconds delayYieldInstruction; /// public override bool IsActive() { return base.IsActive() && HapticProcess != null && HapticProcess.IsActive(); } protected virtual void OnEnable() { OnAfterIntervalChange(); } /// /// Starts the haptic routine. /// protected override void DoBegin() { hapticRoutine = StartCoroutine(HapticProcessRoutine()); } /// /// Cancels any current running haptic routine. /// protected override void DoCancel() { if (hapticRoutine == null) { return; } StopCoroutine(hapticRoutine); hapticRoutine = null; HapticProcess.Cancel(); } /// /// Enumerates for the specified duration calling with a specified interval delay between each call. /// /// An Enumerator to manage the running of the Coroutine. protected virtual IEnumerator HapticProcessRoutine() { if (Interval <= 0) { yield break; } float currentDuration = Duration; while (currentDuration > 0) { HapticProcess.Begin(); yield return delayYieldInstruction; currentDuration -= Interval; } } /// /// Called after has been changed. /// protected virtual void OnAfterIntervalChange() { delayYieldInstruction = new WaitForSeconds(Interval); } [Obsolete("Use `OnAfterIntervalChange` instead.")] protected virtual void OnAfterCheckDelayChange() { OnAfterIntervalChange(); } } }