// 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 import GameData from './GameData'; import { GameTool } from './tool/GameTool'; import { Notification } from './tool/Notification'; const {ccclass, property} = cc._decorator; @ccclass export default class GameManager extends cc.Component { @property(cc.Camera) Camera: cc.Camera = null; @property(cc.Node) topUI: cc.Node = null; @property(cc.Node) Player: cc.Node = null; @property(cc.Node) Ground: cc.Node = null; @property(cc.Node) Xin: cc.Node = null; @property(cc.Label) time: cc.Label = null; @property(cc.Node) GameOver: cc.Node = null; @property(cc.Node) Tip: cc.Node = null; @property([cc.Prefab]) blockPrefab : Array = []; round: number; //回合 level: number; //回合 static _instance: any; blockArray: any; cameraMove:boolean countHeight:number; countTime: number; over: boolean; begin: boolean; interfere: boolean; oldSpeed: number; //上一回合速度 score: number; //总得分 difficultyMax:number; //最高难度系数 difficultyArray:number; drop: number; //摄像机落差 guide: number; //记录引导步骤 Air: any; Air2: any; block: cc.Node; // LIFE-CYCLE CALLBACKS: onLoad () { cc.game.setFrameRate(61); // 示例使用 var manager = cc.director.getCollisionManager(); manager.enabled = true; // manager.enabledDebugDraw = true; GameManager._instance = this; // GameData._instance.GM_INFO.probation = false; this.Air = []; this.Air2 = []; } protected onEnable(): void { Notification.on("createBlock",this.createBlock,this); Notification.on("addScore",this.addScore,this); Notification.on("createCrackBlock",this.createCrackBlock,this); Notification.on("showGround",this.showGround,this); Notification.on("death",this.getDeath,this); Notification.on("jump",this.jump,this); Notification.on("setData",this.setData,this); } protected onDestroy(): void { Notification.off("createBlock",this.createBlock); Notification.off("addScore",this.addScore); Notification.off("createCrackBlock",this.createCrackBlock); Notification.off("death",this.getDeath); Notification.off("jump",this.jump); Notification.off("showGround",this.showGround); Notification.off("setData",this.setData); } //根据是否全面屏,做独立适配方面 fit(){ this.node.getChildByName("bg2").y = -2167.675; var jg = this.setFit(); if(!jg){ this.node.getChildByName("bg2").y = -2100; this.topUI.y = 660; } } //判断全面屏 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.drop = this.topUI.y - this.Camera.node.y; this.score = 0; this.oldSpeed = 10; this.guide = -1; this.begin = true; this.over = false; this.interfere = false; this.round = 0; this.level = 0; GameData._instance.GM_INFO.levelMax = 1; GameData._instance.GM_INFO.difficultyMax = 1; GameData._instance.GM_INFO.life = 3; this.blockArray = []; this.difficultyArray = 0; this.cameraMove = false; this.countTime = 180; this.topUI.getChildByName("xin1").active = true; this.topUI.getChildByName("xin2").active = true; this.topUI.getChildByName("xin3").active = true; this.topUI.getChildByName("star1").active = false; this.topUI.getChildByName("star2").active = false; this.topUI.getChildByName("star3").active = false; GameData._instance.LEVEL_init(); this.schedule(this.updateCountDownTime,1); } start () { // this.node.on(cc.Node.EventType.TOUCH_START, this.jump, this); this.fit(); this.init(); // GameData._instance.GM_INFO.probation = false; if(GameData._instance.GM_INFO.probation){ this.Player.getComponent("Player").jumpPause = true; let tip = this.Tip.getChildByName("tip"); this.tipShow(tip,-1,true); } else{ this.node.getChildByName("Begin").opacity = 0; this.node.getChildByName("Begin").scale = 2; if(this.node.getChildByName("Begin")){ cc.tween(this.node.getChildByName("Begin")) .to(0.5,{opacity:255,scale:1}) .delay(0.5) .to(0.5,{opacity:0,scale:0}) .call(() =>{ this.startGame(); }) .start(); } else{ setTimeout(() => { this.startGame(); }, 1000); } } } //开启引导 startGuide(type,revive){ //type为第几步 this.guide = type; console.log("startGuide",type); if(type == 0){ this.careteGuideBlock(1); } else if(type == 2){ let block = this.node.getChildByName("Block").children[this.node.getChildByName("Block").children.length-1]; cc.tween(block) .to(0.7,{x:0}) .start(); this.tipHide("tip2",3,false); } else if(type == 3){ if(revive == true){ this.round += 1; this.careteGuideBlock(3); } else{ let tip = this.Tip.getChildByName("tip3"); this.tipShow(tip,type,false); setTimeout(() => { this.tipHide("tip3",null,false); this.round += 1; this.careteGuideBlock(3); }, 3000); } } else if(type == 4){ this.round += 1; this.careteGuideBlock(4); } else if(type == 5){ this.round += 1; this.careteGuideBlock(5); } else if(type == 6){ let block = this.node.getChildByName("Block").children[this.node.getChildByName("Block").children.length-1]; cc.tween(block) .to(0.7,{x:0}) .start(); this.tipHide("tip4",7,revive); } else if(type == 7){ if(revive == true){ this.round += 1; this.careteGuideBlock(7); } else{ let tip = this.Tip.getChildByName("tip5"); this.tipShow(tip,type,false); setTimeout(() => { this.tipHide("tip5",null,revive); this.round += 1; this.careteGuideBlock(7); }, 2000); } } else if(type == 8){ this.round += 1; this.careteGuideBlock(8); } else if(type == 9){ this.Player.getComponent("Player").jumpPause = true; let tip = this.Tip.getChildByName("tip6"); setTimeout(() => { this.tipShow(tip,type,false); }, 1000); } } tipShow(tipName,type,showNext){ let tip = tipName; let left = this.Tip.getChildByName("left"); let right = this.Tip.getChildByName("right"); left.width = 0; right.width = 0;tip.opacity = 0; cc.tween(left) .to(0.25,{width:348}) .start(); cc.tween(right) .to(0.25,{width:348}) .start(); cc.tween(tip) .delay(0.15) .to(0.15,{opacity:255}) .call(() =>{ this.guide = type; if(type == 5) this.guide = 6; if(type == 5 || type == 1 ) this.Player.getComponent("Player").jumpPause = false; }) .start(); if(showNext == true){ this.Tip.getChildByName("btn_next").active = true; this.Tip.getChildByName("btn_jump").active = true; cc.tween(this.Tip.getChildByName("btn_next")) .delay(0.5) .to(0.2,{opacity:255}) .start(); cc.tween(this.Tip.getChildByName("btn_jump")) .delay(0.5) .to(0.2,{opacity:255}) .start(); } if(tip.name == "tip6"){ this.Tip.getChildByName("btn_start").active = true; cc.tween(this.Tip.getChildByName("btn_start")) .delay(1) .to(0.2,{opacity:255}) .start(); } } //隐藏 tipHide(tipName,num,revive){ let tip = this.Tip.getChildByName(tipName); let left = this.Tip.getChildByName("left"); let right = this.Tip.getChildByName("right"); cc.tween(left) .delay(0.1) .to(0.25,{width:0}) .start(); cc.tween(right) .delay(0.1) .to(0.25,{width:0}) .start(); cc.tween(tip) .to(0.12,{opacity:0}) .delay(1.5) .call(() =>{ if(tip.name != "tip3" && tipName != "tip5"){ this.Player.getComponent("Player").jumpPause = true; } if(num != null){ this.startGuide(num,revive); } }) .start(); } careteGuideBlock(type){ this.Ground.active = false; let tip = this.Tip.getChildByName("tip2"); if(type == 5){ tip = this.Tip.getChildByName("tip4"); } this.Player.getComponent("Player").jumpPause = true; var block = cc.instantiate(this.blockPrefab[13]); let height = this.Player.getComponent("Player").basicHeight + GameData._instance.GM_INFO.blockMin; if(type > 4){ height += block.height + GameData._instance.GM_INFO.blockMax; GameData._instance.CLICK_DATA.height = true; } block.setPosition(cc.v2(this.round%2==0?480:-480,height)); block.parent = this.node.getChildByName("Block"); let num = 150; let time =3; if(type == 1 || type == 5){ num = this.round%2==0?num:-num; } else if(type == 3 || type == 4 || type ==7 || type == 8 || type == 9){ this.Player.getComponent("Player").jumpPause = false; num = -block.x; time = 7; } cc.tween(block) .to(time,{x:num}) .call(() =>{ if(type == 1 || type == 5){ if(type == 1) this.Player.getComponent("Player").jumpPause = true; this.tipShow(tip,type,false); } }) .start(); } showGround(tempY){ setTimeout(() => { this.Ground.active = true; this.Ground.y = tempY; }, 500); } guideStart(){ this.tipHide("tip6",null,false); this.Tip.getChildByName("btn_next").active = false; this.Tip.getChildByName("btn_jump").active = false; this.Tip.getChildByName("btn_start").active = false; GameData._instance.GM_INFO.probation = false; this.destroyBlock(false); this.onDestroy(); cc.director.loadScene("GameScene"); } //下一步 guideNext(){ if(this.guide == -1){ this.tipHide("tip",0,false); this.Tip.getChildByName("btn_next").active = false; this.Tip.getChildByName("btn_jump").active = false; this.Tip.getChildByName("btn_start").active = false; } } //跳过 guideJump(){ this.Tip.getChildByName("btn_next").active = false; this.Tip.getChildByName("btn_jump").active = false; this.Tip.getChildByName("btn_start").active = false; GameData._instance.GM_INFO.probation = false; this.destroyBlock(false); this.onDestroy(); cc.director.loadScene("GameScene"); } jump(){ this.Player.getComponent("Player").jump(); } startGame(){ this.createBlock(); // this.createAir(); } addScore(score){ GameData._instance.CLICK_DATA.getScore = parseInt(score); GameManager._instance.score += parseInt(score); GameData._instance.CLICK_DATA.totalScore = GameManager._instance.score; this.topUI.getChildByName("nandu").getComponent(cc.Label).string = GameManager._instance.score + ""; var scale = GameManager._instance.score/GameData._instance.LEVEL_INFO.scoreMax; cc.tween(this.topUI.getChildByName("progress").getComponent(cc.Sprite)) .to(0.5,{fillRange:scale}) .call(() =>{ if(scale >= GameData._instance.LEVEL_INFO.star1 && !this.topUI.getChildByName("star1").active){ this.topUI.getChildByName("star1").active = true; } if(scale >= GameData._instance.LEVEL_INFO.star2 && !this.topUI.getChildByName("star2").active){ this.topUI.getChildByName("star2").active = true; } if(scale >= GameData._instance.LEVEL_INFO.star3 && !this.topUI.getChildByName("star3").active){ this.topUI.getChildByName("star3").active = true; // GameData._instance.scoreMax = 150000; } }) .start(); } createBlock(){ if(this.over == false && this.begin == true){ this.Ground.active = false; this.Player.getComponent("Player").jumpPause = false; var num = Math.floor(Math.random()*12); if(this.round <= 10 && num == 5){ num = Math.floor(Math.random()*6 + 6); } var block = cc.instantiate(this.blockPrefab[num]); let height = this.Player.getComponent("Player").basicHeight + GameData._instance.GM_INFO.blockMin; if(num > GameData._instance.GM_INFO.blockScale){ height += block.height + GameData._instance.GM_INFO.blockMax; GameData._instance.CLICK_DATA.height = true; } if(height >= 0 && this.cameraMove == false){ this.countHeight = this.Player.getComponent("Player").basicHeight - this.Camera.node.y; this.cameraMove = true; } block.setPosition(cc.v2(this.round%2==0?480:-480,height)); block.parent = this.node.getChildByName("Block"); block.zIndex = 10; this.block = null; this.block = block; this.round += 1; this.level += 1; if(this.round > GameData._instance.GM_INFO.levelMax) GameData._instance.GM_INFO.levelMax = this.round; GameData._instance.CLICK_DATA.level = this.round; GameData._instance.CLICK_DATA.round = this.level; GameData._instance.LEVEL_INFO.layer += GameData._instance.LEVEL_INFO.layerAdd; if(GameData._instance.LEVEL_INFO.speed<2)GameData._instance.LEVEL_INFO.speed += GameData._instance.LEVEL_INFO.addSpeed; if(GameData._instance.LEVEL_INFO.reduceSpeed <= 100) GameData._instance.LEVEL_INFO.reduceSpeed += GameData._instance.LEVEL_INFO.jiansu; if(this.round == GameData._instance.LEVEL_INFO.round1) GameData._instance.LEVEL_INFO.jiansu = 2; else if(this.round == GameData._instance.LEVEL_INFO.round2) GameData._instance.LEVEL_INFO.jiansu = 1; else if(this.round == GameData._instance.LEVEL_INFO.round3) GameData._instance.LEVEL_INFO.jiansu = 0.5; } } //产生空气 createAir(){ for(let i=0; i<100;i++){ this.Ground.active = false; var num = Math.floor(Math.random()*12); if(this.round <= 10 && num == 5){ num = Math.floor(Math.random()*6 + 6); } var block = cc.instantiate(this.blockPrefab[num]); let height = this.Player.getComponent("Player").basicHeight + GameData._instance.GM_INFO.blockMin; if(num > GameData._instance.GM_INFO.blockScale){ height += block.height + GameData._instance.GM_INFO.blockMax; GameData._instance.CLICK_DATA.height = true; } block.setPosition(cc.v2(this.round%2==0?480:-480,height)); block.parent = this.node.getChildByName("Block"); block.zIndex = 10; this.round += 1; this.level += 1; if(this.round > GameData._instance.GM_INFO.levelMax) GameData._instance.GM_INFO.levelMax = this.round; GameData._instance.CLICK_DATA.level = this.round; GameData._instance.CLICK_DATA.round = this.level; GameData._instance.LEVEL_INFO.layer += GameData._instance.LEVEL_INFO.layerAdd; GameData._instance.LEVEL_INFO.speed += GameData._instance.LEVEL_INFO.addSpeed; if(GameData._instance.LEVEL_INFO.reduceSpeed <= 80) GameData._instance.LEVEL_INFO.reduceSpeed += GameData._instance.LEVEL_INFO.jiansu; if(this.round == GameData._instance.LEVEL_INFO.round1) GameData._instance.LEVEL_INFO.jiansu = 2; else if(this.round == GameData._instance.LEVEL_INFO.round2) GameData._instance.LEVEL_INFO.jiansu = 1; else if(this.round == GameData._instance.LEVEL_INFO.round3) GameData._instance.LEVEL_INFO.jiansu = 0.5; } } clearAir(){ cc.director.loadScene("GameScene"); } //虚假碎裂块 createCrackBlock(){ this.interfere = true; GameData._instance.CLICK_DATA.fake = this.interfere; if(this.round > 1) this.destroyBlock(true); if(this.over == false && this.begin == true){ var block = cc.instantiate(this.blockPrefab[12]); let height = this.Player.getComponent("Player").basicHeight; if(GameData._instance.CLICK_DATA.height == true){ height += block.height/2; } block.setPosition(cc.v2(this.round%2==0?480:-480,height)); block.parent = this.node.getChildByName("CrackBlock"); block.zIndex = 1; } } destroyBlock(type){ if(type == true){ if(this.node){ if(this.node.getChildByName("CrackBlock")){ if(this.node.getChildByName("CrackBlock").children){ var block2 = this.node.getChildByName("CrackBlock").children; for(let i=0; i{ xin.active = false; }) .start(); } //死亡 getDeath(data){ if(data == true){ this.destroyBlock(false); cc.tween(this.Player) .to(1,{y:-500},{easing:'cubicIn'}) .call(()=>{ this.Player.getComponent("Player").init(); // this.Player.getComponent("Player").jumpPause = true; if(this.guide > 5){ if(this.guide == 6){ this.guide = 5; this.round = 2; this.startGuide(5,true); } else{ this.round = 1; this.startGuide(6,true); } } else{ this.round = 0; this.startGuide(3,true); } }) .delay(0.5) .call(() =>{ }) .start(); } else{ if(GameData._instance.GM_INFO.life > 0){ GameData._instance.GM_INFO.life -= 1; this.getDevive(1); }else{ GameData._instance.GM_INFO.life = -1; this.getDevive(3); } } } //复活 getDevive(time){ this.round = 0; GameData._instance.LEVEL_init(); if(this.topUI.getChildByName("star3").active == true){ // GameData._instance.LEVEL_INFO.scoreMax = 150000; } this.interfere = false; GameData._instance.CLICK_DATA.fake = this.interfere; this.destroyBlock(false); var block2 = this.node.getChildByName("CrackBlock").children; for(let i=0; i 500){ actionTime = (this.Player.y + 500)/1000*actionTime; } cc.tween(this.Player) .to(actionTime,{y:-500},{easing:'cubicIn'}) .call(()=>{ this.Player.getComponent("Player").xinAction(); }) .delay(0.5) .call(() =>{ this.xinAction(); this.Player.getComponent("Player").init(); if(time > 1){ this.Xin.parent.active = true; this.Xin.getComponent(cc.Sprite).fillRange = 0; cc.tween(this.Xin.getComponent(cc.Sprite)) .to(time,{fillRange:1}) .delay(0.3) .call(() =>{ this.Xin.parent.active = false; }) .delay(0) .call(() =>{ this.createBlock(); }) .start(); } else{ setTimeout(() => { this.createBlock(); }, (time+0.5)*1000); } }) .start(); } setData(){ GameData._instance.CLICK_DATA.totalScore = GameManager._instance.score; GameData._instance.CLICK_DATA.timer = this.countTime; GameData._instance.CLICK_DATA.level = this.round; GameData._instance.CLICK_DATA.round = this.level; //CLICK_DATA 数据处理完毕后调用上传接口 上传接口内CLICK_DATA不做改变 GameTool.setGameData(); } //获取matchId 用于上传每次点击数据里面记录id方便查询 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.level == 1){ char += 1; } matchId = matchId.slice(0, 10) + char + ""; GameData._instance.GM_INFO.matchId = matchId; cc.sys.localStorage.setItem("matchId",matchId); } } return matchId; } //检测matchId 如果有缓存以前的nanana数据清除 containsNanana(str) { return /na/i.test(str); } //重新设置MatchId 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; } updateCountDownTime () { if (this.countTime > 0 && !GameData._instance.GM_INFO.probation) { 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() } if(this.countTime <= 0){ this.unschedule(this.updateCountDownTime); this.begin = false; this.over = true; this.overStop(); this.setRank(); setTimeout(() => { this.overOpen(); }, 2000); } } } //停止所有动画 overStop(){ if(this.Player.getComponent("Player").jumpAction) this.Player.getComponent("Player").jumpAction.stop(); var block = this.node.getChildByName("Block").children; for(let i=0; i