action-jump.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template>
  2. <view>
  3. <view style="display: flex;justify-content: space-between;">
  4. <button @click="startJumpGame">startGame</button>
  5. <button @click="onClear">onClear</button>
  6. </view>
  7. <view style="display: flex;justify-content: space-between;" class="margin-top margin-bottom">
  8. <button @click="onJumpType(0)">jump</button>
  9. <button @click="onJumpType(1)">left</button>
  10. <button @click="onJumpType(2)">right</button>
  11. <button @click="onJumpType(3)">rLeft</button>
  12. <button @click="onJumpType(4)">rRight</button>
  13. </view>
  14. <view style="display: flex;justify-content: space-around;">
  15. <view style="font-size: 14px;">t:{{countdown}}</view>
  16. <view style="font-size: 14px;">e:{{eliminationCount}}</view>
  17. <view style="font-size: 14px;">f:{{faultCount}}</view>
  18. </view>
  19. <view class="action-jump-parent margin-top margin-bottom">
  20. <view class="action-jump-container">
  21. <canvas canvas-id="actionJumpCanvas" style="width: 375px;height: 50px; background-color: #1CBBB4;"></canvas>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. export default {
  28. data() {
  29. return {
  30. directionJump: null,
  31. midJump: null,
  32. rotateJump: null,
  33. jumpTypeArray: [{
  34. jumpName: 'NORMAL',
  35. jumpCode: 0,
  36. icon: 'midJump',
  37. scaleX: 1,
  38. bTrigger: false,
  39. }, {
  40. jumpName: 'LEFT',
  41. jumpCode: 1,
  42. icon: 'directionJump',
  43. scaleX: -1,
  44. bTrigger: false,
  45. }, {
  46. jumpName: 'RIGHT',
  47. jumpCode: 2,
  48. icon: 'directionJump',
  49. scaleX: 1,
  50. bTrigger: false,
  51. }, {
  52. jumpName: 'LEFT_ROTATE',
  53. jumpCode: 3,
  54. icon: 'rotateJump',
  55. scaleX: 1,
  56. bTrigger: false,
  57. }, {
  58. jumpName: 'RIGHT_ROTATE',
  59. jumpCode: 4,
  60. icon: 'rotateJump',
  61. scaleX: -1,
  62. bTrigger: false,
  63. }],
  64. spawnArray: [],
  65. //下一个生成是相反的方向
  66. bNextSpawnRightDirection: false,
  67. bNextSpawnRightRotateDirection: false,
  68. level: 3,
  69. countdown: 60,
  70. countdownInterval: null,
  71. faultCount:0,
  72. eliminationCount:0,
  73. }
  74. },
  75. created() {
  76. let _self = this;
  77. uni.getImageInfo({
  78. src: "../../../static/modal/action-jump/directionJump.png",
  79. success: function(image) {
  80. _self.directionJump = image;
  81. }
  82. });
  83. uni.getImageInfo({
  84. src: "../../../static/modal/action-jump/midJump.png",
  85. success: function(image) {
  86. _self.midJump = image;
  87. console.log("==============", image);
  88. }
  89. });
  90. uni.getImageInfo({
  91. src: "../../../static/modal/action-jump/rotateJump.png",
  92. success: function(image) {
  93. _self.rotateJump = image;
  94. }
  95. });
  96. this.actionJumpCanvas = uni.createCanvasContext("actionJumpCanvas", this);
  97. console.log("this.actionJumpCanvas:", this.actionJumpCanvas);
  98. },
  99. methods: {
  100. /**
  101. * 重置生成数组,重置倒计时
  102. */
  103. resetJumpGame() {
  104. this.spawnArray = [];
  105. if (this.countdownInterval) {
  106. clearInterval(this.countdownInterval);
  107. this.countdownInterval = null;
  108. }
  109. this.resetCountdown(60);
  110. },
  111. startJumpGame() {
  112. this.resetJumpGame();
  113. //开始游戏
  114. this.index = 0;
  115. // this.levelLabel.string = '关卡' + this.level;
  116. if (this.level == 1) {
  117. for (let i = 0; i < 2; i++) {
  118. this.spawnJumpPrefabs(i);
  119. }
  120. // this.buttonLabel.string = '下一关';
  121. } else if (this.level == 2) {
  122. for (let i = 0; i < 4; i++) {
  123. this.spawnJumpPrefabs(i);
  124. }
  125. // this.buttonLabel.string = '下一关';
  126. } else if (this.level == 3) {
  127. for (let i = 0; i < 6; i++) {
  128. this.spawnJumpPrefabs(i);
  129. }
  130. // this.buttonLabel.string = '最后一关';
  131. //todo 暂时给循环
  132. this.level = 0;
  133. }
  134. this.level++;
  135. //倒计时
  136. this.countdownInterval = setInterval(() => {
  137. if (this.countdown <= 0) {
  138. clearInterval(this.countdownInterval);
  139. this.countdownInterval = null;
  140. //处理下一个关卡
  141. // console.warn('时间到,处理下一个关卡');
  142. this.startJumpGame();
  143. return;
  144. }
  145. this.setCountdown(1);
  146. }, 1000);
  147. this.onDraw();
  148. },
  149. spawnJumpPrefabs(index) {
  150. let ran = Math.floor(Math.random() * 5);
  151. //todo 生成的节点,后面再处理节奏问题。比如生成顺序
  152. let _jumpPrefab = Object.assign({},this.jumpTypeArray[ran]);
  153. //这里处理相反方向,比如生成了一个左旋转,下一个右旋转
  154. if (_jumpPrefab.jumpName == 'LEFT') {
  155. if (this.bNextSpawnRightDirection) {
  156. //如果是对应的需要记录一个对应的准确值
  157. this.bNextSpawnRightDirection = false;
  158. } else {
  159. ran = 2; //RIGHT;
  160. this.bNextSpawnRightDirection = true;
  161. }
  162. } else if (_jumpPrefab.jumpName == 'RIGHT') {
  163. if (this.bNextSpawnRightDirection) {
  164. ran = 1; //LEFT;
  165. this.bNextSpawnRightDirection = false;
  166. } else {
  167. this.bNextSpawnRightDirection = true;
  168. }
  169. }
  170. if (_jumpPrefab.jumpName == 'LEFT_ROTATE') {
  171. if (this.bNextSpawnRightRotateDirection) {
  172. //如果是对应的需要记录一个对应的准确值
  173. this.bNextSpawnRightRotateDirection = false;
  174. } else {
  175. ran = 4; //RIGHT_ROTATE;
  176. this.bNextSpawnRightRotateDirection = true;
  177. }
  178. // console.log('l==rotate', ran);
  179. } else if (_jumpPrefab.jumpName == 'RIGHT_ROTATE') {
  180. if (this.bNextSpawnRightRotateDirection) {
  181. ran = 3; //LEFT_ROTATE;
  182. this.bNextSpawnRightRotateDirection = false;
  183. } else {
  184. //如果是对应的需要记录一个对应的准确值
  185. this.bNextSpawnRightRotateDirection = true;
  186. }
  187. }
  188. this.spawnArray.push(_jumpPrefab);
  189. },
  190. onDraw() {
  191. this.actionJumpCanvas.clearRect(0, 0, 375, 50);
  192. for (let i = 0; i < this.spawnArray.length; i++) {
  193. //默认 mid 图标
  194. let _temp = this.midJump;
  195. if (this.spawnArray[i].icon == 'directionJump') {
  196. _temp = this.directionJump;
  197. } else if (this.spawnArray[i].icon == 'rotateJump') {
  198. _temp = this.rotateJump;
  199. }
  200. this.actionJumpCanvas.save();
  201. this.actionJumpCanvas.globalAlpha = this.spawnArray[i].bTrigger ? 0.5 : 1;
  202. this.actionJumpCanvas.scale(this.spawnArray[i].scaleX, 1);
  203. //如果是相反绘制,需要加多一个自身位置偏移
  204. let _pos = this.spawnArray[i].scaleX < 0 ? i + 1 : i;
  205. this.actionJumpCanvas.drawImage(_temp.path, (_pos * 50 + 20) * this.spawnArray[i].scaleX, 0, 50, 50);
  206. this.actionJumpCanvas.restore();
  207. }
  208. // console.log('draw');
  209. this.actionJumpCanvas.draw();
  210. },
  211. onClear() {
  212. this.resetJumpGame();
  213. this.onDraw();
  214. },
  215. onJumpType(event) {
  216. // console.log("onJumpType:", event);
  217. this.eliminateJumpPrefab(event);
  218. },
  219. // update (dt) {}
  220. eliminateJumpPrefab(_jumpType) {
  221. //如果消除完,需要重新生成
  222. if (this.index >= this.spawnArray.length) return;
  223. let _temp = this.spawnArray[this.index];
  224. _temp.bTrigger = true;
  225. this.index++;
  226. //绘制新触发状态
  227. this.onDraw();
  228. //如果当前的跳类型和预制目标一样
  229. if (_jumpType == _temp.jumpCode) {
  230. //成功
  231. this.setEliminationCount(1);
  232. } else {
  233. //失误
  234. this.setFaultCount(1);
  235. }
  236. },
  237. //设置倒计时
  238. setCountdown(value) {
  239. this.countdown -= value;
  240. // this.countdownLabel.string = '倒计时:' + this.countdown;
  241. },
  242. resetCountdown(value) {
  243. this.countdown = value;
  244. // this.countdownLabel.string = '倒计时:' + this.countdown;
  245. },
  246. //设置ui信息
  247. setEliminationCount(value) {
  248. this.eliminationCount += value;
  249. // this.eliminationLabel.string = '消除数量:' + this.eliminationCount.toString();
  250. },
  251. setFaultCount(value) {
  252. this.faultCount += value;
  253. // this.faultLabel.string = '失误:' + this.faultCount;
  254. }
  255. }
  256. }
  257. </script>
  258. <style lang="scss">
  259. .action-jump-parent {
  260. border: 1rpx solid #ffaa7f;
  261. position: relative;
  262. display: flex;
  263. justify-content: center;
  264. overflow: hidden;
  265. top: 0;
  266. width: 750rpx;
  267. }
  268. .action-jump-container {
  269. width: 100%;
  270. height: 50px;
  271. border: 1rpx solid #000000;
  272. position: relative;
  273. // display: flex;
  274. }
  275. </style>