webview.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. window.onWebViewMessage = function (data) {
  2. let name = data.funName;
  3. if (name == "onWatchAccelerometer") {
  4. /**
  5. * 返回加速计的数据
  6. * {
  7. * xAxis
  8. * yAxis
  9. * zAxis
  10. * }
  11. */
  12. webView.onUpdateAcc(data);
  13. }
  14. };
  15. cc.Class({
  16. extends: cc.Component,
  17. properties: {
  18. playerContro: {
  19. default: null,
  20. type: cc.Node,
  21. serializable: true,
  22. },
  23. playerControScript: {
  24. default: null,
  25. visible: false,
  26. serializable: true,
  27. },
  28. mass: {
  29. default: 50,
  30. type: cc.Integer,
  31. tooltip: "物体质量/kg",
  32. serializable: true,
  33. },
  34. xLCount: {
  35. default: 0,
  36. type: cc.Float,
  37. tooltip: "x轴负向",
  38. visible: false,
  39. serializable: false,
  40. },
  41. xRCount: {
  42. default: 0,
  43. type: cc.Float,
  44. tooltip: "x轴正向",
  45. visible: false,
  46. serializable: false,
  47. },
  48. yLCount: {
  49. default: 0,
  50. type: cc.Float,
  51. tooltip: "y轴负向",
  52. visible: false,
  53. serializable: false,
  54. },
  55. yRCount: {
  56. default: 0,
  57. type: cc.Float,
  58. tooltip: "y轴正向",
  59. visible: false,
  60. serializable: false,
  61. },
  62. zLCount: {
  63. default: 0,
  64. type: cc.Float,
  65. tooltip: "z轴负向",
  66. visible: false,
  67. serializable: false,
  68. },
  69. zRCount: {
  70. default: 0,
  71. type: cc.Float,
  72. tooltip: "z轴正向",
  73. visible: false,
  74. serializable: false,
  75. },
  76. //记录一次打击,如果通方向就更新最大值
  77. hitFirst: {
  78. default: null,
  79. visible: false,
  80. serializable: true,
  81. },
  82. //锁住hit update 更新情况
  83. bLock: {
  84. default: false,
  85. visible: false,
  86. serializable: false,
  87. },
  88. //定义一个状态字典
  89. hitState: {
  90. default: null,
  91. serializable: false,
  92. visible: false,
  93. },
  94. staticTime: {
  95. default: 1,
  96. type: cc.Float,
  97. serializable: true,
  98. tooltip: "立柱静止的检测时间"
  99. },
  100. bSwing: {
  101. default: false,
  102. serializable: true,
  103. tooltip: "是否摆动"
  104. },
  105. showCalorieLabel: {
  106. default: null,
  107. type: cc.Label,
  108. serializable: true
  109. },
  110. LCount: {
  111. default: 0,
  112. type: cc.Integer,
  113. tooltip: "左勾拳打击次数",
  114. visible: false,
  115. serializable: false,
  116. },
  117. RCount: {
  118. default: 0,
  119. type: cc.Integer,
  120. tooltip: "右勾拳打击次数",
  121. visible: false,
  122. serializable: false,
  123. },
  124. ZCount: {
  125. default: 0,
  126. type: cc.Integer,
  127. tooltip: "直拳打击次数",
  128. visible: false,
  129. serializable: false,
  130. },
  131. AllCalorie: {
  132. default: 0,
  133. type: cc.Integer,
  134. tooltip: "总共的卡路里",
  135. visible: false,
  136. serializable: false,
  137. },
  138. AllPower: {
  139. default: 0,
  140. type: cc.Integer,
  141. tooltip: "三次下来的总力量",
  142. visible: false,
  143. serializable: false,
  144. },
  145. bRAnimation: false,
  146. bLAnimation: false,
  147. bMAnimation: false,
  148. currentLimitValue: {
  149. default: 5,
  150. type: cc.Integer,
  151. tooltip: "限制值",
  152. visible: false,
  153. serializable: false,
  154. },
  155. currentLimitDireValue: {
  156. default: 1,
  157. type: cc.Float,
  158. tooltip: "角度限制值",
  159. visible: false,
  160. serializable: false,
  161. }
  162. },
  163. // LIFE-CYCLE CALLBACKS:
  164. onLoad() {
  165. window.webView = this;
  166. this.playerControScript = this.playerContro.getComponent("PlayerController");
  167. this.hitState = {
  168. "xLCount": 0,
  169. "xRCount": 0,
  170. "zLCount": 0,
  171. "zRCount": 0
  172. };
  173. this.oldxA = 0;
  174. this.oldzA = 0;
  175. this.bUpdateOnce = false;
  176. this.bDelayOnce = false;
  177. },
  178. start() {
  179. this.onBind();
  180. },
  181. onLeft() {
  182. let temp = {
  183. gameData: {
  184. xAxis: -10.5,
  185. yAxis: 5,
  186. zAxis: 0
  187. }
  188. };
  189. this.onUpdateAcc(temp);
  190. },
  191. onRight() {
  192. let temp = {
  193. gameData: {
  194. xAxis: 10.5,
  195. yAxis: 5,
  196. zAxis: 0
  197. }
  198. };
  199. this.onUpdateAcc(temp);
  200. },
  201. onMid() {
  202. let temp = {
  203. gameData: {
  204. xAxis: 0,
  205. yAxis: 5,
  206. zAxis: 100.5
  207. }
  208. };
  209. this.onUpdateAcc(temp);
  210. },
  211. onStatic() {
  212. let temp = {
  213. gameData: {
  214. xAxis: 0,
  215. yAxis: 9.8,
  216. zAxis: 0
  217. }
  218. };
  219. this.onUpdateAcc(temp);
  220. },
  221. //重置一下,记录的数据
  222. onResetAccState() {
  223. console.log("重置 onResetAccState");
  224. this.hitFirst = null;
  225. this.bLock = false;
  226. this.hitState = {
  227. "xLCount": 0,
  228. "xRCount": 0,
  229. "zLCount": 0,
  230. "zRCount": 0
  231. }
  232. },
  233. onBind() {
  234. // if (cc.sys.OS_WINDOWS === cc.sys.os) return;
  235. try
  236. {
  237. //在此运行代码
  238. uni.postMessage({
  239. data: {
  240. funName: "openAccelerometer",
  241. gameData: {}
  242. }
  243. })
  244. }
  245. catch(err)
  246. {
  247. //在此处理错误
  248. }
  249. // console.log("onBind");
  250. },
  251. onUnBind() {
  252. // if (cc.sys.OS_WINDOWS === cc.sys.os) return;
  253. try
  254. {
  255. //在此运行代码
  256. uni.postMessage({
  257. data: {
  258. funName: "closeAccelerometer",
  259. gameData: {}
  260. }
  261. })
  262. }
  263. catch(err)
  264. {
  265. //在此处理错误
  266. }
  267. },
  268. onResetXL() {
  269. this.bCanXL = false;
  270. },
  271. onResetXR() {
  272. this.bCanXR = false;
  273. },
  274. onUpdateAcc(data) {
  275. let a = data.gameData;
  276. this.xA = a.xAxis;
  277. this.yA = a.yAxis;
  278. this.zA = a.zAxis;
  279. this.onUpdateData();
  280. },
  281. onUpdateData() {
  282. let tempLimit = this.currentLimitValue;
  283. let directionValue = this.currentLimitDireValue;
  284. //1.当前的加速度矢量减去分量,就是打击的加速度
  285. let tempZ = Math.abs(this.zA);
  286. let tempX = Math.abs(this.xA);
  287. //2.判断 那个轴的打击方向,就走哪个轴的计算流程
  288. //直拳判断
  289. let zAcc = Math.abs(this.zA) - Math.abs(this.oldzA);
  290. let leftAcc = Math.abs(this.xA) - Math.abs(this.oldxA);
  291. let rightAcc = this.xA - this.oldxA;
  292. //判断方位,用比值 直拳方向 和 左右方向 比。
  293. //如果比值相同 等于约定的数值,以1为标准,则是正勾拳
  294. if( Math.abs(leftAcc) !=0){
  295. let tempDirection = Math.abs(zAcc) / Math.abs(leftAcc);
  296. // console.log(tempDirection,directionValue);
  297. if (tempDirection > directionValue) {
  298. // console.log(tempDirection,directionValue);
  299. if (zAcc > tempLimit && this.zA < -tempLimit) {
  300. if (this.bUpdateOnce)
  301. return;
  302. console.log("走直拳");
  303. this.bUpdateOnce = true;
  304. let _endPower = Math.abs(tempZ) * this.mass;
  305. this.onHit("zLCount", tempZ, Math.ceil(_endPower));
  306. // console.log("zLCount:", this.zA, leftAcc, rightAcc, zAcc);
  307. // console.log("xA:", this.xA, " zA:", this.zA, " leftAcc:", leftAcc, " rightAcc:", rightAcc, " zAcc:", zAcc);
  308. }
  309. } else {
  310. if ((leftAcc > tempLimit ||zAcc > tempLimit) && this.xA > tempLimit) {
  311. if (this.bUpdateOnce)
  312. return;
  313. console.log("走左勾拳");
  314. this.bUpdateOnce = true;
  315. let _endPower = (Math.abs(tempX) + Math.abs(tempZ)) * this.mass;
  316. this.onHit("xRCount", tempZ, Math.ceil(_endPower));
  317. // console.log("xRCount:", this.xA, leftAcc, rightAcc);
  318. // console.log("xA:", this.xA, " zA:", this.zA, " leftAcc:", leftAcc, " rightAcc:", rightAcc, " zAcc:", zAcc);
  319. }
  320. if( (rightAcc < -tempLimit||zAcc > tempLimit) && this.xA < -tempLimit) {
  321. if (this.bUpdateOnce)
  322. return;
  323. console.log("走右勾拳");
  324. this.bUpdateOnce = true;
  325. let _endPower = (Math.abs(tempX) + Math.abs(tempZ)) * this.mass;
  326. this.onHit("xLCount", tempZ, Math.ceil(_endPower));
  327. // console.log("xLCount:", this.xA, leftAcc, rightAcc);
  328. // console.log("xA:", this.xA, " zA:", this.zA, " leftAcc:", leftAcc, " rightAcc:", rightAcc, " zAcc:", zAcc);
  329. }
  330. }
  331. }
  332. this.oldzA = this.zA;
  333. this.oldxA = this.xA;
  334. if (!this.bDelayOnce && this.bUpdateOnce) {
  335. this.bDelayOnce = true;
  336. setTimeout(() => {
  337. this.bUpdateOnce = false;
  338. this.bDelayOnce = false;
  339. }, 500);
  340. }
  341. },
  342. // "xLCount": 0,
  343. // "xRCount": 0,
  344. // "zLCount": 0,
  345. // "zRCount": 0
  346. onHit(direction, direValue, power) {
  347. // console.log(direction, direValue, power);
  348. // if (this.bCanZ || this.bCanXL || this.bCanXR) return;
  349. // this.luckyScript.onHitFromDevice(temp, () => {
  350. // //重新记录值
  351. // this.onResetAccState();
  352. // });
  353. // let temp = {
  354. // direction: direction,
  355. // value: direValue,
  356. // mass: this.mass, //质量
  357. // hitPower: power //计算的力
  358. // }
  359. //总的力量
  360. // this.AllPower += power;
  361. //每10次更新一次卡路里
  362. if (0 == this.hitCount % 10) {
  363. //打一拳,大约消耗的热量,是450*4/60=1.875 焦耳。
  364. //因为打拳一小时,需要消耗的热量是450大卡,而一分钟约打一下,一大卡是4焦耳。
  365. this.AllCalorie += Math.floor((10 * 1.875) / 4);
  366. // this.$emit('updateCalorie',this.calorie);
  367. }
  368. // console.log("this.powerCount:",this.powerCount);
  369. if (direction == "xLCount") {
  370. // console.log("右勾拳1");
  371. //右勾拳
  372. if (!this.bRAnimation) {
  373. this.RCount++;
  374. this.bRAnimation = true;
  375. console.log("右勾拳2");
  376. setTimeout(() => {
  377. this.bRAnimation = false;
  378. }, 1000);
  379. this.playerControScript.rightHook();
  380. }
  381. } else if (direction == "xRCount") {
  382. // console.log("左勾拳1");
  383. //左勾拳
  384. if (!this.bLAnimation) {
  385. this.LCount++;
  386. this.bLAnimation = true;
  387. console.log("左勾拳2");
  388. setTimeout(() => {
  389. this.bLAnimation = false;
  390. }, 1000);
  391. this.playerControScript.leftHook();
  392. }
  393. } else if (direction == "zLCount" || direction == "zRCount") {
  394. // console.log("直拳1");
  395. //直拳
  396. if (!this.bMAnimation) {
  397. this.ZCount++;
  398. this.bMAnimation = true;
  399. console.log("直拳2");
  400. setTimeout(() => {
  401. this.bMAnimation = false;
  402. }, 1000);
  403. this.playerControScript.jar();
  404. }
  405. }
  406. this.hitCount = this.ZCount + this.LCount + this.RCount;
  407. },
  408. onResetAllValue() {
  409. this.AllCalorie = 0;
  410. this.AllPower = 0;
  411. this.showCalorieLabel.string = "消耗卡路里:" + this.AllCalorie + "大卡";
  412. }
  413. });