var gameConfig = require("./GameConfig.js"); var webView = require("../WebView"); var lib = require("../Library"); var mgobe = require("../Mgobe"); var EquipmentAction = require("../EquipmentAction"); cc.Class({ extends: require("BasePlayerController"), properties: { mainCamera: { default: null, type: cc.Node, serializable: true, }, mapArr: { default: [], type: [cc.Node], // type 同样写成数组,提高代码可读性 }, locationLine: { default: null, type: cc.Node, serializable: true, }, staggerAudio: { default: null, type: cc.AudioClip, }, resultBar: { default: null, type: cc.Node, serializable: true, }, cheerAudio: { default: null, type: cc.AudioClip, }, rival: { default: null, type: cc.Node, serializable: true, }, //设备数据判断对象 equipmentObj: null, }, init() {//override this._super(); this.locationLine.zIndex = 2; this.resultBarSt = this.resultBar.getComponent('ResultBar'); }, start()//override { this._super(); if (lib.openInWebview()) { // 在app内Webview打开 webView.register(this.node); this.node.on('onDeviceUpdateData', this.onDeviceUpdateData, this); // this.gStatesSt.playerId = mgobe.player.id; //监听回调 uni.postMessage({ data: { funName: "addDeviceUpdateListener", gameData: {} } }) //开启设备回调 uni.postMessage({ data: { funName: "writeBLEConnectionValue", gameData: { value: "3" //开启设备数据 } } }) //开启设备频率 uni.postMessage({ data: { funName: "writeBLEConnectionValue", gameData: { value: "b" //60ms } } }) this.equipmentObj = new EquipmentAction(); this.equipmentObj.addEventListener("resultant", (res) => { let data = res; // console.log("resultant:",data); if(data.type == "jump"){ this.jump(); }else if(data.type == "runing"){ this.speedUp(); } }) } }, update(dt) {//override this._super(); //make ui and camera follow the player this.gmSt.ui.x = this.node.x; this.mainCamera.x = this.node.x; if (this.pStatesSt.currentProState == this.pStatesSt.pProStates.end) return; //indicator if (this.node.x - this.rival.x > 360)//forward show left bar { this.ctorSt.showIndicater(1, Math.abs(Math.round(this.node.x - this.rival.x - 38))); } else if (this.node.x - this.rival.x < -360)//backward show right bar { this.ctorSt.showIndicater(0, Math.abs(Math.round(this.node.x - this.rival.x + 38))); } else { this.ctorSt.hideIndicater(); } //生成主角前方地图 this.switchMap(); }, switchMap() { if (this.node.x > this.pStatesSt.targetPoint) { this.mapArr[0].x += 2160; lib.swapLeft(this.mapArr); this.pStatesSt.targetPoint += 720; } }, setEndSpeed() {//overwrite this.pStatesSt.intoTheEndSpeed = this.pStatesSt.currentSpeed / (this.gmSt.terminal.x - this.pStatesSt.endLineStopPoint); }, passedHandrail()//overwrite { let handrail = this.pStatesSt.nextHandrail.getChildByName('Kuolan1'); let spine = handrail.getComponent(sp.Skeleton); spine.setAnimation(0, 'Idl1_3', false); this.ctorSt.stagger(function () { this.pStatesSt.Combo = 0; this.createAndDestroyHandrail(); }.bind(this)); cc.audioEngine.playEffect(this.staggerAudio, 0, function () { }); }, createAndDestroyHandrail() { while (true) { let hurdrailPX = this.gmSt.createHandrail(); if (hurdrailPX - this.node.x > 360 || hurdrailPX == -1) { break; } } //GC drawedhandrail let deleteHandrail = this.gStatesSt.drawedhandrailArr.shift(); this.gStatesSt.handrailGCArr.push(deleteHandrail); if (this.gStatesSt.drawedhandrailArr.length != 0) { this.pStatesSt.nextHandrail = this.gStatesSt.drawedhandrailArr[0].handrail; } //destroy hurdrail let len = this.gStatesSt.handrailGCArr.length; for (let i = 0; i < len; i++) { if (this.node.x - this.gStatesSt.handrailGCArr[i].x > 720) { let handrail = this.gStatesSt.handrailGCArr[i]; delete this.gStatesSt.handrailGCArr[i]; handrail.destroy(); } } // because delete array will left undefined element so we need to remove it from array for (let i = 0; i < len; i++) { if (this.gStatesSt.handrailGCArr[i] == undefined) { this.gStatesSt.handrailGCArr.splice(i); } } }, enterEndline() {//override this._super(); if (this.gStatesSt.arrEndPArr.length == 1) { this.scheduleOnce(function () { this.setResuletBar(); }, 3); this.firework(); } else { this.schedule(function () { this.resultBar.active = true; this.resultBar.getChildByName('Win').active = false; this.resultBar.getChildByName('Fail').active = true; this.setResuletBar(); }, 1, 0, 0); } }, setResuletBar() { this.resultBarSt.init(); this.gmSt.unschedule(this.gmSt.countGameTime); this.resultBarSt.updateCostTime(this.gStatesSt.gameTime); this.resultBarSt.updateAvatar(); //cal calorie let runUnit = 0; let jumpUnit = 0; let calorie = 0; if( webView.calorieParams != null) { runUnit = webView.calorieParams.runUnit; jumpUnit = webView.calorieParams.jumpUnit; calorie = parseInt(this.pStatesSt.runTimes * runUnit + this.pStatesSt.jumpTimes * jumpUnit); } this.resultBarSt.updateCalorie(calorie); if (lib.openInWebview()) { webView.postMessage(100, this.gmSt.countGameTime, calorie); } }, firework() { cc.audioEngine.playEffect(this.cheerAudio, 0, function () { }); let firework = this.gmSt.terminal.getChildByName("Firework"); firework.active = true; firework.zIndex = 1005; }, speedUp()//override { this._super(); if (lib.openInWebview() && !webView.bAi) { mgobe.sendFrame('speedUp'); } }, jump() {//overwrite //super class return is not effect to sub class so we need to write if return every subclass // if (this.gStatesSt.drawedhandrailArr.length == 0) return; if (this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump) return; //init locationLine do once this.locationLine.opacity = 0; this.locationLine.x = this.node.x; //No perfect jump area if (this.locationLine.x > this.pStatesSt.nextHandrail.x && this.locationLine.x < this.pStatesSt.nextHandrail.x + gameConfig.handrailArea1.end) { this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump; this.locationLine.opacity = 255; this.ctorSt.jump('hurdling_1', function () { this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run; this.createAndDestroyHandrail(); this.pStatesSt.passedHurdrailNum++; this.pStatesSt.jumpTimes++; }.bind(this),1); if (lib.openInWebview() && !webView.bAi) { mgobe.sendFrame('jump'); } return; } //perfect jump area if (this.locationLine.x > this.pStatesSt.nextHandrail.x + gameConfig.handrailArea2.start && this.locationLine.x < this.pStatesSt.nextHandrail.x + gameConfig.handrailArea2.end) { this.pStatesSt.nextHandrail.getChildByName('AccelerationBandCenter').destroy(); this.locationLine.opacity = 255; this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump; this.ctorSt.jump('hurdling_1', function () { this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run; this.createAndDestroyHandrail(); this.pStatesSt.passedHurdrailNum++; this.pStatesSt.jumpTimes++; if (lib.openInWebview() && !webView.bAi) { mgobe.sendFrame('jump'); } }.bind(this),1); return; } // this.pStatesSt.currentState = this.pStatesSt.playerAnimState.jump; // this.locationLine.opacity = 255; // this.ctorSt.jump('hurdling_1', function () { // this.pStatesSt.currentState = this.pStatesSt.playerAnimState.run; // // this.createAndDestroyHandrail(); // // this.pStatesSt.passedHurdrailNum++; // // this.pStatesSt.jumpTimes++; // }.bind(this),0); // if (lib.openInWebview() && !webView.bAi) { // mgobe.sendFrame('jump'); // } }, //interact with device onDeviceUpdateData(data) { // console.log(data); //判断数据类型 if (data.dataType == "Box") { // webView.onBLEBoxUpdate(data); let gameData = data.data; this.equipmentObj.updateJumpAndRun({ // xA: data.data.acc.ax, // zA: data.data.acc.ay, // yA: data.data.acc.az, acc: gameData.acc, gyro: gameData.gyro, bLimitRebound: false }) } // if (data.F == 0) { // console.log("F == 0"); // this.speedUp(); // } else if (data.F == 1) { // console.log("F == 1"); // this.speedUp(); // } // else if (data.F == 2) { // console.log("F == 2"); // this.jump(); // } }, }); // mgobe