GameTime.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  7. import EventName, { PopName } from "../EventName/EventName";
  8. import { GlobalCountDownTime } from "../GameLogic/DataConfig";
  9. import MyComponent from "../Template/MyComponent";
  10. import GameLevel from "./GameLevel";
  11. import GamePause from "./GamePause";
  12. import HonorManger from "./HonorManger";
  13. import PopManger from "./PopManger";
  14. const { ccclass, property } = cc._decorator;
  15. @ccclass
  16. export default class GameTime extends MyComponent {
  17. //人物预制体
  18. @property(cc.Label)
  19. countdown: cc.Label = null
  20. @property(cc.Sprite)
  21. sp_progress: cc.Sprite = null
  22. //当前死亡次数
  23. CurrentDieCount = 0
  24. onLoad(): void {
  25. super.onLoad();
  26. this.CurrentDieCount = 0
  27. this.regEvent(EventName.InitCountDown, this.InitCountDown, this)
  28. this.regEvent(EventName.StartCountDown, this.StartCountDown, this)
  29. this.regEvent(EventName.PauseCountDown, this.PauseCountDown, this)
  30. this.regEvent(EventName.RestoreCountDown, this.RestoreCountDown, this)
  31. this.regEvent(EventName.ADDCountDown, this.ADDCountDown, this)
  32. this.regEvent(EventName.RestartCurrentLevel, this.RestartCurrentLevel, this)
  33. this.regEvent(EventName.GameOver, this.GameOver, this)
  34. this.regEvent(EventName.NextCoustomGame, this.NextCoustomGame, this)
  35. this.regEvent(EventName.NextCoustomGameStop, this.NextCoustomGameStop, this)
  36. }
  37. ADDCountDown() {
  38. this.CountDownTime = 20
  39. //恢复暂停
  40. GamePause.GamePause = false
  41. }
  42. RestartCurrentLevel() {
  43. //恢复暂停
  44. GamePause.GamePause = false
  45. }
  46. NextCoustomGameStop() {
  47. this.unschedule(this.updateCountdown);
  48. }
  49. InitCountDown() {
  50. this.CountDownTime = GlobalCountDownTime
  51. this.sp_progress.fillRange = -1 * (this.CountDownTime / GlobalCountDownTime)
  52. this.setTime()
  53. this.begin = true
  54. }
  55. begin = false
  56. StartCountDown() {
  57. if (this.begin == true) {
  58. this.begin = false
  59. ///
  60. this.CountDownTime = GlobalCountDownTime
  61. this.schedule(this.updateCountdown, 1, cc.macro.REPEAT_FOREVER, 0.01);
  62. this.Shake()
  63. if (GameLevel.gameLevel > 1) {
  64. // // 暂时先关闭
  65. // this.timeShow = 2
  66. }
  67. }
  68. }
  69. PauseCountDown() {
  70. this.PauseTime = true
  71. this.updateCountdown()
  72. }
  73. RestoreCountDown() {
  74. this.PauseTime = false
  75. }
  76. NextCoustomGame() {
  77. HonorManger.getInstance().Game602()
  78. HonorManger.getInstance().Game604()
  79. // HonorManger.getInstance().Game605()
  80. this.unschedule(this.updateCountdown);
  81. //恢复
  82. this.RestoreCountDown()
  83. }
  84. GameOver() {
  85. HonorManger.getInstance().Game603()
  86. this.unschedule(this.updateCountdown);
  87. //恢复
  88. this.RestoreCountDown()
  89. }
  90. //展示几秒闯关时间已重置
  91. timeShow = -1;
  92. //更新截止时间
  93. CountDownTime: number = 30
  94. //是否暂停
  95. PauseTime: boolean = false
  96. setTime() {
  97. const minutes = Math.floor(this.CountDownTime / 60);
  98. const remainingSeconds = this.CountDownTime % 60;
  99. this.countdown.string = `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
  100. }
  101. private updateCountdown() {
  102. if (GamePause.GamePause == true) {
  103. return
  104. }
  105. this.setTime()
  106. HonorManger.getInstance().remainder = this.CountDownTime
  107. HonorManger.getInstance().ElapsedTime = GlobalCountDownTime - this.CountDownTime
  108. if (!this.PauseTime) {
  109. if (this.CountDownTime > 0) {
  110. let Step = 0
  111. this.schedule(() => {
  112. Step = Step + 0.2
  113. this.sp_progress.fillRange = -1 * ((this.CountDownTime + 1 - Step) / GlobalCountDownTime)
  114. }, 0.2, 4, 0.1)//3
  115. }
  116. // this.sp_progress.fillRange = -1 * (this.CountDownTime / GlobalCountDownTime)
  117. this.CountDownTime--
  118. }
  119. if (this.CountDownTime < 0) {
  120. this.countdown.string = "00:00";
  121. //先暂停
  122. GamePause.GamePause = true
  123. switch (this.CurrentDieCount++) {
  124. case 0:
  125. //推送延迟20s
  126. PopManger.getInstance().Pop(PopName.TimeOver_1)
  127. break;
  128. case 1:
  129. //再次挑战本关卡
  130. PopManger.getInstance().Pop(PopName.TimeOver_2)
  131. break;
  132. default:
  133. cc.systemEvent.emit(EventName.GameOver)
  134. PopManger.getInstance().Pop(PopName.GameOverMid)
  135. break;
  136. }
  137. }
  138. if (this.CountDownTime == 20) {
  139. this.Shake()
  140. }
  141. if (this.timeShow >= 0) {
  142. this.timeShow--
  143. this.countdown.string = '已重置'
  144. }
  145. }
  146. Shake() {
  147. let x = this.node.x;
  148. let y = this.node.y;
  149. let action = cc.repeatForever(
  150. cc.sequence(
  151. cc.scaleTo(0.018, 1.05, 1.05),
  152. cc.scaleTo(0.018, 1.08, 1.08),
  153. cc.scaleTo(0.018, 1.1, 1.1),
  154. cc.scaleTo(0.018, 1.08, 1.08),
  155. cc.scaleTo(0.018, 1.05, 1.05),
  156. cc.scaleTo(0.018, 1, 1),
  157. )
  158. )
  159. this.node.runAction(action);
  160. this.scheduleOnce(() => {
  161. this.node.stopAction(action);
  162. this.node.x = x; this.node.y = y;
  163. }, 1)
  164. }
  165. }