action-jump.vue 34 KB

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