Moveflow.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace StageController
  8. {
  9. public class Moveflow : List<Workflow>
  10. {
  11. public bool Done => this.All((e) => e.Done == true);
  12. public bool IsStoped = false;
  13. public bool IsPause = false;
  14. private bool Running = false;
  15. public event Action OnMoveflowDone;
  16. public void Start()
  17. {
  18. if (Done || IsStoped)
  19. throw new Exception("AlreadyDone");
  20. if (Running)
  21. throw new Exception("AlreadyStart");
  22. Running = true;
  23. new Task(() =>
  24. {
  25. foreach (var workflow in this)
  26. {
  27. workflow.Process();
  28. while ((!workflow.Judgement() || IsPause) && !IsStoped)
  29. {
  30. Thread.Sleep(50);
  31. }
  32. workflow.Done = true;
  33. if (IsStoped) return;
  34. }
  35. OnMoveflowDone?.Invoke();
  36. }).Start();
  37. }
  38. public void Stop()
  39. {
  40. IsStoped = true;
  41. }
  42. /// <summary>
  43. /// 暂停/继续
  44. /// </summary>
  45. public void Pause()
  46. {
  47. IsPause = !IsPause;
  48. }
  49. public void Add(Action a = null, Func<bool> j = null)
  50. {
  51. var workflow = new Workflow(this);
  52. if (a != null)
  53. workflow.Process = a;
  54. if (j != null)
  55. workflow.Judgement = j;
  56. Add(workflow);
  57. }
  58. }
  59. //public interface IMoveFlow
  60. //{
  61. // public static Moveflow ToCenter()
  62. // {
  63. // }
  64. // public static Moveflow OutStage() { }
  65. // public static Moveflow InStage() { }
  66. // public static Moveflow ResetStage() { }
  67. //}
  68. }