// Learn cc.Class: // - https://docs.cocos.com/creator/manual/en/scripting/class.html // Learn Attribute: // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html // window.onWebViewMessage = function (data) { // let name = data.funName; // if (name == "onWatchAccelerometer") { // /** // * 返回加速计的数据 // * { // * xAxis // * yAxis // * zAxis // * } // */ // webView.onUpdateAcc(data); // } // } // window.onWebViewMessage = function (data) { // let name = data.funName; // if (name == "onWatchAccelerometer") { // /** // * 返回加速计的数据 // * { // * xAxis // * yAxis // * zAxis // * } // */ // webView.onUpdateAcc(data); // } else if (name == "onWatchOrientation") { // /** // * 返回加速计的数据 // * { // * xAxis // * yAxis // * zAxis // * } // */ // webView.onUpdateOri(data); // } // } import notifyCenter from '../utils/global'; cc.Class({ extends: cc.Component, properties: { luckyNode: { default: null, type: cc.Node, serializable: true, }, luckyScript: { default: null, visible: false, serializable: true, }, mass: { default: 50, type: cc.Integer, tooltip: "物体质量/kg", serializable: true, }, xLCount: { default: 0, type: cc.Float, tooltip: "x轴负向", visible: false, serializable: false, }, xRCount: { default: 0, type: cc.Float, tooltip: "x轴正向", visible: false, serializable: false, }, yLCount: { default: 0, type: cc.Float, tooltip: "y轴负向", visible: false, serializable: false, }, yRCount: { default: 0, type: cc.Float, tooltip: "y轴正向", visible: false, serializable: false, }, zLCount: { default: 0, type: cc.Float, tooltip: "z轴负向", visible: false, serializable: false, }, zRCount: { default: 0, type: cc.Float, tooltip: "z轴正向", visible: false, serializable: false, }, //记录一次打击,如果通方向就更新最大值 hitFirst: { default: null, visible: false, serializable: true, }, //锁住hit update 更新情况 bLock: { default: false, visible: false, serializable: false, }, //定义一个状态字典 hitState: { default: null, serializable: false, visible: false, }, staticTime: { default: 1, type: cc.Float, serializable: true, tooltip: "立柱静止的检测时间" }, bSwing: { default: false, serializable: true, tooltip: "是否摆动" }, showCalorieLabel: { default: null, type: cc.Label, serializable: true }, LCount: { default: 0, type: cc.Integer, tooltip: "左勾拳打击次数", visible: false, serializable: false, }, RCount: { default: 0, type: cc.Integer, tooltip: "右勾拳打击次数", visible: false, serializable: false, }, ZCount: { default: 0, type: cc.Integer, tooltip: "直拳打击次数", visible: false, serializable: false, }, AllCalorie: { default: 0, type: cc.Integer, tooltip: "总共的卡路里", visible: false, serializable: false, }, AllPower: { default: 0, type: cc.Integer, tooltip: "三次下来的总力量", visible: false, serializable: false, }, }, // LIFE-CYCLE CALLBACKS: onLoad() { window.webView = this; this.luckyScript = this.luckyNode.getComponent("lucky"); this.hitState = { "xLCount": 0, "xRCount": 0, "zLCount": 0, "zRCount": 0 } }, start() { this.onBind(); }, onLeft() { let temp = { gameData: { xAxis: -10.5, yAxis: 5, zAxis: 0 } } this.onUpdateAcc(temp); }, onRight() { let temp = { gameData: { xAxis: 10.5, yAxis: 5, zAxis: 0 } } this.onUpdateAcc(temp); }, onMid() { let temp = { gameData: { xAxis: 0, yAxis: 5, zAxis: 100.5 } } this.onUpdateAcc(temp); }, onStatic() { let temp = { gameData: { xAxis: 0, yAxis: 9.8, zAxis: 0 } } this.onUpdateAcc(temp); }, //重置一下,记录的数据 onResetAccState() { console.log("重置 onResetAccState"); this.hitFirst = null; this.bLock = false; this.hitState = { "xLCount": 0, "xRCount": 0, "zLCount": 0, "zRCount": 0 } }, onBind() { // uni.postMessage({ // data: { // funName: "openAccelerometer", // gameData: {} // } // }) // uni.postMessage({ // data: { // funName: "openOrientation", // gameData: {} // } // }) uni.postMessage({ data: { funName: "bindHitBoxingPost", gameData: {} } }) notifyCenter.on('webViewMessage', (data) => { let name = data.funName; if (name == "onBoxingPostHit") { console.log(data); webView.onUpdateBoxingPostHit(data.gameData); } }); }, onUnBind() { // uni.postMessage({ // data: { // funName: "closeAccelerometer", // gameData: {} // } // }) // uni.postMessage({ // data: { // funName: "closeOrientation", // gameData: {} // } // }) notifyCenter.off('webViewMessage'); }, onUpdateBoxingPostHit(gamedata) { let gdata = gamedata; if (gdata.direction == "leftPunch") { this.LCount++; this.onHit("xRCount", gdata.value, Math.ceil(gdata.value / 10)); } else if (gdata.direction == "rightPunch") { this.RCount++; this.onHit("xLCount", gdata.value, Math.ceil(gdata.value / 10)); } else if (gdata.direction == "straightPunch") { this.ZCount++; this.onHit("zLCount", gdata.value, Math.ceil(gdata.value / 10)); } }, onResetXL() { this.bCanXL = false; }, onResetXR() { this.bCanXR = false; }, onUpdateAcc(data) { let a = data.gameData; this.xA = a.xAxis; this.yA = a.yAxis; this.zA = a.zAxis; }, onUpdateOri(data) { let o = data.gameData; this.xO = o.beta; this.zO = o.alpha; this.yO = o.gamma; // 游戏未开始,不记录数据 if (!this.luckyScript.gameRun) return; //1.求出z 和 x 的加速度矢量 //z轴的重力加速度矢量分量 this.calZVector = (Math.cos(this.xO / 180 * Math.PI) * 9.8); //x轴的重力加速度的矢量分量 if (this.yA > 9.8) this.yA = 9.8; let tempXVector = Math.pow(9.8, 2) - (Math.pow(this.calZVector, 2) + Math.pow(this.yA, 2)); this.calXVector = Math.sqrt(Math.abs(tempXVector)); //2.当前的加速度矢量减去分量,就是打击的加速度 let tempZ = Math.abs(this.zA) - Math.abs(this.calZVector); let tempX = Math.abs(this.xA) - Math.abs(this.calXVector); //3.判断 那个轴的打击方向,就走哪个轴的计算流程 //左勾拳,手机的左边受力,向右运动 let tempValue = 2; //|| (this.yO > this.oriRefValue["gamma"] + tempValue && this.xO < this.oriRefValue["beta"] + tempValue) if ((this.xA > tempValue && this.zA < tempValue)) { if (this.bCanXL) { setTimeout(() => { this.onResetXL(); }, 1000); return; } let _endPower = (Math.abs(tempX) + Math.abs(tempZ)) * this.mass; if (Math.abs(tempZ) > 3) { this.LCount++; this.onHit("xRCount", tempZ, Math.ceil(_endPower)); } this.bCanXL = true; } //右勾拳,手机右边受力,向左运动 if ((this.xA < -tempValue && this.zA < tempValue)) { if (this.bCanXR) { setTimeout(() => { this.onResetXR(); }, 1000); return; } // console.log("有力量啊:", tempX, tempZ); let _endPower = (Math.abs(tempX) + Math.abs(tempZ)) * this.mass; if (Math.abs(tempZ) > 3) { this.RCount++; this.onHit("xLCount", tempZ, Math.ceil(_endPower)); } this.bCanXR = true; } //直拳判断 && Math.abs(tempZ) > 0.5 if (this.zA > 2 && Math.abs(this.xA) < tempValue) { // if (Math.abs(this.yO - this.oriRefValue["gamma"]) > 2 || this.xO > this.oriRefValue["beta"]) return; if (this.bCanZ) { setTimeout(() => { this.bCanZ = false; }, 1000); return; } let _endPower = Math.abs(tempZ) * this.mass; if (Math.abs(tempZ) > 3) { this.ZCount++; this.onHit("zLCount", tempZ, Math.ceil(_endPower)); } this.bCanZ = true; } let allCount = this.ZCount + this.LCount + this.RCount; //打一拳,大约消耗的热量,是450*4/60=1.875 焦耳。 //因为打拳一小时,需要消耗的热量是450大卡,而一分钟约打一下,一大卡是4焦耳。 this.AllCalorie = Math.floor((allCount * 1.875) / 4); this.showCalorieLabel.string = "消耗卡路里:" + this.AllCalorie + "大卡"; }, // "xLCount": 0, // "xRCount": 0, // "zLCount": 0, // "zRCount": 0 onHit(direction, direValue, power) { console.log(direction, direValue, power); // if (this.bCanZ || this.bCanXL || this.bCanXR) return; let temp = { direction: direction, value: direValue, mass: this.mass, //质量 hitPower: power//计算的力 } //总的力量 this.AllPower += power; this.luckyScript.onHitFromDevice(temp, () => { //重新记录值 this.onResetAccState(); }); }, onResetAllValue(){ this.AllCalorie = 0; this.AllPower = 0; this.showCalorieLabel.string = "消耗卡路里:" + this.AllCalorie + "大卡"; } });