| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- window.onWebViewMessage = function (data) {
- let name = data.funName;
- if (name == "onWatchAccelerometer") {
- /**
- * 返回加速计的数据
- * {
- * xAxis
- * yAxis
- * zAxis
- * }
- */
- webView.onUpdateAcc(data);
- }
- };
- cc.Class({
- extends: cc.Component,
- properties: {
- playerContro: {
- default: null,
- type: cc.Node,
- serializable: true,
- },
- playerControScript: {
- 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,
- },
- bRAnimation: false,
- bLAnimation: false,
- bMAnimation: false,
- currentLimitValue: {
- default: 5,
- type: cc.Integer,
- tooltip: "限制值",
- visible: false,
- serializable: false,
- },
- currentLimitDireValue: {
- default: 1,
- type: cc.Float,
- tooltip: "角度限制值",
- visible: false,
- serializable: false,
- }
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad() {
- window.webView = this;
- this.playerControScript = this.playerContro.getComponent("PlayerController");
- this.hitState = {
- "xLCount": 0,
- "xRCount": 0,
- "zLCount": 0,
- "zRCount": 0
- };
- this.oldxA = 0;
- this.oldzA = 0;
- this.bUpdateOnce = false;
- this.bDelayOnce = false;
- },
- 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() {
- // if (cc.sys.OS_WINDOWS === cc.sys.os) return;
- try
- {
- //在此运行代码
- uni.postMessage({
- data: {
- funName: "openAccelerometer",
- gameData: {}
- }
- })
- }
- catch(err)
- {
- //在此处理错误
- }
- // console.log("onBind");
- },
- onUnBind() {
- // if (cc.sys.OS_WINDOWS === cc.sys.os) return;
- try
- {
- //在此运行代码
- uni.postMessage({
- data: {
- funName: "closeAccelerometer",
- gameData: {}
- }
- })
- }
- catch(err)
- {
- //在此处理错误
- }
- },
- 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;
- this.onUpdateData();
- },
- onUpdateData() {
- let tempLimit = this.currentLimitValue;
- let directionValue = this.currentLimitDireValue;
- //1.当前的加速度矢量减去分量,就是打击的加速度
- let tempZ = Math.abs(this.zA);
- let tempX = Math.abs(this.xA);
- //2.判断 那个轴的打击方向,就走哪个轴的计算流程
- //直拳判断
- let zAcc = Math.abs(this.zA) - Math.abs(this.oldzA);
- let leftAcc = Math.abs(this.xA) - Math.abs(this.oldxA);
- let rightAcc = this.xA - this.oldxA;
- //判断方位,用比值 直拳方向 和 左右方向 比。
- //如果比值相同 等于约定的数值,以1为标准,则是正勾拳
- if( Math.abs(leftAcc) !=0){
- let tempDirection = Math.abs(zAcc) / Math.abs(leftAcc);
- // console.log(tempDirection,directionValue);
- if (tempDirection > directionValue) {
- // console.log(tempDirection,directionValue);
- if (zAcc > tempLimit && this.zA < -tempLimit) {
- if (this.bUpdateOnce)
- return;
- console.log("走直拳");
- this.bUpdateOnce = true;
- let _endPower = Math.abs(tempZ) * this.mass;
- this.onHit("zLCount", tempZ, Math.ceil(_endPower));
- // console.log("zLCount:", this.zA, leftAcc, rightAcc, zAcc);
- // console.log("xA:", this.xA, " zA:", this.zA, " leftAcc:", leftAcc, " rightAcc:", rightAcc, " zAcc:", zAcc);
-
- }
- } else {
- if ((leftAcc > tempLimit ||zAcc > tempLimit) && this.xA > tempLimit) {
- if (this.bUpdateOnce)
- return;
- console.log("走左勾拳");
- this.bUpdateOnce = true;
- let _endPower = (Math.abs(tempX) + Math.abs(tempZ)) * this.mass;
- this.onHit("xRCount", tempZ, Math.ceil(_endPower));
- // console.log("xRCount:", this.xA, leftAcc, rightAcc);
- // console.log("xA:", this.xA, " zA:", this.zA, " leftAcc:", leftAcc, " rightAcc:", rightAcc, " zAcc:", zAcc);
-
- }
- if( (rightAcc < -tempLimit||zAcc > tempLimit) && this.xA < -tempLimit) {
- if (this.bUpdateOnce)
- return;
- console.log("走右勾拳");
- this.bUpdateOnce = true;
- let _endPower = (Math.abs(tempX) + Math.abs(tempZ)) * this.mass;
- this.onHit("xLCount", tempZ, Math.ceil(_endPower));
- // console.log("xLCount:", this.xA, leftAcc, rightAcc);
- // console.log("xA:", this.xA, " zA:", this.zA, " leftAcc:", leftAcc, " rightAcc:", rightAcc, " zAcc:", zAcc);
-
- }
-
- }
- }
- this.oldzA = this.zA;
- this.oldxA = this.xA;
- if (!this.bDelayOnce && this.bUpdateOnce) {
- this.bDelayOnce = true;
- setTimeout(() => {
- this.bUpdateOnce = false;
- this.bDelayOnce = false;
- }, 500);
- }
- },
- // "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;
- // this.luckyScript.onHitFromDevice(temp, () => {
- // //重新记录值
- // this.onResetAccState();
- // });
- // let temp = {
- // direction: direction,
- // value: direValue,
- // mass: this.mass, //质量
- // hitPower: power //计算的力
- // }
- //总的力量
- // this.AllPower += power;
- //每10次更新一次卡路里
- if (0 == this.hitCount % 10) {
- //打一拳,大约消耗的热量,是450*4/60=1.875 焦耳。
- //因为打拳一小时,需要消耗的热量是450大卡,而一分钟约打一下,一大卡是4焦耳。
- this.AllCalorie += Math.floor((10 * 1.875) / 4);
- // this.$emit('updateCalorie',this.calorie);
- }
- // console.log("this.powerCount:",this.powerCount);
- if (direction == "xLCount") {
- // console.log("右勾拳1");
- //右勾拳
- if (!this.bRAnimation) {
- this.RCount++;
- this.bRAnimation = true;
- console.log("右勾拳2");
- setTimeout(() => {
- this.bRAnimation = false;
- }, 1000);
- this.playerControScript.rightHook();
- }
- } else if (direction == "xRCount") {
- // console.log("左勾拳1");
- //左勾拳
- if (!this.bLAnimation) {
- this.LCount++;
- this.bLAnimation = true;
- console.log("左勾拳2");
- setTimeout(() => {
- this.bLAnimation = false;
- }, 1000);
- this.playerControScript.leftHook();
- }
- } else if (direction == "zLCount" || direction == "zRCount") {
- // console.log("直拳1");
- //直拳
- if (!this.bMAnimation) {
- this.ZCount++;
- this.bMAnimation = true;
- console.log("直拳2");
- setTimeout(() => {
- this.bMAnimation = false;
- }, 1000);
- this.playerControScript.jar();
- }
- }
- this.hitCount = this.ZCount + this.LCount + this.RCount;
- },
- onResetAllValue() {
- this.AllCalorie = 0;
- this.AllPower = 0;
- this.showCalorieLabel.string = "消耗卡路里:" + this.AllCalorie + "大卡";
- }
- });
|