TimerEngine.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. TagTimerInfo = cc.Class({
  2. ctor :function(){
  3. this.TimeLen = 0;
  4. this.EndTime = 0;
  5. this.CallHandle = null;
  6. this.CallBack = null;
  7. this.Param = null;
  8. this.wChairID = null;
  9. },
  10. });
  11. TimerEngine = cc.Class({
  12. extends: cc.Component,
  13. ctor :function(){
  14. //可执行多个 时间结束回调
  15. this.m_TimerMap = new Object();
  16. //只执行1个 按精度刷新回调
  17. this.m_GameTimerMap = new Object();
  18. this.m_RecordPause = null;
  19. this.m_RecordGamePause = null;
  20. this.m_bStart = false;
  21. },
  22. //检查启动
  23. CheckStart:function(){
  24. if(this.m_bStart) return
  25. this.m_bStart = true;
  26. this.schedule(this.OnTimer, 0.02);
  27. },
  28. //刷新执行
  29. OnTimer :function(){
  30. var NowTime = new Date().getTime();
  31. var RemainTime = 0;
  32. for(var i in this.m_TimerMap){
  33. if(this.m_RecordPause != null) break;
  34. if(this.m_TimerMap[i] == null) continue;
  35. RemainTime = this.m_TimerMap[i].EndTime - NowTime;
  36. if(RemainTime <= 0) {
  37. this.ExecCallBack(this.m_TimerMap[i]);
  38. this.m_TimerMap[i] = null;
  39. }
  40. }
  41. for(var i in this.m_GameTimerMap){
  42. if(this.m_RecordGamePause != null) break;
  43. if(this.m_GameTimerMap[i] == null) continue;
  44. RemainTime = this.m_GameTimerMap[i].EndTime - NowTime;
  45. if(RemainTime < 0) RemainTime = 0;
  46. this.ExecGameCallBack(i, this.m_GameTimerMap[i], RemainTime);
  47. if(RemainTime == 0) this.m_GameTimerMap[i] = null;
  48. }
  49. },
  50. //普通结束执行
  51. ExecCallBack:function(TimerInfo){
  52. try {
  53. if(TimerInfo.CallHandle != null && TimerInfo.CallBack != null){
  54. return TimerInfo.CallHandle[TimerInfo.CallBack](TimerInfo.Param);
  55. }
  56. if(TimerInfo.CallBack != null){
  57. return TimerInfo.CallBack(TimerInfo.Param);
  58. }
  59. } catch (error) {
  60. if(window.LOG_NET_DATA)console.log("timerengine err : "+error)
  61. if(window.LOG_NET_DATA)console.log(JSON.stringify(TimerInfo))
  62. }
  63. },
  64. //游戏刷新执行
  65. ExecGameCallBack:function(TimerID, TimerInfo, RemainTime){
  66. try {
  67. var Progress = RemainTime/TimerInfo.TimeLen;
  68. if(TimerInfo.CallHandle != null && TimerInfo.CallBack != null){
  69. return TimerInfo.CallHandle[TimerInfo.CallBack](TimerInfo.wChairID, RemainTime, parseInt(TimerID), Progress);
  70. }
  71. if(TimerInfo.CallBack != null){
  72. return TimerInfo.CallBack(TimerInfo.wChairID, RemainTime, parseInt(TimerID), Progress);
  73. }
  74. } catch (error) {
  75. if(window.LOG_NET_DATA)console.log("timerengine game err,clock close "+TimerID)
  76. this.m_GameTimerMap[TimerID] = null;
  77. }
  78. },
  79. //设置普通定时器
  80. SetTimer:function(TimerID, TimerLen, Data, Handle, Call){
  81. this.CheckStart();
  82. if(this.m_TimerMap[TimerID] == null){
  83. this.m_TimerMap[TimerID] = new TagTimerInfo();
  84. }
  85. this.m_TimerMap[TimerID].TimeLen = TimerLen;
  86. this.m_TimerMap[TimerID].EndTime = TimerLen + new Date().getTime();
  87. this.m_TimerMap[TimerID].CallHandle = Handle;
  88. this.m_TimerMap[TimerID].CallBack = Call;
  89. this.m_TimerMap[TimerID].Param = Data;
  90. },
  91. //暂停定时器
  92. PauseTimer:function(){
  93. this.m_RecordPause = new Date().getTime();
  94. },
  95. //继续定时器
  96. UnPauseTimer:function(){
  97. if(this.m_RecordPause == null) return;
  98. var PauseTime = new Date().getTime() - this.m_RecordPause;
  99. this.m_RecordPause = null;
  100. for(var i in this.m_TimerMap){
  101. if(this.m_TimerMap[i] == null) continue;
  102. this.m_TimerMap[i].EndTime += PauseTime;
  103. }
  104. },
  105. //删除定时器
  106. KillTimer:function(TimerID){
  107. this.m_TimerMap[TimerID] = null;
  108. },
  109. KillAllTimer:function(){
  110. this.m_TimerMap = new Object();
  111. },
  112. //游戏定时器
  113. SetGameTimer:function(wChairID, TimerID, TimerLen, Data, Handle, Call){
  114. this.CheckStart();
  115. if(this.m_GameTimerMap[TimerID] == null){
  116. this.m_GameTimerMap[TimerID] = new TagTimerInfo();
  117. }
  118. this.m_GameTimerMap[TimerID].TimeLen = TimerLen;
  119. this.m_GameTimerMap[TimerID].EndTime = TimerLen + new Date().getTime();
  120. this.m_GameTimerMap[TimerID].CallHandle = Handle;
  121. this.m_GameTimerMap[TimerID].CallBack = Call;
  122. this.m_GameTimerMap[TimerID].Param = Data;
  123. this.m_GameTimerMap[TimerID].wChairID = wChairID;
  124. },
  125. KillGameTimer:function(){
  126. this.m_GameTimerMap = new Object();
  127. },
  128. PauseGameTimer:function(){
  129. this.m_RecordGamePause = new Date().getTime();
  130. },
  131. UnPauseGameTimer:function(){
  132. if(this.m_RecordGamePause == null) return;
  133. var PauseTime = new Date().getTime() - this.m_RecordGamePause;
  134. this.m_RecordGamePause = null;
  135. for(var i in this.m_GameTimerMap){
  136. if(this.m_GameTimerMap[i] == null) continue;
  137. this.m_GameTimerMap[i].EndTime += PauseTime;
  138. }
  139. },
  140. });
  141. window.g_TimerEngine = new TimerEngine();