game.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // Learn TypeScript:
  2. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  3. // Learn Attribute:
  4. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  5. // Learn life-cycle callbacks:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  7. const { ccclass, property } = cc._decorator;
  8. import { JumpType } from '../game/enumConfig';
  9. // var jumpTypeTemp = cc.Class({
  10. // name:'jumpTypeTemp',
  11. // properties:{
  12. // index:0,
  13. // item:null,
  14. // bTrigger:false
  15. // }
  16. // })
  17. @ccclass
  18. export default class game extends cc.Component {
  19. @property(cc.Prefab)
  20. jumpPrefab: cc.Prefab = null;
  21. @property(cc.Node)
  22. spawnNode: cc.Node = null;
  23. //关卡
  24. @property
  25. level: number = 1;
  26. @property(cc.Label)
  27. levelLabel: cc.Label = null;
  28. // LIFE-CYCLE CALLBACKS:
  29. // onLoad () {}
  30. spawnArray = [];
  31. spawnTempArray = [];
  32. index = 0;
  33. //倒计时时间
  34. @property
  35. countdown: number = 60;
  36. @property(cc.Label)
  37. countdownLabel: cc.Label = null;
  38. @property
  39. eliminationCount: number = 0;
  40. @property(cc.Label)
  41. eliminationLabel: cc.Label = null;
  42. @property
  43. faultCount: number = 0;
  44. @property(cc.Label)
  45. faultLabel: cc.Label = null;
  46. @property(cc.Label)
  47. buttonLabel: cc.Label = null;
  48. countdownInterval = null;
  49. //下一个生成是相反的方向
  50. bNextSpawnRightDirection = false;
  51. bNextSpawnRightRotateDirection = false;
  52. //生成预制的模板,用count 来判断生成哪一种
  53. template = [{
  54. count: 1,
  55. spawnList: [[0]]
  56. }, {
  57. count: 2,
  58. spawnList: [[2, 1], [1, 2], [3, 4], [4, 3]]
  59. }, {
  60. count: 3,
  61. spawnList: [[2, 0, 1], [1, 0, 2], [3, 0, 4], [4, 0, 3]]
  62. },];
  63. isY = true;
  64. start() {
  65. }
  66. //监听跳的状态数据
  67. listenStateDataOfJump(data, isY) {
  68. this.isY = isY;
  69. let _jumpType = this.getCurrentJumpType();
  70. //初始全部默认状态
  71. let _tempState =
  72. [{ jumpType: JumpType.NORMAL, bTrigger: true, describe: '正常跳' },
  73. { jumpType: JumpType.LEFT, bTrigger: false, describe: '左直跳' },
  74. { jumpType: JumpType.RIGHT, bTrigger: false, describe: '右直跳' },
  75. { jumpType: JumpType.LEFT_ROTATE, bTrigger: false, describe: '左旋转跳' },
  76. { jumpType: JumpType.RIGHT_ROTATE, bTrigger: false, describe: '右旋转跳' }];
  77. let { currentMaxValue, oGyroValue } = data
  78. console.log('stateDataOfJump:', JSON.stringify(data));
  79. let _rotateLimit = 100;
  80. if (currentMaxValue == 0) {
  81. if (_jumpType == JumpType.NORMAL) {
  82. _tempState[0].bTrigger = true;
  83. this.eliminateJumpPrefabFormTemp(_tempState);
  84. } else if (_jumpType == JumpType.RIGHT_ROTATE && oGyroValue > _rotateLimit) {
  85. console.log('right1:', oGyroValue);
  86. _tempState[4].bTrigger = true;
  87. this.eliminateJumpPrefabFormTemp(_tempState);
  88. } else if (_jumpType == JumpType.LEFT_ROTATE && oGyroValue < -_rotateLimit) {
  89. console.log('left1:', oGyroValue);
  90. _tempState[3].bTrigger = true;
  91. this.eliminateJumpPrefabFormTemp(_tempState);
  92. }
  93. } else {
  94. //如果是检测到旋转跳
  95. if (oGyroValue > _rotateLimit) {
  96. // console.log('y right:', oGyroValue);
  97. _tempState[4].bTrigger = true;
  98. } else if (oGyroValue < - _rotateLimit) {
  99. // console.log('y left:', oGyroValue);
  100. _tempState[3].bTrigger = true;
  101. } else if (currentMaxValue > 5) {
  102. _tempState[2].bTrigger = true;
  103. } else if (currentMaxValue < -5) {
  104. _tempState[1].bTrigger = true;
  105. }
  106. this.eliminateJumpPrefabFormTemp(_tempState);
  107. }
  108. }
  109. /**
  110. * 重置生成数组,重置倒计时
  111. */
  112. resetJumpGame() {
  113. for (let i = 0; i < this.spawnArray.length; i++) {
  114. this.spawnArray[i].destroy();
  115. }
  116. this.spawnArray = [];
  117. if (this.countdownInterval) {
  118. clearInterval(this.countdownInterval);
  119. this.countdownInterval = null;
  120. }
  121. this.resetCountdown(60);
  122. }
  123. startJumpGame() {
  124. this.resetJumpGame();
  125. //开始游戏
  126. this.index = 0;
  127. this.levelLabel.string = '关卡' + this.level;
  128. let _ranType = Math.floor(Math.random() * 2);
  129. if (this.level == 1) {
  130. // for (let i = 0; i < 2; i++) {
  131. // this.spawnJumpPrefabs(i);
  132. // }
  133. //随便生成一组数据
  134. let _spawnList = this.template[1].spawnList;
  135. let ran = Math.floor(Math.random() * 2);
  136. if (_ranType >= 1) ran += 2;
  137. for (let i = 0; i < _spawnList[ran].length; i++) {
  138. this.spawnJumpPrefabsFromType(i, _spawnList[ran][i]);
  139. }
  140. this.buttonLabel.string = '下一关';
  141. } else if (this.level == 2) {
  142. // for (let i = 0; i < 4; i++) {
  143. // this.spawnJumpPrefabs(i);
  144. // }
  145. //随便生成二组数据
  146. let _newArray = [];
  147. let _spawnList1 = this.template[1].spawnList;
  148. let ran1 = Math.floor(Math.random() * 2);
  149. _newArray = _newArray.concat(_spawnList1[ran1]);
  150. let _spawnList2 = this.template[2].spawnList;
  151. let ran2 = Math.floor(Math.random() * 2);
  152. if (_ranType >= 1) ran2 += 2;
  153. _newArray = _newArray.concat(_spawnList2[ran2]);
  154. console.log(_newArray);
  155. for (let i = 0; i < _newArray.length; i++) {
  156. this.spawnJumpPrefabsFromType(i, _newArray[i]);
  157. }
  158. this.buttonLabel.string = '下一关';
  159. } else if (this.level == 3) {
  160. // for (let i = 0; i < 6; i++) {
  161. // this.spawnJumpPrefabs(i);
  162. // }
  163. //随便生成三组数据
  164. let _newArray = [];
  165. // let _spawnList1 = this.template[1].spawnList;
  166. // let ran1 = Math.floor(Math.random() * 2);
  167. // _newArray = _newArray.concat(_spawnList1[ran1]);
  168. let _spawnList2 = this.template[2].spawnList;
  169. let ran2 = Math.floor(Math.random() * 2);
  170. let ran3 = Math.floor(Math.random() * 2);
  171. if (_ranType >= 1) {
  172. ran2 += 2;
  173. ran3 += 2;
  174. }
  175. _newArray = _newArray.concat(_spawnList2[ran2]);
  176. _newArray = _newArray.concat(_spawnList2[ran3]);
  177. for (let i = 0; i < _newArray.length; i++) {
  178. this.spawnJumpPrefabsFromType(i, _newArray[i]);
  179. }
  180. this.buttonLabel.string = '最后一关';
  181. //todo 暂时给循环
  182. this.level = 0;
  183. }
  184. this.level++;
  185. //倒计时
  186. this.countdownInterval = setInterval(() => {
  187. if (this.countdown <= 0) {
  188. clearInterval(this.countdownInterval);
  189. this.countdownInterval = null;
  190. //处理下一个关卡
  191. // console.warn('时间到,处理下一个关卡');
  192. this.startJumpGame();
  193. return;
  194. }
  195. this.setCountdown(1);
  196. }, 1000);
  197. }
  198. onJumpType(self, event) {
  199. // console.log(event);
  200. this.eliminateJumpPrefab(event);
  201. }
  202. // update (dt) {}
  203. eliminateJumpPrefab(_jumpType: JumpType) {
  204. //如果消除完,需要重新生成
  205. // if (this.index >= this.spawnArray.length) return;
  206. let _temp = this.spawnArray[this.index].getComponent('jumpPrefab');
  207. //如果当前的跳类型和预制目标一样
  208. if (_jumpType == _temp.currentJumpType) {
  209. _temp.setBTrigger();
  210. this.index++;
  211. if (this.index >= this.spawnArray.length) {
  212. clearInterval(this.countdownInterval);
  213. this.countdownInterval = null;
  214. this.startJumpGame();
  215. }
  216. //成功
  217. this.setEliminationCount(1);
  218. } else {
  219. //失误
  220. this.setFaultCount(1);
  221. }
  222. }
  223. getCurrentJumpType() {
  224. let _temp = this.spawnArray[this.index].getComponent('jumpPrefab');
  225. return _temp.currentJumpType;
  226. }
  227. eliminateJumpPrefabFormTemp(_tempState) {
  228. //如果消除完,需要重新生成
  229. // if (this.index >= this.spawnArray.length) return;
  230. let _temp = this.spawnArray[this.index].getComponent('jumpPrefab');
  231. let bSuccess = false;
  232. // console.log(JSON.stringify(_tempState));
  233. for (let i = 0; i < _tempState.length; i++) {
  234. let _state = _tempState[i];
  235. if (_state.bTrigger)
  236. console.log(JSON.stringify(_state));
  237. //如果当前的跳类型和预制目标一样
  238. if (_state.jumpType == _temp.currentJumpType && _state.bTrigger) {
  239. //成功
  240. bSuccess = true;
  241. break;
  242. }
  243. }
  244. //如果存在其中一个为true
  245. if (bSuccess) {
  246. // console.log("bSuccess:", bSuccess);
  247. _temp.setBTrigger();
  248. this.index++;
  249. if (this.index >= this.spawnArray.length) {
  250. clearInterval(this.countdownInterval);
  251. this.countdownInterval = null;
  252. this.startJumpGame();
  253. }
  254. //成功
  255. this.setEliminationCount(1);
  256. } else {
  257. //失误
  258. this.setFaultCount(1);
  259. }
  260. }
  261. spawnJumpPrefabsFromType(index: number, _jumpType: JumpType) {
  262. //todo 生成的节点,后面再处理节奏问题。比如生成顺序
  263. let _jumpPrefab = cc.instantiate(this.jumpPrefab);
  264. _jumpPrefab.setParent(this.spawnNode);
  265. let _jumpPrefabScript = _jumpPrefab.getComponent('jumpPrefab');
  266. _jumpPrefabScript.currentJumpType = _jumpType;
  267. _jumpPrefabScript.index = index;
  268. this.spawnArray.push(_jumpPrefab);
  269. }
  270. spawnJumpPrefabs(index: number) {
  271. //todo 生成的节点,后面再处理节奏问题。比如生成顺序
  272. let _jumpPrefab = cc.instantiate(this.jumpPrefab);
  273. _jumpPrefab.setParent(this.spawnNode);
  274. let _jumpPrefabScript = _jumpPrefab.getComponent('jumpPrefab');
  275. let ran = Math.floor(Math.random() * 3);
  276. //这里处理相反方向,比如生成了一个左旋转,下一个右旋转
  277. if (ran == JumpType.LEFT) {
  278. if (this.bNextSpawnRightDirection) {
  279. //如果是对应的需要记录一个对应的准确值
  280. this.bNextSpawnRightDirection = false;
  281. } else {
  282. ran = JumpType.RIGHT;
  283. this.bNextSpawnRightDirection = true;
  284. }
  285. } else if (ran == JumpType.RIGHT) {
  286. if (this.bNextSpawnRightDirection) {
  287. ran = JumpType.LEFT;
  288. this.bNextSpawnRightDirection = false;
  289. } else {
  290. this.bNextSpawnRightDirection = true;
  291. }
  292. }
  293. if (ran == JumpType.LEFT_ROTATE) {
  294. if (this.bNextSpawnRightRotateDirection) {
  295. //如果是对应的需要记录一个对应的准确值
  296. this.bNextSpawnRightRotateDirection = false;
  297. } else {
  298. ran = JumpType.RIGHT_ROTATE;
  299. this.bNextSpawnRightRotateDirection = true;
  300. }
  301. // console.log('l==rotate', ran);
  302. } else if (ran == JumpType.RIGHT_ROTATE) {
  303. if (this.bNextSpawnRightRotateDirection) {
  304. ran = JumpType.LEFT_ROTATE;
  305. this.bNextSpawnRightRotateDirection = false;
  306. } else {
  307. //如果是对应的需要记录一个对应的准确值
  308. this.bNextSpawnRightRotateDirection = true;
  309. }
  310. }
  311. _jumpPrefabScript.currentJumpType = ran;
  312. _jumpPrefabScript.index = index;
  313. this.spawnArray.push(_jumpPrefab);
  314. }
  315. //设置ui信息
  316. setEliminationCount(value: number) {
  317. this.eliminationCount += value;
  318. this.eliminationLabel.string = '消除数量:' + this.eliminationCount.toString();
  319. }
  320. //设置倒计时
  321. setCountdown(value: number) {
  322. this.countdown -= value;
  323. this.countdownLabel.string = '倒计时:' + this.countdown;
  324. }
  325. resetCountdown(value: number) {
  326. this.countdown = value;
  327. this.countdownLabel.string = '倒计时:' + this.countdown;
  328. }
  329. setFaultCount(value: number) {
  330. this.faultCount += value;
  331. this.faultLabel.string = '失误:' + this.faultCount;
  332. }
  333. }