// 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 GameData from './GameData'; import { Notification } from './Notification'; import HttpUtil from './crypto/serverAPI'; @ccclass export default class GameManager extends cc.Component { @property(cc.Node) ball: cc.Node = null; @property(cc.Prefab) qiu1: cc.Prefab = null; @property(cc.Prefab) qiu2: cc.Prefab = null; @property(cc.Prefab) qiu3: cc.Prefab = null; @property(cc.Prefab) linePrefab: cc.Prefab = null; @property(cc.Node) hit: cc.Node = null; bg1: cc.Node = null; bg2: cc.Node = null; time: cc.Label = null; // LIFE-CYCLE CALLBACKS: begin: boolean = false; fuhuo: boolean = false; over: boolean = false; show: boolean = false; countTime:number; timeCount:number; fitTop:number; fitBot:number; level:number; clickNow:number; startTime:number; overTime:number; winTime:number; clickCount:number; timeStart:number; route_Array: any[]; now_Array: any[]; ball_Array: any[]; Line_Array: any[]; Pos_Array: any[]; time_Array: any[]; color_Array: any[]; @property(cc.Label) levelLabel: cc.Label = null; static _instance: any; moveArray: number[]; repeat: number; numberRepeat: number; round: number; pause: boolean; onLoad () { } start () { this.fit(); this.init(); } fit(){ this.fitTop = 450; //600 this.fitBot = -600; //-700 var jg = this.setFit(); if(!jg){ this.fitTop = 600; this.fitBot = -700; } } getSetScreenResolutionFlag () { let size = cc.winSize; let width = size.width; let height = size.height; if ((height / width) > (16.2 / 9)) return false; return true; } //判断全面屏适配 setFit () { let flag = this.getSetScreenResolutionFlag(); if (flag) { } else { } return flag; } //初始化数据 init(){ this.node.getChildByName("GameOver").zIndex = 100; this.node.getChildByName("GameOver").active = false; this.ball.zIndex = 100; this.bg1 = this.node.getChildByName("bg1"); this.bg2 = this.node.getChildByName("bg2"); this.time = this.node.getChildByName("Top").getChildByName("time").getComponent(cc.Label); this.node.getChildByName("showBtn").active = false; this.begin = false; this.over = false; this.fuhuo = true; this.pause = true; this.show = false; this.round = 0; this.level = 2; this.countTime = 60; if(GameData._instance.GM_INFO.custom != 0){ this.level = GameData._instance.GM_INFO.custom; // this.levelLabel.string = this.level + ""; GameData._instance.GM_INFO.custom += 1; this.countTime = 12000000; } // this.levelLabel.string = this.level + ""; this.timeCount = 0.1; this.clickNow = 0; this.clickCount = 0; this.timeStart = 0; this.repeat = -1; this.numberRepeat = -1; this.route_Array = []; this.moveArray = []; this.now_Array = []; this.ball_Array = []; this.Pos_Array = []; this.time_Array = []; this.color_Array = []; GameData._instance.GAME_DATA = []; GameData._instance.GM_INFO.success = false; this.startTime = 0; this.overTime = 0; this.winTime = 0; GameData._instance.GM_INFO.total = 0; GameData._instance.GM_INFO.mean_Time = 0; this.schedule(this.updateCountDownTime,1); this.startGame(); } backScene(){ cc.director.loadScene("LoadScene"); } //下一关,或者重新开始,或者返回上一关,根据level决定 reStart(type){ // if(GameData._instance.GM_INFO.custom != 0){ // cc.director.loadScene("LoadScene"); // return; // } this.node.getChildByName("showBtn").active = false; this.destroyLine(); this.timeCount = 0.1; this.clickNow = 0; this.timeStart = 0; this.repeat = -1; this.numberRepeat = -1; this.begin = false; this.route_Array = []; this.now_Array = []; this.ball_Array = []; this.Pos_Array = []; this.moveArray = []; this.time_Array = []; this.color_Array = []; if(type == 0){ this.bgMove(1); } else if(type == 1){ this.startGame(); } else if(type == 2){ this.bgMove(-1); } } begin_Guide(){ } //获取时间戳 getTime(){ const timestamp = new Date().getTime(); return timestamp; } //获胜 passLevel(){ this.destroyLine(); if(this.over == false && this.countTime >= 0){ let timeArray = []; let timeCount = 0; let ball = this.ball_Array[0]; for(let i=1; i< this.ball_Array.length; i++){ let time = this.calculateDistance2(this.ball_Array[i].x,this.ball_Array[i].y, this.ball_Array[i-1].x,this.ball_Array[i-1].y); timeArray.push(time); } for(let k=1; k< this.ball_Array.length; k++){ setTimeout(() => { cc.tween(ball) .to(timeArray[k-1],{x:this.ball_Array[k].x,y:this.ball_Array[k].y}) .call(() =>{ this.ball_Array[k].active = false; if(k == this.ball_Array.length-1){ setTimeout(() => { this.ball.opacity = 255; this.ball.position = cc.v3(ball.x,ball.y,0); ball.active = false; this.clearSun(); this.reStart(0); }, 200); } }) .start(); }, timeCount*1000); timeCount += (timeArray[k-1]+0.1); } } } //失败 loseLevel(type){ this.destroyLine(); if(this.over == false && this.countTime >= 0){ let ball = this.ball_Array[0]; for(let k=1; k< this.ball_Array.length; k++){ cc.tween(this.ball_Array[k]) .to(2,{x:ball.x,y:ball.y}) .call(() =>{ this.ball_Array[k].active = false; if(k == this.ball_Array.length-1){ this.ball.opacity = 255; this.ball.position = cc.v3(ball.x,ball.y,0); this.clearSun(); this.reStart(type); } }) .start() } } } clearSun(){ for(let k=0; k< this.ball_Array.length; k++){ this.ball_Array[k].removeFromParent(); this.ball_Array[k] = null; } } //点击太阳执行方法 clickSun(data){ if(this.begin == true){ this.clickCount += 1; let clickTime = this.getTime(); this.time_Array.push((clickTime - this.timeStart)) this.timeStart = this.getTime(); if(data == this.clickNow){ GameData._instance.AudioManager.playAudioButton(); if(this.clickNow > 0){ this.createLine(cc.v2(this.ball_Array[this.clickNow-1].x,this.ball_Array[this.clickNow-1].y), cc.v2(this.ball_Array[this.clickNow].x,this.ball_Array[this.clickNow].y), 0.2); } this.clickNow += 1; // GameData._instance.GM_INFO.total += 1; if(this.ball_Array[this.clickNow-1]) this.ball_Array[this.clickNow-1].getChildByName("guang").active = false; // setTimeout(() => { // }, 200); if(this.clickNow == this.now_Array.length){ this.pause = true; GameData._instance.GM_INFO.total += this.clickNow; this.overTime = this.getTime(); if(this.startTime > 0 && this.overTime > 0){ GameData._instance.GM_INFO.mean_Time += (this.overTime - this.startTime); } // console.log("成功",GameData._instance.GM_INFO.mean_Time,this.overTime,this.startTime); // console.log(GameData._instance.GM_INFO.mean_Time); let finish_time = parseInt((this.overTime - this.startTime)/100+""); this.startTime = 0; this.overTime = 0; // console.log("33333333"); let color = "y"; if(this.level < 4 && this.level > 1)color = "yr"; else if(this.level >= 4) color = "yrb"; let win = this.node.getChildByName("Win"); win.active = true; win.setScale(0); GameData._instance.AudioManager.playWin(); this.round += 1; let sunArray = []; if(this.moveArray.length > 0){ for(let m=0 ; m{ win.active = false; this.destroyLine(); if(this.over == false && this.countTime >= 0){ if(this.level >= 15){ GameData._instance.GM_INFO.success = true; this.level += 0; // this.levelLabel.string = this.level + ""; this.fuhuo = true; this.passLevel(); } else{ this.level += 1; // this.levelLabel.string = this.level + ""; this.fuhuo = true; this.passLevel(); } } }) .start(); } } else{ this.setLoss(); } // this.hitAction(data) } } setLoss(){ GameData._instance.AudioManager.playWarning(); this.begin = false; this.overTime = this.getTime(); if(this.startTime > 0 && this.overTime > 0){ GameData._instance.GM_INFO.mean_Time += (this.overTime - this.startTime); } let finish_time = parseInt((this.overTime - this.startTime)/100+""); this.startTime = 0; this.overTime = 0; let color = "y"; if(this.level < 4 && this.level > 1)color = "yr"; else if(this.level >= 4) color = "yrb"; this.round += 1; let sunArray = []; if(this.moveArray.length > 0){ for(let m=0 ; m 2){ this.level -= 1; // this.levelLabel.string = this.level + ""; this.fuhuo = true; // this.fuhuo = false; this.loseLevel(2); } else{ this.loseLevel(1); } } let over = this.node.getChildByName("Over"); cc.tween(over) .to(0.25,{opacity:255}) .delay(0.1) .to(0.2,{opacity:0}) .delay(0.1) .to(0.15,{opacity:255}) .delay(0.1) .to(0.1,{opacity:0}) .start(); } hitAction(data){ this.hit.active = true; this.hit.opacity = 0; this.hit.setPosition this.hit.setPosition(this.ball_Array[data].x+35,this.ball_Array[data].y+35); cc.tween(this.hit) .to(0,{opacity:255}) .to(0.2,{position:cc.v3(this.hit.x-35,this.hit.y-35,0)}) .delay(0.2) .to(0.1,{opacity:0}) .call(() =>{ this.hit.active = false; }) .start(); } //开始游戏 startGame(){ if(this.over == false && this.countTime > 0){ this.pause = true; this.createSun(); this.begin = false; cc.tween(this.ball) .to(0.5,{opacity:255,scale:1.3}) .start(); // this.runGame() } } runGame(){ let time = -650; for(let i=0; i { if(this.over == false){ let temp = this.now_Array[i].timeCount; if(temp > 0.5) temp = 0.5; if(i != 0){ if(this.ball && this.now_Array[i]){ this.createLine(cc.v2(this.ball.x,this.ball.y),cc.v2(this.now_Array[i].x,this.now_Array[i].y), this.now_Array[i].time-temp); } } cc.tween(this.ball) .to(this.now_Array[i].time-temp,{x:this.now_Array[i].x,y:this.now_Array[i].y}) .to(0.1,{scale:1}) .call(()=>{ this.createBall(i,this.now_Array[i].color,this.now_Array[i].speed,this.now_Array[i].repeat); if(i == this.now_Array.length -1){ this.ball.opacity = 0; setTimeout(() => { this.destroyLine(); this.beginGame(); if(GameData._instance.LEVEL_INFO[this.level-1].moveNumber<1){ this.pause = false; } else{ this.moveAction(); } }, 150); } }) .start() } }, time); } } createMove(){ if(this.over == false){ let info = GameData._instance.LEVEL_INFO[this.level-1]; //改变移动速度 var speed = 7; if(info.moveSpeed >= 2){ speed = 7 - info.moveSpeed/2; } if(info.repeat == 1){ this.repeat = parseInt((Math.random()*(info.number-1)+1) + ""); let tempNumber = 0; // this.repeat = 4; if(this.repeat > 4){ this.numberRepeat = parseInt(Math.random()*4 + ""); if(this.repeat == (info.number-1)) //如果是最后一个 不能跟第一个重叠 this.numberRepeat = this.repeat - parseInt( (Math.random()*3 + 2)+ "") tempNumber = this.Pos_Array[this.numberRepeat]; this.Pos_Array[this.repeat ] = tempNumber; } else{ // 0 1 2 3 4 5 // 6 - 3 - 1 = 2 4 01 4 if(this.repeat == 0) this.repeat = 1; if((info.number-1-this.repeat ) == 2){ this.numberRepeat = info.number - 1; tempNumber = this.Pos_Array[this.numberRepeat]; } else if((info.number-1-this.repeat ) == 1){ this.numberRepeat = this.repeat - 2; tempNumber = this.Pos_Array[this.numberRepeat]; } else if((info.number-1-this.repeat ) == 0){ this.numberRepeat = this.repeat - 2; tempNumber = this.Pos_Array[this.numberRepeat]; } else if((info.number-1-this.repeat ) > 2){ this.numberRepeat = parseInt(Math.random()*(info.number-this.repeat -2) + "") + this.repeat + 2 ; tempNumber = this.Pos_Array[this.numberRepeat]; } else{ this.numberRepeat = this.repeat - 2; tempNumber = this.Pos_Array[this.numberRepeat]; } this.Pos_Array[this.repeat] = tempNumber; } } if(info.moveNumber > 0){ //0 1 2 3 4 5 6 7 8 9 //可移动区分 let pos = cc.v2(0,0); for(let z=0; z Math.random() - 0.5); if(this.moveArray[0] == 1){ for(let r=info.number-1; r>=0; r--){ if(this.moveArray[r] == 0){ this.moveArray[r] = 1; this.moveArray[0] = 0; r = -1; } } } // debugger; //设置重叠 // this.repeat = -1; // this.numberRepeat = -1; //将重叠的设置为不可移动 1 1 1 1 0 0 1 0 0 0 / 2 7 for(let k =0; k= 0; m--){ if(m != k && this.moveArray[m] == 0 && m!= this.repeat && m!= this.numberRepeat){ this.moveArray[m] = 1; this.moveArray[k] = 0; m = -1; } } } } for(let i =0; i { pos = this.getPos2(true,i); this.Pos_Array[i] = pos; }, timeOut*300); } } } } } //移动后数据 重新刷一遍赋值 updateArray(){ for(let i=0; i= 2){ speed = 7 - info.moveSpeed/2; } if(info.moveNumber > 0){ //0 1 2 3 4 5 6 7 8 9 //可移动区分 let pos = cc.v2(0,0); // this.repeat = -1; // this.numberRepeat = -1; //将重叠的设置为不可移动 1 1 1 1 0 0 1 0 0 0 / 2 7 for(let k =0; k= 0; m--){ if(m != k && this.moveArray[m] == 0 && m!= this.repeat && m!= this.numberRepeat){ this.moveArray[m] = 1; this.moveArray[k] = 0; m = -1; } } } } let lastNumber = 0; //锁定最后一个可移动的 for(let j =0; j{ this.ball_Array[i].getComponent("Sun").move = false; }) .start(); } //如果是最后一个 else{ let time = speed*this.calculateDistance3(this.ball_Array[i].x,this.ball_Array[i].y,this.Pos_Array[i].x,this.Pos_Array[i].y); this.ball_Array[i].getComponent("Sun").move = true; cc.tween(this.ball_Array[i]) .to(time,{position:this.Pos_Array[i]}) .call(() =>{ this.ball_Array[i].getComponent("Sun").move = false; }) .start(); } } } } } beginGame(){ this.begin = true; this.startTime = this.getTime(); this.timeStart = this.getTime(); this.pause = false; // this.node.getChildByName("showBtn").active = true; } getRandomArray(lastNumber){ let num = parseInt(Math.random()*8 - 4 + ""); let jg = num + lastNumber; if(num > 1 || num <-1){ if( num == 0 || ( jg<0 || jg >= this.Pos_Array.length)){ jg = this.getRandomArray(lastNumber); } } else{ jg = this.getRandomArray(lastNumber); } return jg; } //创建太阳所需数据 createSun(){ let levelData = GameData._instance.LEVEL_INFO[this.level-1]; let tiemOut = 0; for(let l=0; l { if(this.over == false){ let pos = this.getPos(false,l); this.Pos_Array.push(pos); if(this.Pos_Array.length == levelData.number){ this.createMove(); this.createPos(); } } }, tiemOut*100); } // 0 0 0 0 0 0 0 0 0 0 0 } createPos(){ let data = {x:this.ball.x,y:this.ball.y,time:0,timeCount:0,color:0,speed:0,repeat:false}; let levelData = GameData._instance.LEVEL_INFO[this.level-1]; //颜色区分 let colorArray = [0,1]; for(let j=0; j Math.random() - 0.5); } colorArray.sort(() => Math.random() - 0.5); if(levelData.number == 4) colorArray = [0,0,0,0]; this.color_Array = colorArray; for(let i=0; i 0){ for(let i=0; i 0){ for(let i=0; i 160){ if(num == true){ if(distance < GameData._instance.LEVEL_INFO[this.level-1].maxMove){ jg = true; } else jg = false; } else{ if(distance < GameData._instance.LEVEL_INFO[this.level-1].maxMove){ jg = true; } else jg = false; } } return jg; } getDistance2(x1, y1, x2, y2,num) { var jg = false; const dx = x2 - x1; const dy = y2 - y1; var distance = Math.sqrt(dx*dx + dy*dy); if(distance > 160){ if(num == true){ if(distance < GameData._instance.LEVEL_INFO[this.level-1].maxDistance) jg = true; else jg = false; } else{ jg = true; } } return jg; } createBall(number,color,speed,repeat){ let ball = null; if(color == 0)ball = cc.instantiate(this.qiu1); else if(color == 1)ball = cc.instantiate(this.qiu2); else if(color == 2)ball = cc.instantiate(this.qiu3); ball.parent = this.node.getChildByName("Sun"); ball.zIndex = this.now_Array.length - number; ball.setPosition(this.ball.x,this.ball.y); ball.getComponent("Sun").init(number,color,speed,repeat); this.ball_Array.push(ball); } destroyLine() { for(let i =0; i< this.node.getChildByName("Line").children.length;i++){ if(this.node.getChildByName("Line").children[i]){ this.node.getChildByName("Line").children[i].removeFromParent(); this.node.getChildByName("Line").children[i] = null; } } this.node.getChildByName("Line").removeAllChildren(); } createLine(start,end,time){ let line = cc.instantiate(this.linePrefab); line.parent = this.node.getChildByName("Line"); line.setPosition(start.x,start.y); const dx = end.x - start.x; const dy = end.y - start.y; var distance = Math.sqrt(dx*dx + dy*dy); line.height = distance; line.getComponent(cc.Sprite).fillRange = 0; line.angle = -this.calculateAngle(start.x,start.y,end.x,end.y); cc.tween(line.getComponent(cc.Sprite)) .to(time,{fillRange:1}) .start(); } calculateAngle(x1, y1, x2, y2) { var angle = Math.atan2(y2-y1,x2-x1)*180 / Math.PI - 90; return -angle; } calculateDistance(x1, y1, x2, y2) { const dx = x2 - x1; const dy = y2 - y1; var distance = Math.sqrt(dx*dx + dy*dy); if(distance > 800) distance = 800; const time = distance/800 * 0.6; return time; } calculateDistance2(x1, y1, x2, y2) { const dx = x2 - x1; const dy = y2 - y1; var distance = Math.sqrt(dx*dx + dy*dy); if(distance > 1200) distance = 1200; const time = distance/1200 * 1; return time; } calculateDistance3(x1, y1, x2, y2) { const dx = x2 - x1; const dy = y2 - y1; var distance = Math.sqrt(dx*dx + dy*dy); if(distance > 1200) distance = 1200; const time = distance/1200 * 1.5; return time; } //背景移动 bgMove(type){ let random = Math.random()*300 + 300; let posX = Math.random()*600 - 300; let y1 = this.bg1.y - random*type; // let y2 = this.bg2.y - random*type; let time1 = 0.7*(random/400); cc.tween(this.ball) .to(0.3,{scale:1.3}) .to(time1,{x:posX}) .start(); cc.tween(this.bg1) .delay(0.3) .to(time1,{y:y1}) .start(); // cc.tween(this.bg2) // .delay(0.3) // .to(time1,{y:y2}) // .start(); setTimeout(() => { // if(this.over == false && GameData._instance.GM_INFO.custom == 0){ // this.startGame(); // } if(this.over == false){ this.startGame(); } }, time1*1500); } //时间格式转换 getTimeMargin(second:number) { let total = 0; total = second; let hour = 0; hour = parseInt((total / 3600) + "");//计算整数小时数 let afterHour = total - hour * 60 * 60;//取得算出小时数后剩余的秒数 let min = parseInt((afterHour / 60)+"");//计算整数分 let m = "" + min; if(min < 10) m = "0"+min; let afterMin = total - hour * 60 * 60 - min * 60;//取得算出分后剩余的秒数 let miao = afterMin + ""; if(afterMin < 10) miao = "0" + afterMin; return m + ':' + miao } //如果是倒计时 调用此方法 updateCountDownTime () { if (this.countTime > 0 && this.pause == false) { this.countTime -= 1; this.time.string = this.getTimeMargin(this.countTime); if(this.countTime < 5){ cc.tween(this.time.node) .to(0.25,{scale:1.5,color:cc.color(255,0,0)}) .to(0.25,{scale:1,color:cc.color(255,255,255)}) .start() let over = this.node.getChildByName("Over"); cc.tween(over) .to(0.2,{opacity:255}) .delay(0.1) .to(0.2,{opacity:0}) .start(); } if(this.countTime <= 0){ this.begin = false; this.over = true; this.unschedule(this.updateCountDownTime); this.setLoss(); var time = 0; // console.log(GameData._instance.GM_INFO.mean_Time,this.clickCount); if(this.clickCount > 0) time = parseInt(GameData._instance.GM_INFO.mean_Time/this.clickCount + ""); GameData._instance.GM_INFO.mean_Time = parseInt(time/100 + ""); GameData._instance.GM_INFO.mean_Time = GameData._instance.GM_INFO.mean_Time /10; // console.log( GameData._instance.GM_INFO.mean_Time,time); // debugger; if(time > 10000) time = (Math.random()*10+5)*100; this.setRank(time); } } } setData(){ let data = GameData._instance.GAME_DATA; let matchId = this.getMatchId(); console.log(matchId); let postData = { "matchId":matchId, "data": data }; console.log("上传数据:") HttpUtil.uploadUserLogData(postData,function(){}) } getMatchId (){ let matchId = cc.sys.localStorage.getItem("matchId"); if(matchId == "undifend" || matchId==null){ matchId = this.setMatchId(); } else{ if(this.containsNanana(matchId) == true){ matchId = this.setMatchId(); } else{ let char = parseInt(matchId[10]); if(this.round == 1){ char += 1; } matchId = matchId.slice(0, 10) + char + ""; GameData._instance.GM_INFO.matchId = matchId; cc.sys.localStorage.setItem("matchId",matchId); } } return matchId; } containsNanana(str) { return /na/i.test(str); } setMatchId (){ // 定义包含可用字符的字符集 const characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; // 创建一个数组以保存随机字符 const uuidArray = []; // 循环10次 生成10位的UUID for (let i = 0; i < 10; i++) { // 生成随机索引,范围是字符集的长度 const randomIndex = Math.floor(Math.random() * characters.length); // 从字符集中获取随机字符 const randomChar = characters.charAt(randomIndex); // 将字符添加到数组中 uuidArray.push(randomChar); } let data = uuidArray.join('') + 1 + ""; cc.sys.localStorage.setItem("matchNumber",1); cc.sys.localStorage.setItem("matchId",data); GameData._instance.GM_INFO.matchId = data; return data; } setRank(time){ let postData = { "type":1, "reactionTime": time, "totalSunCount": GameData._instance.GM_INFO.total, "success": GameData._instance.GM_INFO.success }; // console.log(postData); HttpUtil.rankData(1,function(){},postData); this.node.getChildByName("GameOver").active = true; this.node.getChildByName("GameOver").opacity = 0; cc.tween(this.node.getChildByName("GameOver")) .to(0.4,{opacity:255}) .delay(2) .to(0.4,{opacity:50}) .call(() =>{ cc.director.loadScene("OverScene"); }) .start() } outPut(){ let data = GameData._instance.CLICK_DATA; GameData._instance.GAME_DATA = []; GameData._instance.GAME_DATA.push(GameData._instance.CLICK_DATA); GameData._instance.CLICK_init(); } showNumber(){ if(this.show == false) this.show = true; else if(this.show == true) this.show = false; for(let i=0; i