TimerComponent.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. namespace KYFramework;
  2. public enum TimerClass
  3. {
  4. None,
  5. OnceTimer,
  6. OnceWaitTimer,
  7. RepeatedTimer,
  8. }
  9. public class TimerAction
  10. {
  11. public static TimerAction Create(long id, TimerClass timerClass, long startTime, long time, int type, object obj)
  12. {
  13. TimerAction timerAction = Game.ObjectPool.Fetch<TimerAction>();
  14. timerAction.Id = id;
  15. timerAction.TimerClass = timerClass;
  16. timerAction.StartTime = startTime;
  17. timerAction.Object = obj;
  18. timerAction.Time = time;
  19. timerAction.Type = type;
  20. return timerAction;
  21. }
  22. public long Id;
  23. public TimerClass TimerClass;
  24. public object Object;
  25. public long StartTime;
  26. public long Time;
  27. public int Type;
  28. public void Recycle()
  29. {
  30. this.Id = 0;
  31. this.Object = null;
  32. this.StartTime = 0;
  33. this.Time = 0;
  34. this.TimerClass = TimerClass.None;
  35. this.Type = 0;
  36. Game.ObjectPool.Recycle(this);
  37. }
  38. }
  39. public struct TimerCallback
  40. {
  41. public object Args;
  42. }
  43. public class TimerComponent : Component
  44. {
  45. public static TimerComponent Instance;
  46. /// <summary>
  47. /// key: time, value: timer id
  48. /// </summary>
  49. public readonly MultiMap<long, long> TimeId = new();
  50. public readonly Queue<long> timeOutTime = new();
  51. public readonly Queue<long> timeOutTimerIds = new();
  52. public readonly Dictionary<long, TimerAction> timerActions = new();
  53. public long idGenerator;
  54. // 记录最小时间,不用每次都去MultiMap取第一个值
  55. public long minTime = long.MaxValue;
  56. }