action-jump.vue 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. <template>
  2. <view style="width: 750rpx;">
  3. <view class="flex justify-center" style="margin-top: 32rpx;">
  4. <!-- 目前格子数量不代表数字,显示完成度而已 -->
  5. <view class="grid-progress-vertical-container" style="top:-460rpx; ">
  6. <view class="grid-progress-vertical-bar">
  7. <view class="grid-progress-vertical-child" v-for="(item, index) in maxShowCount +1" :key="index"
  8. v-if="index!==0">
  9. <view
  10. :class="showCurCount>=index? 'grid-progress-vertical-active':'grid-progress-vertical-inactive'">
  11. </view>
  12. <view :class="(index%5 == 0||index == 1)?'':'grid-progress-text-hidden'"
  13. class="grid-progress-vertical-text">
  14. {{Math.floor(taskSignCount / 40 * index)}}
  15. </view>
  16. </view>
  17. </view>
  18. <view class="grid-progress-vertical-bar">
  19. <view class="grid-progress-vertical-child" v-for="(item, index) in maxShowCount +1" :key="index"
  20. v-if="index!==0">
  21. <view :class="(index%5 == 0||index == 1)?'':'grid-progress-text-hidden'"
  22. class="grid-progress-vertical-text">
  23. {{Math.floor(taskSignCount / 40 * index)+taskSignCount/2}}
  24. </view>
  25. <view
  26. :class="showCurCount>=index+20? 'grid-progress-vertical-active':'grid-progress-vertical-inactive'">
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. <view class="flex justify-center align-center" style="height: 100rpx; width: 444rpx; ">
  32. <view class="text-12px text-white text-center">
  33. {{taskDescribe}}
  34. </view>
  35. </view>
  36. </view>
  37. <canvas canvas-id="actionJumpCanvas" :style="{ width: canvasW + 'px', height: canvasH + 'px' }"></canvas>
  38. <view class="flex align-center justify-center" style="font-size: 14px;">
  39. <view style="position: relative;">
  40. <image style="width: 135px;height: 75px;" src="../../../static/modal/action-jump/Fill.png"></image>
  41. <view class="mid-absolute flex flex-direction align-center justify-center text-white">
  42. <view class="flex align-center">
  43. <image style="width: 22rpx;height: 28rpx;"
  44. src="../../../static/modal/action-jump/jumpSpeed.png"></image>
  45. <view style="margin-left: 6rpx;">平均速度</view>
  46. </view>
  47. <view style="margin-top: 12rpx;">{{jumpSpeed}}</view>
  48. </view>
  49. </view>
  50. <view class="flex" style="position: relative;" @click="onControllerPlay">
  51. <image style="width: 112px;height: 111px;" src="../../../static/modal/action-jump/midButton.png">
  52. </image>
  53. <image v-if="!bJumpPlay" class="mid-absolute" style=" width: 28rpx;height: 28rpx;"
  54. src="../../../static/modal/action-jump/midPlay.png">
  55. </image>
  56. <image v-else class="mid-absolute" style="width: 28rpx;height: 28rpx;"
  57. src="../../../static/modal/action-jump/midPause.png">
  58. </image>
  59. </view>
  60. <view style="position: relative;">
  61. <image style="width: 135px;height: 75px; transform: scaleX(-1);"
  62. src="../../../static/modal/action-jump/Fill.png"></image>
  63. <view class="mid-absolute flex flex-direction align-center justify-center text-white">
  64. <view class="flex align-center">
  65. <image style="width: 22rpx;height: 28rpx;"
  66. src="../../../static/modal/action-jump/jumpCalorie.png"></image>
  67. <view style="margin-left: 6rpx;">卡路里</view>
  68. </view>
  69. <view style="margin-top: 12rpx;">{{jumpCalorie}}</view>
  70. </view>
  71. </view>
  72. </view>
  73. <!-- <view style="height: 41px;"></view> -->
  74. <!-- 测试按钮 -->
  75. <view style="display: flex;justify-content: space-between;">
  76. <view>{{isY?'Y':'X'}}</view>
  77. <button @click="onChangeY">Y为旋转轴</button>
  78. <button @click="onChangeX">X为旋转轴</button>
  79. </view>
  80. <view style="display: flex;justify-content: space-between;" class="margin-top margin-bottom">
  81. <button @click="onJumpType(0)">jump</button>
  82. <button @click="onJumpType(1)">left</button>
  83. <button @click="onJumpType(2)">right</button>
  84. <button @click="onJumpType(3)">rLeft</button>
  85. <button @click="onJumpType(4)">rRight</button>
  86. </view>
  87. <!-- <view style="display: flex;justify-content: space-around; padding-bottom: 100rpx;">
  88. <view style="font-size: 14px;">t:{{countdown}}</view>
  89. <view style="font-size: 14px;">e:{{eliminationCount}}</view>
  90. <view style="font-size: 14px;">f:{{faultCount}}</view>
  91. </view> -->
  92. </view>
  93. </template>
  94. <script>
  95. import ActionJump from "@/util/util-js/action/jump.js"
  96. import {
  97. mapMutations
  98. } from 'vuex';
  99. export default {
  100. components: {
  101. },
  102. props: {
  103. showTime: {
  104. type: Number,
  105. default: 0
  106. },
  107. levelData: {
  108. type: Object,
  109. default: null
  110. }
  111. },
  112. data() {
  113. return {
  114. isY: true,
  115. canvasW: 0, // 画布宽
  116. canvasH: 0, // 画布高
  117. SystemInfo: {}, // 设备信息
  118. directionJump: null,
  119. midJump: null,
  120. rotateJump: null,
  121. directionJumpWhite: null,
  122. midJumpWhite: null,
  123. rotateJumpWhite: null,
  124. //参考图片
  125. cankao: null,
  126. jumpTipImage: null,
  127. jumpNormalImage: null,
  128. jumpTypeArray: [{
  129. jumpName: 'NORMAL',
  130. jumpCode: 0,
  131. icon: 'midJump',
  132. scaleX: 1,
  133. bTrigger: false,
  134. }, {
  135. jumpName: 'LEFT',
  136. jumpCode: 1,
  137. icon: 'directionJump',
  138. scaleX: -1,
  139. bTrigger: false,
  140. }, {
  141. jumpName: 'RIGHT',
  142. jumpCode: 2,
  143. icon: 'directionJump',
  144. scaleX: 1,
  145. bTrigger: false,
  146. }, {
  147. jumpName: 'LEFT_ROTATE',
  148. jumpCode: 3,
  149. icon: 'rotateJump',
  150. scaleX: -1,
  151. bTrigger: false,
  152. }, {
  153. jumpName: 'RIGHT_ROTATE',
  154. jumpCode: 4,
  155. icon: 'rotateJump',
  156. scaleX: 1,
  157. bTrigger: false,
  158. }],
  159. spawnArray: [],
  160. //下一个生成是相反的方向
  161. bNextSpawnRightDirection: false,
  162. bNextSpawnRightRotateDirection: false,
  163. //生成预制的模板,用count 来判断生成哪一种
  164. template: [{ //原地
  165. count: 1,
  166. spawnList: [
  167. [0]
  168. ]
  169. }, {
  170. count: 2,
  171. spawnList: [
  172. [2, 1],
  173. [1, 2],
  174. [3, 4],
  175. [4, 3]
  176. ]
  177. }, {
  178. count: 3,
  179. spawnList: [
  180. [2, 0, 1],
  181. [1, 0, 2],
  182. [3, 0, 4],
  183. [4, 0, 3]
  184. ]
  185. }, ],
  186. level: 3,
  187. levelType: 0,
  188. countdown: 60,
  189. countdownInterval: null,
  190. faultCount: 0,
  191. eliminationCount: 0,
  192. bJumpPlay: false,
  193. jumpCalorie: 1000,
  194. jumpSpeed: 1000,
  195. //硬件设备处理
  196. BLEAccIndex: 0,
  197. bJump: false,
  198. xA: 0,
  199. yA: 0,
  200. zA: 0,
  201. actionJumpObj: null,
  202. /**
  203. * 交互数据处理
  204. */
  205. //当前选择的关卡信息
  206. selfLevelData: null,
  207. // 双人模式下 数据
  208. //标记块数量
  209. taskSignCount: 0, //总数
  210. taskSignCurCount: 0, //当前计数
  211. //{"type":1,"explain":"规定时间内跳完所有标志块","limitType":1}
  212. taskConditionPassed: null,
  213. taskDescribe: '',
  214. //满格的数值是 25
  215. maxShowCount: 20,
  216. addShowCountUnit: 0,
  217. showCurCount: 0,
  218. roundingCount: 0,
  219. //游戏结束
  220. isGameOver: false
  221. }
  222. },
  223. watch: {
  224. levelData(val) {
  225. //更新数据
  226. // console.log("levelData ============>", JSON.stringify(val));
  227. if (val) {
  228. this.taskSignCount = val.signCount;
  229. this.taskConditionPassed = val.conditionPassed;
  230. this.taskDescribe = val.describe;
  231. this.addShowCountUnit = (this.maxShowCount * 2) / this.taskSignCount;
  232. // console.log("this.addShowCountUnit:", this.addShowCountUnit, this.maxShowCount * 2, this
  233. // .taskSignCount);
  234. this.showCount = 0;
  235. //用signType 做等级类型
  236. this.levelType = val.signType;
  237. }
  238. }
  239. },
  240. created() {
  241. let _self = this;
  242. this.actionJumpCanvas = uni.createCanvasContext("actionJumpCanvas", this);
  243. // console.log("this.actionJumpCanvas:", this.actionJumpCanvas);
  244. this.SystemInfo = uni.getSystemInfoSync();
  245. this.canvasW = this.SystemInfo.windowWidth; // 画布宽度
  246. this.canvasH = 114;
  247. this.onLoadImage();
  248. this.onListenActionJump();
  249. },
  250. methods: {
  251. ...mapMutations(['onWriteBLEConnectionValue']),
  252. onListenActionJump() {
  253. let _self = this;
  254. _self.actionJumpObj = new ActionJump();
  255. _self.actionJumpObj.addEventListener('resultant', (e) => {
  256. if (e.type == 'jump') {
  257. // this.jumpCount++;
  258. this.bJump = true;
  259. } else if (e.type == 'stateDataOfJump') {
  260. //发送给game,在game里面处理判断
  261. this.listenStateDataOfJump(e);
  262. } else if (e.type == 'stop') {
  263. this.onClearData();
  264. // console.log('stop');
  265. }
  266. })
  267. },
  268. //load 相关图片
  269. onLoadImage() {
  270. let _self = this;
  271. uni.getImageInfo({
  272. src: "../../../static/modal/action-jump/directionJump.png",
  273. success: function(image) {
  274. _self.directionJump = image;
  275. }
  276. });
  277. uni.getImageInfo({
  278. src: "../../../static/modal/action-jump/midJump.png",
  279. success: function(image) {
  280. _self.midJump = image;
  281. }
  282. });
  283. uni.getImageInfo({
  284. src: "../../../static/modal/action-jump/rotateJump.png",
  285. success: function(image) {
  286. _self.rotateJump = image;
  287. }
  288. });
  289. uni.getImageInfo({
  290. src: "../../../static/modal/action-jump/directionJumpWhite.png",
  291. success: function(image) {
  292. _self.directionJumpWhite = image;
  293. }
  294. });
  295. uni.getImageInfo({
  296. src: "../../../static/modal/action-jump/midJumpWhite.png",
  297. success: function(image) {
  298. _self.midJumpWhite = image;
  299. }
  300. });
  301. uni.getImageInfo({
  302. src: "../../../static/modal/action-jump/rotateJumpWhite.png",
  303. success: function(image) {
  304. _self.rotateJumpWhite = image;
  305. }
  306. });
  307. uni.getImageInfo({
  308. src: "../../../static/modal/action-jump/jumpTip.png",
  309. success: function(image) {
  310. _self.jumpTipImage = image;
  311. }
  312. });
  313. uni.getImageInfo({
  314. src: "../../../static/modal/action-jump/jumpNormal.png",
  315. success: function(image) {
  316. _self.jumpNormalImage = image;
  317. _self.onDrawBg();
  318. }
  319. });
  320. uni.getImageInfo({
  321. src: "../../../static/modal/action-jump/cankao.png",
  322. success: function(image) {
  323. _self.cankao = image;
  324. }
  325. });
  326. },
  327. /**
  328. * 重置生成数组,重置倒计时
  329. */
  330. resetJumpGame() {
  331. this.spawnArray = [];
  332. if (this.countdownInterval) {
  333. clearInterval(this.countdownInterval);
  334. this.countdownInterval = null;
  335. }
  336. this.resetCountdown(60);
  337. },
  338. startJumpGame() {
  339. this.isGameOver = false;
  340. this.resetJumpGame();
  341. //开始游戏
  342. this.index = 0;
  343. // this.levelLabel.string = '关卡' + this.level;
  344. let _ranType = Math.floor(Math.random() * 2);
  345. if (this.levelType == 0) {
  346. //随便生成一组跳的数据
  347. let _spawnList = this.template[0].spawnList;
  348. for (let i = 0; i < 4; i++) {
  349. this.spawnJumpPrefabsFromType(i, _spawnList[0][0]);
  350. }
  351. } else if (this.levelType == 1) {
  352. //生成一组 左跳右跳
  353. let _spawnList = this.template[1].spawnList;
  354. let ran = Math.floor(Math.random() * 2);
  355. // if (_ranType >= 1) ran += 2;
  356. for (let i = 0; i < _spawnList[ran].length; i++) {
  357. this.spawnJumpPrefabsFromType(i, _spawnList[ran][i]);
  358. }
  359. } else if (this.levelType == 2) {
  360. //生成一组 左跳右跳原地
  361. let _spawnList = this.template[2].spawnList;
  362. let ran = Math.floor(Math.random() * 2);
  363. // if (_ranType >= 1) ran += 2;
  364. for (let i = 0; i < _spawnList[ran].length; i++) {
  365. this.spawnJumpPrefabsFromType(i, _spawnList[ran][i]);
  366. }
  367. } else if (this.levelType == 3) {
  368. //生成一组 左旋跳右旋跳
  369. let _spawnList = this.template[1].spawnList;
  370. let ran = Math.floor(Math.random() * 2) + 2;
  371. for (let i = 0; i < _spawnList[ran].length; i++) {
  372. this.spawnJumpPrefabsFromType(i, _spawnList[ran][i]);
  373. }
  374. } else if (this.levelType == 4) {
  375. //生成一组 左旋跳右旋跳
  376. let _spawnList = this.template[2].spawnList;
  377. let ran = Math.floor(Math.random() * 2) + 2;
  378. for (let i = 0; i < _spawnList[ran].length; i++) {
  379. this.spawnJumpPrefabsFromType(i, _spawnList[ran][i]);
  380. }
  381. } else if (this.levelType == 5) {
  382. //随便生成三组数据
  383. let _newArray = [];
  384. // let _spawnList1 = this.template[1].spawnList;
  385. // let ran1 = Math.floor(Math.random() * 2);
  386. // _newArray = _newArray.concat(_spawnList1[ran1]);
  387. let _spawnList2 = this.template[2].spawnList;
  388. let ran2 = Math.floor(Math.random() * 2);
  389. let ran3 = Math.floor(Math.random() * 2);
  390. if (_ranType >= 1) {
  391. ran2 += 2;
  392. ran3 += 2;
  393. }
  394. _newArray = _newArray.concat(_spawnList2[ran2]);
  395. _newArray = _newArray.concat(_spawnList2[ran3]);
  396. for (let i = 0; i < _newArray.length; i++) {
  397. this.spawnJumpPrefabsFromType(i, _newArray[i]);
  398. }
  399. //todo 暂时给循环
  400. this.level = 0;
  401. }
  402. // if (this.level == 2) {
  403. // //随便生成二组数据
  404. // let _newArray = [];
  405. // let _spawnList1 = this.template[1].spawnList;
  406. // let ran1 = Math.floor(Math.random() * 2);
  407. // _newArray = _newArray.concat(_spawnList1[ran1]);
  408. // let _spawnList2 = this.template[2].spawnList;
  409. // let ran2 = Math.floor(Math.random() * 2);
  410. // if (_ranType >= 1) ran2 += 2;
  411. // _newArray = _newArray.concat(_spawnList2[ran2]);
  412. // // console.log(_newArray);
  413. // for (let i = 0; i < _newArray.length; i++) {
  414. // this.spawnJumpPrefabsFromType(i, _newArray[i]);
  415. // }
  416. // }
  417. this.level++;
  418. //倒计时
  419. this.countdownInterval = setInterval(() => {
  420. if (this.countdown <= 0) {
  421. clearInterval(this.countdownInterval);
  422. this.countdownInterval = null;
  423. //处理下一个关卡
  424. // console.warn('时间到,处理下一个关卡');
  425. this.startJumpGame();
  426. return;
  427. }
  428. this.setCountdown(1);
  429. }, 1000);
  430. this.onDraw();
  431. },
  432. spawnJumpPrefabsFromType(index, _jumpType) {
  433. //todo 生成的节点,后面再处理节奏问题。比如生成顺序
  434. for (let i = 0; i < this.jumpTypeArray.length; i++) {
  435. if (this.jumpTypeArray[i].jumpCode == _jumpType) {
  436. let _jumpPrefab = Object.assign({}, this.jumpTypeArray[i]);
  437. this.spawnArray.push(_jumpPrefab);
  438. break;
  439. }
  440. }
  441. },
  442. spawnJumpPrefabs(index) {
  443. let ran = Math.floor(Math.random() * 5);
  444. //todo 生成的节点,后面再处理节奏问题。比如生成顺序
  445. let _jumpPrefab = Object.assign({}, this.jumpTypeArray[ran]);
  446. //这里处理相反方向,比如生成了一个左旋转,下一个右旋转
  447. if (_jumpPrefab.jumpName == 'LEFT') {
  448. if (this.bNextSpawnRightDirection) {
  449. //如果是对应的需要记录一个对应的准确值
  450. this.bNextSpawnRightDirection = false;
  451. } else {
  452. ran = 2; //RIGHT;
  453. this.bNextSpawnRightDirection = true;
  454. }
  455. } else if (_jumpPrefab.jumpName == 'RIGHT') {
  456. if (this.bNextSpawnRightDirection) {
  457. ran = 1; //LEFT;
  458. this.bNextSpawnRightDirection = false;
  459. } else {
  460. this.bNextSpawnRightDirection = true;
  461. }
  462. }
  463. if (_jumpPrefab.jumpName == 'LEFT_ROTATE') {
  464. if (this.bNextSpawnRightRotateDirection) {
  465. //如果是对应的需要记录一个对应的准确值
  466. this.bNextSpawnRightRotateDirection = false;
  467. } else {
  468. ran = 4; //RIGHT_ROTATE;
  469. this.bNextSpawnRightRotateDirection = true;
  470. }
  471. // console.log('l==rotate', ran);
  472. } else if (_jumpPrefab.jumpName == 'RIGHT_ROTATE') {
  473. if (this.bNextSpawnRightRotateDirection) {
  474. ran = 3; //LEFT_ROTATE;
  475. this.bNextSpawnRightRotateDirection = false;
  476. } else {
  477. //如果是对应的需要记录一个对应的准确值
  478. this.bNextSpawnRightRotateDirection = true;
  479. }
  480. }
  481. this.spawnArray.push(_jumpPrefab);
  482. },
  483. //单纯的绘制八个背景
  484. onDrawBg() {
  485. this.actionJumpCanvas.clearRect(0, 0, this.canvasW, this.canvasH);
  486. let _currentBgStartX = this.canvasW / 2;
  487. let count = 4;
  488. for (let i = 0; i < count; i++) {
  489. let _currentStartPos = _currentBgStartX - i * 50 - 50;
  490. this.actionJumpCanvas.drawImage(this.jumpNormalImage.path, _currentStartPos, 0, 50, 114);
  491. let _currentEndPos = _currentBgStartX + i * 50;
  492. this.actionJumpCanvas.drawImage(this.jumpNormalImage.path, _currentEndPos, 0, 50, 114);
  493. }
  494. this.actionJumpCanvas.draw();
  495. },
  496. onDraw() {
  497. this.actionJumpCanvas.clearRect(0, 0, this.canvasW, 114);
  498. //计算一个对象
  499. let _drawObj = {
  500. path: null,
  501. startX: 0,
  502. width: 0,
  503. bgWidth: 0,
  504. bgStartXAllNormal: 0,
  505. bgStartXHasTip: 0,
  506. }
  507. //计算居中的点
  508. _drawObj.width = this.spawnArray.length * (50 + 10);
  509. _drawObj.startX = this.canvasW / 2 - _drawObj.width / 2;
  510. //全部是普通背景
  511. _drawObj.bgStartXAllNormal = this.canvasW / 2 - (this.spawnArray.length * 50) / 2;
  512. _drawObj.bgStartXHasTip = this.canvasW / 2 - (this.spawnArray.length * 50 + 40) / 2;
  513. let _currentBgStartX = this.index == this.spawnArray.length ? _drawObj.bgStartXAllNormal : _drawObj
  514. .bgStartXHasTip;
  515. // console.log("_drawObj.startX:", _drawObj.startX, _drawObj.width / 2);
  516. //绘制八个位置。如果生成数量不够的话。补上对应的数量
  517. let count = 8 - this.spawnArray.length;
  518. count = Math.ceil(count / 2);
  519. //计算一个节点数组
  520. for (let i = 0; i < this.spawnArray.length; i++) {
  521. //默认 mid 图标
  522. let _temp = this.spawnArray[i].bTrigger ? this.midJump : this.midJumpWhite;
  523. if (this.spawnArray[i].icon == 'directionJump') {
  524. _temp = this.spawnArray[i].bTrigger ? this.directionJump : this.directionJumpWhite;
  525. } else if (this.spawnArray[i].icon == 'rotateJump') {
  526. _temp = this.spawnArray[i].bTrigger ? this.rotateJump : this.rotateJumpWhite;
  527. }
  528. //如果是相反绘制,需要加多一个自身位置偏移
  529. let _pos = this.spawnArray[i].scaleX < 0 ? i + 1 : i;
  530. let _spacing = i == 0 ? 0 : 1;
  531. let _addData = 0;
  532. //大图提示后面要加上大图占的位置,为大图宽度减小图( 90 - 50 )
  533. if (i >= this.index) {
  534. _addData = 40;
  535. }
  536. if (i == 0) {
  537. //_frontBgData 为减去大图位置,10 为偏移位置
  538. let _frontBgData = 40;
  539. //绘制前背景
  540. for (let i = 0; i < count; i++) {
  541. let _currentStartPos = _currentBgStartX - _frontBgData - i * 50 - 10;
  542. this.actionJumpCanvas.drawImage(this.jumpNormalImage.path, _currentStartPos, 0, 50, 114)
  543. }
  544. }
  545. this.actionJumpCanvas.save();
  546. this.actionJumpCanvas.globalAlpha = this.spawnArray[i].bTrigger ? 0.7 : 1;
  547. this.actionJumpCanvas.scale(this.spawnArray[i].scaleX, 1);
  548. let _currentPos = 0;
  549. let _currentCenterPos = 0;
  550. if (i == this.index) {
  551. _currentPos = (_currentBgStartX + _pos * 90 - i * 40) *
  552. this
  553. .spawnArray[i]
  554. .scaleX;
  555. this.actionJumpCanvas.drawImage(this.jumpTipImage.path, _currentPos, 0, 90, 114);
  556. //计算对应中心点的值
  557. _currentCenterPos = _currentPos + 90 / 2 - 25 / 2;
  558. //57 - 40/2 是取画布中心y点,减去图标本身高的一半的值
  559. this.actionJumpCanvas.drawImage(_temp.path, _currentCenterPos - 7.5, 37, 40, 40);
  560. } else {
  561. _currentPos = (_currentBgStartX + _addData + _pos *
  562. 50) * this
  563. .spawnArray[i]
  564. .scaleX;
  565. this.actionJumpCanvas.drawImage(this.jumpNormalImage.path, _currentPos, 0, 50, 114)
  566. //计算对应中心点的值
  567. _currentCenterPos = _currentPos + 50 / 2 - 25 / 2;
  568. //44.5 是取画布中心y点,减去图标本身高的一半的值
  569. this.actionJumpCanvas.drawImage(_temp.path, _currentCenterPos, 44.5, 25, 25);
  570. }
  571. this.actionJumpCanvas.restore();
  572. if (i == this.spawnArray.length - 1) {
  573. //绘制结束位置
  574. // console.log("count:", count);
  575. for (let i = 0; i < count; i++) {
  576. let _currentEndPos = _currentBgStartX + _addData + (this.spawnArray.length + i) * 50;
  577. // console.log("end _currentEndPos:", _currentEndPos);
  578. this.actionJumpCanvas.drawImage(this.jumpNormalImage.path, _currentEndPos, 0, 50, 114)
  579. }
  580. }
  581. }
  582. this.actionJumpCanvas.draw();
  583. },
  584. onClear() {
  585. this.resetJumpGame();
  586. this.onDrawBg();
  587. this.xA = 0;
  588. this.yA = 0;
  589. this.zA = 0;
  590. this.bJump = false;
  591. this.actionJumpObj.resetAll();
  592. },
  593. onJumpType(event) {
  594. // console.log("onJumpType:", event);
  595. if (this.isGameOver || !this.bJumpPlay) return;
  596. this.eliminateJumpPrefab(event);
  597. },
  598. // update (dt) {}
  599. eliminateJumpPrefab(_jumpType) {
  600. let _temp = this.spawnArray[this.index];
  601. //如果当前的跳类型和预制目标一样
  602. if (_jumpType == _temp.jumpCode) {
  603. _temp.bTrigger = true;
  604. this.index++;
  605. if (this.index >= this.spawnArray.length) {
  606. clearInterval(this.countdownInterval);
  607. this.countdownInterval = null;
  608. this.startJumpGame();
  609. } else {
  610. //绘制新触发状态
  611. this.onDraw();
  612. }
  613. //成功
  614. this.setEliminationCount(1);
  615. } else {
  616. //失误
  617. this.setFaultCount(1);
  618. }
  619. },
  620. //设置倒计时
  621. setCountdown(value) {
  622. this.countdown -= value;
  623. // this.countdownLabel.string = '倒计时:' + this.countdown;
  624. this.$emit('actionJumpCountDownUpdate', {
  625. countDown: this.countdown
  626. });
  627. },
  628. resetCountdown(value) {
  629. this.countdown = value;
  630. // this.countdownLabel.string = '倒计时:' + this.countdown;
  631. this.$emit('actionJumpCountDownUpdate', {
  632. countDown: this.countdown
  633. });
  634. },
  635. //设置ui信息
  636. setEliminationCount(value) {
  637. this.taskSignCurCount++;
  638. this.roundingCount += this.addShowCountUnit;
  639. //取两位后计算Math floor,不然会有偏差值问题 Math.floor(39.9999999+ 0.1111111) = 39
  640. this.showCurCount = Math.floor(this.roundingCount.toFixed(2));
  641. this.eliminationCount += value;
  642. // this.eliminationLabel.string = '消除数量:' + this.eliminationCount.toString();
  643. this.$emit('actionJumpDataUpdate', {
  644. eliminationCount: this.eliminationCount,
  645. faultCount: this.faultCount
  646. });
  647. //todo 能量槽满的时候,说明踩中次数到达
  648. if (this.taskSignCurCount >= this.taskSignCount) {
  649. this.onGameOver();
  650. }
  651. },
  652. onGameOver() {
  653. this.isGameOver = true;
  654. //处理参数
  655. this.onClear();
  656. this.onClearData();
  657. this._changePlay();
  658. /**
  659. * 判断胜利和失败
  660. */
  661. let myWin = false; //默认失败
  662. //比如,1 规定时间内跳完所有标志块.2 充满左右两边蓄能槽
  663. if (this.taskSignCurCount >= this.taskSignCount) {
  664. //todo 有情况是2条件情况和1 其实这里处理一样,判断跳的完成度是否符合条件
  665. myWin = true;
  666. }
  667. this.$emit('gameOver', {
  668. myWin: myWin
  669. });
  670. },
  671. setFaultCount(value) {
  672. this.faultCount += value;
  673. // this.faultLabel.string = '失误:' + this.faultCount;
  674. this.$emit('actionJumpDataUpdate', {
  675. eliminationCount: this.eliminationCount,
  676. faultCount: this.faultCount
  677. });
  678. },
  679. //控制播放
  680. onControllerPlay() {
  681. if (this.bJumpPlay) {
  682. //仅仅暂停,没有清空数据
  683. this._changePlay(false);
  684. this.$emit("actionJumpControllerPlay", false);
  685. } else {
  686. this.$emit("actionJumpCheck");
  687. }
  688. },
  689. onContinueGame() {
  690. this._changePlay(true);
  691. },
  692. onClearActionJumpData() {
  693. this.onClear();
  694. this.onClearData();
  695. },
  696. //修改状态
  697. _changePlay(bPlaying) {
  698. if (bPlaying) {
  699. this.bJumpPlay = bPlaying;
  700. } else {
  701. this.bJumpPlay = !this.bJumpPlay;
  702. }
  703. },
  704. onActionJumpPlay() {
  705. this.onClear();
  706. this.onClearData();
  707. this.startJumpGame();
  708. this._changePlay(true);
  709. //reset 对应计算数据
  710. this.taskSignCurCount = 0;
  711. this.showCurCount = 0;
  712. this.roundingCount = 0;
  713. },
  714. getCurrentJumpType() {
  715. let _temp = this.spawnArray[this.index];;
  716. return _temp.jumpCode;
  717. },
  718. //监听跳的状态数据
  719. listenStateDataOfJump(data) {
  720. if (this.isGameOver || !this.bJumpPlay) return;
  721. let _jumpType = this.getCurrentJumpType();
  722. //初始全部默认状态
  723. let _tempState = [{
  724. jumpName: 'NORMAL',
  725. jumpCode: 0,
  726. bTrigger: true,
  727. describe: '正常跳'
  728. },
  729. {
  730. jumpName: 'LEFT',
  731. jumpCode: 1,
  732. bTrigger: false,
  733. describe: '左直跳'
  734. },
  735. {
  736. jumpName: 'RIGHT',
  737. jumpCode: 2,
  738. bTrigger: false,
  739. describe: '右直跳'
  740. },
  741. {
  742. jumpName: 'LEFT_ROTATE',
  743. jumpCode: 3,
  744. bTrigger: false,
  745. describe: '左旋转跳'
  746. },
  747. {
  748. jumpName: 'RIGHT_ROTATE',
  749. jumpCode: 4,
  750. bTrigger: false,
  751. describe: '右旋转跳'
  752. }
  753. ];
  754. let {
  755. currentMaxValue,
  756. oGyroValue
  757. } = data
  758. // console.log(data);
  759. if (currentMaxValue == 0) {
  760. //JumpType.NORMAL = 0
  761. if (_jumpType == 0) {
  762. // console.log('1====', data);
  763. this.eliminateJumpPrefabFormTemp(_tempState);
  764. }
  765. //JumpType.RIGHT_ROTATE = 4
  766. else if (_jumpType == 4 && (!this.isY && oGyroValue < 0 || this.isY && oGyroValue > 0)) {
  767. // console.log('right1:', oGyroValue);
  768. _tempState[4].bTrigger = true;
  769. this.eliminateJumpPrefabFormTemp(_tempState);
  770. }
  771. //JumpType.LEFT_ROTATE = 3
  772. else if (_jumpType == 3 && (!this.isY && oGyroValue > 0 || this.isY && oGyroValue < 0)) {
  773. // console.log('left1:', oGyroValue);
  774. _tempState[3].bTrigger = true;
  775. this.eliminateJumpPrefabFormTemp(_tempState);
  776. }
  777. } else {
  778. console.log('2====', data);
  779. if (currentMaxValue > 5) {
  780. _tempState[1].bTrigger = true;
  781. } else if (currentMaxValue < -5) {
  782. _tempState[2].bTrigger = true;
  783. }
  784. if (this.isY) {
  785. //如果是检测到旋转跳
  786. if (oGyroValue > 5) {
  787. // console.log('y right:', oGyroValue);
  788. _tempState[4].bTrigger = true;
  789. } else if (oGyroValue < -5) {
  790. // console.log('y left:', oGyroValue);
  791. _tempState[3].bTrigger = true;
  792. }
  793. } else {
  794. //如果是检测到旋转跳
  795. if (oGyroValue < -5) {
  796. // console.log('x right:', oGyroValue);
  797. _tempState[4].bTrigger = true;
  798. } else if (oGyroValue > 5) {
  799. // console.log('x left:', oGyroValue);
  800. _tempState[3].bTrigger = true;
  801. }
  802. }
  803. this.eliminateJumpPrefabFormTemp(_tempState);
  804. }
  805. },
  806. eliminateJumpPrefabFormTemp(_tempState) {
  807. //如果消除完,需要重新生成
  808. let _temp = this.spawnArray[this.index];
  809. let bSuccess = false;
  810. console.log(_tempState);
  811. for (let i = 0; i < _tempState.length; i++) {
  812. let _state = _tempState[i];
  813. //如果当前的跳类型和预制目标一样
  814. if (_state.jumpCode == _temp.jumpCode && _state.bTrigger) {
  815. //成功
  816. bSuccess = true;
  817. break;
  818. }
  819. }
  820. //如果存在其中一个为true
  821. if (bSuccess) {
  822. console.log("bSuccess:", bSuccess);
  823. _temp.bTrigger = true;
  824. this.index++;
  825. if (this.index >= this.spawnArray.length) {
  826. clearInterval(this.countdownInterval);
  827. this.countdownInterval = null;
  828. this.startJumpGame();
  829. } else {
  830. //绘制新触发状态
  831. this.onDraw();
  832. }
  833. //成功
  834. this.setEliminationCount(1);
  835. } else {
  836. //失误
  837. this.setFaultCount(1);
  838. }
  839. },
  840. /**
  841. * @param {Object} gameData
  842. * 识别跳部分数据处理
  843. */
  844. onBLERopeUpdate(gameData) {
  845. if (!this.bJumpPlay || this.isGameOver) return;
  846. //********陀螺仪角速度********
  847. let {
  848. gx,
  849. gy,
  850. gz
  851. } = gameData.gyro;
  852. let {
  853. min,
  854. s,
  855. ms
  856. } = gameData;
  857. let _ax = -gameData.acc.ax * 10;
  858. let _ay = gameData.acc.ay * 10;
  859. let _az = gameData.acc.az * 10;
  860. //低通滤波分离重力
  861. let alpha = 0.8;
  862. this.xA = alpha * this.xA + (1 - alpha) * _ax;
  863. this.yA = alpha * this.yA + (1 - alpha) * _ay;
  864. this.zA = alpha * this.zA + (1 - alpha) * _az;
  865. //高通滤波获取线性速度
  866. let linear_acceleration_x = _ax - this.xA;
  867. let linear_acceleration_y = _az - this.zA;
  868. let linear_acceleration_z = _ay - this.yA;
  869. let _temp = {
  870. linearAcc: {
  871. lAccX: linear_acceleration_x,
  872. lAccY: linear_acceleration_y,
  873. lAccZ: linear_acceleration_z
  874. }, //gameData.acc,
  875. oriAcc: {
  876. oAccX: _ax,
  877. oAccY: _ay,
  878. oAccZ: _az
  879. },
  880. gravityAcc: {
  881. gravityX: this.xA,
  882. gravityY: this.yA,
  883. gravityZ: this.zA
  884. },
  885. bLimitRebound: false,
  886. resultant: Math.sqrt(_ax * _ax +
  887. _ay * _ay + _az * _az),
  888. runIndex: this.BLEAccIndex,
  889. //陀螺仪
  890. oriGyro: {
  891. oGyroX: gx,
  892. oGyroY: gy,
  893. oGyroZ: gz
  894. },
  895. //输入当前轴
  896. bYAxis: this.isY,
  897. };
  898. this.actionJumpObj.updateJump(_temp);
  899. if (this.BLEAccIndex > 150) {
  900. this.onClearData();
  901. this.bJump = false;
  902. return;
  903. }
  904. this.BLEAccIndex++;
  905. },
  906. onClearData() {
  907. this.BLEAccIndex = 0;
  908. },
  909. onChangeY() {
  910. this.isY = true;
  911. },
  912. onChangeX() {
  913. this.isY = false;
  914. }
  915. }
  916. }
  917. </script>
  918. <style lang="scss">
  919. // .action-jump-parent {
  920. // border: 1rpx solid #ffaa7f;
  921. // position: relative;
  922. // display: flex;
  923. // justify-content: center;
  924. // overflow: hidden;
  925. // top: 0;
  926. // width: 100%;
  927. // }
  928. // .action-jump-container {
  929. // width: 100%;
  930. // height: 114px;
  931. // border: 1rpx solid #000000;
  932. // position: relative;
  933. // }
  934. .mid-absolute {
  935. position: absolute;
  936. top: 0;
  937. bottom: 0;
  938. right: 0;
  939. left: 0;
  940. margin: auto;
  941. }
  942. .action-jump-timer {
  943. position: absolute;
  944. top: -150rpx;
  945. }
  946. </style>