TimerSystem.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * 计时器管理类
  3. * @description
  4. * @author 冯聪
  5. */
  6. export default class TimerSystem extends cc.Component {
  7. //参数部分-----------------------------------------------------------------------------------
  8. /**计数器数组 */
  9. public gameTimerArr: GameTimer[] = [];
  10. //逻辑部分-----------------------------------------------------------------------------------
  11. /**开启游戏计时器 */
  12. public startGameTimer() {
  13. this.schedule(() => {
  14. this.runGameTimer();
  15. }, 1);
  16. }
  17. /**运行游戏计时器 */
  18. private runGameTimer() {
  19. for (var i: number = 0; i < this.gameTimerArr.length; i++) {
  20. var gameTimer: GameTimer = this.gameTimerArr[i];
  21. var callBack: Function = gameTimer.callBack;
  22. var object: any = gameTimer.object;
  23. gameTimer.curTime--;
  24. if (gameTimer.curTime <= 0) {
  25. gameTimer.curTime = gameTimer.totalTime;
  26. callBack.call(object);
  27. }
  28. }
  29. }
  30. /**
  31. * 添加游戏计时器
  32. * @description
  33. * @param time 计时的时间
  34. * @param callBack 计时结束之后的回调
  35. * @param object 计时绑定的对象
  36. */
  37. public addGameTimer(time: number, callBack: Function, object: any) {
  38. if (!this.ifHasSameTimer(callBack, object)) {
  39. let gameTimer = new GameTimer();
  40. gameTimer.curTime = time;
  41. gameTimer.totalTime = time;
  42. gameTimer.callBack = callBack;
  43. gameTimer.object = object;
  44. this.gameTimerArr.push(gameTimer);
  45. }
  46. }
  47. /**
  48. * 是否有相同的时间管理器
  49. * @param callBack 比对的回调
  50. * @param object 比对的对象
  51. */
  52. private ifHasSameTimer(callBack: Function, object: any): boolean {
  53. if (this.getGameTimer(callBack, object)) {
  54. console.error(`[GameTimerMgr]:${object.name}重复添加时间计时器`)
  55. return true;
  56. }
  57. else {
  58. return false;
  59. }
  60. }
  61. /**获取时间管理器 */
  62. private getGameTimer(callBack: Function, object: any): GameTimer {
  63. for (var i: number = this.gameTimerArr.length - 1; i >= 0; i--) {
  64. let gameTimer: GameTimer = this.gameTimerArr[i];
  65. if (gameTimer.callBack == callBack && gameTimer.object == object) {
  66. return gameTimer;
  67. }
  68. }
  69. return null;
  70. }
  71. /**
  72. * 移除时间管理器
  73. * @param callBack
  74. * @param object
  75. */
  76. public removeGameTimer(callBack: Function, object: any) {
  77. for (var i: number = this.gameTimerArr.length - 1; i >= 0; i--) {
  78. let gameTimer: GameTimer = this.gameTimerArr[i];
  79. if (gameTimer.callBack == callBack && gameTimer.object == object) {
  80. this.gameTimerArr.splice(i, 1)
  81. //gameTimer = null; //要不要加null 看最后的效果
  82. }
  83. }
  84. }
  85. }
  86. /**游戏计时器类 */
  87. export class GameTimer {
  88. /**当前计时时间 */
  89. curTime: number = 0;
  90. /**计时总时间 */
  91. totalTime: number = 0;
  92. /**计时的回调 */
  93. callBack: Function = null;
  94. /**绑定的脚本对象 */
  95. object: any = null;
  96. }