GameMode.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. var aGameInstance = require('../GameInstance');
  2. let webView = require("../WebView");
  3. let lib = require("../Library");
  4. cc.Class({
  5. extends: cc.Component,
  6. properties: {
  7. AudioControllerNode: {
  8. default: null,
  9. type: cc.Node,
  10. serializable: true,
  11. },
  12. TrumpNode: {
  13. default: null,
  14. type: cc.Node,
  15. serializable: true,
  16. },
  17. TimeLabelNode: {
  18. default: null,
  19. type: cc.Node,
  20. serializable: true,
  21. },
  22. TargetTimeLabel: {
  23. default: null,
  24. type: cc.Label,
  25. serializable: true,
  26. },
  27. CurTimeLabel: {
  28. default: null,
  29. type: cc.Label,
  30. serializable: true,
  31. },
  32. StartOrStopButton: {
  33. default: null,
  34. type: cc.Node,
  35. serializable: true,
  36. },
  37. ResultNode: {
  38. default: null,
  39. type: cc.Node,
  40. serializable: true,
  41. },
  42. },
  43. onLoad () {
  44. //获取外部元素
  45. this.aGameStates = cc.find('GameStates').getComponent("GameStates");
  46. this.TrumpAnimScp = this.TrumpNode.getComponent("TrumpAnim");
  47. this.AudioControllerScp = this.AudioControllerNode.getComponent('AudioController');
  48. this.ResultScp = this.ResultNode.getComponent('Result');
  49. //初始化
  50. this.bTimerStart = false;//0停止 1开始
  51. this.bRunScene = false;
  52. this.bPunchCD = false;
  53. this.btnCD = false;
  54. switch (aGameInstance.sceneIdx) {
  55. case 0:
  56. this.aGameStates.targetTime.second = 3;
  57. this.aGameStates.targetTime.millisecond = 0;
  58. this.TargetTimeLabel.string = this.TimeFormat(this.aGameStates.targetTime.second,this.aGameStates.targetTime.millisecond);
  59. break;
  60. case 1:
  61. this.aGameStates.targetTime.second = 10;
  62. this.aGameStates.targetTime.millisecond = 0;
  63. this.TargetTimeLabel.string = this.TimeFormat(this.aGameStates.targetTime.second,this.aGameStates.targetTime.millisecond);
  64. break;
  65. case 2:
  66. this.TargetTimeLabel.string = '';
  67. break;
  68. case 3:
  69. this.TargetTimeLabel.string = '';
  70. break;
  71. default:
  72. break;
  73. }
  74. },
  75. start()
  76. {
  77. //是否在pC
  78. if(!lib.isMobile()) return;
  79. let self = this;
  80. self.bPunchCD = true;
  81. self.scheduleOnce(()=>{
  82. self.bPunchCD = false;
  83. },2);
  84. self.bRunScene = true;
  85. self.AudioControllerScp.playBGM('Game/Audios/BGM/Funny.wav',1,()=>{
  86. self.bRunScene = false;
  87. });
  88. webView.init(self.node, ()=>{
  89. webView.onBindHitBoxingPost();
  90. self.node.on('onBoxingPostHit',self.onBoxingPostHit,self);
  91. });
  92. },
  93. onBoxingPostHit(data)
  94. {
  95. this.Punch();
  96. },
  97. //页面退出回调
  98. onQuit(data)
  99. {
  100. console.log('onQuit=',data);
  101. },
  102. //弹出框回调
  103. onQuitModal(res)
  104. {
  105. if (res.data.confirm) {
  106. console.log("退出");
  107. }else if(res.data.cancel){
  108. console.log("取消退出");
  109. }
  110. },
  111. Back(event, param) {
  112. if(this.bRunScene)
  113. {
  114. return;
  115. }
  116. this.bRunScene = true;
  117. this.AudioControllerScp.stopAll();
  118. //取消这个组件得所有计时器
  119. this.unscheduleAllCallbacks();
  120. //是否在pC
  121. if(lib.isMobile()){
  122. webView.onUnbindHitBoxingPost();
  123. webView.unRegister(this.node);
  124. }
  125. this.scheduleOnce(()=>{
  126. cc.director.loadScene("Menu");
  127. },0.1);
  128. },
  129. StartOrStopOnClick(event, param) {
  130. //let target = event.target;//必须要给组件挂载target节点
  131. this.Punch();
  132. },
  133. Punch()
  134. {
  135. if(this.bPunchCD) return;
  136. this.bPunchCD = true;
  137. let target = this.StartOrStopButton;
  138. switch (aGameInstance.sceneIdx) {
  139. case 0:
  140. if(this.bTimerStart)
  141. {
  142. //停止
  143. target.active = false;
  144. target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
  145. this.StopTimer();
  146. this.TrumpAnimScp.StopAnim();
  147. if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime) == 0)
  148. {
  149. this.ResultScp.ShowResult(true);
  150. this.TrumpAnimScp.Win(function(){
  151. target.active = true;
  152. this.ResultScp.ShutDown();
  153. this.TrumpAnimScp.StartAnim();
  154. this.bPunchCD = false;
  155. }.bind(this));
  156. }
  157. else
  158. {
  159. this.ResultScp.ShowResult(false);
  160. this.TrumpAnimScp.Fail(function(){
  161. target.active = true;
  162. this.ResultScp.ShutDown();
  163. this.TrumpAnimScp.StartAnim();
  164. this.bPunchCD = false;
  165. }.bind(this));
  166. }
  167. }
  168. else
  169. {
  170. //开始
  171. target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'STOP';
  172. this.StartTimer();
  173. }
  174. this.bTimerStart = !this.bTimerStart;
  175. break;
  176. case 1:
  177. if(this.bTimerStart)
  178. {
  179. //停止
  180. target.active = false;
  181. target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
  182. this.StopTimer();
  183. this.TrumpAnimScp.StopAnim();
  184. if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime) == 0)
  185. {
  186. this.ResultScp.ShowResult(true);
  187. this.TrumpAnimScp.Win(function(){
  188. target.active = true;
  189. this.ResultScp.ShutDown();
  190. this.TrumpAnimScp.StartAnim();
  191. this.bPunchCD = false;
  192. }.bind(this));
  193. }
  194. else
  195. {
  196. this.ResultScp.ShowResult(false);
  197. this.TrumpAnimScp.Fail(function(){
  198. target.active = true;
  199. this.ResultScp.ShutDown();
  200. this.TrumpAnimScp.StartAnim();
  201. this.bPunchCD = false;
  202. }.bind(this));
  203. }
  204. }
  205. else
  206. {
  207. //开始
  208. target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'STOP';
  209. this.StartTimer();
  210. }
  211. this.bTimerStart = !this.bTimerStart;
  212. break;
  213. case 2:
  214. if(this.bTimerStart)
  215. {
  216. this.aGameStates.curRound++;
  217. //停止
  218. this.StopTimer();
  219. this.TrumpAnimScp.StopAnim();
  220. if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime) == 0)
  221. {
  222. if(this.aGameStates.curRound == 3)
  223. {
  224. target.active = false;
  225. target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
  226. this.StopTimer();
  227. this.TrumpAnimScp.StopAnim();
  228. this.bTimerStart = false;
  229. this.ResultScp.ShowResult(true);
  230. this.TrumpAnimScp.Win(function(){
  231. target.active = true;
  232. this.aGameStates.curRound = 0;
  233. this.ResultScp.ShutDown();
  234. this.TrumpAnimScp.StartAnim();
  235. this.TargetTimeLabel.string = '';
  236. this.bPunchCD = false;
  237. }.bind(this));
  238. }
  239. else
  240. {
  241. this.StartTimer();
  242. }
  243. }
  244. else
  245. {
  246. target.active = false;
  247. target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
  248. this.StopTimer();
  249. this.TrumpAnimScp.StopAnim();
  250. this.bTimerStart = false;
  251. this.ResultScp.ShowResult(false);
  252. this.TrumpAnimScp.Fail(function(){
  253. target.active = true;
  254. this.aGameStates.curRound = 0;
  255. this.ResultScp.ShutDown();
  256. this.TrumpAnimScp.StartAnim();
  257. this.TargetTimeLabel.string = '';
  258. this.bPunchCD = false;
  259. }.bind(this));
  260. }
  261. }
  262. else
  263. {
  264. //开始
  265. target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'STOP';
  266. this.StartTimer();
  267. }
  268. if(this.aGameStates.curRound == 0)
  269. {
  270. this.bTimerStart = !this.bTimerStart;
  271. }
  272. break;
  273. case 3:
  274. if(this.bTimerStart)
  275. {
  276. this.aGameStates.curRound++;
  277. //停止
  278. this.StopTimer();
  279. this.TrumpAnimScp.StopAnim();
  280. if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime) == 0)
  281. {
  282. if(this.aGameStates.curRound == 3)
  283. {
  284. target.active = false;
  285. target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
  286. this.StopTimer();
  287. this.TrumpAnimScp.StopAnim();
  288. this.bTimerStart = false;
  289. this.ResultScp.ShowResult(true);
  290. this.TrumpAnimScp.Win(function(){
  291. target.active = true;
  292. this.aGameStates.curRound = 0;
  293. this.ResultScp.ShutDown();
  294. this.TrumpAnimScp.StartAnim();
  295. this.TargetTimeLabel.string = '';
  296. this.bPunchCD = false;
  297. }.bind(this));
  298. }
  299. else
  300. {
  301. this.StartTimer();
  302. }
  303. }
  304. else
  305. {
  306. target.active = false;
  307. target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
  308. this.StopTimer();
  309. this.TrumpAnimScp.StopAnim();
  310. this.bTimerStart = false;
  311. this.ResultScp.ShowResult(false);
  312. this.TrumpAnimScp.Fail(function(){
  313. target.active = true;
  314. this.aGameStates.curRound = 0;
  315. this.ResultScp.ShutDown();
  316. this.TrumpAnimScp.StartAnim();
  317. this.TargetTimeLabel.string = '';
  318. this.bPunchCD = false;
  319. }.bind(this));
  320. }
  321. }
  322. else
  323. {
  324. //开始
  325. target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'STOP';
  326. this.StartTimer();
  327. }
  328. if(this.aGameStates.curRound == 0)
  329. {
  330. this.bTimerStart = !this.bTimerStart;
  331. }
  332. break;
  333. default:
  334. break;
  335. }
  336. },
  337. StartTimer()
  338. {
  339. this.AudioControllerScp.playBGM('Game/Audios/BGM/CountTime.wav',0.5,()=>{
  340. this.bRunScene = false;
  341. });
  342. this.scheduleOnce(function(){
  343. this.bPunchCD = false;
  344. },1);
  345. this.aGameStates.curtTime.second = 0;
  346. this.aGameStates.curtTime.millisecond = 0;
  347. this.CurTimeLabel.string = this.TimeFormat(this.aGameStates.curtTime.second,this.aGameStates.curtTime.millisecond);
  348. switch (aGameInstance.sceneIdx) {
  349. case 0:
  350. this.aGameStates.targetTime.second = 3;
  351. this.aGameStates.targetTime.millisecond = 0;
  352. this.TargetTimeLabel.string = this.TimeFormat(this.aGameStates.targetTime.second,this.aGameStates.targetTime.millisecond);
  353. this.aTimer = this.schedule(this.CountUp,0.1, cc.macro.REPEAT_FOREVER);
  354. break;
  355. case 1:
  356. this.aGameStates.targetTime.second = 10;
  357. this.aGameStates.targetTime.millisecond = 0;
  358. this.TargetTimeLabel.string = this.TimeFormat(this.aGameStates.targetTime.second,this.aGameStates.targetTime.millisecond);
  359. this.aTimer = this.schedule(this.CountUp,0.01, cc.macro.REPEAT_FOREVER);
  360. break;
  361. case 2:
  362. while(true)
  363. {
  364. this.aGameStates.targetTime.second = this.RandomNum(this.aGameStates.targetSpan[0],this.aGameStates.targetSpan[1]);
  365. this.aGameStates.targetTime.millisecond = 0;
  366. // console.log('11=',this.aGameStates.lastTargetTime)
  367. // console.log('22=',this.aGameStates.targetTime)
  368. if(this.Dif(this.aGameStates.lastTargetTime,this.aGameStates.targetTime) != 0)
  369. {
  370. break;
  371. }
  372. }
  373. // //保存之前的随机值
  374. this.aGameStates.lastTargetTime.second = this.aGameStates.targetTime.second;
  375. this.aGameStates.lastTargetTime.millisecond = this.aGameStates.targetTime.millisecond;
  376. this.TargetTimeLabel.string = this.TimeFormat(this.aGameStates.targetTime.second,this.aGameStates.targetTime.millisecond);
  377. this.aTimer = this.schedule(this.CountUp,0.1, cc.macro.REPEAT_FOREVER);
  378. break;
  379. case 3:
  380. while(true)
  381. {
  382. this.aGameStates.targetTime.second = this.RandomNum(this.aGameStates.targetSpan[0],this.aGameStates.targetSpan[1]);
  383. this.aGameStates.targetTime.millisecond = 0;
  384. if(this.Dif(this.aGameStates.lastTargetTime,this.aGameStates.targetTime) != 0)
  385. {
  386. break;
  387. }
  388. }
  389. while(true)
  390. {
  391. this.aGameStates.curtTime.second = this.RandomNum(this.aGameStates.curSpan[0],this.aGameStates.curSpan[1]);
  392. this.aGameStates.curtTime.millisecond = 0;
  393. if(this.Dif(this.aGameStates.lastCurtTime,this.aGameStates.curtTime) != 0 && this.Dif(this.aGameStates.targetTime,this.aGameStates.curtTime) != 0)
  394. {
  395. break;
  396. }
  397. }
  398. //保存之前的随机值
  399. this.aGameStates.lastTargetTime.second = this.aGameStates.targetTime.second;
  400. this.aGameStates.lastTargetTime.millisecond = this.aGameStates.targetTime.millisecond;
  401. this.aGameStates.lastCurtTime.second = this.aGameStates.curtTime.second;
  402. this.aGameStates.lastCurtTime.millisecond = this.aGameStates.curtTime.millisecond;
  403. this.TargetTimeLabel.string = this.TimeFormat(this.aGameStates.targetTime.second,this.aGameStates.targetTime.millisecond);
  404. this.CurTimeLabel.string = this.TimeFormat(this.aGameStates.curtTime.second,this.aGameStates.curtTime.millisecond);
  405. let speed = 0.1;
  406. if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime)>0)
  407. {
  408. this.aTimer = this.schedule(this.CountUp,speed, cc.macro.REPEAT_FOREVER);
  409. }
  410. else if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime)<0)
  411. {
  412. this.aTimer = this.schedule(this.CountDown,speed, cc.macro.REPEAT_FOREVER);
  413. }
  414. break;
  415. default:
  416. break;
  417. }
  418. },
  419. CountUp()
  420. {
  421. this.aGameStates.bCountUp = true;
  422. if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime)<=-10)
  423. {
  424. this.TimeUp();
  425. return;
  426. }
  427. this.TimeAdd();
  428. this.CurTimeLabel.string = this.TimeFormat(this.aGameStates.curtTime.second,this.aGameStates.curtTime.millisecond );
  429. },
  430. CountDown()
  431. {
  432. this.aGameStates.bCountUp = false;
  433. if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime)>=10)
  434. {
  435. this.TimeUp();
  436. return;
  437. }
  438. this.TimeMinus();
  439. this.CurTimeLabel.string = this.TimeFormat(this.aGameStates.curtTime.second,this.aGameStates.curtTime.millisecond );
  440. },
  441. TimeUp()
  442. {
  443. this.bPunchCD = true;
  444. this.StartOrStopButton.active = false;
  445. this.StopTimer();
  446. this.TrumpAnimScp.StopAnim();
  447. this.ResultScp.ShowResult(false);
  448. this.TrumpAnimScp.Fail(function ()
  449. {
  450. this.bTimerStart = false;//0停止 1开始
  451. this.StartOrStopButton.active = true;
  452. this.ResultScp.ShutDown();
  453. this.TrumpAnimScp.StartAnim();
  454. this.StartOrStopButton.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
  455. this.bPunchCD = false;
  456. }.bind(this));
  457. },
  458. StopTimer()
  459. {
  460. this.AudioControllerScp.stop('Game/Audios/BGM/CountTime.wav');
  461. if(this.aGameStates.bCountUp)
  462. {
  463. this.unschedule(this.CountUp);
  464. }
  465. else
  466. {
  467. this.unschedule(this.CountDown);
  468. }
  469. },
  470. TimeAdd()
  471. {
  472. if((this.aGameStates.curtTime.millisecond+1) >=10)
  473. {
  474. this.aGameStates.curtTime.millisecond = 0;
  475. this.aGameStates.curtTime.second++;
  476. return;
  477. }
  478. this.aGameStates.curtTime.millisecond++;
  479. },
  480. TimeMinus()
  481. {
  482. if((this.aGameStates.curtTime.millisecond-1) < 0)
  483. {
  484. this.aGameStates.curtTime.millisecond = 9;
  485. this.aGameStates.curtTime.second--;
  486. return;
  487. }
  488. this.aGameStates.curtTime.millisecond--;
  489. },
  490. Dif(curTime,targetTime)
  491. {
  492. let secondDif = targetTime.second - curTime.second;
  493. let millisecondDif = targetTime.millisecond - curTime.millisecond;
  494. return secondDif*10+millisecondDif;
  495. },
  496. TimeFormat(aSecond,aMillisecond)
  497. {
  498. let second = aSecond;
  499. let millisecond = aMillisecond;
  500. if(aSecond<10)
  501. {
  502. second = '0'+ aSecond;
  503. }
  504. if(aMillisecond<10)
  505. {
  506. millisecond = '0'+ aMillisecond;
  507. }
  508. return second+':'+millisecond;
  509. },
  510. RandomNum(minNum,maxNum){
  511. switch(arguments.length){
  512. case 1:
  513. return parseInt(Math.random()*minNum+1,10);
  514. break;
  515. case 2:
  516. return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
  517. break;
  518. default:
  519. return 0;
  520. break;
  521. }
  522. }
  523. });