Title.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { _decorator, Component, Label, Node, Sprite, tween, Vec3 } from 'cc';
  2. import { endPos } from '../../../ui/main/UI_Main';
  3. import { UI_Revive } from '../../../ui/UI_Revive/UI_Revive';
  4. import { gui } from 'db://assets/core/ui/ui';
  5. import { UI_Lose } from '../../../ui/UI_Lose/UI_Lose';
  6. import { sdkMe } from '../../MySdk';
  7. import { audioManager } from '../../../Audio/AudioManager';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('Title')
  10. export class Title extends Component {
  11. @property(Number)
  12. stoneNumber: number = 0;
  13. @property(Label)
  14. stoneLabel: Label | null = null;
  15. @property(Sprite)
  16. process: Sprite | null = null;
  17. @property(Label)
  18. timeNum: Label | null = null;
  19. @property(Node)
  20. Quan: Node;
  21. @property(Node)
  22. buttomNode: Node;
  23. public totalStones: number = 200; // 总石头数量
  24. public remainingTime: number = 0; // 剩余时间变量
  25. private maxTime: number = 150; // 最大时间限制
  26. private isPalyAd: boolean = false; // 最大时间限制
  27. private isPaused: boolean = false; //设置暂停
  28. private hasShownRevive: boolean = false; // 复活状态
  29. start() {
  30. this.remainingTime = endPos.time; // 初始化剩余时间
  31. this.updateProgressBar();
  32. this.schedule(this.updateTime, 1); // 每秒更新一次倒计时
  33. // 初始化时间显示
  34. if (this.timeNum) {
  35. this.timeNum.string = this.FormatTime(this.remainingTime);
  36. }
  37. this.hasShownRevive = false; // 每局开始时重置状态
  38. sdkMe.evt.on('adShow',this.stop,this)
  39. sdkMe.evt.on('adOff',this.resume,this)
  40. }
  41. protected onDisable(): void {
  42. sdkMe.evt.off('adShow',this.stop,this)
  43. sdkMe.evt.off('adOff',this.resume,this)
  44. }
  45. stop(){
  46. this.isPalyAd = true;
  47. }
  48. resume()
  49. {
  50. this.isPalyAd = false;
  51. audioManager.resume();
  52. }
  53. public set_stop() {
  54. this.isPaused = true;
  55. }
  56. public set_resume() {
  57. this.isPaused = false;
  58. audioManager.resume();
  59. }
  60. addTime(seconds: number) {
  61. //const wasTimerRunning = this.schedule(this.updateTime); // 记录原定时器状态
  62. const previousTime = this.remainingTime; // 记录原剩余时间
  63. const newTime = this.remainingTime + seconds;
  64. this.remainingTime = Math.min(newTime, this.maxTime); // 不超过最大时间
  65. // 立即更新显示
  66. if (this.timeNum) {
  67. this.timeNum.string = this.FormatTime(this.remainingTime);
  68. }
  69. if (
  70. previousTime <= 0 && // 原剩余时间已耗尽
  71. this.remainingTime > 0 // 新剩余时间为正数
  72. ) {
  73. this.schedule(this.updateTime, 1);
  74. }
  75. }
  76. filled(num: number) {
  77. this.stoneNumber = num;
  78. if (this.stoneLabel) {
  79. this.stoneLabel.string = '消除120块石头:' + num + '/120';
  80. }
  81. this.updateProgressBar();
  82. }
  83. updateProgressBar() {
  84. if (this.process) {
  85. this.process.fillRange = this.stoneNumber / 120;
  86. }
  87. }
  88. updateTime() {
  89. if (this.isPalyAd||this.isPaused) return; // 防止负数
  90. if (this.remainingTime <= 0) return; // 防止负数
  91. this.remainingTime--;
  92. // 更新显示
  93. if (this.timeNum) {
  94. this.timeNum.string = this.FormatTime(this.remainingTime);
  95. }
  96. // 时间耗尽处理
  97. if (this.remainingTime <= 0) {
  98. if (!this.hasShownRevive) {
  99. this.unschedule(this.updateTime);
  100. gui.show(UI_Revive);
  101. this.hasShownRevive = true;
  102. } else {
  103. console.log("游戏失败!!!");
  104. gui.show(UI_Lose);
  105. }
  106. }
  107. //按钮左右摇摆
  108. if (this.remainingTime < 90) {
  109. // 左右摇摆动画
  110. tween(this.buttomNode)
  111. .to(0.2, { eulerAngles: new Vec3(0, 0, 10) })
  112. .to(0.4, { eulerAngles: new Vec3(0, 0, -10) })
  113. .to(0.2, { eulerAngles: new Vec3(0, 0, 0) })
  114. .union()
  115. .repeat(2) // 重复2次形成完整摇摆
  116. .start();
  117. }
  118. }
  119. FormatTime(totalSeconds: number): string {
  120. let minutes: number = Math.floor(totalSeconds / 60);
  121. let seconds: number = totalSeconds % 60;
  122. let mm: string = minutes < 10 ? `0${minutes}` : minutes.toString();
  123. let ss: string = seconds < 10 ? `0${seconds}` : seconds.toString();
  124. return `${mm}:${ss}`;
  125. }
  126. public getRemainingTime(): number {
  127. return this.remainingTime;
  128. }
  129. // 当游戏重新开始时调用这个方法重置状态
  130. public resetGameState() {
  131. this.hasShownRevive = false;
  132. }
  133. }