action-jump.vue 34 KB

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