GameMode.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. readyGo: {
  12. default: null,
  13. type: cc.Node,
  14. serializable: true,
  15. },
  16. totalTime: {
  17. default: null,
  18. type: cc.Label,
  19. serializable: true,
  20. },
  21. timeProgress: {
  22. default: null,
  23. type: cc.Node,
  24. serializable: true,
  25. },
  26. scareCrow: {
  27. default: null,
  28. type: cc.Node,
  29. serializable: true,
  30. },
  31. result: {
  32. default: null,
  33. type: cc.Node,
  34. serializable: true,
  35. },
  36. },
  37. start () {
  38. this.init();
  39. this.armatureDisplay.playAnimation("animation",1);
  40. },
  41. init()
  42. {
  43. //init node
  44. this.gStatesScp = this.gameStates.getComponent('GameStates');
  45. this.audioPContrScp = this.gameStates.getComponent('AudioController');
  46. this.armatureDisplay = this.readyGo.getComponent(dragonBones.ArmatureDisplay);
  47. //获取 Armatrue
  48. this.armature = this.armatureDisplay.armature();
  49. //添加动画监听
  50. this.armatureDisplay.addEventListener(dragonBones.EventObject.COMPLETE, this.animationEventHandler, this)
  51. this.totalTime.string = library.formatSeconds(this.gStatesScp.currentTime);
  52. this.timeProgressScp = this.timeProgress.getComponent('ProgressBar');
  53. this.gStatesScp.curretState = this.gStatesScp.playing;
  54. this.lastAppearMouse = null;
  55. },
  56. animationEventHandler(event)
  57. {
  58. let self = this;
  59. if (event.type === dragonBones.EventObject.COMPLETE)
  60. {
  61. if (event.animationState.name === "animation")
  62. {
  63. this.getComponent(cc.AudioSource).play();
  64. this.fireTick();
  65. //come in
  66. cc.tween(self.scareCrow)
  67. .by(0.1, { position: cc.v2(self.scareCrow.width,self.scareCrow.y)})
  68. .start()
  69. this.scheduleOnce(function () {
  70. this.startComeOutMouse();
  71. },2)
  72. }
  73. }
  74. },
  75. fireTick()
  76. {
  77. this.schedule(this.tick, 1);
  78. },
  79. tick()
  80. {
  81. if(this.gStatesScp.currentTime==0)
  82. {
  83. this.untick();
  84. this.gameOver();
  85. return;
  86. }
  87. //total time
  88. this.gStatesScp.currentTime--;
  89. this.totalTime.string = library.formatSeconds(this.gStatesScp.currentTime);
  90. this.timeProgressScp.setValue(1-(this.gStatesScp.currentTime/gameConfig.roundTime));
  91. this.checkCurrentLV();
  92. },
  93. untick()
  94. {
  95. this.unschedule(this.tick);
  96. },
  97. checkCurrentLV()
  98. {
  99. let c_percentage = (1-this.gStatesScp.currentTime/gameConfig.roundTime)*100;
  100. let percentage = gameConfig.comeOutAiInLv[this.gStatesScp.currentLv].percentage;
  101. // console.log('00000=',c_percentage)
  102. // console.log('11111=',percentage)
  103. if(c_percentage>=percentage &&
  104. this.gStatesScp.currentLv<gameConfig.comeOutAiInLv.length-1)
  105. {
  106. this.gStatesScp.currentLv++;
  107. }
  108. },
  109. startComeOutMouse()
  110. {
  111. let self = this;
  112. let lv = gameConfig.comeOutAiInLv;
  113. let gStates = this.gStatesScp;
  114. //结束后关闭计时器
  115. if(gStates.curretState == gStates.finished)
  116. {
  117. self.unschedule(this.startComeOutMouse);
  118. return;
  119. }
  120. let appearDur = lv[gStates.currentLv].appearDur;
  121. let num = lv[gStates.currentLv].num;
  122. // console.log('111=',gStates.hiddenMouseArr.length);
  123. // console.log('num=',num);
  124. //如果数组里面的数量小于num 就不要生成那么多地鼠
  125. if(num>gStates.hiddenMouseArr.length)
  126. {
  127. num = gStates.hiddenMouseArr.length;
  128. }
  129. //随机1-num 出地鼠保证出地鼠不是看起来都是 同样的数量 好看
  130. if(gStates.hiddenMouseArr.length!=0)
  131. {
  132. num = library.randomInt(1,num);
  133. }
  134. // console.log('num33=',num)
  135. //创建地鼠
  136. for(let i=0;i<num;i++)
  137. {
  138. let index = library.randomInt(0,gStates.hiddenMouseArr.length-1);
  139. let aMouse = gStates.hiddenMouseArr[index];
  140. if(self.lastAppearMouse == aMouse)
  141. {
  142. if(index+1>=gStates.hiddenMouseArr.length)
  143. {
  144. index = 0;
  145. }
  146. else
  147. {
  148. aMouse = gStates.hiddenMouseArr[index+1];
  149. }
  150. }
  151. self.lastAppearMouse = aMouse;
  152. let aMouseScp = aMouse.getComponent('Actor');
  153. aMouseScp.spawn(appearDur);
  154. }
  155. // next turn
  156. let nextTurnDur = lv[gStates.currentLv].appearDur+lv[gStates.currentLv].duration+0.1+0.1;
  157. // console.log('1111=',lv[gStates.currentLv].appearDur)
  158. // console.log('1111=',lv[gStates.currentLv].duration)
  159. self.scheduleOnce(function () {
  160. self.startComeOutMouse();
  161. },nextTurnDur);
  162. },
  163. gameOver()
  164. {
  165. this.untick();
  166. this.result.active = true;
  167. let resultScp = this.result.getComponent('Result');
  168. resultScp.setResult();
  169. this.gStatesScp.curretState = this.gStatesScp.finished;
  170. },
  171. replay()
  172. {
  173. // cc.director.loadScene("Pairing");
  174. cc.director.loadScene("Game");
  175. },
  176. });