action-jump.vue 33 KB

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