| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471 |
- // 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 + "大卡";
- }
- });
|