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; } } });