/* * SPDX-License-Identifier: AGPL-3.0-or-later * Copyright (C) 2025 Sergej Görzen * This file is part of OmiLAXR. */ using System; using System.Collections; using UnityEngine; namespace OmiLAXR.Schedules { /// /// A scheduler that executes a single delayed action after a specified timeout. /// Useful for deferring execution rather than repeating it periodically. /// [CreateAssetMenu( fileName = "TimeoutTicker", menuName = "OmiLAXR/Scheduler/TimeoutTicker", order = 2)] public sealed class TimeoutTicker : Scheduler { [Tooltip("Time in seconds before the action is triggered."), Min(0.01f)] public float timeoutSeconds = 1.0f; /// /// Runtime initialization for the TimeoutTicker. /// public void Init( MonoBehaviour owner, float timeoutSeconds, Action onTick, Action onTickStart = null, Action onTickEnd = null, bool runImmediate = false) { this.timeoutSeconds = timeoutSeconds; // Timeout should not tick immediately, otherwise it fires twice: // once immediately (RunInternal) and once after timeout. tickImmediate = false; base.Init(owner, onTick, onTickStart, onTickEnd, runImmediate); } /// /// Executes the scheduled action once after the configured timeout. /// protected override IEnumerator Run() { yield return new WaitForSeconds(timeoutSeconds); if (!isActive) yield break; TriggerOnTick(); // One-shot scheduler: stop after the tick so OnTickEnd fires exactly once (via Stop()). Stop(); } /// /// Factory for programmatic creation. /// public static TimeoutTicker Create( MonoBehaviour owner, float timeoutSeconds, Action onTick, Action onTickStart = null, Action onTickEnd = null, bool runImmediate = false) { var ticker = CreateInstance(); ticker.Init(owner, timeoutSeconds, onTick, onTickStart, onTickEnd, runImmediate); return ticker; } } }