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.

Repeatable.cs 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /// </summary>
  13. public class Repeatable : ISchedulable
  14. {
  15. /// <summary>
  16. /// Determines if the task should continue to repeat
  17. /// </summary>
  18. /// <returns>Whether the task should run again (true) or end (false)</returns>
  19. public delegate bool ShouldContinue();
  20. private ShouldContinue shouldContinue;
  21. private Action task;
  22. private float delay;
  23. public IEnumerator<TaskContract> Run()
  24. {
  25. while (shouldContinue())
  26. {
  27. task();
  28. yield return new WaitForSecondsEnumerator(delay).Continue();
  29. }
  30. yield return Yield.It;
  31. }
  32. /// <summary>
  33. /// Construct a repeating task
  34. /// </summary>
  35. /// <param name="task">The task to repeat</param>
  36. /// <param name="shouldContinue">The check to determine if the task should run again</param>
  37. /// <param name="delay">The time to wait between repeats (in seconds)</param>
  38. public Repeatable(Action task, ShouldContinue shouldContinue, float delay = 0.0f)
  39. {
  40. this.task = task;
  41. this.shouldContinue = shouldContinue;
  42. this.delay = delay;
  43. }
  44. /// <summary>
  45. /// Construct a repeating task
  46. /// </summary>
  47. /// <param name="task">The task to repeat</param>
  48. /// <param name="count">The amount of times to repeat</param>
  49. /// <param name="delay">The time to wait between repeats (in seconds)</param>
  50. public Repeatable(Action task, int count, float delay = 0.0f)
  51. {
  52. this.task = task;
  53. this.shouldContinue = () => { return count-- != 0; };
  54. this.delay = delay;
  55. }
  56. }
  57. }