TimerSystem.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. namespace KYFramework;
  2. [ObjectSystem]
  3. public class TimerAwakeSystem: AwakeSystem<Timer>
  4. {
  5. public override void Awake(Timer self)
  6. {
  7. self.dt1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  8. self.dt = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  9. self.FrameTime = self.ClientNow();
  10. }
  11. }
  12. [ObjectSystem]
  13. public class TimerUpdateSystem : UpdateSystem<Timer>
  14. {
  15. public override void Update(Timer self)
  16. {
  17. self.FrameTime = self.ClientNow();
  18. }
  19. }
  20. public static class TimerSystem
  21. {
  22. /// <summary>
  23. /// 根据时间戳获取时间
  24. /// </summary>
  25. public static DateTime ToDateTime(this Timer self,long timeStamp)
  26. {
  27. return self.dt.AddTicks(timeStamp * 10000);
  28. }
  29. // 线程安全
  30. public static long ClientNow(this Timer self)
  31. {
  32. return (DateTime.UtcNow.Ticks - self.dt1970.Ticks) / 10000;
  33. }
  34. public static long ServerNow(this Timer self)
  35. {
  36. return self.ClientNow() + self.ServerMinusClientTime;
  37. }
  38. public static long ClientFrameTime(this Timer self)
  39. {
  40. return self.FrameTime;
  41. }
  42. public static long ServerFrameTime(this Timer self)
  43. {
  44. return self.FrameTime + self.ServerMinusClientTime;
  45. }
  46. public static long Transition(this Timer self,DateTime d)
  47. {
  48. return (d.Ticks - self.dt.Ticks) / 10000;
  49. }
  50. }