Main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. let gameConfig = require("GameConfig");
  2. let library = require("../Library");
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. gameStates: {
  7. default: null,
  8. type: cc.Node,
  9. serializable: true,
  10. },
  11. gameMode: {
  12. default: null,
  13. type: cc.Node,
  14. serializable: true,
  15. },
  16. playerController: {
  17. default: null,
  18. type: cc.Node,
  19. serializable: true,
  20. },
  21. countDown: {
  22. default: null,
  23. type: cc.Node,
  24. serializable: true,
  25. },
  26. kCal: {
  27. default: null,
  28. type: cc.Node,
  29. serializable: true,
  30. },
  31. restTimeRing: {
  32. default: null,
  33. type: cc.Node,
  34. serializable: true,
  35. },
  36. roundCurrentTime: {
  37. default: null,
  38. type: cc.Node,
  39. serializable: true,
  40. },
  41. roundTxt: {
  42. default: null,
  43. type: cc.Node,
  44. serializable: true,
  45. },
  46. nameTxt: {
  47. default: null,
  48. type: cc.Node,
  49. serializable: true,
  50. },
  51. videoController: {
  52. default: null,
  53. type: cc.Node,
  54. serializable: true,
  55. },
  56. videoplayer: {
  57. default: null,
  58. type: cc.Node,
  59. serializable: true,
  60. },progressBar: {
  61. default: null,
  62. type: cc.Node,
  63. serializable: true,
  64. },
  65. comb: {
  66. default: null,
  67. type: cc.Node,
  68. serializable: true,
  69. },
  70. countDownAudio: {
  71. default: null,
  72. type: cc.AudioClip,
  73. serializable: true,
  74. },
  75. startGunAudio: {
  76. default: null,
  77. type: cc.AudioClip,
  78. serializable: true,
  79. },
  80. actions: {
  81. default: null,
  82. type: cc.Node,
  83. serializable: true,
  84. },
  85. hint: {
  86. default: null,
  87. type: cc.Node,
  88. serializable: true,
  89. },
  90. loopTxt: {
  91. default: null,
  92. type: cc.Label,
  93. serializable: true,
  94. },
  95. nextBtn: {
  96. default: null,
  97. type: cc.Node,
  98. serializable: true,
  99. },
  100. },
  101. onLoad () {
  102. this.gStatesScp = this.gameStates.getComponent('GameStates');
  103. this.gameModeScp = this.gameMode.getComponent('GameMode');
  104. this.playerControllerScp = this.playerController.getComponent('PlayerController');
  105. this.restTimeRingScp = this.restTimeRing.getComponent('ProgressBar');
  106. this.videoControllerScp = this.videoController.getComponent('SpriteVideoPlayer');
  107. this.audioControllerScp = this.node.getComponent('AudioController');
  108. this.combScp = this.comb.getComponent('Combo');
  109. this.punchInteractScp = this.actions.getComponent('PunchInteract');
  110. this.hintScp = this.hint.getComponent('Hint');
  111. if(!this.gStatesScp.bLoop)
  112. {
  113. this.loopTxt.string = '循环';
  114. }
  115. else {
  116. this.loopTxt.string = '顺序';
  117. }
  118. },
  119. resetRound()
  120. {
  121. this.round = gameConfig.round[this.gStatesScp.currentRound];
  122. this.gStatesScp.curretState = this.gStatesScp.countDown;
  123. this.gStatesScp.bSecondOrTimes = 0;
  124. if(this.round.second != 0)
  125. {
  126. this.gStatesScp.bSecondOrTimes = 1;
  127. }
  128. if(this.gStatesScp.bSecondOrTimes)
  129. {
  130. this.roundCurrentTime.getComponent(cc.Label).string = this.round.second+' ″';
  131. }
  132. else
  133. {
  134. this.roundCurrentTime.getComponent(cc.Label).string = this.round.times+' ″';
  135. }
  136. this.roundTxt.getComponent(cc.Label).string = this.gStatesScp.currentRound+1;
  137. this.nameTxt.getComponent(cc.Label).string = this.round.name;
  138. this.restTimeRingScp.getComponent(cc.Sprite).fillRange = 1;
  139. this.gStatesScp.roundTime = 0;
  140. this.gStatesScp.currentSoundIndex = 0;
  141. this.progressBar.getComponent(cc.ProgressBar).progress = (this.gStatesScp.currentRound+1)/24;
  142. if(this.gStatesScp.currentRound+1==24)
  143. {
  144. this.progressBar.getComponent(cc.ProgressBar).progress = 1;
  145. }
  146. for(let i=0; i<this.gStatesScp.hightLightActionArr.length;i++)
  147. {
  148. if(this.gStatesScp.hightLightActionArr[i]&&this.gStatesScp.hightLightActionArr[i].node)
  149. this.gStatesScp.hightLightActionArr[i].node.destroy();
  150. }
  151. this.gStatesScp.hightLightActionArr.length = 0;
  152. this.nextBtn.getComponent(cc.Button).interactable = true;
  153. },
  154. // startPlay(callback)
  155. startPlay()
  156. {
  157. let self = this;
  158. // this.callback = callback;
  159. this.gStatesScp.curretState = self.gStatesScp.countDown;
  160. this.playVideo();
  161. this.showActionBar();
  162. let times = 3;
  163. let countDown = function()
  164. {
  165. if(times==0)
  166. {
  167. this.unschedule(countDown);
  168. this.scheduleOnce(function () {
  169. self.audioControllerScp.playAudio('Audios/Game/Others/3、GO',function () {
  170. self.gStatesScp.curretState = self.gStatesScp.playing;
  171. self.countDown.getComponent(cc.Label).string = '';
  172. self.playSound();
  173. //start
  174. self.playRoundBySecond();
  175. });
  176. },0.2);
  177. return;
  178. }
  179. times--;
  180. this.countDown.getComponent(cc.Label).string = (times+1).toString();
  181. this.audioControllerScp.play('Audios/Game/Nums/'+(times+1).toString());
  182. };
  183. this.schedule(countDown, 1);
  184. },
  185. playSound()
  186. {
  187. let self = this;
  188. if(this.gStatesScp.currentSoundIndex == gameConfig.audioRound[this.gStatesScp.currentRound].length ||
  189. gameConfig.audioRound[this.gStatesScp.currentRound].length == 0
  190. )
  191. {
  192. return;
  193. }
  194. let currentAudioObj = gameConfig.audioRound[this.gStatesScp.currentRound][this.gStatesScp.currentSoundIndex];
  195. if (!currentAudioObj) return;
  196. let currentAudioPathStr = "Audios/Game/"+currentAudioObj.name;
  197. // console.log('44444=',currentAudioObj.name);
  198. // console.log('xxxx=',currentAudioObj.delay);
  199. this.scheduleOnce(function ()
  200. {
  201. self.audioControllerScp.playAudio(currentAudioPathStr,function () {
  202. self.gStatesScp.currentSoundIndex++;
  203. self.playSound();
  204. });
  205. // console.log('44444=',currentAudioPathStr);
  206. // console.log('3333=',this.gStatesScp.currentRound);
  207. },currentAudioObj.delay);
  208. },
  209. showActionBar()
  210. {
  211. let actionPlay = gameConfig.actionPlay[this.gStatesScp.currentRound];
  212. if(actionPlay.length == 0)
  213. {
  214. this.actions.parent.getChildByName('BG').y = -100;
  215. this.actions.active = false;
  216. }
  217. else {
  218. this.actions.parent.getChildByName('BG').y = 0;
  219. this.actions.active = true;
  220. }
  221. },
  222. playVideo()
  223. {
  224. this.videoControllerScp.play(this.videoplayer,this.gStatesScp.currentRound,this,this.videoCompleted);
  225. },
  226. videoCompleted(self)
  227. {
  228. if(self.gStatesScp.curretState == self.gStatesScp.countDown)
  229. {
  230. self.videoControllerScp.play(self.videoplayer,self.gStatesScp.currentRound,self,self.videoCompleted);
  231. }else if(self.gStatesScp.curretState == self.gStatesScp.playing)
  232. {
  233. self.videoControllerScp.play(self.videoplayer,self.gStatesScp.currentRound,self,self.videoCompleted);
  234. self.punchInteractScp.playActions();
  235. }
  236. },
  237. playRoundBySecond()
  238. {
  239. //start
  240. let self = this;
  241. this.gameModeScp.fireTick();
  242. this.restTimeRingScp.fire(this.round.second);
  243. this.timer = function ()
  244. {
  245. if (this.gStatesScp.roundTime == this.round.second)
  246. {
  247. this.finished();
  248. return;
  249. }
  250. if(this.round.second - this.gStatesScp.roundTime == 7)
  251. {
  252. self.audioControllerScp.play('Audios/Game/Others/再坚持5秒');
  253. }
  254. this.gStatesScp.roundTime++;
  255. this.roundCurrentTime.getComponent(cc.Label).string = (this.gStatesScp.roundTime).toString()+' ″';
  256. cc.audioEngine.play(this.countDownAudio, false, 0.5);
  257. };
  258. this.schedule(this.timer, 1);
  259. },
  260. finished()
  261. {
  262. let self = this;
  263. this.stop();
  264. cc.audioEngine.play(this.startGunAudio, false, 1);
  265. // roundFinished
  266. this.gStatesScp.curretState = this.gStatesScp.default;
  267. if(this.gStatesScp.bLoop)
  268. {
  269. this.playerControllerScp.playRound();
  270. return;
  271. }
  272. this.gStatesScp.currentRound++;
  273. if(this.gStatesScp.currentRound == gameConfig.round.length-1)
  274. {
  275. this.gStatesScp.curretState = this.gStatesScp.default;
  276. //播放结束后
  277. this.gameModeScp.playOver();
  278. return;
  279. }
  280. //delay to start
  281. this.playerControllerScp.scheduleOnce(function()
  282. {
  283. self.playerControllerScp.rest.x += self.playerControllerScp.rest.width;
  284. self.playerControllerScp.rest.active = true;
  285. self.playerControllerScp.rest.opacity = 255;
  286. self.playerControllerScp.playRest();
  287. cc.tween(self.playerControllerScp.rest)
  288. .by(0.5, { position: cc.v2(-self.playerControllerScp.rest.width, 0) })
  289. .start()
  290. },1);
  291. },
  292. next(event, customEventData)
  293. {
  294. let self = this;
  295. //press Onetime
  296. let node = event.target;
  297. node.getComponent(cc.Button).interactable = false;
  298. this.stop();
  299. cc.audioEngine.play(this.startGunAudio, false, 1);
  300. // roundFinished
  301. this.gStatesScp.curretState = this.gStatesScp.default;
  302. this.gStatesScp.currentRound++;
  303. if(this.gStatesScp.currentRound == gameConfig.round.length-1)
  304. {
  305. this.gStatesScp.curretState = this.gStatesScp.default;
  306. //播放结束后
  307. this.gameModeScp.playOver();
  308. return;
  309. }
  310. //delay to start
  311. this.playerControllerScp.scheduleOnce(function()
  312. {
  313. self.playerControllerScp.rest.x += self.playerControllerScp.rest.width;
  314. self.playerControllerScp.rest.active = true;
  315. self.playerControllerScp.rest.opacity = 255;
  316. self.playerControllerScp.playRest();
  317. cc.tween(self.playerControllerScp.rest)
  318. .by(0.5, { position: cc.v2(-self.playerControllerScp.rest.width, 0) })
  319. .start()
  320. },1);
  321. },
  322. playMode(event, customEventData)
  323. {
  324. if(!this.gStatesScp.bLoop)
  325. {
  326. this.loopTxt.string = '顺序';
  327. this.hintScp.showHintStr('已切换到循环播放');
  328. }
  329. else {
  330. this.loopTxt.string = '循环';
  331. this.hintScp.showHintStr('已切换到顺序播放');
  332. }
  333. this.gStatesScp.bLoop = !this.gStatesScp.bLoop;
  334. },
  335. stop()
  336. {
  337. // this.unscheduleAllCallbacks();
  338. this.unschedule(this.timer);
  339. this.gameModeScp.untick();
  340. this.videoControllerScp.stop();
  341. }
  342. });