A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1KB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Svelto.Tasks;
  7. using Svelto.Tasks.Enumerators;
  8. namespace GamecraftModdingAPI.Tasks
  9. {
  10. /// <summary>
  11. /// An asynchronous repeating task.
  12. /// Once constructed, this can be run by scheduling it with Scheduler.Schedule()
  13. /// </summary>
  14. public class Repeatable : ISchedulable
  15. {
  16. /// <summary>
  17. /// Determines if the task should continue to repeat
  18. /// </summary>
  19. /// <returns>Whether the task should run again (true) or end (false)</returns>
  20. public delegate bool ShouldContinue();
  21. private ShouldContinue shouldContinue;
  22. private Action task;
  23. private float delay;
  24. public IEnumerator<TaskContract> Run()
  25. {
  26. while (shouldContinue())
  27. {
  28. task();
  29. yield return new WaitForSecondsEnumerator(delay).Continue();
  30. }
  31. yield return Yield.It;
  32. }
  33. /// <summary>
  34. /// Construct a repeating task
  35. /// </summary>
  36. /// <param name="task">The task to repeat</param>
  37. /// <param name="shouldContinue">The check to determine if the task should run again</param>
  38. /// <param name="delay">The time to wait between repeats (in seconds)</param>
  39. public Repeatable(Action task, ShouldContinue shouldContinue, float delay = 0.0f)
  40. {
  41. this.task = task;
  42. this.shouldContinue = shouldContinue;
  43. this.delay = delay;
  44. }
  45. /// <summary>
  46. /// Construct a repeating task
  47. /// </summary>
  48. /// <param name="task">The task to repeat</param>
  49. /// <param name="count">The amount of times to repeat</param>
  50. /// <param name="delay">The time to wait between repeats (in seconds)</param>
  51. public Repeatable(Action task, int count, float delay = 0.0f)
  52. {
  53. this.task = task;
  54. this.shouldContinue = () => { return count-- != 0; };
  55. this.delay = delay;
  56. }
  57. }
  58. }