GameMode.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. let library = require("../Library");
  2. let gameConfig = require("GameConfig");
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. gameStates: {
  7. default: null,
  8. type: cc.Node,
  9. serializable: true,
  10. },
  11. totalTime: {
  12. default: null,
  13. type: cc.Label,
  14. serializable: true,
  15. },
  16. timeProgress: {
  17. default: null,
  18. type: cc.Node,
  19. serializable: true,
  20. },
  21. countDown: {
  22. default: null,
  23. type: cc.Label,
  24. serializable: true,
  25. },
  26. },
  27. start () {
  28. this.init();
  29. this.fireTick();
  30. },
  31. init()
  32. {
  33. //init node
  34. this.gameStatesScpt = this.gameStates.getComponent('GameStates');
  35. this.totalTime.string = library.formatSeconds(this.gameStatesScpt.currentTime);
  36. this.timeProgressScpt = this.timeProgress.getComponent('ProgressBar');
  37. this.countDownNum = 3;
  38. this.gameStatesScpt.curretState = this.gameStatesScpt.playing;
  39. this.lastAppearMouse = null;
  40. },
  41. fireTick()
  42. {
  43. this.schedule(this.tick, 1);
  44. },
  45. tick()
  46. {
  47. if(this.gameStatesScpt.currentTime==0)
  48. {
  49. this.untick();
  50. this.gameOver();
  51. return;
  52. }
  53. //total time
  54. this.gameStatesScpt.currentTime--;
  55. this.totalTime.string = library.formatSeconds(this.gameStatesScpt.currentTime);
  56. this.timeProgressScpt.setValue(1-(this.gameStatesScpt.currentTime/gameConfig.roundTime));
  57. this.checkCurrentLV();
  58. //countdown
  59. if(this.countDownNum<0) return;
  60. if(this.countDownNum==0)
  61. {
  62. this.countDown.string = '';
  63. this.startComeOutMouse();
  64. }
  65. else
  66. {
  67. this.countDown.string = this.countDownNum.toString();
  68. }
  69. this.countDownNum--;
  70. },
  71. untick()
  72. {
  73. this.unschedule(this.tick);
  74. },
  75. checkCurrentLV()
  76. {
  77. let c_percentage = (1-this.gameStatesScpt.currentTime/gameConfig.roundTime)*100;
  78. let percentage = gameConfig.comeOutAiInLv[this.gameStatesScpt.currentLv].percentage;
  79. if(c_percentage>=percentage &&
  80. this.gameStatesScpt.currentLv<gameConfig.comeOutAiInLv.length-1)
  81. {
  82. this.gameStatesScpt.currentLv++;
  83. }
  84. },
  85. startComeOutMouse()
  86. {
  87. let self = this;
  88. let lv = gameConfig.comeOutAiInLv;
  89. let gStates = this.gameStatesScpt;
  90. //结束后关闭计时器
  91. if(gStates.curretState == gStates.finished)
  92. {
  93. self.unschedule(this.startComeOutMouse);
  94. return;
  95. }
  96. let appearDur = lv[gStates.currentLv].appearDur;
  97. let num = lv[gStates.currentLv].num;
  98. //如果数组里面的数量小于num 就不要生成那么多地鼠
  99. if(num>gStates.hiddenMouseArr.length)
  100. {
  101. num = gStates.hiddenMouseArr.length;
  102. }
  103. //随机1-num 出地鼠保证出地鼠不是看起来都是 同样的数量 好看
  104. if(gStates.hiddenMouseArr.length!=0)
  105. {
  106. num = library.randomInt(1,num);
  107. }
  108. //创建地鼠
  109. for(let i=0;i<num;i++)
  110. {
  111. let index = library.randomInt(0,gStates.hiddenMouseArr.length-1);
  112. let aMouse = gStates.hiddenMouseArr[index];
  113. if(self.lastAppearMouse == aMouse)
  114. {
  115. if(index+1>=gStates.hiddenMouseArr.length)
  116. {
  117. index = 0;
  118. }
  119. else
  120. {
  121. aMouse = gStates.hiddenMouseArr[index+1];
  122. }
  123. }
  124. self.lastAppearMouse = aMouse;
  125. let aMouseScp = aMouse.getComponent('Actor');
  126. aMouseScp.spawn(aMouse,appearDur,function (aMouse) {
  127. aMouseScp.die();
  128. if(i==num-1)
  129. {
  130. self.scheduleOnce(function () {
  131. self.startComeOutMouse();
  132. },lv[gStates.currentLv].duration);
  133. }
  134. });
  135. }
  136. },
  137. gameOver()
  138. {
  139. this.gameStatesScpt.curretState = this.gameStatesScpt.finished;
  140. }
  141. });