TickTime.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. public abstract class TimeBase
  6. {
  7. protected bool _start = false;
  8. protected float _LeftTime = 0;
  9. public TimeBase(float time) { _LeftTime = time; _start = false; }
  10. virtual public void Start()
  11. {
  12. _start = true;
  13. }
  14. virtual public void Pause()
  15. {
  16. _start = false;
  17. }
  18. abstract public void Update(float deltaTime, Action cb);
  19. }
  20. public class TickTime: TimeBase
  21. {
  22. private float mtime = 0;
  23. public TickTime(float time) : base(time) { mtime = time; }
  24. override public void Start()
  25. {
  26. base.Start();
  27. _LeftTime = mtime;
  28. }
  29. public override void Update(float deltaTime, Action cb)
  30. {
  31. if (_start)
  32. {
  33. _LeftTime -= deltaTime;
  34. if (_LeftTime <= 0)
  35. {
  36. if(cb != null)
  37. {
  38. cb.Invoke();
  39. }
  40. }
  41. _start = false;
  42. }
  43. }
  44. }
  45. public class RepeatTime : TimeBase
  46. {
  47. private float _PassTime = 0;
  48. public RepeatTime(float time) : base(time) { }
  49. public override void Update(float deltaTime, Action cb)
  50. {
  51. if (_start)
  52. {
  53. _PassTime += deltaTime;
  54. if (_PassTime >= _LeftTime)
  55. {
  56. if (cb != null)
  57. {
  58. cb.Invoke();
  59. }
  60. _PassTime = 0;
  61. }
  62. }
  63. }
  64. }