6360c7e0d185d497bd73d655c8badec52515d3b6.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, Component, game, StringUtil, Timer, TimerManager, _crd;
  4. function _reportPossibleCrUseOfStringUtil(extras) {
  5. _reporterNs.report("StringUtil", "../../utils/StringUtil", _context.meta, extras);
  6. }
  7. function _reportPossibleCrUseOfTimer(extras) {
  8. _reporterNs.report("Timer", "./Timer", _context.meta, extras);
  9. }
  10. _export("TimerManager", void 0);
  11. return {
  12. setters: [function (_unresolved_) {
  13. _reporterNs = _unresolved_;
  14. }, function (_cc) {
  15. _cclegacy = _cc.cclegacy;
  16. __checkObsolete__ = _cc.__checkObsolete__;
  17. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  18. Component = _cc.Component;
  19. game = _cc.game;
  20. }, function (_unresolved_2) {
  21. StringUtil = _unresolved_2.StringUtil;
  22. }, function (_unresolved_3) {
  23. Timer = _unresolved_3.Timer;
  24. }],
  25. execute: function () {
  26. _crd = true;
  27. _cclegacy._RF.push({}, "73600VLsIBLOKhOhd7td4P8", "TimerManager", undefined);
  28. /*
  29. * @Author: dgflash
  30. * @Date: 2023-01-19 10:33:49
  31. * @LastEditors: dgflash
  32. * @LastEditTime: 2023-01-19 14:37:19
  33. */
  34. __checkObsolete__(['Component', 'game']);
  35. /** 时间管理 */
  36. _export("TimerManager", TimerManager = class TimerManager extends Component {
  37. constructor() {
  38. super(...arguments);
  39. /** 倒计时数据 */
  40. this.times = {};
  41. /** 服务器时间 */
  42. this.date_s = new Date();
  43. /** 服务器初始时间 */
  44. this.date_s_start = new Date();
  45. /** 服务器时间后修正时间 */
  46. this.polymeric_s = 0;
  47. /** 客户端时间 */
  48. this.date_c = new Date();
  49. }
  50. /** 后台管理倒计时完成事件 */
  51. update(dt) {
  52. for (var key in this.times) {
  53. var data = this.times[key];
  54. var timer = data.timer;
  55. if (timer.update(dt)) {
  56. if (data.object[data.field] > 0) {
  57. data.object[data.field]--; // 倒计时结束触发
  58. if (data.object[data.field] == 0) {
  59. this.onTimerComplete(data);
  60. } // 触发每秒回调事件
  61. else if (data.onSecond) {
  62. data.onSecond.call(data.object);
  63. }
  64. }
  65. }
  66. }
  67. }
  68. /** 触发倒计时完成事件 */
  69. onTimerComplete(data) {
  70. if (data.onComplete) data.onComplete.call(data.object);
  71. if (data.event) this.node.dispatchEvent(data.event);
  72. delete this.times[data.id];
  73. }
  74. /**
  75. * 在指定对象上注册一个倒计时的回调管理器
  76. * @param object 注册定时器的对象
  77. * @param field 时间字段
  78. * @param onSecond 每秒事件
  79. * @param onComplete 倒计时完成事件
  80. * @returns
  81. * @example
  82. export class Test extends Component {
  83. private timeId!: string;
  84. start() {
  85. // 在指定对象上注册一个倒计时的回调管理器
  86. this.timeId = oops.timer.register(this, "countDown", this.onSecond, this.onComplete);
  87. }
  88. private onSecond() {
  89. console.log("每秒触发一次");
  90. }
  91. private onComplete() {
  92. console.log("倒计时完成触发");
  93. }
  94. }
  95. */
  96. register(object, field, onSecond, onComplete) {
  97. var timer = new (_crd && Timer === void 0 ? (_reportPossibleCrUseOfTimer({
  98. error: Error()
  99. }), Timer) : Timer)();
  100. timer.step = 1;
  101. var data = {};
  102. data.id = (_crd && StringUtil === void 0 ? (_reportPossibleCrUseOfStringUtil({
  103. error: Error()
  104. }), StringUtil) : StringUtil).guid();
  105. data.timer = timer;
  106. data.object = object; // 管理对象
  107. data.field = field; // 时间字段
  108. data.onSecond = onSecond; // 每秒事件
  109. data.onComplete = onComplete; // 倒计时完成事件
  110. this.times[data.id] = data;
  111. return data.id;
  112. }
  113. /**
  114. * 在指定对象上注销一个倒计时的回调管理器
  115. * @param id 时间对象唯一表示
  116. * @example
  117. export class Test extends Component {
  118. private timeId!: string;
  119. start() {
  120. this.timeId = oops.timer.register(this, "countDown", this.onSecond, this.onComplete);
  121. }
  122. onDestroy() {
  123. // 在指定对象上注销一个倒计时的回调管理器
  124. oops.timer.unRegister(this.timeId);
  125. }
  126. }
  127. */
  128. unRegister(id) {
  129. if (this.times[id]) delete this.times[id];
  130. }
  131. /**
  132. * 服务器时间与本地时间同步
  133. * @param value 服务器时间刻度
  134. */
  135. setServerTime(value) {
  136. this.polymeric_s = this.getTime();
  137. this.date_s_start.setTime(value);
  138. }
  139. /** 获取写服务器同步的时间刻度 */
  140. getServerTime() {
  141. return this.date_s_start.getTime() + this.getTime() - this.polymeric_s;
  142. }
  143. /** 获取服务器时间对象 */
  144. getServerDate() {
  145. this.date_s.setTime(this.getServerTime());
  146. return this.date_s;
  147. }
  148. /** 获取本地时间刻度 */
  149. getClientTime() {
  150. return Date.now();
  151. }
  152. /** 获取本地时间对象 */
  153. getClientDate() {
  154. this.date_c.setTime(this.getClientTime());
  155. return this.date_c;
  156. }
  157. /** 获取游戏开始到现在逝去的时间 */
  158. getTime() {
  159. return game.totalTime;
  160. }
  161. /** 游戏最小化时记录时间数据 */
  162. save() {
  163. for (var key in this.times) {
  164. this.times[key].startTime = this.getTime();
  165. }
  166. }
  167. /** 游戏最大化时回复时间数据 */
  168. load() {
  169. for (var key in this.times) {
  170. var interval = Math.floor((this.getTime() - (this.times[key].startTime || this.getTime())) / 1000);
  171. var data = this.times[key];
  172. data.object[data.field] = data.object[data.field] - interval;
  173. if (data.object[data.field] <= 0) {
  174. data.object[data.field] = 0;
  175. this.onTimerComplete(data);
  176. } else {
  177. this.times[key].startTime = null;
  178. }
  179. }
  180. }
  181. });
  182. _cclegacy._RF.pop();
  183. _crd = false;
  184. }
  185. };
  186. });
  187. //# sourceMappingURL=6360c7e0d185d497bd73d655c8badec52515d3b6.js.map