using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Svelto.Tasks; using Svelto.Tasks.Enumerators; namespace GamecraftModdingAPI.Tasks { /// /// An asynchronous repeating task. /// Once constructed, this can be run by scheduling it with Scheduler.Schedule() /// public class Repeatable : ISchedulable { /// /// Determines if the task should continue to repeat /// /// Whether the task should run again (true) or end (false) public delegate bool ShouldContinue(); private ShouldContinue shouldContinue; private Action task; private float delay; public IEnumerator Run() { while (shouldContinue()) { task(); yield return new WaitForSecondsEnumerator(delay).Continue(); } yield return Yield.It; } /// /// Construct a repeating task /// /// The task to repeat /// The check to determine if the task should run again /// The time to wait between repeats (in seconds) public Repeatable(Action task, ShouldContinue shouldContinue, float delay = 0.0f) { this.task = task; this.shouldContinue = shouldContinue; this.delay = delay; } /// /// Construct a repeating task /// /// The task to repeat /// The amount of times to repeat /// The time to wait between repeats (in seconds) public Repeatable(Action task, int count, float delay = 0.0f) { this.task = task; this.shouldContinue = () => { return count-- != 0; }; this.delay = delay; } } }