| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- var aGameInstance = require('../GameInstance');
- let webView = require("../WebView");
- let lib = require("../Library");
- cc.Class({
- extends: cc.Component,
- properties: {
- AudioControllerNode: {
- default: null,
- type: cc.Node,
- serializable: true,
- },
- TrumpNode: {
- default: null,
- type: cc.Node,
- serializable: true,
- },
- TimeLabelNode: {
- default: null,
- type: cc.Node,
- serializable: true,
- },
- TargetTimeLabel: {
- default: null,
- type: cc.Label,
- serializable: true,
- },
- CurTimeLabel: {
- default: null,
- type: cc.Label,
- serializable: true,
- },
- StartOrStopButton: {
- default: null,
- type: cc.Node,
- serializable: true,
- },
- ResultNode: {
- default: null,
- type: cc.Node,
- serializable: true,
- },
- },
- onLoad () {
- //获取外部元素
- this.aGameStates = cc.find('GameStates').getComponent("GameStates");
- this.TrumpAnimScp = this.TrumpNode.getComponent("TrumpAnim");
- this.AudioControllerScp = this.AudioControllerNode.getComponent('AudioController');
- this.ResultScp = this.ResultNode.getComponent('Result');
- //初始化
- this.bTimerStart = false;//0停止 1开始
- this.bRunScene = false;
- this.bPunchCD = false;
- this.btnCD = false;
- switch (aGameInstance.sceneIdx) {
- case 0:
- this.aGameStates.targetTime.second = 3;
- this.aGameStates.targetTime.millisecond = 0;
- this.TargetTimeLabel.string = this.TimeFormat(this.aGameStates.targetTime.second,this.aGameStates.targetTime.millisecond);
- break;
- case 1:
- this.aGameStates.targetTime.second = 10;
- this.aGameStates.targetTime.millisecond = 0;
- this.TargetTimeLabel.string = this.TimeFormat(this.aGameStates.targetTime.second,this.aGameStates.targetTime.millisecond);
- break;
- case 2:
- this.TargetTimeLabel.string = '';
- break;
- case 3:
- this.TargetTimeLabel.string = '';
- break;
- default:
- break;
- }
- },
- start()
- {
- //是否在pC
- if(!lib.isMobile()) return;
- let self = this;
- self.bPunchCD = true;
- self.scheduleOnce(()=>{
- self.bPunchCD = false;
- },2);
- self.bRunScene = true;
- self.AudioControllerScp.playBGM('Game/Audios/BGM/Funny.wav',1,()=>{
- self.bRunScene = false;
- });
- webView.init(self.node, ()=>{
- webView.onBindHitBoxingPost();
- self.node.on('onBoxingPostHit',self.onBoxingPostHit,self);
- });
- },
- onBoxingPostHit(data)
- {
- this.Punch();
- },
- //页面退出回调
- onQuit(data)
- {
- console.log('onQuit=',data);
- },
- //弹出框回调
- onQuitModal(res)
- {
- if (res.data.confirm) {
- console.log("退出");
- }else if(res.data.cancel){
- console.log("取消退出");
- }
- },
- Back(event, param) {
-
- if(this.bRunScene)
- {
- return;
- }
- this.bRunScene = true;
- this.AudioControllerScp.stopAll();
- //取消这个组件得所有计时器
- this.unscheduleAllCallbacks();
- //是否在pC
- if(lib.isMobile()){
- webView.onUnbindHitBoxingPost();
- webView.unRegister(this.node);
- }
-
- this.scheduleOnce(()=>{
- cc.director.loadScene("Menu");
- },0.1);
- },
- StartOrStopOnClick(event, param) {
- //let target = event.target;//必须要给组件挂载target节点
- this.Punch();
- },
- Punch()
- {
- if(this.bPunchCD) return;
-
- this.bPunchCD = true;
- let target = this.StartOrStopButton;
- switch (aGameInstance.sceneIdx) {
- case 0:
- if(this.bTimerStart)
- {
- //停止
- target.active = false;
- target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
- this.StopTimer();
- this.TrumpAnimScp.StopAnim();
- if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime) == 0)
- {
- this.ResultScp.ShowResult(true);
- this.TrumpAnimScp.Win(function(){
- target.active = true;
- this.ResultScp.ShutDown();
- this.TrumpAnimScp.StartAnim();
- this.bPunchCD = false;
- }.bind(this));
- }
- else
- {
- this.ResultScp.ShowResult(false);
- this.TrumpAnimScp.Fail(function(){
- target.active = true;
- this.ResultScp.ShutDown();
- this.TrumpAnimScp.StartAnim();
- this.bPunchCD = false;
- }.bind(this));
- }
- }
- else
- {
- //开始
- target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'STOP';
-
- this.StartTimer();
- }
- this.bTimerStart = !this.bTimerStart;
- break;
- case 1:
- if(this.bTimerStart)
- {
- //停止
- target.active = false;
- target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
- this.StopTimer();
- this.TrumpAnimScp.StopAnim();
- if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime) == 0)
- {
- this.ResultScp.ShowResult(true);
- this.TrumpAnimScp.Win(function(){
- target.active = true;
- this.ResultScp.ShutDown();
- this.TrumpAnimScp.StartAnim();
- this.bPunchCD = false;
- }.bind(this));
- }
- else
- {
- this.ResultScp.ShowResult(false);
- this.TrumpAnimScp.Fail(function(){
- target.active = true;
- this.ResultScp.ShutDown();
- this.TrumpAnimScp.StartAnim();
- this.bPunchCD = false;
- }.bind(this));
- }
- }
- else
- {
- //开始
- target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'STOP';
-
- this.StartTimer();
- }
- this.bTimerStart = !this.bTimerStart;
- break;
- case 2:
- if(this.bTimerStart)
- {
- this.aGameStates.curRound++;
- //停止
-
- this.StopTimer();
- this.TrumpAnimScp.StopAnim();
- if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime) == 0)
- {
- if(this.aGameStates.curRound == 3)
- {
- target.active = false;
- target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
- this.StopTimer();
- this.TrumpAnimScp.StopAnim();
- this.bTimerStart = false;
- this.ResultScp.ShowResult(true);
- this.TrumpAnimScp.Win(function(){
- target.active = true;
- this.aGameStates.curRound = 0;
- this.ResultScp.ShutDown();
- this.TrumpAnimScp.StartAnim();
- this.TargetTimeLabel.string = '';
- this.bPunchCD = false;
- }.bind(this));
- }
- else
- {
- this.StartTimer();
- }
- }
- else
- {
- target.active = false;
- target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
- this.StopTimer();
- this.TrumpAnimScp.StopAnim();
- this.bTimerStart = false;
- this.ResultScp.ShowResult(false);
- this.TrumpAnimScp.Fail(function(){
- target.active = true;
- this.aGameStates.curRound = 0;
- this.ResultScp.ShutDown();
- this.TrumpAnimScp.StartAnim();
- this.TargetTimeLabel.string = '';
- this.bPunchCD = false;
- }.bind(this));
- }
- }
- else
- {
- //开始
- target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'STOP';
- this.StartTimer();
- }
- if(this.aGameStates.curRound == 0)
- {
- this.bTimerStart = !this.bTimerStart;
- }
- break;
- case 3:
- if(this.bTimerStart)
- {
- this.aGameStates.curRound++;
- //停止
-
- this.StopTimer();
- this.TrumpAnimScp.StopAnim();
- if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime) == 0)
- {
- if(this.aGameStates.curRound == 3)
- {
- target.active = false;
- target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
- this.StopTimer();
- this.TrumpAnimScp.StopAnim();
- this.bTimerStart = false;
- this.ResultScp.ShowResult(true);
- this.TrumpAnimScp.Win(function(){
- target.active = true;
- this.aGameStates.curRound = 0;
- this.ResultScp.ShutDown();
- this.TrumpAnimScp.StartAnim();
- this.TargetTimeLabel.string = '';
- this.bPunchCD = false;
- }.bind(this));
- }
- else
- {
- this.StartTimer();
- }
- }
- else
- {
- target.active = false;
- target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
- this.StopTimer();
- this.TrumpAnimScp.StopAnim();
- this.bTimerStart = false;
- this.ResultScp.ShowResult(false);
- this.TrumpAnimScp.Fail(function(){
- target.active = true;
- this.aGameStates.curRound = 0;
- this.ResultScp.ShutDown();
- this.TrumpAnimScp.StartAnim();
- this.TargetTimeLabel.string = '';
- this.bPunchCD = false;
- }.bind(this));
- }
- }
- else
- {
- //开始
- target.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'STOP';
- this.StartTimer();
- }
- if(this.aGameStates.curRound == 0)
- {
- this.bTimerStart = !this.bTimerStart;
- }
- break;
- default:
- break;
- }
- },
- StartTimer()
- {
- this.AudioControllerScp.playBGM('Game/Audios/BGM/CountTime.wav',0.5,()=>{
- this.bRunScene = false;
- });
- this.scheduleOnce(function(){
- this.bPunchCD = false;
- },1);
- this.aGameStates.curtTime.second = 0;
- this.aGameStates.curtTime.millisecond = 0;
- this.CurTimeLabel.string = this.TimeFormat(this.aGameStates.curtTime.second,this.aGameStates.curtTime.millisecond);
- switch (aGameInstance.sceneIdx) {
- case 0:
- this.aGameStates.targetTime.second = 3;
- this.aGameStates.targetTime.millisecond = 0;
- this.TargetTimeLabel.string = this.TimeFormat(this.aGameStates.targetTime.second,this.aGameStates.targetTime.millisecond);
- this.aTimer = this.schedule(this.CountUp,0.1, cc.macro.REPEAT_FOREVER);
- break;
- case 1:
- this.aGameStates.targetTime.second = 10;
- this.aGameStates.targetTime.millisecond = 0;
- this.TargetTimeLabel.string = this.TimeFormat(this.aGameStates.targetTime.second,this.aGameStates.targetTime.millisecond);
- this.aTimer = this.schedule(this.CountUp,0.01, cc.macro.REPEAT_FOREVER);
- break;
- case 2:
- while(true)
- {
- this.aGameStates.targetTime.second = this.RandomNum(this.aGameStates.targetSpan[0],this.aGameStates.targetSpan[1]);
- this.aGameStates.targetTime.millisecond = 0;
- // console.log('11=',this.aGameStates.lastTargetTime)
- // console.log('22=',this.aGameStates.targetTime)
- if(this.Dif(this.aGameStates.lastTargetTime,this.aGameStates.targetTime) != 0)
- {
- break;
- }
- }
- // //保存之前的随机值
- this.aGameStates.lastTargetTime.second = this.aGameStates.targetTime.second;
- this.aGameStates.lastTargetTime.millisecond = this.aGameStates.targetTime.millisecond;
- this.TargetTimeLabel.string = this.TimeFormat(this.aGameStates.targetTime.second,this.aGameStates.targetTime.millisecond);
- this.aTimer = this.schedule(this.CountUp,0.1, cc.macro.REPEAT_FOREVER);
- break;
- case 3:
- while(true)
- {
- this.aGameStates.targetTime.second = this.RandomNum(this.aGameStates.targetSpan[0],this.aGameStates.targetSpan[1]);
- this.aGameStates.targetTime.millisecond = 0;
- if(this.Dif(this.aGameStates.lastTargetTime,this.aGameStates.targetTime) != 0)
- {
- break;
- }
- }
- while(true)
- {
- this.aGameStates.curtTime.second = this.RandomNum(this.aGameStates.curSpan[0],this.aGameStates.curSpan[1]);
- this.aGameStates.curtTime.millisecond = 0;
- if(this.Dif(this.aGameStates.lastCurtTime,this.aGameStates.curtTime) != 0 && this.Dif(this.aGameStates.targetTime,this.aGameStates.curtTime) != 0)
- {
- break;
- }
- }
- //保存之前的随机值
- this.aGameStates.lastTargetTime.second = this.aGameStates.targetTime.second;
- this.aGameStates.lastTargetTime.millisecond = this.aGameStates.targetTime.millisecond;
- this.aGameStates.lastCurtTime.second = this.aGameStates.curtTime.second;
- this.aGameStates.lastCurtTime.millisecond = this.aGameStates.curtTime.millisecond;
-
- this.TargetTimeLabel.string = this.TimeFormat(this.aGameStates.targetTime.second,this.aGameStates.targetTime.millisecond);
- this.CurTimeLabel.string = this.TimeFormat(this.aGameStates.curtTime.second,this.aGameStates.curtTime.millisecond);
- let speed = 0.1;
- if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime)>0)
- {
- this.aTimer = this.schedule(this.CountUp,speed, cc.macro.REPEAT_FOREVER);
- }
- else if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime)<0)
- {
- this.aTimer = this.schedule(this.CountDown,speed, cc.macro.REPEAT_FOREVER);
- }
- break;
- default:
- break;
- }
- },
- CountUp()
- {
- this.aGameStates.bCountUp = true;
- if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime)<=-10)
- {
- this.TimeUp();
- return;
- }
- this.TimeAdd();
- this.CurTimeLabel.string = this.TimeFormat(this.aGameStates.curtTime.second,this.aGameStates.curtTime.millisecond );
- },
- CountDown()
- {
- this.aGameStates.bCountUp = false;
- if(this.Dif(this.aGameStates.curtTime,this.aGameStates.targetTime)>=10)
- {
- this.TimeUp();
- return;
- }
- this.TimeMinus();
- this.CurTimeLabel.string = this.TimeFormat(this.aGameStates.curtTime.second,this.aGameStates.curtTime.millisecond );
- },
- TimeUp()
- {
- this.bPunchCD = true;
- this.StartOrStopButton.active = false;
- this.StopTimer();
- this.TrumpAnimScp.StopAnim();
- this.ResultScp.ShowResult(false);
- this.TrumpAnimScp.Fail(function ()
- {
- this.bTimerStart = false;//0停止 1开始
- this.StartOrStopButton.active = true;
- this.ResultScp.ShutDown();
- this.TrumpAnimScp.StartAnim();
- this.StartOrStopButton.getChildByName("Background").getChildByName("Label").getComponent(cc.Label).string = 'RESTART';
- this.bPunchCD = false;
- }.bind(this));
- },
- StopTimer()
- {
- this.AudioControllerScp.stop('Game/Audios/BGM/CountTime.wav');
- if(this.aGameStates.bCountUp)
- {
- this.unschedule(this.CountUp);
- }
- else
- {
- this.unschedule(this.CountDown);
- }
- },
- TimeAdd()
- {
- if((this.aGameStates.curtTime.millisecond+1) >=10)
- {
- this.aGameStates.curtTime.millisecond = 0;
- this.aGameStates.curtTime.second++;
- return;
- }
- this.aGameStates.curtTime.millisecond++;
- },
- TimeMinus()
- {
- if((this.aGameStates.curtTime.millisecond-1) < 0)
- {
- this.aGameStates.curtTime.millisecond = 9;
- this.aGameStates.curtTime.second--;
- return;
- }
- this.aGameStates.curtTime.millisecond--;
- },
- Dif(curTime,targetTime)
- {
- let secondDif = targetTime.second - curTime.second;
- let millisecondDif = targetTime.millisecond - curTime.millisecond;
- return secondDif*10+millisecondDif;
- },
- TimeFormat(aSecond,aMillisecond)
- {
- let second = aSecond;
- let millisecond = aMillisecond;
- if(aSecond<10)
- {
- second = '0'+ aSecond;
- }
- if(aMillisecond<10)
- {
- millisecond = '0'+ aMillisecond;
- }
- return second+':'+millisecond;
- },
- RandomNum(minNum,maxNum){
- switch(arguments.length){
- case 1:
- return parseInt(Math.random()*minNum+1,10);
- break;
- case 2:
- return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10);
- break;
- default:
- return 0;
- break;
- }
- }
- });
|