// Learn TypeScript: // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html const { ccclass, property } = cc._decorator; import { JumpType } from '../game/enumConfig'; // var jumpTypeTemp = cc.Class({ // name:'jumpTypeTemp', // properties:{ // index:0, // item:null, // bTrigger:false // } // }) @ccclass export default class game extends cc.Component { @property(cc.Prefab) jumpPrefab: cc.Prefab = null; @property(cc.Node) spawnNode: cc.Node = null; //关卡 @property level: number = 1; @property(cc.Label) levelLabel: cc.Label = null; // LIFE-CYCLE CALLBACKS: // onLoad () {} spawnArray = []; spawnTempArray = []; index = 0; //倒计时时间 @property countdown: number = 60; @property(cc.Label) countdownLabel: cc.Label = null; @property eliminationCount: number = 0; @property(cc.Label) eliminationLabel: cc.Label = null; @property faultCount: number = 0; @property(cc.Label) faultLabel: cc.Label = null; @property(cc.Label) buttonLabel: cc.Label = null; countdownInterval = null; //下一个生成是相反的方向 bNextSpawnRightDirection = false; bNextSpawnRightRotateDirection = false; //生成预制的模板,用count 来判断生成哪一种 template = [{ count: 1, spawnList: [[0]] }, { count: 2, spawnList: [[2, 1], [1, 2], [3, 4], [4, 3]] }, { count: 3, spawnList: [[2, 0, 1], [1, 0, 2], [3, 0, 4], [4, 0, 3]] },]; isY = true; start() { } //监听跳的状态数据 listenStateDataOfJump(data, isY) { this.isY = isY; let _jumpType = this.getCurrentJumpType(); //初始全部默认状态 let _tempState = [{ jumpType: JumpType.NORMAL, bTrigger: true, describe: '正常跳' }, { jumpType: JumpType.LEFT, bTrigger: false, describe: '左直跳' }, { jumpType: JumpType.RIGHT, bTrigger: false, describe: '右直跳' }, { jumpType: JumpType.LEFT_ROTATE, bTrigger: false, describe: '左旋转跳' }, { jumpType: JumpType.RIGHT_ROTATE, bTrigger: false, describe: '右旋转跳' }]; let { currentMaxValue, oGyroValue } = data console.log('stateDataOfJump:', JSON.stringify(data)); let _rotateLimit = 100; if (currentMaxValue == 0) { if (_jumpType == JumpType.NORMAL) { _tempState[0].bTrigger = true; this.eliminateJumpPrefabFormTemp(_tempState); } else if (_jumpType == JumpType.RIGHT_ROTATE && oGyroValue > _rotateLimit) { console.log('right1:', oGyroValue); _tempState[4].bTrigger = true; this.eliminateJumpPrefabFormTemp(_tempState); } else if (_jumpType == JumpType.LEFT_ROTATE && oGyroValue < -_rotateLimit) { console.log('left1:', oGyroValue); _tempState[3].bTrigger = true; this.eliminateJumpPrefabFormTemp(_tempState); } } else { //如果是检测到旋转跳 if (oGyroValue > _rotateLimit) { // console.log('y right:', oGyroValue); _tempState[4].bTrigger = true; } else if (oGyroValue < - _rotateLimit) { // console.log('y left:', oGyroValue); _tempState[3].bTrigger = true; } else if (currentMaxValue > 5) { _tempState[2].bTrigger = true; } else if (currentMaxValue < -5) { _tempState[1].bTrigger = true; } this.eliminateJumpPrefabFormTemp(_tempState); } } /** * 重置生成数组,重置倒计时 */ resetJumpGame() { for (let i = 0; i < this.spawnArray.length; i++) { this.spawnArray[i].destroy(); } this.spawnArray = []; if (this.countdownInterval) { clearInterval(this.countdownInterval); this.countdownInterval = null; } this.resetCountdown(60); } startJumpGame() { this.resetJumpGame(); //开始游戏 this.index = 0; this.levelLabel.string = '关卡' + this.level; let _ranType = Math.floor(Math.random() * 2); if (this.level == 1) { // for (let i = 0; i < 2; i++) { // this.spawnJumpPrefabs(i); // } //随便生成一组数据 let _spawnList = this.template[1].spawnList; let ran = Math.floor(Math.random() * 2); if (_ranType >= 1) ran += 2; for (let i = 0; i < _spawnList[ran].length; i++) { this.spawnJumpPrefabsFromType(i, _spawnList[ran][i]); } this.buttonLabel.string = '下一关'; } else if (this.level == 2) { // for (let i = 0; i < 4; i++) { // this.spawnJumpPrefabs(i); // } //随便生成二组数据 let _newArray = []; let _spawnList1 = this.template[1].spawnList; let ran1 = Math.floor(Math.random() * 2); _newArray = _newArray.concat(_spawnList1[ran1]); let _spawnList2 = this.template[2].spawnList; let ran2 = Math.floor(Math.random() * 2); if (_ranType >= 1) ran2 += 2; _newArray = _newArray.concat(_spawnList2[ran2]); console.log(_newArray); for (let i = 0; i < _newArray.length; i++) { this.spawnJumpPrefabsFromType(i, _newArray[i]); } this.buttonLabel.string = '下一关'; } else if (this.level == 3) { // for (let i = 0; i < 6; i++) { // this.spawnJumpPrefabs(i); // } //随便生成三组数据 let _newArray = []; // let _spawnList1 = this.template[1].spawnList; // let ran1 = Math.floor(Math.random() * 2); // _newArray = _newArray.concat(_spawnList1[ran1]); let _spawnList2 = this.template[2].spawnList; let ran2 = Math.floor(Math.random() * 2); let ran3 = Math.floor(Math.random() * 2); if (_ranType >= 1) { ran2 += 2; ran3 += 2; } _newArray = _newArray.concat(_spawnList2[ran2]); _newArray = _newArray.concat(_spawnList2[ran3]); for (let i = 0; i < _newArray.length; i++) { this.spawnJumpPrefabsFromType(i, _newArray[i]); } this.buttonLabel.string = '最后一关'; //todo 暂时给循环 this.level = 0; } this.level++; //倒计时 this.countdownInterval = setInterval(() => { if (this.countdown <= 0) { clearInterval(this.countdownInterval); this.countdownInterval = null; //处理下一个关卡 // console.warn('时间到,处理下一个关卡'); this.startJumpGame(); return; } this.setCountdown(1); }, 1000); } onJumpType(self, event) { // console.log(event); this.eliminateJumpPrefab(event); } // update (dt) {} eliminateJumpPrefab(_jumpType: JumpType) { //如果消除完,需要重新生成 // if (this.index >= this.spawnArray.length) return; let _temp = this.spawnArray[this.index].getComponent('jumpPrefab'); //如果当前的跳类型和预制目标一样 if (_jumpType == _temp.currentJumpType) { _temp.setBTrigger(); this.index++; if (this.index >= this.spawnArray.length) { clearInterval(this.countdownInterval); this.countdownInterval = null; this.startJumpGame(); } //成功 this.setEliminationCount(1); } else { //失误 this.setFaultCount(1); } } getCurrentJumpType() { let _temp = this.spawnArray[this.index].getComponent('jumpPrefab'); return _temp.currentJumpType; } eliminateJumpPrefabFormTemp(_tempState) { //如果消除完,需要重新生成 // if (this.index >= this.spawnArray.length) return; let _temp = this.spawnArray[this.index].getComponent('jumpPrefab'); let bSuccess = false; // console.log(JSON.stringify(_tempState)); for (let i = 0; i < _tempState.length; i++) { let _state = _tempState[i]; if (_state.bTrigger) console.log(JSON.stringify(_state)); //如果当前的跳类型和预制目标一样 if (_state.jumpType == _temp.currentJumpType && _state.bTrigger) { //成功 bSuccess = true; break; } } //如果存在其中一个为true if (bSuccess) { // console.log("bSuccess:", bSuccess); _temp.setBTrigger(); this.index++; if (this.index >= this.spawnArray.length) { clearInterval(this.countdownInterval); this.countdownInterval = null; this.startJumpGame(); } //成功 this.setEliminationCount(1); } else { //失误 this.setFaultCount(1); } } spawnJumpPrefabsFromType(index: number, _jumpType: JumpType) { //todo 生成的节点,后面再处理节奏问题。比如生成顺序 let _jumpPrefab = cc.instantiate(this.jumpPrefab); _jumpPrefab.setParent(this.spawnNode); let _jumpPrefabScript = _jumpPrefab.getComponent('jumpPrefab'); _jumpPrefabScript.currentJumpType = _jumpType; _jumpPrefabScript.index = index; this.spawnArray.push(_jumpPrefab); } spawnJumpPrefabs(index: number) { //todo 生成的节点,后面再处理节奏问题。比如生成顺序 let _jumpPrefab = cc.instantiate(this.jumpPrefab); _jumpPrefab.setParent(this.spawnNode); let _jumpPrefabScript = _jumpPrefab.getComponent('jumpPrefab'); let ran = Math.floor(Math.random() * 3); //这里处理相反方向,比如生成了一个左旋转,下一个右旋转 if (ran == JumpType.LEFT) { if (this.bNextSpawnRightDirection) { //如果是对应的需要记录一个对应的准确值 this.bNextSpawnRightDirection = false; } else { ran = JumpType.RIGHT; this.bNextSpawnRightDirection = true; } } else if (ran == JumpType.RIGHT) { if (this.bNextSpawnRightDirection) { ran = JumpType.LEFT; this.bNextSpawnRightDirection = false; } else { this.bNextSpawnRightDirection = true; } } if (ran == JumpType.LEFT_ROTATE) { if (this.bNextSpawnRightRotateDirection) { //如果是对应的需要记录一个对应的准确值 this.bNextSpawnRightRotateDirection = false; } else { ran = JumpType.RIGHT_ROTATE; this.bNextSpawnRightRotateDirection = true; } // console.log('l==rotate', ran); } else if (ran == JumpType.RIGHT_ROTATE) { if (this.bNextSpawnRightRotateDirection) { ran = JumpType.LEFT_ROTATE; this.bNextSpawnRightRotateDirection = false; } else { //如果是对应的需要记录一个对应的准确值 this.bNextSpawnRightRotateDirection = true; } } _jumpPrefabScript.currentJumpType = ran; _jumpPrefabScript.index = index; this.spawnArray.push(_jumpPrefab); } //设置ui信息 setEliminationCount(value: number) { this.eliminationCount += value; this.eliminationLabel.string = '消除数量:' + this.eliminationCount.toString(); } //设置倒计时 setCountdown(value: number) { this.countdown -= value; this.countdownLabel.string = '倒计时:' + this.countdown; } resetCountdown(value: number) { this.countdown = value; this.countdownLabel.string = '倒计时:' + this.countdown; } setFaultCount(value: number) { this.faultCount += value; this.faultLabel.string = '失误:' + this.faultCount; } }