DelayHelper.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System.Collections.Generic;
  2. public delegate void DelayTriggeredCallback<T> (T val);
  3. public class FrameDelayInfo<T>
  4. {
  5. public T mValue;
  6. public int mFrame;
  7. public FrameDelayInfo (T val, int frame)
  8. {
  9. mValue = val;
  10. mFrame = frame;
  11. }
  12. }
  13. public class TimeDelayInfo<T>
  14. {
  15. public T mValue;
  16. public float mTime;
  17. public int nSeq;
  18. public TimeDelayInfo (T val, float time,int seq)
  19. {
  20. mValue = val;
  21. mTime = time;
  22. nSeq = seq;
  23. }
  24. }
  25. public class FrameDelayManager<T>
  26. {
  27. List<FrameDelayInfo<T>> mList;
  28. DelayTriggeredCallback<T> mCallback;
  29. public FrameDelayManager (DelayTriggeredCallback<T> cb)
  30. {
  31. mList = new List<FrameDelayInfo<T>> ();
  32. mCallback = cb;
  33. }
  34. public void Update ()
  35. {
  36. for (int i = mList.Count - 1; i >= 0; i--) {
  37. mList [i].mFrame--;
  38. if (mList [i].mFrame <= 0) {
  39. mCallback (mList [i].mValue);
  40. mList.RemoveAt (i);
  41. }
  42. }
  43. }
  44. public void Add (T val, int delayFrame)
  45. {
  46. if (delayFrame <= 0)
  47. mCallback (val);
  48. else
  49. mList.Insert (0, new FrameDelayInfo<T> (val, delayFrame));
  50. }
  51. public void Clear ()
  52. {
  53. mList.Clear ();
  54. }
  55. }
  56. public class TimeDelayManager<T>
  57. {
  58. List<TimeDelayInfo<T>> mList;
  59. DelayTriggeredCallback<T> mCallback;
  60. public TimeDelayManager (DelayTriggeredCallback<T> cb)
  61. {
  62. mList = new List<TimeDelayInfo<T>> ();
  63. mCallback = cb;
  64. }
  65. public void Update (float deltaTime)
  66. {
  67. for (int i = mList.Count - 1; i >= 0; i--) {
  68. mList [i].mTime -= deltaTime;
  69. if (mList [i].mTime <= 0) {
  70. mCallback (mList [i].mValue);
  71. mList.RemoveAt (i);
  72. }
  73. }
  74. }
  75. public void Add (T val, float delayTime)
  76. {
  77. if (delayTime <= 0)
  78. mCallback (val);
  79. else
  80. mList.Insert (0, new TimeDelayInfo<T> (val, delayTime, mList.Count+1));
  81. }
  82. public void Clear ()
  83. {
  84. mList.Clear ();
  85. }
  86. }