action-jump.vue 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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','onConvertDeviceData']),
  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('stateDataOfJump:',JSON.stringify(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. //left jump
  781. _tempState[1].bTrigger = true;
  782. } else if (currentMaxValue > 5 ) {
  783. //right jump
  784. _tempState[2].bTrigger = true;
  785. }
  786. if (this.isY) {
  787. //如果是检测到旋转跳
  788. if (oGyroValue > 5) {
  789. // console.log('y right:', oGyroValue);
  790. _tempState[4].bTrigger = true;
  791. } else if (oGyroValue < -5) {
  792. // console.log('y left:', oGyroValue);
  793. _tempState[3].bTrigger = true;
  794. }
  795. } else {
  796. //如果是检测到旋转跳
  797. if (oGyroValue < -5) {
  798. // console.log('x right:', oGyroValue);
  799. _tempState[4].bTrigger = true;
  800. } else if (oGyroValue > 5) {
  801. // console.log('x left:', oGyroValue);
  802. _tempState[3].bTrigger = true;
  803. }
  804. }
  805. this.eliminateJumpPrefabFormTemp(_tempState);
  806. }
  807. },
  808. eliminateJumpPrefabFormTemp(_tempState) {
  809. //如果消除完,需要重新生成
  810. let _temp = this.spawnArray[this.index];
  811. let bSuccess = false;
  812. console.log(JSON.stringify(_tempState));
  813. for (let i = 0; i < _tempState.length; i++) {
  814. let _state = _tempState[i];
  815. //如果当前的跳类型和预制目标一样
  816. if (_state.jumpCode == _temp.jumpCode && _state.bTrigger) {
  817. //成功
  818. bSuccess = true;
  819. break;
  820. }
  821. }
  822. //如果存在其中一个为true
  823. if (bSuccess) {
  824. // console.log("bSuccess:", bSuccess);
  825. _temp.bTrigger = true;
  826. this.index++;
  827. if (this.index >= this.spawnArray.length) {
  828. clearInterval(this.countdownInterval);
  829. this.countdownInterval = null;
  830. this.startJumpGame();
  831. } else {
  832. //绘制新触发状态
  833. this.onDraw();
  834. }
  835. //成功
  836. this.setEliminationCount(1);
  837. } else {
  838. //失误
  839. this.setFaultCount(1);
  840. }
  841. },
  842. /**
  843. * @param {Object} gameData
  844. * 识别跳部分数据处理
  845. */
  846. onBLERopeUpdate(gameData) {
  847. if (!this.bJumpPlay || this.isGameOver) return;
  848. //********陀螺仪角速度********
  849. let {
  850. gx,
  851. gy,
  852. gz
  853. } = gameData.gyro;
  854. let {
  855. min,
  856. s,
  857. ms
  858. } = gameData;
  859. //-gameData.acc.ax * 10;
  860. let _ax = gameData.acc.ax * 10;
  861. let _ay = gameData.acc.ay * 10;
  862. let _az = gameData.acc.az * 10;
  863. //低通滤波分离重力
  864. let alpha = 0.8;
  865. this.xA = alpha * this.xA + (1 - alpha) * _ax;
  866. this.yA = alpha * this.yA + (1 - alpha) * _ay;
  867. this.zA = alpha * this.zA + (1 - alpha) * _az;
  868. //高通滤波获取线性速度
  869. let linear_acceleration_x = _ax - this.xA;
  870. let linear_acceleration_y = _az - this.zA;
  871. let linear_acceleration_z = _ay - this.yA;
  872. let _temp = {
  873. linearAcc: {
  874. lAccX: linear_acceleration_x,
  875. lAccY: linear_acceleration_y,
  876. lAccZ: linear_acceleration_z
  877. }, //gameData.acc,
  878. oriAcc: {
  879. oAccX: _ax,
  880. oAccY: _ay,
  881. oAccZ: _az
  882. },
  883. gravityAcc: {
  884. gravityX: this.xA,
  885. gravityY: this.yA,
  886. gravityZ: this.zA
  887. },
  888. bLimitRebound: false,
  889. resultant: Math.sqrt(_ax * _ax +
  890. _ay * _ay + _az * _az),
  891. runIndex: this.BLEAccIndex,
  892. //陀螺仪
  893. oriGyro: {
  894. oGyroX: gx,
  895. oGyroY: gy,
  896. oGyroZ: gz
  897. },
  898. //输入当前轴
  899. bYAxis: true,
  900. };
  901. this.actionJumpObj.updateJump(_temp);
  902. if (this.BLEAccIndex > 150) {
  903. this.onClearData();
  904. this.bJump = false;
  905. return;
  906. }
  907. this.BLEAccIndex++;
  908. },
  909. onClearData() {
  910. this.BLEAccIndex = 0;
  911. },
  912. onChangeY() {
  913. this.isY = true;
  914. },
  915. onChangeX() {
  916. this.isY = false;
  917. }
  918. }
  919. }
  920. </script>
  921. <style lang="scss">
  922. // .action-jump-parent {
  923. // border: 1rpx solid #ffaa7f;
  924. // position: relative;
  925. // display: flex;
  926. // justify-content: center;
  927. // overflow: hidden;
  928. // top: 0;
  929. // width: 100%;
  930. // }
  931. // .action-jump-container {
  932. // width: 100%;
  933. // height: 114px;
  934. // border: 1rpx solid #000000;
  935. // position: relative;
  936. // }
  937. .mid-absolute {
  938. position: absolute;
  939. top: 0;
  940. bottom: 0;
  941. right: 0;
  942. left: 0;
  943. margin: auto;
  944. }
  945. .action-jump-timer {
  946. position: absolute;
  947. top: -150rpx;
  948. }
  949. </style>