| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809 |
- // window.onWebViewMessage = function (data) {
- // let name = data.funName;
- // if (name == "onWatchAccelerometer") {
- // /**
- // * 返回加速计的数据
- // * {
- // * xAxis
- // * yAxis
- // * zAxis
- // * }
- // */
- // testWeb.onUpdateAcc(data);
- // } else if (name == "onWatchOrientation") {
- // /**
- // * 返回加速计的数据
- // * {
- // * xAxis
- // * yAxis
- // * zAxis
- // * }
- // */
- // testWeb.onUpdateOri(data);
- // } else if (name == "onBoxingPostHit") {
- // console.log(data);
- // testWeb.onUpdateBoxingPostHit(data.gameData);
- // } else if (name == "updateAccAndGyro") {
- // // console.log(data.gameData);
- // testWeb.onUpdateAccAndGyro(data.gameData);
- // }
- // }
- import notifyCenter from '../utils/global';
- var o0 = require('o0');
- var o0Project = require('o0Project');
- // var o0ProjectRelease = require('o0ProjectRelease');
- var o0ProjectRelease = require('o0ProjectRelease0.1');
- cc.Class({
- extends: cc.Component,
- properties: {
- count: 10,
- mass: {
- default: 50,
- type: cc.Integer,
- tooltip: "物体质量/kg",
- 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,
- },
- showLCount: {
- default: null,
- type: cc.Label,
- serializable: true
- },
- showLPower: {
- default: null,
- type: cc.Label,
- serializable: true
- },
- showRCount: {
- default: null,
- type: cc.Label,
- serializable: true
- },
- showRPower: {
- default: null,
- type: cc.Label,
- serializable: true
- },
- showMCount: {
- default: null,
- type: cc.Label,
- serializable: true
- },
- showMPower: {
- default: null,
- type: cc.Label,
- serializable: true
- },
- showOriLabel: {
- default: null,
- type: cc.Label,
- serializable: true
- },
- showAccLabel: {
- default: null,
- type: cc.Label,
- serializable: true
- },
- showCalLabel: {
- default: null,
- type: cc.Label,
- serializable: true
- },
- xA: {
- default: 0,
- type: cc.Float,
- tooltip: "x轴负向",
- visible: false,
- serializable: false,
- },
- yA: {
- default: 0,
- type: cc.Float,
- tooltip: "y轴负向",
- visible: false,
- serializable: false,
- },
- zA: {
- default: 0,
- type: cc.Float,
- tooltip: "z轴负向",
- visible: false,
- serializable: false,
- },
- xO: {
- default: 0,
- type: cc.Float,
- tooltip: "以x方向为轴心的旋转角度,beta",
- visible: false,
- serializable: false,
- },
- yO: {
- default: 0,
- type: cc.Float,
- tooltip: "以y方向为轴心的旋转角度, gamma",
- visible: false,
- serializable: false,
- },
- zO: {
- default: 0,
- type: cc.Float,
- tooltip: "以z方向为轴心的旋转角度,alpha",
- visible: false,
- serializable: false,
- },
- calZVector: {
- default: 0,
- type: cc.Float,
- tooltip: "通过陀螺仪计算的z轴加速度分量",
- visible: false,
- serializable: false,
- },
- oldZ: {
- default: 0,
- type: cc.Float,
- tooltip: "相等时候,重置旧的值",
- visible: false,
- serializable: false,
- },
- oldXL: {
- default: 0,
- type: cc.Float,
- tooltip: "相等时候,重置旧的值",
- visible: false,
- serializable: false,
- },
- //检测左手打击
- bCanXL: {
- default: false,
- visible: false,
- serializable: true
- },
- oldXLArr: {
- default: [],
- visible: false,
- serializable: true,
- },
- oldXLOriArr: {
- default: [],
- visible: false,
- serializable: true,
- },
- oldXLTempZArr: {
- default: [],
- visible: false,
- serializable: true,
- },
- oldXLTempArr: {
- default: [],
- visible: false,
- serializable: true,
- },
- // 检测右手打击
- bCanXR: {
- default: false,
- visible: false,
- serializable: true
- },
- oldXRAccArr: {
- default: [],
- visible: false,
- serializable: true,
- },
- oldXROriArr: {
- default: [],
- visible: false,
- serializable: true,
- },
- oldXRTempArr: {
- default: [],
- visible: false,
- serializable: true,
- },
- oldXRTempZArr: {
- default: [],
- visible: false,
- serializable: true,
- },
- bCanZ: {
- default: false,
- visible: false,
- serializable: true
- },
- oldXR: {
- default: 0,
- type: cc.Float,
- tooltip: "相等时候,重置旧的值",
- visible: false,
- serializable: false,
- },
- oldGamma: {
- default: 0,
- type: cc.Float,
- tooltip: "旧的y旋转值",
- visible: false,
- serializable: false,
- },
- calXVector: {
- default: 0,
- type: cc.Float,
- tooltip: "加速度矢量计算的x分量",
- visible: false,
- serializable: false,
- },
- // 打一拳,大约消耗的热量,是450*4/60=1.875 焦耳。
- // 因为打拳一小时,需要消耗的热量是450大卡,而一分钟约打一下,一大卡是4焦耳。
- calorie: {
- default: 0,
- type: cc.Float,
- tooltip: "x轴负向",
- visible: false,
- serializable: false,
- },
- showCalorieLabel: {
- default: null,
- type: cc.Label,
- serializable: true
- },
- //ori基准值
- oriRefValue: {
- default: null,
- visible: false,
- serializable: false,
- },
- audioNode: {
- default: null,
- type: cc.Node,
- serializable: true,
- },
- audioScript: {
- default: null,
- visible: false,
- },
- accAndGyroLabel: {
- default: null,
- type: cc.Label,
- serializable: true
- },
- Filter: null,
- LastTime: new Date().getTime(),
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad() {
- window.testWeb = this;
- this.audioScript = this.audioNode.getComponent("testAudio");
- // this.Filter = new o0Project.Filter();
- this.Filter = new o0ProjectRelease.SandbagAlgorithm();
- },
- start() {
- this.oriRefValue = {
- "alpha": 0,
- "beta": 0,
- "gamma": 0,
- }
- this.onBind();
- },
- onResetRef() {
- // this.xO = o.beta;
- // this.zO = o.alpha;
- // this.yO = o.gamma;
- this.oriRefValue["alpha"] = this.zO;
- this.oriRefValue["beta"] = this.xO;
- this.oriRefValue["gamma"] = this.yO;
- },
- onBind() {
- uni.postMessage({
- data: {
- funName: "openAccelerometer",
- gameData: {}
- }
- })
- // uni.postMessage({
- // data: {
- // funName: "openOrientation",
- // gameData: {}
- // }
- // })
- // console.log("onBind");
- // uni.postMessage({
- // data: {
- // funName: "bindHitBoxingPost",
- // gameData: {}
- // }
- // })
- notifyCenter.on('webViewMessage', (data) => {
- // console.log(data);
- let name = data.funName;
- if (name == "onWatchAccelerometer") {
- /**
- * 返回加速计的数据
- * {
- * xAxis
- * yAxis
- * zAxis
- * }
- */
- testWeb.onUpdateAcc(data);
- } else if (name == "onWatchOrientation") {
- /**
- * 返回加速计的数据
- * {
- * xAxis
- * yAxis
- * zAxis
- * }
- */
- testWeb.onUpdateOri(data);
- } else if (name == "onBoxingPostHit") {
- console.log(data);
- testWeb.onUpdateBoxingPostHit(data.gameData);
- } else if (name == "updateAccAndGyro") {
- // console.log(data.gameData);
- testWeb.onUpdateAccAndGyro(data.gameData);
- }
- });
- // uni.postMessage({
- // data: {
- // funName: "onStartAccAndGyro",
- // gameData: {}
- // }
- // })
- console.log("onStartAccAndGyro");
- },
- onUnBind() {
- notifyCenter.off('webViewMessage');
- // uni.postMessage({
- // data: {
- // funName: "onStopAccAndGyro",
- // gameData: {}
- // }
- // })
- // uni.postMessage({
- // data: {
- // funName: "unbindHitBoxingPost",
- // gameData: {}
- // }
- // })
- uni.postMessage({
- data: {
- funName: "closeAccelerometer",
- gameData: {}
- }
- })
- // uni.postMessage({
- // data: {
- // funName: "closeOrientation",
- // gameData: {}
- // }
- // })
- },
- onResetXL() {
- this.bCanXL = false;
- this.oldXLArr = [];
- this.oldXLOriArr = [];
- this.oldXLTempArr = [];
- this.oldXLTempZArr = [];
- },
- onResetXR() {
- this.bCanXR = false;
- this.oldXRAccArr = [];
- this.oldXROriArr = [];
- this.oldXRTempArr = [];
- this.oldXRTempZArr = [];
- },
- onUpdateAcc(data) {
- let a = data.gameData;
- this.xA = a.xAxis;
- this.yA = a.yAxis;
- this.zA = a.zAxis;
- },
- onPlayAudio() {
- this.audioScript.onPlayBoxingTip(0);
- },
- onUpdateOri(data) {
- let o = data.gameData;
- this.showAccLabel.string = "x:" + this.xA.toFixed(2) + "\ny:" + this.yA.toFixed(2) + "\nz:" + this.zA.toFixed(2);
- this.showOriLabel.string = "Orientation\nz-Alpha:" + o.alpha + "\nx-Beta:" + o.beta + "\ny-Gamma:" + o.gamma;
- this.xO = o.beta;
- this.zO = o.alpha;
- this.yO = o.gamma;
- //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.判断 那个轴的打击方向,就走哪个轴的计算流程
- //左勾拳,手机的左边受力,向右运动 && this.oldXLArr.length < 5 && this.xA < -1
- 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;
- }
- this.bCanXL = true;
- let _endPower = (Math.abs(tempX) + Math.abs(tempZ)) * this.mass;
- if (Math.abs(tempZ) > 3) {
- this.LCount++;
- this.showLCount.string = "L击中次数" + this.LCount;
- // this.showLPower.string = _endPower.toFixed(3) + " kg";
- this.showCalLabel.string = _endPower.toFixed(1) + " 斤";
- this.audioScript.onPlayBoxingTip(0);
- }
- // this.oldXLArr.push(this.xA);
- // this.oldXLOriArr.push(this.yO);
- // this.oldXLTempArr.push(tempX);
- // this.oldXLTempZArr.push(tempZ);
- // //如果记录第一次时候,只有一个值
- // if (this.oldXLArr.length == 1) {
- // // setTimeout(() => {
- // if (this.bCanXL) return;
- // this.bCanXL = true;
- // //去第一帧受力,和 记录的最大值 gamma 角度 方向相同
- // let _max = Math.max.apply(null, this.oldXLOriArr);
- // let _maxIndex = 0;
- // for (var i = 0; i < this.oldXLOriArr.length - 1; i++) {
- // if (this.oldXLOriArr[i] == _max) {
- // _maxIndex = i;
- // break;
- // }
- // }
- // let _power = this.oldXLTempArr[_maxIndex] ? this.oldXLTempArr[_maxIndex].toFixed(3) : 0;
- // let _zPower = this.oldXRTempZArr[_maxIndex] ? this.oldXRTempZArr[_maxIndex].toFixed(3) : 0;
- // if (_power != 0) {
- // this.LCount++;
- // this.showLCount.string = "L击中次数" + this.LCount;
- // let _endPower = (Math.abs(_power) + Math.abs(_zPower)) * this.mass;
- // this.showLPower.string = _endPower.toFixed(3) + " kg";
- // }
- // // console.log(this.oldXLArr, this.oldXLOriArr);
- // // }, 100);
- // }
- }
- //右勾拳,手机右边受力,向左运动 && this.oldXRAccArr.length < 5 && this.xA > 2
- // ||
- // (this.yO < this.oriRefValue["gamma"] - tempValue && this.xO < this.oriRefValue["beta"] - tempValue)
- if ((this.xA < -tempValue && this.zA < tempValue)) {
- if (this.bCanXR) {
- setTimeout(() => {
- this.onResetXR();
- }, 1000);
- return;
- }
- this.bCanXR = true;
- // console.log("有力量啊:", tempX, tempZ);
- let _endPower = (Math.abs(tempX) + Math.abs(tempZ)) * this.mass;
- if (Math.abs(tempZ) > 3) {
- this.RCount++;
- this.showRCount.string = "R击中次数" + this.RCount;
- // this.showRPower.string = _endPower.toFixed(3) + " kg";
- this.showCalLabel.string = _endPower.toFixed(1) + " 斤";
- this.audioScript.onPlayBoxingTip(2);
- }
- // this.oldXRAccArr.push(this.xA);
- // this.oldXROriArr.push(this.yO);
- // this.oldXRTempArr.push(tempX);
- // this.oldXRTempZArr.push(tempZ);
- // console.log("tempX", tempX);
- //如果记录第一次时候,只有一个值
- // if (this.oldXRAccArr.length == 1) {
- // // setTimeout(() => {
- // if (this.bCanXR) return;
- // this.bCanXR = true;
- // //去第一帧受力,和 记录的最大值 gamma 角度 方向相同
- // let _max = Math.max.apply(null, this.oldXROriArr);
- // let _maxIndex = 0;
- // for (var i = 0; i < this.oldXROriArr.length - 1; i++) {
- // if (this.oldXROriArr[i] == _max) {
- // _maxIndex = i;
- // console.log("==_maxIndex:", _maxIndex);
- // break;
- // }
- // }
- // let _power = this.oldXRTempArr[_maxIndex] ? this.oldXRTempArr[_maxIndex].toFixed(3) : 0;
- // let _zPower = this.oldXRTempZArr[_maxIndex] ? this.oldXRTempZArr[_maxIndex].toFixed(3) : 0;
- // if (_power != 0) {
- // console.log("有力量啊:", this.oldXRTempArr[_maxIndex]);
- // this.RCount++;
- // this.showRCount.string = "R击中次数" + this.RCount;
- // let _endPower = (Math.abs(_power) + Math.abs(_zPower)) * this.mass;
- // this.showRPower.string = _endPower.toFixed(3) + " kg";
- // }
- // // console.log("R=", this.oldXRAccArr, this.oldXROriArr, this.oldXRTempArr[_maxIndex], _maxIndex);
- // // }, 100);
- // }
- }
- //直拳判断 && 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;
- }
- this.bCanZ = true;
- let _endPower = Math.abs(tempZ) * this.mass;
- if (Math.abs(tempZ) > 3) {
- this.ZCount++;
- this.showMCount.string = "击中" + this.ZCount;
- // this.showMPower.string = _endPower.toFixed(3) + " kg";
- this.showCalLabel.string = _endPower.toFixed(1) + " 斤";
- this.audioScript.onPlayBoxingTip(1);
- }
- //如果当前的加速度大于旧的值,并且 差值为 deltaV,判断打击
- // let deltaV = 3;
- // if (tempZ > this.oldZ + deltaV) {
- // this.oldZ = tempZ;
- // if (this.bCanZ) return;
- // this.bCanZ = true;
- // this.ZCount++;
- // this.showMCount.string = "击中" + this.ZCount;
- // this.showMPower.string = tempZ.toFixed(1) * this.mass + " kg";
- // setTimeout(() => {
- // this.oldZ = 0;
- // this.bCanZ = false;
- // console.log("重置this.oldZ", this.oldZ);
- // }, 200);
- // }
- }
- // if (this.yO < 0 && Math.abs(_yo) > 2 && this.xA < 0 && Math.abs(tempX) > 0.3) {
- // // if (this.yO >= 0) return;
- // if (this.xO > 95) return;
- // if (this.bCanXL) return;
- // //用陀螺仪判断立柱的方向,静止或者方向是正确方向
- // //记录 矢量的最大值
- // if (tempX > this.oldXR) {
- // this.oldXR = tempX;
- // if (this.bCanR) return;
- // this.bCanR = true;
- // // console.log(this.xA + " R= " + this.calXVector.toFixed(2) + " = " + tempX.toFixed(2));
- // this.RCount++;
- // this.showRCount.string = "R击中次数" + this.RCount;
- // this.showRPower.string = tempX.toFixed(1) * this.mass + " kg";
- // setTimeout(() => {
- // this.oldXR = 0;
- // this.bCanR = false;
- // console.log("重置this.oldXR", this.oldXR);
- // }, 1000);
- // }
- // }
- // let _yo = this.yO - this.oldGamma;
- // if (Math.abs(_yo) > 2)
- // if (this.xO < 95)
- //直拳判断
- // if (this.zA > 0 && Math.abs(tempZ) > 0.5) {
- // if (Math.abs(this.yO) > 3 || this.xO > 95) return;
- // //如果当前的加速度大于旧的值,并且 差值为 deltaV,判断打击
- // let deltaV = 5;
- // if (tempZ > this.oldZ + deltaV) {
- // this.oldZ = tempZ;
- // if (this.bCanZ) return;
- // this.bCanZ = true;
- // this.ZCount++;
- // this.showMCount.string = "击中" + this.ZCount;
- // this.showMPower.string = tempZ.toFixed(1) * this.mass + " kg";
- // setTimeout(() => {
- // this.oldZ = 0;
- // this.bCanZ = false;
- // console.log("重置this.oldZ", this.oldZ);
- // }, 800);
- // }
- // }
- let allCount = this.ZCount + this.LCount + this.RCount;
- //打一拳,大约消耗的热量,是450*4/60=1.875 焦耳。
- //因为打拳一小时,需要消耗的热量是450大卡,而一分钟约打一下,一大卡是4焦耳。
- this.showCalorieLabel.string = Math.floor((allCount * 1.875) / 4) + "大卡";
- },
- onUpdateBoxingPostHit(gamedata) {
- let gdata = gamedata;
- if (gdata.direction == "leftPunch") {
- this.LCount++;
- this.showLCount.string = "L击中次数" + this.LCount;
- this.showCalLabel.string = gdata.hitPower.toFixed(1) + " 斤";
- this.audioScript.onPlayBoxingTip(0);
- } else if (gdata.direction == "rightPunch") {
- this.RCount++;
- this.showRCount.string = "R击中次数" + this.RCount;
- this.showCalLabel.string = gdata.hitPower.toFixed(1) + " 斤";
- this.audioScript.onPlayBoxingTip(2);
- } else if (gdata.direction == "straightPunch") {
- this.ZCount++;
- this.showMCount.string = "击中" + this.ZCount;
- this.showCalLabel.string = gdata.hitPower.toFixed(1) + " 斤";
- this.audioScript.onPlayBoxingTip(1);
- }
- },
- onUpdateAccAndGyro(gameData) {
- let { ax, ay, az } = gameData.acc;
- let { gx, gy, gz } = gameData.gyro;
- this.showAccLabel.string = "acc\nx:" + (ax / 10).toFixed(2) + "\ny:" + (ay / 10).toFixed(2) + "\nz:" + (az / 10).toFixed(2);
- this.showOriLabel.string = "gyro\nx:" + (gx * 100).toFixed(2) + "\ny:" + (gy * 100).toFixed(2) + "\nz:" + (gz * 100).toFixed(2);
- if (this.count > 0) {
- console.log(gameData);
- this.count--;
- }
- let [hit, dir] = this.Filter.Update(ax / 10, az / 10, gz, -gx, gameData.ms);
- if (dir != undefined) {
- console.log("X " + dir.x + " Y " + dir.y + ' == ' + dir.length);
- var result = Math.atan2(dir.y, dir.x) * 180 / (Math.PI);
- result = Math.round(result);
- let curAngle = result > 0 ? result : (360 + result);
- console.log("result=" + result + " , curAngle = " + curAngle);
- this.accAndGyroLabel.string = "result=" + result + " , curAngle = " + curAngle;
- let res = this.Filter.getTempValue(dir);
- let temp = {
- direction: res.direction,
- angle: res.angle,
- name: res.name,
- ename: res.ename,
- value: res.hit,
- mass: 10, //质量
- hitPower: res.hit //计算的力
- }
- this.onUpdateBoxingPostHit(temp);
- }
- // if (hit != 0) {
- // console.log("Release Hit " + hit);
- // }
- // let temp = this.Filter.Update(ax / 10, ay / 10, gx, gy, gameData.ms, (res) => {
- // });//我自己的更新acc的函数
- // console.log(temp);
- // this.Filter.Update(new o0.Vector3(ax/10, -ay/10, -az/10), gameData.ms, new o0.Vector3(gx*100, -gy*100, -gz*100),(res)=>{
- // let temp = {
- // direction: res.direction,
- // angle:res.angle,
- // name: res.name,
- // ename: res.ename,
- // value: res.hit,
- // mass: 10, //质量
- // hitPower: res.hit //计算的力
- // }
- // this.onUpdateBoxingPostHit(temp);
- // console.log(temp);
- // this.accAndGyroLabel.string = res.angle;
- // });
- }
- // update(dt) {
- // },
- });
|