webview.js 12 KB

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