class Game extends egret.DisplayObjectContainer { public static Instance: Game = new Game(); public levelConfig:any; public ballConfig:any; public balls_pool: Array; public ballContainer: egret.DisplayObjectContainer = new egret.DisplayObjectContainer(); public mapConfig:any; public mapPosition:any; public startIndex:number = 0; public startjian:boolean = false; //当前移动的第一个球的下标 public index:number = 0; public canPlay:boolean = false; public playNumEnterFrame:number = 0; public levelIndex:number = 0; public targetX:number = 0; public targetY:number = 0; public isAttack:boolean = false; //是否通关 public pass:boolean = false; public passFrame:number = 0; public passIndex:number = 1; //玩家操作的球 public playerBall:Ball; public fireBalls:Ball[] = new Array(); public nextPlayerBall:Ball; public cosValue:number; public sinValue:number; public boomMC:egret.MovieClip; //游戏是否结束 public end:boolean = false; public timeIndex:number = 1; //每执行一次定时器球的移动速度 public speed:number = 25; //回退速度 public backSpeed:number = 25; //前进定时器 public timer: egret.Timer = new egret.Timer(20); //插入动画定时器 public insertTimer: egret.Timer = new egret.Timer(20); //插入动画的移动速度 public insertSpeed:number = 10; public insertstart:number; public insertend:number; public insertCheckIndex:number; public insertNum:number = 0; //是否添加插入定时器监听 private insertTimerInit:boolean = false; //回退定时器 private backTimer: egret.Timer = new egret.Timer(20); //是否添加定时器监听事件 private backTimerInit:boolean = false; //是否初始化 public inited:boolean = false; public backIndex:number; public backstartIndex:number; public delNum:number = 0; public numji:number = -1; public canFire:boolean; //准线小点的个数 public dotLength:number = 20; //是否有准线 public haveDot:boolean = false; //玩家球的炮台 public ballServe:egret.Bitmap; public gameBg:egret.Bitmap; public mapBg:egret.Bitmap; public lianjiEffect:egret.BitmapText; public dengji:egret.BitmapText; //道具 public back:egret.Bitmap; public boom:egret.Bitmap; public propbg:egret.Bitmap; //结束警告 public mapend:egret.Bitmap; public blast:egret.Bitmap; //头部背景 public gamehead:egret.Bitmap; //经验条 public experbg:egret.Bitmap; public exper:egret.Bitmap; //分数背景 public fraction:egret.Bitmap; public score: egret.TextField; //等级 public grade: egret.TextField; public gradeScore: egret.TextField; private power: egret.TextField; private percent: egret.TextField; private touchArea: egret.Bitmap; public dead: boolean = false; public dotContainer: egret.DisplayObjectContainer = new egret.DisplayObjectContainer(); public throughContainer: egret.DisplayObjectContainer = new egret.DisplayObjectContainer(); public rewardContainer: egret.DisplayObjectContainer = new egret.DisplayObjectContainer(); public shakeTween: egret.Tween; //游戏定义 public leftBullet = 0; private fireing: boolean = false; private constructor() { super(); this.once(egret.Event.ADDED_TO_STAGE, (event: egret.Event) => { this.levelConfig = RES.getRes("level_json"); this.ballConfig = RES.getRes("ball_json"); this.balls_pool = new Array(); this.throughContainer.width = this.stage.width; this.throughContainer.height = this.stage.height; this.throughContainer.x = 0; this.throughContainer.y = 0; this.gamehead = new egret.Bitmap(); this.gamehead.texture = RES.getRes("gamehead_jpg"); this.gamehead.x = 0; this.gamehead.y = 0; this.gamehead.width = this.stage.width; this.gamehead.height = 177; this.addChild(this.gamehead); this.experbg = new egret.Bitmap(); this.experbg.texture = RES.getRes("experiencebg_png"); this.experbg.x = 0; this.experbg.y = 162; this.experbg.width = this.stage.width; this.experbg.height = 12; this.addChild(this.experbg); this.exper = new egret.Bitmap(); this.exper.texture = RES.getRes("experience_png"); this.exper.x = 0; this.exper.y = 162; this.exper.width = this.stage.width; this.exper.height = 12; this.exper.mask = new egret.Rectangle(0,0,0,0); this.addChild(this.exper); let course = new egret.DisplayObjectContainer(); this.addChild(course); this.ballContainer.width = 600; this.ballContainer.height = 900; this.ballContainer.anchorOffsetX = 300; //this.ballContainer.anchorOffsetY = 450; this.ballContainer.x = this.stage.width / 2; this.ballContainer.y = 177; this.ballContainer.mask = new egret.Rectangle(0,0,600,900); this.dotContainer.width = 600; this.dotContainer.height = 900; this.dotContainer.anchorOffsetX = 300; //this.dotContainer.anchorOffsetY = 450; this.dotContainer.x = this.stage.width / 2; this.dotContainer.y = 177; this.addChild(this.ballContainer); this.addChild(this.dotContainer); let kongmingJson = RES.getRes("boom_json"); let kongmingImg = RES.getRes("boom_png"); let mcFactory = new egret.MovieClipDataFactory(kongmingJson,kongmingImg); this.boomMC = new egret.MovieClip(mcFactory.generateMovieClipData()); this.boomMC.anchorOffsetX = this.boomMC.width /2 + 20; this.boomMC.anchorOffsetY = this.boomMC.height / 2 + 20; this.boomMC.visible = false; this.propbg = new egret.Bitmap(); this.propbg.texture = RES.getRes("propbg_png"); this.propbg.x = this.stage.width / 2 - 225; this.propbg.y = this.stage.height - 89; this.propbg.width = 450; this.propbg.height = 89; this.addChild(this.propbg); this.touchArea = new egret.Bitmap(); this.touchArea.touchEnabled = true; this.touchArea.width = 600; this.touchArea.height = 900; this.touchArea.anchorOffsetX = 0; this.touchArea.anchorOffsetY = 0; this.touchArea.x = 0; this.touchArea.y = 0; this.ballContainer.addChild(this.touchArea); let offsetY = 0 if (window.innerWidth == 375 && window.innerHeight == 812) { offsetY = 40; } this.fraction = new egret.Bitmap(); this.fraction.texture = RES.getRes("fraction_png"); this.fraction.x = this.stage.width / 2 - this.fraction.width / 2; this.fraction.y = 0; this.addChild(this.fraction); this.mapend = new egret.Bitmap(); this.mapend.texture = RES.getRes("mapend_png"); this.mapend.anchorOffsetX = this.mapend.width / 2; this.mapend.anchorOffsetY = this.mapend.height / 2; this.mapend.x = 300; this.mapend.y = 450; this.mapend.visible = false; this.blast = new egret.Bitmap(); this.blast.texture = RES.getRes("blast_png"); this.blast.anchorOffsetX = this.blast.width / 2; this.blast.anchorOffsetY = this.blast.height / 2; this.blast.x = 250; this.blast.y = 450; this.blast.visible = false; this.score = new egret.TextField(); //this.score.font = RES.getRes('wwt_score_fnt'); this.score.text = '0'; this.score.textColor = 0x422904; this.score.size = 48; this.score.bold = true; this.score.anchorOffsetY = this.score.height / 2; //console.log(this.fraction.height); this.score.y = 99; this.score.anchorOffsetX = this.score.width / 2; this.score.x = this.stage.stageWidth / 2; this.addChild(this.score); this.grade = new egret.TextField(); this.grade.text = '1'; this.grade.textColor = 0x422904; this.grade.size = 30; this.grade.y = offsetY + 50; this.grade.x = 20; //this.addChild(this.grade); this.gradeScore = new egret.TextField(); this.gradeScore.text = '0/0'; this.gradeScore.textColor = 0x422904; this.gradeScore.size = 30; this.gradeScore.y = offsetY + 100; this.gradeScore.x = 20; //this.addChild(this.gradeScore); this.mapBg = new egret.Bitmap(); this.mapBg.touchEnabled = true; this.mapBg.texture = RES.getRes("map_png"); this.mapBg.width = 600; this.mapBg.height = 900; this.mapBg.anchorOffsetX = 300; this.mapBg.anchorOffsetY = 450; this.mapBg.x = 300; this.mapBg.y = 450; this.ballServe = new egret.Bitmap(); this.ballServe.texture = RES.getRes("serve_png"); this.ballServe.width = 141; this.ballServe.height = 190; this.ballServe.anchorOffsetX = 70.5; this.ballServe.anchorOffsetY = 95; this.ballServe.x = 300; this.ballServe.y = 700; this.boom = new egret.Bitmap(); this.boom.texture = RES.getRes("boomprop_png"); this.boom.width = 70; this.boom.height = 70; this.boom.anchorOffsetX = 35; this.boom.anchorOffsetY = 35; this.boom.x = this.stage.width / 2 - 85; this.boom.y = this.stage.height - 70; this.boom.touchEnabled = true; this.addChild(this.boom); this.boom.addEventListener(egret.TouchEvent.TOUCH_TAP,async()=>{ let result: any = await Network.Instance.share(4); let shareConfig = DataBus.Instance.share_config['4']; let data = await platform.shareAppMessage(true, shareConfig.title, shareConfig.image, ''); Network.Instance.shareSuccess(); if(data.shareTickets){ let rebackData= await platform.getShareInfo(data.shareTickets[0]); let result=await Network.Instance.checkGroupShareCount(rebackData.encryptedData,rebackData.iv); if(result==1){ platform.showToast('领取道具成功',false); //群分享成功 if(this.playerBall){ if(this.playerBall.x == this.mapConfig.playerX && this.playerBall.y == this.mapConfig.playerY){ this.playerBall.changeBoom(); }else{ this.nextPlayerBall.changeBoom(); } } }else{ platform.showToast('换个群试试~',false); } }else{ platform.showToast('请分享到群',false); } },this); this.back = new egret.Bitmap(); this.back.texture = RES.getRes("backprop_png"); this.back.width = 70; this.back.height = 70; this.back.anchorOffsetX = 35; this.back.anchorOffsetY = 35; this.back.touchEnabled = true; this.back.x = this.stage.width / 2 + 65; this.back.y = this.stage.height - 70; this.addChild(this.back); this.back.addEventListener(egret.TouchEvent.TOUCH_TAP,async()=>{ let result: any = await Network.Instance.share(4); let shareConfig = DataBus.Instance.share_config['4']; let data = await platform.shareAppMessage(true, shareConfig.title, shareConfig.image, ''); Network.Instance.shareSuccess(); if(data.shareTickets){ let rebackData= await platform.getShareInfo(data.shareTickets[0]); let result=await Network.Instance.checkGroupShareCount(rebackData.encryptedData,rebackData.iv); if(result==1){ //群分享成功 platform.showToast('领取道具成功',false); this.timer.stop(); this.backIndex = this.balls_pool.length - 2; this.backstartIndex = this.index; this.delNum = 10 * (~~(this.playerBall.width / this.backSpeed)); this.startIndex -= 10; this.numji = 0; if(!this.backTimerInit){ this.backTimer.addEventListener(egret.TimerEvent.TIMER, () => { this.backTween(); }, this); this.backTimerInit = true; } this.backTimer.start(); }else{ platform.showToast('换个群试试~',false); } }else{ platform.showToast('请分享到群',false); } },this); this.lianjiEffect = new egret.BitmapText(); this.lianjiEffect.text = ''; this.lianjiEffect.x = 100; this.lianjiEffect.y = 100; this.lianjiEffect.font = RES.getRes('lianji_fnt'); this.dengji = new egret.BitmapText(); this.dengji.text = '1级'; this.dengji.x = this.stage.width / 2 - 15; this.dengji.y = 152; this.dengji.font = RES.getRes('dengji_fnt'); this.addChild(this.dengji); this.touchArea.addEventListener(egret.TouchEvent.TOUCH_BEGIN, (e: egret.TouchEvent) => { //if (this.canPlay) { this.targetX = e.localX; this.targetY = e.localY; this.ballServe.rotation = this.getAngle(this.nextPlayerBall.x,this.nextPlayerBall.y,this.targetX,this.targetY); this.cos(this.nextPlayerBall.x,this.nextPlayerBall.y,this.targetX,this.targetY); this.haveDot = true; //} }, this); this.touchArea.addEventListener(egret.TouchEvent.TOUCH_MOVE, (e: egret.TouchEvent) => { //if (this.canPlay) { this.targetX = e.localX; this.targetY = e.localY; this.ballServe.rotation = this.getAngle(this.nextPlayerBall.x,this.nextPlayerBall.y,this.targetX,this.targetY); this.cos(this.nextPlayerBall.x,this.nextPlayerBall.y,this.targetX,this.targetY); //} }, this); this.touchArea.addEventListener(egret.TouchEvent.TOUCH_END, (e: egret.TouchEvent) => { //if(this.canPlay){ this.boomMC.x = 300; this.boomMC.y = 800; this.haveDot = false; this.targetX = e.localX; this.targetY = e.localY; if(this.canFire && this.inited){ this.playNumEnterFrame = 0; this.canFire = false; this.playerBall = this.nextPlayerBall; this.cos(this.playerBall.x,this.playerBall.y,this.targetX,this.targetY); this.createPlayerBall(); this.playerBall.sinValue = this.sinValue; this.playerBall.cosValue = this.cosValue; this.fireBalls.push(this.playerBall); this.isAttack = true; } //} }, this); this.timer.addEventListener(egret.TimerEvent.TIMER, this.moveStage, this); this.insertTimer.addEventListener(egret.TimerEvent.TIMER, this.insertTween, this); this.addEventListener(egret.Event.ENTER_FRAME, this.enterFrame, this); /*let blackBg = new egret.Bitmap(RES.getRes('bg_black1_png')); blackBg.x = 0; blackBg.y = 0; blackBg.width = this.stage.stageWidth; blackBg.height = this.stage.stageHeight; course.addChild(blackBg); let hand = new egret.Bitmap(RES.getRes('hand1_png')); hand.x = this.stage.stageWidth / 2 - 200 + 34; hand.y = this.stage.stageHeight / 2 - 80; hand.width = 80; hand.height = 130; course.addChild(hand); var handBg = new egret.Bitmap(RES.getRes('hand_bg_png')); handBg.x = this.stage.stageWidth / 2 - 200 + 34; handBg.y = this.stage.stageHeight / 2 - 82; handBg.width = 334; handBg.height = 32; course.addChild(handBg); course.addChild(Player.Instance); var textCouse = new egret.BitmapText(); textCouse.text = '边打边躲才能取得高分哦!~'; textCouse.font = RES.getRes('wwt_font_6_fnt'); textCouse.x = (this.stage.stageWidth - textCouse.width) / 2; textCouse.y = this.stage.stageHeight / 2 - 200 + 10; course.addChild(textCouse); var textCouse1 = new egret.BitmapText(); textCouse1.text = '滑动屏幕来射击'; textCouse1.font = RES.getRes('wwt_font_7_fnt'); textCouse1.x = (this.stage.stageWidth - textCouse1.width) / 2; textCouse1.y = this.stage.stageHeight / 2 - 140 + 6; course.addChild(textCouse1); egret.Tween.get(hand, { loop: true, onChange: () => { Player.Instance.setX(hand.x); Player.Instance.update(); } }).to({ x: handBg.x + handBg.width - hand.width }, 1000).to({ x: handBg.x }, 1000); blackBg.touchEnabled = true; blackBg.addEventListener(egret.TouchEvent.TOUCH_TAP, (event: egret.TouchEvent) => { event.stopImmediatePropagation(); egret.Tween.removeTweens(hand); this.removeChild(course); let offsetY = 0 if (window.innerWidth == 375 && window.innerHeight == 812) { offsetY = 40; } let touchArea = new egret.Bitmap(); touchArea.touchEnabled = true; touchArea.width = this.stage.width; touchArea.height = this.stage.height; this.addChild(touchArea); this.score = new egret.BitmapText(); this.score.font = RES.getRes('wwt_score_fnt'); this.score.text = '0'; this.score.anchorOffsetY = this.score.height / 2; this.score.y = 116 + this.score.height / 2 this.addChild(this.score); this.smallBg = new egret.Bitmap(RES.getRes('boll_home_smallbg_png')); this.smallBg.x = 28; this.smallBg.y = 49 + offsetY; this.smallBg.width = 144; this.smallBg.height = 34; this.addChild(this.smallBg); this.powerBg = new egret.Bitmap(RES.getRes('boll_home_energy_png')); this.powerBg.x = 28; this.powerBg.y = 44 + offsetY; this.powerBg.width = 44; this.powerBg.height = 44; this.addChild(this.powerBg); this.powerEffect = new egret.Bitmap(RES.getRes('circular_yellow_png')); this.powerEffect.x = 50; this.powerEffect.y = 66 + offsetY; this.powerEffect.anchorOffsetX = 22; this.powerEffect.anchorOffsetY = 22; this.powerEffect.width = 44; this.powerEffect.height = 44; // this.powerEffect.visible = false; this.addChild(this.powerEffect); this.smallBg1 = new egret.Bitmap(RES.getRes('boll_home_smallbg_png')); this.smallBg1.x = 190; this.smallBg1.y = 49 + offsetY; this.smallBg1.width = 144; this.smallBg1.height = 34; this.addChild(this.smallBg1); this.percentBg = new egret.Bitmap(RES.getRes('boll_home_fair_png')); this.percentBg.x = 190; this.percentBg.y = 44 + offsetY; this.percentBg.width = 44; this.percentBg.height = 44; this.addChild(this.percentBg); this.percentEffect = new egret.Bitmap(RES.getRes('circular_red_png')); this.percentEffect.x = 212; this.percentEffect.y = 66 + offsetY; this.percentEffect.anchorOffsetX = 22; this.percentEffect.anchorOffsetY = 22; this.percentEffect.width = 44; this.percentEffect.height = 44; // this.percentEffect.visible = false; this.addChild(this.percentEffect); this.power = new egret.TextField(); this.power.size = 24; this.power.x = 80; this.power.y = 56 + offsetY; this.power.textColor = 0x191F48; this.power.fontFamily = "Arial"; this.addChild(this.power); this.percent = new egret.TextField(); this.percent.size = 24; this.percent.x = 242; this.percent.y = 56 + offsetY; this.percent.textColor = 0x191F48; this.percent.fontFamily = "Arial"; this.addChild(this.percent); this.addChild(Player.Instance); this.addChild(this.rewardContainer); this.addChild(this.enemyContainer); this.addChild(this.bulletContainer); this.addChild(this.enemyContainer); touchArea.addEventListener(egret.TouchEvent.TOUCH_BEGIN, (e: egret.TouchEvent) => { if (!this.dead) { this.fireing = true; Player.Instance.setX(e.stageX); Player.Instance.setFire(this.fireing); } }, this); touchArea.addEventListener(egret.TouchEvent.TOUCH_MOVE, (e: egret.TouchEvent) => { if (!this.dead) { this.fireing = true; Player.Instance.setFire(this.fireing); Player.Instance.setX(e.stageX); } }, this); touchArea.addEventListener(egret.TouchEvent.TOUCH_END, () => { this.fireing = false; Player.Instance.setFire(this.fireing); }, this); this.addEventListener(egret.Event.ENTER_FRAME, this.enterFrame, this); DataBus.Instance.course += 1; }, this);*/ }, this); } public getAngle(px,py,mx,my){//获得人物中心和鼠标坐标连线,与y轴正半轴之间的夹角,设置炮台方向 var x = Math.abs(px-mx); var y = Math.abs(py-my); var z = Math.sqrt(Math.pow(x,2)+Math.pow(y,2)); var cos = y/z; var radina = Math.acos(cos);//用反三角函数求弧度 var angle = Math.floor(180/(Math.PI/radina));//将弧度转换成角度 if(mx>px&&my>py){//鼠标在第四象限 angle = 180 - angle; } if(mx==px&&my>py){//鼠标在y轴负方向上 angle = 180; } if(mx>px&&my==py){//鼠标在x轴正方向上 angle = 90; } if(mxpy){//鼠标在第三象限 angle = 180+angle; } if(mx{ this.canFire = true; }); if(this.playerBall == null){ this.playerBall = this.nextPlayerBall; } } //开局动画 public startTween(){ this.timer.reset(); this.timer.start(); } public backStartIndex(index:number){ let startindex:number = index; for(let i = startindex; i > 0 ; i--){ //如果后面运动的球的距离与前面静止的球的距离小于球的宽度,则前面的球也要运动 if(this.checkDifMax(this.balls_pool[i],this.balls_pool[i-1])){ startindex = i - 1; continue; } break; } console.log("倒退的startindex:" + startindex); return startindex; } //倒退动画 public backTween(){ console.log("进入backtween"); let balls_pool:Array = this.balls_pool; //如果倒退的不是最后一段,也就是有间隔的地方,进行如下判定 if(this.backIndex!= this.balls_pool.length-2 && this.checkDif(balls_pool[this.backIndex],balls_pool[this.backIndex+1])){ this.backTimer.stop(); GameUtil.Sound.playEffect("ball_wav"); //如果不能消除 if(!this.checkCanMove(this.backIndex)){ for(let i = this.backIndex;i < this.balls_pool.length - 2; i++){ if(this.checkDif(balls_pool[i],balls_pool[i+1])){ this.backIndex = i + 1; }else{ break; } } this.backTimer.start(); DataBus.Instance.scorePower = 0; this.numji = 0; if(!this.startjian && this.backIndex == this.balls_pool.length-2){ this.startIndex -= ~~(this.delNum / ~~(this.playerBall.width / this.backSpeed)); this.startjian = true; } } //this.backSpeed = 5; console.log("碰到了"); return; } if(this.numji >= 0 && this.numji <= ~~(this.playerBall.width / this.backSpeed)){ this.numji++; this.delNum--; console.log("delNum:" + this.delNum); } //如果回退一个单位球 if(this.numji == ~~(this.playerBall.width / this.backSpeed)){ this.numji = 0; //this.startIndex--; } //倒退结束处理 if(this.delNum == -1){ console.log("timeIndex:" + this.timeIndex); this.startjian = false; this.backTimer.stop(); this.timer.start(); this.numji = -1; this.canPlay = true; if(DataBus.Instance.scorePower > 1){ DataBus.Instance.lianjiBigTwoNum++; } DataBus.Instance.scorePower = 0; this.checkMin(0,this.startIndex - 1); return; } let endindex = this.backIndex + 1; for(let i = this.backstartIndex ;i < endindex ; i++){ let ball:Ball = balls_pool[i]; let crux:any; //如果在直线状态 if(!ball.inCrux){ //如果是起点那一段直线 if(ball.cruxIndex == 0){ let dif = this.diffPoint(ball.x,ball.y,this.mapConfig.startX,this.mapConfig.startY); ball.x += (this.backSpeed / dif) * (this.mapConfig.startX - ball.x); ball.y += (this.backSpeed / dif) * (this.mapConfig.startY - ball.y); //如果回退到开始处了 if(Math.abs(this.mapConfig.crux[0].startpoint.x - ball.x)> Math.abs(this.mapConfig.crux[0].startpoint.x - this.mapConfig.startX)||Math.abs(this.mapConfig.crux[0].startpoint.y - ball.y)> Math.abs(this.mapConfig.crux[0].startpoint.y - this.mapConfig.startY)){ //this.startIndex--; ball.x = this.mapConfig.startX; ball.y = this.mapConfig.startY; }else{ } }else{ crux = this.mapConfig.crux[ball.cruxIndex - 1]; let dif = this.diffPoint(ball.x,ball.y,crux.endpoint.x,crux.endpoint.y); ball.x += (this.backSpeed / dif) * (crux.endpoint.x - ball.x); ball.y += (this.backSpeed / dif) * (crux.endpoint.y - ball.y); //如果是终点那一段直线 if(ball.cruxIndex == this.mapConfig.crux.length){ if(this.diffPoint(ball.x,ball.y,this.mapConfig.endX,this.mapConfig.endY) >= this.diffPoint(crux.endpoint.x,crux.endpoint.y,this.mapConfig.endX,this.mapConfig.endY)){ ball.x = crux.endpoint.x; ball.y = crux.endpoint.y; ball.inCrux = true; ball.cruxIndex--; ball.startAngle = ~~ball.angle - crux.angle; } } else{ //如果移动到指定地点,则计算误差 let difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.crux[ball.cruxIndex].startpoint.x,this.mapConfig.crux[ball.cruxIndex].startpoint.y); let difCon = this.diffPoint(crux.endpoint.x,crux.endpoint.y,this.mapConfig.crux[ball.cruxIndex].startpoint.x,this.mapConfig.crux[ball.cruxIndex].startpoint.y); if(difBall >= difCon){ ball.difLine = difBall - difCon; ball.x = crux.endpoint.x; ball.y = crux.endpoint.y; ball.inCrux = true; ball.startAngle = ~~ball.angle - crux.angle; ball.cruxIndex--; crux = this.mapConfig.crux[ball.cruxIndex]; //圆心的坐标 let circleX = crux.circle.x; let circleY = crux.circle.y; //半径 let radius:number = this.diffPoint(crux.startpoint.x,crux.startpoint.y,circleX,circleY); //要移动的弧长,根据弧长和速度计算移动时间 let perimeter:number = parseFloat((2 * Math.PI * radius * Math.abs(ball.startAngle - ball.angle)/360) .toFixed(2)); let time = perimeter / this.backSpeed; let difAngle = (ball.startAngle - ball.angle) / time; let wuAngle = ball.difLine / this.backSpeed * difAngle; ball.difLine = 0; ball.angle += wuAngle; let dify = radius * Math.cos(Math.PI * ball.angle / 180); let difx = radius * Math.sin(Math.PI * ball.angle / 180); if(crux.angle < 0){ ball.y = circleY - dify; ball.x = circleX - difx; }else{ ball.y = circleY + dify; ball.x = circleX + difx; } } } } }else{ //如果在拐歪状态 crux = this.mapConfig.crux[ball.cruxIndex]; //圆心的坐标 let circleX = crux.circle.x; let circleY = crux.circle.y; //半径 let radius:number = this.diffPoint(crux.startpoint.x,crux.startpoint.y,circleX,circleY); //要移动的弧长,根据弧长和速度计算移动时间 let perimeter:number = parseFloat((2 * Math.PI * radius * Math.abs(ball.startAngle - ball.angle)/360) .toFixed(2)); let time = perimeter / this.backSpeed; let difAngle = (ball.startAngle - ball.angle) / time; ball.difLine = 0; //如果刚好到关键点的开始处就后退,处理这个误差 if(ball.startAngle == ball.angle){ ball.difLine = this.backSpeed; }else{ ball.angle += difAngle; } if(crux.angle < 0){ //旋转结束 if((ball.angle - crux.angle) >= (ball.startAngle - crux.angle)){ ball.difLine = parseFloat((2 * Math.PI * radius * Math.abs(ball.startAngle - ball.angle)/360) .toFixed(2)) || ball.difLine; ball.inCrux = false; ball.angle = ball.startAngle; ball.targetAngle = ball.angle; if(ball.angle == 180){ ball.x = crux.startpoint.x + ball.difLine; ball.y = crux.startpoint.y; }else if(ball.angle == 360){ ball.x = crux.startpoint.x - ball.difLine; ball.y = crux.startpoint.y; }else if(ball.angle == 270){ ball.x = crux.startpoint.x; ball.y = crux.startpoint.y - ball.difLine; }else if(ball.angle == 450){ ball.x = crux.startpoint.x; ball.y = crux.startpoint.y + ball.difLine; }else{ ball.x = crux.startpoint.x; ball.y = crux.startpoint.y; } continue; } }else{ //旋转结束 if((ball.angle - crux.angle) <= (ball.startAngle - crux.angle)){ ball.inCrux = false; ball.difLine = parseFloat((2 * Math.PI * radius * Math.abs(ball.startAngle - ball.angle)/360) .toFixed(2)); ball.angle = ball.startAngle; ball.targetAngle = ball.angle; if(ball.angle == 180){ ball.x = crux.startpoint.x + ball.difLine; ball.y = crux.startpoint.y; }else if(ball.angle == 360){ ball.x = crux.startpoint.x - ball.difLine; ball.y = crux.startpoint.y; }else if(ball.angle == 270){ ball.x = crux.startpoint.x; ball.y = crux.startpoint.y - ball.difLine; }else if(ball.angle == 450){ ball.x = crux.startpoint.x; ball.y = crux.startpoint.y + ball.difLine; }else{ ball.x = crux.startpoint.x; ball.y = crux.startpoint.y; } continue; } } let dify = radius * Math.cos(Math.PI * ball.angle / 180); let difx = radius * Math.sin(Math.PI * ball.angle / 180); if(crux.angle < 0){ ball.y = circleY - dify; ball.x = circleX - difx; }else{ ball.y = circleY + dify; ball.x = circleX + difx; } } } } //校验小球之间的距离过大 public check(){ let balls_pool:Array = this.balls_pool; if(this.index > this.startIndex){ this.index = this.startIndex; console.log("thisindex:" + this.index); } //如果两个小球之间的间距过大,则前面的球先停下,由于速度过快,玩家并不会有察觉 for(let i = this.startIndex - 1; i > this.index; i--){ let dif = this.diffPoint(balls_pool[i].x,balls_pool[i].y,balls_pool[i-1].x,balls_pool[i-1].y); if(dif > this.playerBall.width + 3){ console.log("距离过大:" + i); this.index = i; break; } } } //校验两个小球之间的间距过小 public checkMin(index:number,endindex:number){ let balls_pool:Array = this.balls_pool; //如果两个小球之间的间距小于一个球的宽度,说明两个球重合了,进行处理 for(let i = endindex; i > index; i--){ let dif = this.diffPoint(balls_pool[i].x,balls_pool[i].y,balls_pool[i-1].x,balls_pool[i-1].y); if( dif < this.playerBall.width - 5){ console.log("距离过小" + i); for(let j = 0; j < (this.playerBall.width - dif) / this.speed;j++){ this.moveTween(index,i); } } } } //移动动画,start为前面运动的球的下标,index为后面的球的下标 public moveTween(start:number,index:number,inPause?:boolean){ if(this.end){ return; } let speed = this.speed; let balls_pool:Array = this.balls_pool; if(!inPause){ for(let i = start; i > 0 ; i--){ //如果后面运动的球的距离与前面静止的球的距离小于球的宽度,则前面的球也要运动 //console.log("start:" + start + " i:" + i); if(this.checkDif(balls_pool[i],balls_pool[i-1])){ this.index = i-1; start = i - 1; continue; } break; } } for(let i = start ;i < index ; i++){ let ball:Ball = balls_pool[i]; let crux:any; //获取配置文件的拐点坐标 if(ball.cruxIndex < this.mapConfig.crux.length){ crux = this.mapConfig.crux[ball.cruxIndex]; }else{ //如果已经走过所有节点,则直接向终点移动 let dif = this.diffPoint(ball.x,ball.y,this.mapConfig.endX,this.mapConfig.endY); ball.x += (speed / dif) * (this.mapConfig.endX - ball.x); ball.y += (speed / dif) * (this.mapConfig.endY - ball.y); let difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y); let difCon = this.diffPoint(this.mapConfig.endX,this.mapConfig.endY,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y); if(difBall >= difCon){ this.end = true; this.canPlay = false; ball.x = this.mapConfig.endX; ball.y = this.mapConfig.endY; } continue; } //如果在直线状态 if(!ball.inCrux ){ let dif = this.diffPoint(ball.x,ball.y,crux.startpoint.x,crux.startpoint.y); ball.x += (speed / dif) * (crux.startpoint.x - ball.x); ball.y += (speed / dif) * (crux.startpoint.y - ball.y); let difBall; let difCon; //如果是刚开始那一段直线 if(ball.cruxIndex == 0){ difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.startX,this.mapConfig.startY); difCon = this.diffPoint(crux.startpoint.x,crux.startpoint.y,this.mapConfig.startX,this.mapConfig.startY); } else{ //如果移动到指定地点,则将坐标锁定在指定地点 difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y); difCon = this.diffPoint(crux.startpoint.x,crux.startpoint.y,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y); } if(difBall >= difCon){ ball.difLine = difBall - difCon; ball.x = crux.startpoint.x; ball.y = crux.startpoint.y; ball.inCrux = true; ball.startAngle = ball.angle; ball.targetAngle = ~~ball.angle + crux.angle; //圆心的坐标 let circleX = crux.circle.x; let circleY = crux.circle.y; //半径 let radius:number = this.diffPoint(crux.startpoint.x,crux.startpoint.y,circleX,circleY); //要移动的弧长,根据弧长和速度计算移动时间 let perimeter:number = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2)); let time = perimeter / speed; let difAngle = (ball.targetAngle - ball.angle) / time; let wuAngle = ball.difLine / speed * difAngle; ball.difLine = 0; ball.angle += wuAngle; let dify = radius * Math.cos(Math.PI * ball.angle / 180); let difx = radius * Math.sin(Math.PI * ball.angle / 180); if(crux.angle < 0){ ball.y = circleY - dify; ball.x = circleX - difx; }else{ ball.y = circleY + dify; ball.x = circleX + difx; } } }else{ //如果在拐歪状态 let circleX = crux.circle.x; let circleY = crux.circle.y; //半径 let radius:number = this.diffPoint(crux.startpoint.x,crux.startpoint.y,circleX,circleY); ball.difLine = 0; //如果刚好到关键点的开始处就后退,处理这个误差 if(ball.targetAngle == ball.angle){ ball.difLine = speed; } //要移动的弧长,根据弧长和速度计算移动时间 if(ball.targetAngle != ball.angle){ let perimeter:number = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2)); let time = perimeter / speed; let difAngle = (ball.targetAngle - ball.angle) / time; ball.angle += difAngle; } if(crux.angle < 0){ //旋转结束 if((ball.angle - crux.angle) <= (ball.targetAngle - crux.angle)){ ball.difLine = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2)) || ball.difLine; ball.inCrux = false; ball.x = crux.endpoint.x; ball.y = crux.endpoint.y; ball.angle = ball.targetAngle; ball.cruxIndex++; if(ball.angle == 180){ ball.x = crux.endpoint.x - ball.difLine; ball.y = crux.endpoint.y; }else if(ball.angle == 360){ ball.x = crux.endpoint.x + ball.difLine; ball.y = crux.endpoint.y; }else if(ball.angle == 270){ ball.x = crux.endpoint.x; ball.y = crux.endpoint.y + ball.difLine; }else if(ball.angle == 450){ ball.x = crux.endpoint.x; ball.y = crux.endpoint.y - ball.difLine; }else{ ball.x = crux.endpoint.x; ball.y = crux.endpoint.y; } continue; } }else{ //旋转结束 if((ball.angle - crux.angle) >= (ball.targetAngle - crux.angle)){ ball.difLine = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2)) || ball.difLine; ball.inCrux = false; ball.x = crux.endpoint.x; ball.y = crux.endpoint.y; ball.angle = ball.targetAngle; ball.cruxIndex++; if(ball.angle == 180){ ball.x = crux.endpoint.x - ball.difLine; ball.y = crux.endpoint.y; }else if(ball.angle == 360){ ball.x = crux.endpoint.x + ball.difLine; ball.y = crux.endpoint.y; }else if(ball.angle == 270){ ball.x = crux.endpoint.x; ball.y = crux.endpoint.y + ball.difLine; }else if(ball.angle == 450){ ball.x = crux.endpoint.x; ball.y = crux.endpoint.y - ball.difLine; }else{ ball.x = crux.endpoint.x; ball.y = crux.endpoint.y; } continue; } } let dify = radius * Math.cos(Math.PI * ball.angle / 180); let difx = radius * Math.sin(Math.PI * ball.angle / 180); if(crux.angle < 0){ ball.y = circleY - dify; ball.x = circleX - difx; }else{ ball.y = circleY + dify; ball.x = circleX + difx; } } } if(this.end){ this.gameOver(); this.timer.stop(); } } public moveStage(){ if(this.end){ //处理游戏结束效果 //this.timer.stop(); } this.timeIndex++; if(this.timer.delay == 20 && this.startIndex == this.balls_pool.length - 1){ this.createBall(); } //每执行一个球的距离,开始坐标+1 if(this.timeIndex % (~~(this.playerBall.width / this.speed))==0 && !this.end){ this.timeIndex = 0; this.startIndex ++; //如果还未初始化并且长度到达最后,则放慢移动速度,初始化动画播放完成 //console.log("this.startIndex:" + this.startIndex); if(!this.inited && this.startIndex == this.balls_pool.length - 1){ this.timer.delay = 20; this.canPlay = true; this.inited = true; this.speed = this.levelConfig[this.levelIndex].speed[DataBus.Instance.nanduIndex]; this.timer.stop(); this.timer.reset(); this.timer.start(); this.checkMin(this.index,this.balls_pool.length -2); } } //console.log("index:" + this.index + " startIndex:" + this.startIndex); if(this.startIndex < 0 ){ this.startIndex = 0; } this.check(); //this.checkMin(); //最后球全部弹进去之后,进行一次矫正 /*if(this.startIndex < this.index){ this.startIndex++; }*/ this.moveTween(this.index,this.startIndex); } public inserttween(start:number,index:number,inPause?:boolean){ if(this.end){ return; } let speed = this.insertSpeed; let balls_pool:Array = this.balls_pool; if(!inPause){ for(let i = start; i > 0 ; i--){ //如果后面运动的球的距离与前面静止的球的距离小于球的宽度,则前面的球也要运动 if(this.checkDif(balls_pool[i],balls_pool[i-1])){ this.index = i-1; start = i - 1; continue; } break; } } for(let i = start ;i < index ; i++){ let ball:Ball = balls_pool[i]; let crux:any; //获取配置文件的拐点坐标 if(ball.cruxIndex < this.mapConfig.crux.length){ crux = this.mapConfig.crux[ball.cruxIndex]; }else{ //如果已经走过所有节点,则直接向终点移动 let dif = this.diffPoint(ball.x,ball.y,this.mapConfig.endX,this.mapConfig.endY); ball.x += (speed / dif) * (this.mapConfig.endX - ball.x); ball.y += (speed / dif) * (this.mapConfig.endY - ball.y); let difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y); let difCon = this.diffPoint(this.mapConfig.endX,this.mapConfig.endY,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y); if(difBall >= difCon){ this.end = true; this.canPlay = false; ball.x = this.mapConfig.endX; ball.y = this.mapConfig.endY; } continue; } //如果在直线状态 if(!ball.inCrux ){ let dif = this.diffPoint(ball.x,ball.y,crux.startpoint.x,crux.startpoint.y); ball.x += (speed / dif) * (crux.startpoint.x - ball.x); ball.y += (speed / dif) * (crux.startpoint.y - ball.y); let difBall; let difCon; //如果是刚开始那一段直线 if(ball.cruxIndex == 0){ difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.startX,this.mapConfig.startY); difCon = this.diffPoint(crux.startpoint.x,crux.startpoint.y,this.mapConfig.startX,this.mapConfig.startY); } else{ //如果移动到指定地点,则将坐标锁定在指定地点 difBall = this.diffPoint(ball.x,ball.y,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y); difCon = this.diffPoint(crux.startpoint.x,crux.startpoint.y,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.x,this.mapConfig.crux[ball.cruxIndex - 1].endpoint.y); } if(difBall >= difCon){ ball.difLine = difBall - difCon; ball.x = crux.startpoint.x; ball.y = crux.startpoint.y; ball.inCrux = true; ball.startAngle = ball.angle; ball.targetAngle = ~~ball.angle + crux.angle; //圆心的坐标 let circleX = crux.circle.x; let circleY = crux.circle.y; //半径 let radius:number = this.diffPoint(crux.startpoint.x,crux.startpoint.y,circleX,circleY); //要移动的弧长,根据弧长和速度计算移动时间 let perimeter:number = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2)); let time = perimeter / speed; let difAngle = (ball.targetAngle - ball.angle) / time; let wuAngle = ball.difLine / speed * difAngle; ball.difLine = 0; ball.angle += wuAngle; let dify = radius * Math.cos(Math.PI * ball.angle / 180); let difx = radius * Math.sin(Math.PI * ball.angle / 180); if(crux.angle < 0){ ball.y = circleY - dify; ball.x = circleX - difx; }else{ ball.y = circleY + dify; ball.x = circleX + difx; } } }else{ //如果在拐歪状态 let circleX = crux.circle.x; let circleY = crux.circle.y; //半径 let radius:number = this.diffPoint(crux.startpoint.x,crux.startpoint.y,circleX,circleY); ball.difLine = 0; //如果刚好到关键点的开始处就后退,处理这个误差 if(ball.targetAngle == ball.angle){ ball.difLine = speed; } //要移动的弧长,根据弧长和速度计算移动时间 if(ball.targetAngle != ball.angle){ let perimeter:number = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2)); let time = perimeter / speed; let difAngle = (ball.targetAngle - ball.angle) / time; ball.angle += difAngle; } if(crux.angle < 0){ //旋转结束 if((ball.angle - crux.angle) <= (ball.targetAngle - crux.angle)){ ball.difLine = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2)) || ball.difLine; ball.inCrux = false; ball.x = crux.endpoint.x; ball.y = crux.endpoint.y; ball.angle = ball.targetAngle; ball.cruxIndex++; if(ball.angle == 180){ ball.x = crux.endpoint.x - ball.difLine; ball.y = crux.endpoint.y; }else if(ball.angle == 360){ ball.x = crux.endpoint.x + ball.difLine; ball.y = crux.endpoint.y; }else if(ball.angle == 270){ ball.x = crux.endpoint.x; ball.y = crux.endpoint.y + ball.difLine; }else if(ball.angle == 450){ ball.x = crux.endpoint.x; ball.y = crux.endpoint.y - ball.difLine; }else{ ball.x = crux.endpoint.x; ball.y = crux.endpoint.y; } continue; } }else{ //旋转结束 if((ball.angle - crux.angle) >= (ball.targetAngle - crux.angle)){ ball.difLine = parseFloat((2 * Math.PI * radius * Math.abs(ball.targetAngle - ball.angle)/360) .toFixed(2)) || ball.difLine; ball.inCrux = false; ball.x = crux.endpoint.x; ball.y = crux.endpoint.y; ball.angle = ball.targetAngle; ball.cruxIndex++; if(ball.angle == 180){ ball.x = crux.endpoint.x - ball.difLine; ball.y = crux.endpoint.y; }else if(ball.angle == 360){ ball.x = crux.endpoint.x + ball.difLine; ball.y = crux.endpoint.y; }else if(ball.angle == 270){ ball.x = crux.endpoint.x; ball.y = crux.endpoint.y + ball.difLine; }else if(ball.angle == 450){ ball.x = crux.endpoint.x; ball.y = crux.endpoint.y - ball.difLine; }else{ ball.x = crux.endpoint.x; ball.y = crux.endpoint.y; } continue; } } let dify = radius * Math.cos(Math.PI * ball.angle / 180); let difx = radius * Math.sin(Math.PI * ball.angle / 180); if(crux.angle < 0){ ball.y = circleY - dify; ball.x = circleX - difx; }else{ ball.y = circleY + dify; ball.x = circleX + difx; } } } if(this.end){ this.gameOver(); this.timer.stop(); } } public checkBack(index:number){ console.log("insertend:" + index); if(index == 0 || index >= this.startIndex - 1){ return; } //如果插入的球和前面的球有间隔,且插入的球与前面的球类型相同,则后退操作 if(!this.checkDif(this.balls_pool[index],this.balls_pool[index - 1])){ if(this.balls_pool[index].type == this.balls_pool[index - 1].type){ console.log("进来了"); this.backIndex = index - 1; this.timer.stop(); this.backstartIndex = this.backStartIndex(index - 1); if(!this.backTimerInit){ this.backTimer.addEventListener(egret.TimerEvent.TIMER, () => { this.backTween(); }, this); this.backTimerInit = true; } this.delNum = Math.floor(2/2) * (~~(this.playerBall.width / this.backSpeed)); this.backTimer.reset(); this.backTimer.start(); //this.isAttack = false; } }else if(!this.checkDif(this.balls_pool[index],this.balls_pool[index + 1])){ if(this.balls_pool[index].type == this.balls_pool[index + 1].type){ console.log("进来了"); this.backIndex = index; this.timer.stop(); this.backstartIndex = this.backStartIndex(index); if(!this.backTimerInit){ this.backTimer.addEventListener(egret.TimerEvent.TIMER, () => { this.backTween(); }, this); this.backTimerInit = true; } this.delNum = Math.floor(2/2) * (~~(this.playerBall.width / this.backSpeed)); this.backTimer.reset(); this.backTimer.start(); //this.isAttack = false; } } } //插入动画 public insertTween(){ if(this.insertNum == ~~(this.playerBall.width / this.insertSpeed)){ this.insertNum = 0; console.log("insertend:" + this.insertend); this.checkCanMove(this.insertCheckIndex); this.insertTimer.stop(); console.log("checkminend:" + this.insertend); this.checkMin(this.insertstart,this.startIndex - 1); this.checkBack(this.insertCheckIndex); return; } this.insertNum++; this.inserttween(this.insertstart,this.insertend+1); } //将球插入到已有数组 public insertBall(index:number,ball:Ball,isTail:boolean){ console.log("index " + index + " insertball"); this.balls_pool.splice(index,0,ball); if(isTail){ this.balls_pool[index].moveBy(this.balls_pool[index - 1]); this.insertend = index - 1; }else{ console.log("不是尾部"); this.balls_pool[index].moveBy(this.balls_pool[index + 1]); this.insertend = index; } if(index >= this.backIndex){ //this.backIndex--; }else{ this.backIndex++; } let frontBreakIndex:number = 0; this.insertCheckIndex = index; this.insertstart = this.index; //this.insertend = index; //如果插入的是移动队列中 console.log("this.index:" + this.index); if(index >= this.index){ this.insertTimer.start(); } else{ console.log("index:" + index + " this.index:" + this.index); //如果插入到前面已经停止的队列上,进行如下处理 let i; //计算当前这个停止队列的首个球的下标 for(i = index; i > 0 ; i--){ if(!this.checkDif(this.balls_pool[i],this.balls_pool[i-1])){ frontBreakIndex = i; break; } } if(i == 0 ){ frontBreakIndex = 0; } //如果在最前面插入 if(index == frontBreakIndex){ this.balls_pool[frontBreakIndex].moveBy(this.balls_pool[frontBreakIndex + 1]); } this.insertstart = frontBreakIndex; this.insertTimer.start(); this.index++; } this.startIndex++; } private async gameOver() { //游戏结束给开放数据域发送当前用户信息进行历史最高分比较 if (!DataBus.Instance.userInfo) { DataBus.Instance.power_added = 0; Main.Instance.login(); return; } if (DataBus.Instance.buff_expire - new Date().getMilliseconds() < 0) { Network.Instance.getHelpPowerInfo().catch(e => { DataBus.Instance.power_added = 0; DataBus.Instance.percent_added = 0; }); } await Network.Instance.gameOver().then((game_info: any) => { console.log(game_info); DataBus.Instance.game_id = game_info ? game_info.game_id : 0; if (game_info.user_info) { let uper:boolean = false; if(DataBus.Instance.rebornCount < 3){ uper = true; } DataBus.Instance.userInfo = game_info.user_info; platform.openDataContext.postMessage({ text: 'score', score: DataBus.Instance.userInfo.max_score >= DataBus.Instance.score ? DataBus.Instance.userInfo.max_score : DataBus.Instance.score, open_id: DataBus.Instance.userInfo.open_id, uper: uper, current_score: DataBus.Instance.score }); } }).catch(e => { console.log(e) }); Main.Instance.removeChild(Game.Instance); if (DataBus.Instance.rebornCount < 3) { Main.Instance.addChild(GameOver1.Instance); } else { platform.openDataContext.postMessage({ text: 'overRank', }); Main.Instance.addChild(RebackFirstPage.Instance); } } public cos(x,y,x1,y1){ let difX = x1 - x; let difY = y1 - y; let z = Math.sqrt(Math.pow(difX, 2) + Math.pow(difY, 2)); this.sinValue = difX / z; this.cosValue = difY / z; } //检测是否相撞 public checkAttack(index1:number){ let length = this.startIndex; let playerBall = this.fireBalls[index1]; for(let i = 0 ; i < length ; i++){ let dif = this.diffPoint(this.balls_pool[i].x,this.balls_pool[i].y,this.fireBalls[index1].x,this.fireBalls[index1].y); //如果相撞,进行碰撞处理 if(dif < this.playerBall.width * 2 / 3){ GameUtil.Sound.playEffect("ball_wav"); let index = i; //向上运动 if(this.balls_pool[i].angle % 360 == 90){ if(this.playerBall.y > this.balls_pool[i].y){ index++; } }else if(this.balls_pool[i].angle % 360 == 270){ if(this.playerBall.y < this.balls_pool[i].y){ index++; } }else if(this.balls_pool[i].angle % 360 > 90 && this.balls_pool[i].angle % 360 < 270){ if(this.playerBall.x > this.balls_pool[i].x){ index++; } }else{ if(this.playerBall.x < this.balls_pool[i].x){ index++; } } //是否是尾部插入 let isTail:boolean = false; if(index == i + 1){ //如果插入地方的两个球距离小,则不是在后面插入 if(this.checkDif(this.balls_pool[i],this.balls_pool[index])){ isTail = false; }else{ isTail = true; } } this.isAttack = false; //如果是爆裂球 if(playerBall.type == GameProp.propType.boom){ this.booms(playerBall,index); }else{ this.insertBall(index,playerBall,isTail); } this.fireBalls.splice(index1,1); //this.checkCanMove(i); break; } } } //爆裂球 public booms(ball:Ball,boomindex:number){ this.ballContainer.removeChild(ball); let index:number = 0; let idArr:number[] = new Array(); let indexjian:number = 0; for(let i = 0;i < this.startIndex - 1;i++){ if(this.diffPoint(this.balls_pool[i].x,this.balls_pool[i].y,ball.x,ball.y) < 100){ let obj = this.balls_pool[i]; egret.Tween.get(obj).to({scaleX:1.2,scaleY:1.2},100).to({scaleX:0.1,scaleY:0.1},100).call(() => { obj.visible = false; }); idArr.push(i); index = i; } } this.boomEffect(boomindex,idArr.length); this.updateScore(idArr.length); for(let i = this.startIndex - 1;i > 0 ;i--){ for(let j = 0; j < idArr.length;j++){ if(idArr[j]==i){ this.balls_pool.splice(i,1); this.startIndex--; if(i > this.index){ indexjian++; } break; } } } if(index > this.index){ this.index = index + 1; this.index -= indexjian; } } //检测两个球的距离是否相邻 public checkDif(ball1:Ball,ball2:Ball){ if(ball1 == null || ball2 == null){ return false; } if(this.diffPoint(ball1.x,ball1.y,ball2.x,ball2.y) > this.playerBall.width + 3){ return false; } return true; } public checkDifMax(ball1:Ball,ball2:Ball){ if(this.diffPoint(ball1.x,ball1.y,ball2.x,ball2.y) > this.playerBall.width + 5){ return false; } return true; } //检测是否可消除 public checkCanMove(index:number){ if(this.end){ return; } console.log("checkCanMove"); let i:number = index - 1; let j:number = index + 1; let startIndex:number = index; let num:number = 1; let length = this.balls_pool.length; for(i ; i >= 0 ;i--){ if(this.balls_pool[i].type == this.balls_pool[index].type && this.checkDif(this.balls_pool[i],this.balls_pool[i + 1])){ startIndex = i; num++; }else{ break; } } for(j ; j < this.startIndex - 1; j++){ if(this.balls_pool[j].type == this.balls_pool[index].type && this.checkDif(this.balls_pool[j],this.balls_pool[j - 1])){ num++; }else{ break; } } if(num > 2){ console.log("xiaochu"); //GameUtil.Sound.playEffect("glass_wav"); this.boomEffect(index,num); for(let i = 0;i { obj.visible = false; }); console.log("startIndex:" + (startIndex+i)) } //如果不是首 //j!= this.startIndex - 1 && if(i!= -1){ //如果消除后边上的两个球要是相同颜色,则前面的球后退 console.log("i:" + i); console.log("j:" + j); if(this.balls_pool[i].type == this.balls_pool[j].type){ this.balls_pool.splice(startIndex,num); this.insertend -= num; this.backIndex = i; this.timer.stop(); this.backstartIndex = this.backStartIndex(i); if(!this.backTimerInit){ this.backTimer.addEventListener(egret.TimerEvent.TIMER, () => { //this.backIndex = this.balls_pool.length - 2; this.backTween(); }, this); this.backTimerInit = true; } this.updateScore(num); this.delNum = Math.floor(num/2) * (~~(this.playerBall.width / this.backSpeed)); DataBus.Instance.scorePower++; if(DataBus.Instance.scorePower > 1){ DataBus.Instance.lianjiNum++; } this.backTimer.reset(); this.backTimer.start(); //this.isAttack = false; this.startIndex -= num; return true; }else{ //前面的球不动 console.log("qianmianbudong"); this.balls_pool.splice(startIndex,num); this.insertend -= num; this.updateScore(num); //如果插入的坐标处于正在移动的对列 if(index >= this.index){ this.index = i + 1; } this.delNum = 1; //this.isAttack = false; this.canPlay = true; } }else if(i == -1){ //如果是头 this.balls_pool.splice(startIndex,num); this.insertend -= num; if(index >= this.index){ this.index = i + 1; } this.delNum = 1; this.updateScore(num); //this.isAttack = false; this.canPlay = true; } this.startIndex -= num; this.backIndex -= num; //return true; }else{ //this.isAttack = false; this.canPlay = true; return false; } } public boomByIndex(index:number){ this.boomMC.x = this.balls_pool[index].x; this.boomMC.y = this.balls_pool[index].y; //this.ballContainer.swapChildren(this.boomMC,this.balls_pool[this.startIndex - 1]); this.boomMC.visible = true; this.boomMC.gotoAndPlay(1,1); } public boomEffect(index:number,num:number){ this.boomMC.x = this.balls_pool[index].x; this.boomMC.y = this.balls_pool[index].y; this.ballContainer.swapChildren(this.boomMC,this.balls_pool[this.startIndex - 1]); this.boomMC.visible = true; this.boomMC.gotoAndPlay(1,1); if(DataBus.Instance.scorePower > 0){ if(DataBus.Instance.shock == GameProp.shockType.on){ platform.vibrate(false).catch((e) => { console.log(e) }); } //this.ballContainer.swapChildren(this.lianjiEffect,this.balls_pool[this.startIndex]); this.lianjiEffect.text = "连击x" + (DataBus.Instance.scorePower + 1) + "\n +" + num *10 * Math.pow(2,DataBus.Instance.scorePower);; this.lianjiEffect.x = this.balls_pool[index].x; this.lianjiEffect.y = this.balls_pool[index].y - 20; this.lianjiEffect.scaleX = 0; this.lianjiEffect.scaleY = 0; egret.Tween.removeTweens(this.lianjiEffect); egret.Tween.get(this.lianjiEffect).to({scaleX:1.2,scaleY:1.2},200).wait(500).to({scaleX:0,scaleY:0},100); }else{ this.ballContainer.swapChildren(this.lianjiEffect,this.balls_pool[this.startIndex]); this.lianjiEffect.text = " " + "\n+" + num *10 * Math.pow(2,DataBus.Instance.scorePower);; this.lianjiEffect.x = this.balls_pool[index].x; this.lianjiEffect.y = this.balls_pool[index].y - 20; this.lianjiEffect.scaleX = 0; this.lianjiEffect.scaleY = 0; egret.Tween.removeTweens(this.lianjiEffect); egret.Tween.get(this.lianjiEffect).to({scaleX:1.2,scaleY:1.2},200).wait(500).to({scaleX:0,scaleY:0},100); } } //更新游戏分数,并根据分数调整游戏难度 public updateScore(num:number){ DataBus.Instance.score += num *10 * Math.pow(2,DataBus.Instance.scorePower); DataBus.Instance.gradeScore += num *10 * Math.pow(2,DataBus.Instance.scorePower); DataBus.Instance.ballNum += num; for(let i = this.levelConfig[this.levelIndex].score.length - 1; i > 0;i--){ if(DataBus.Instance.score > this.levelConfig[this.levelIndex].score[i]){ DataBus.Instance.nanduIndex = i; if(this.inited){ this.speed = this.levelConfig[this.levelIndex].speed[i]; }break; } } this.exper.mask = new egret.Rectangle(0,0,DataBus.Instance.gradeScore/DataBus.Instance.gradeNeedScore*this.stage.width,DataBus.Instance.gradeScore/DataBus.Instance.gradeNeedScore*this.stage.width); this.score.text = '' + DataBus.Instance.score; this.gradeScore.text = DataBus.Instance.gradeScore + '/' + DataBus.Instance.gradeNeedScore; this.score.anchorOffsetX = this.score.width / 2; egret.Tween.get(this.score).to({scaleX:1.2,scaleY:1.2},100).to({scaleX:1,scaleY:1},100); if(DataBus.Instance.gradeScore >= DataBus.Instance.gradeNeedScore && !this.backTimer.running){ //通关 this.end = true; this.timer.stop(); this.backTimer.stop(); this.insertTimer.stop(); this.pass = true; //this.passThrough(); } } public passThrough(){ /*for(let i = 1;i < this.startIndex - 1;i = i + 3){ setTimeout(()=>{ this.boomMC.x = this.balls_pool[1].x; this.boomMC.y = this.balls_pool[1].y; //this.ballContainer.swapChildren(this.boomMC,this.balls_pool[this.startIndex - 1]); this.boomMC.visible = true; this.boomMC.gotoAndPlay(1,1); },this,1000); }*/ this.addChild(this.throughContainer); DataBus.Instance.grade++; //等级缓存到本地 //egret.localStorage.setItem("grade",DataBus.Instance.grade + ''); let blackBg = new egret.Bitmap(RES.getRes('bg_black1_png')); blackBg.x = 0; blackBg.y = 0; blackBg.width = this.stage.stageWidth; blackBg.height = this.stage.stageHeight; this.throughContainer.addChild(blackBg); blackBg.touchEnabled = true; blackBg.addEventListener(egret.TouchEvent.TOUCH_TAP, (event: egret.TouchEvent) => { event.stopImmediatePropagation(); this.throughContainer.removeChildren(); this.removeChild(this.throughContainer); this.restart(-1); }, this); let through = new egret.Bitmap(RES.getRes('through_png')); through.width = 640; through.height = 841; through.x = this.stage.stageWidth; through.y = 64; this.throughContainer.addChild(through); egret.Tween.get(through).to({x:0},1000,egret.Ease.bounceOut); let ball = new egret.TextField(); ball.text = '+' + DataBus.Instance.ballNum; ball.y = this.stage.stageHeight; ball.size=25; ball.bold = true; ball.textColor=0x422904; ball.x = this.stage.stageWidth / 2 - 300 + 346; ball.fontFamily="Arial"; this.throughContainer.addChild(ball); egret.Tween.get(ball).to({y:518},1200,egret.Ease.bounceInOut); let lianji = new egret.TextField(); lianji.text = '+' + DataBus.Instance.lianjiNum * 10; lianji.y = this.stage.stageHeight; lianji.size=25; lianji.bold = true; lianji.textColor=0x422904; lianji.x = this.stage.stageWidth / 2 - 300 + 346 lianji.fontFamily="Arial"; this.throughContainer.addChild(lianji); egret.Tween.get(lianji).to({y:579},1400,egret.Ease.bounceInOut); let crazy = new egret.TextField(); crazy.text = '+' + DataBus.Instance.lianjiBigTwoNum * 100; crazy.y = this.stage.stageHeight; crazy.size=25; crazy.bold = true; crazy.textColor=0x422904; crazy.x = this.stage.stageWidth / 2 - 300 + 346; crazy.fontFamily="Arial"; this.throughContainer.addChild(crazy); egret.Tween.get(crazy).to({y:645},1600,egret.Ease.bounceInOut); let juli = new egret.TextField(); juli.text = '+' + (this.mapConfig.crux.length - this.balls_pool[0].cruxIndex) * 10; juli.y = this.stage.stageHeight; juli.size=25; juli.bold = true; juli.textColor=0x422904; juli.x = this.stage.stageWidth / 2 - 300 + 346; juli.fontFamily="Arial"; this.throughContainer.addChild(juli); egret.Tween.get(juli).to({y:709},1800,egret.Ease.bounceInOut); let zong = new egret.TextField(); zong.text = '+' + (DataBus.Instance.ballNum + DataBus.Instance.lianjiNum * 10 + DataBus.Instance.lianjiBigTwoNum * 100 + (this.mapConfig.crux.length - this.balls_pool[0].cruxIndex) * 10); zong.y = this.stage.stageHeight; zong.size=30; zong.bold = true; zong.textColor=0xf9372d; zong.x = this.stage.stageWidth / 2 - 300 + 346; zong.fontFamily="Arial"; this.throughContainer.addChild(zong); egret.Tween.get(zong).to({y:785},2000,egret.Ease.bounceInOut); let jixu = new egret.TextField(); jixu.text = '点击任意位置继续'; jixu.y = 900; jixu.size=25; jixu.textColor=0xffffff; jixu.anchorOffsetX = jixu.width / 2; jixu.anchorOffsetY = jixu.height / 2; jixu.scaleX = 1; jixu.scaleY = 1; jixu.x = this.stage.stageWidth / 2; jixu.fontFamily="Arial"; this.throughContainer.addChild(jixu); egret.Tween.get(jixu,{loop:true}).to({scaleX:1.1,scaleY:1.1},1000).to({scaleX:1.0,scaleY:1.0},1000); DataBus.Instance.score += DataBus.Instance.ballNum + DataBus.Instance.lianjiNum * 10 + DataBus.Instance.lianjiBigTwoNum * 100 + (this.mapConfig.crux.length - this.balls_pool[0].cruxIndex) * 10; } //计算玩家的球与目标球的距离 public diffPoint(x,y,x1,y1){ let difX = x - x1; let difY = y - y1; //如果两点相同,则返回1进行误差处理 if(difX == 0 && difY == 0){ return 1; } return Math.sqrt(Math.pow(difX, 2) + Math.pow(difY, 2)); } private enterFrame(event: egret.TouchEvent) { DataBus.Instance.frame++; this.playNumEnterFrame++; if(this.pass){ this.passFrame++; if(this.passIndex > this.balls_pool.length - 2){ this.pass = false; this.passIndex = 2; for(let i = 0; i < this.balls_pool.length - 1;i++){ this.balls_pool[i].visible = false; } this.passThrough(); return; } if(this.passFrame > 10){ this.boomByIndex(this.passIndex); for(let i = 0; i < this.passIndex;i++){ this.balls_pool[i].visible = false; } //this.balls_pool.splice(0,5); this.passIndex += 5; this.passFrame = 0; } } if(this.end){ return ; } //console.log(DataBus.Instance.frame); //结束特效 /*if (DataBus.Instance.frame >= 10){ DataBus.Instance.frame = 0; if(this.balls_pool[0].cruxIndex == this.mapConfig.crux.length && this.balls_pool[0].x < 400){ this.blast.visible = !this.blast.visible; this.mapend.visible = !this.mapend.visible; }else{ this.blast.visible = false; this.mapend.visible = false; } }*/ if(this.haveDot){ this.dotContainer.getChildAt(0).visible = true; let sinValue = this.sinValue; let cosValue = this.cosValue; let i:number; for(i = 0;i < this.dotLength - 1;i++){ this.dotContainer.getChildAt(i+1).x = this.dotContainer.getChildAt(i).x + this.playerBall.width / 3 * sinValue * 2; this.dotContainer.getChildAt(i+1).y = this.dotContainer.getChildAt(i).y + this.playerBall.width / 3 * cosValue * 2; this.dotContainer.getChildAt(i+1).visible = true; let j:number; for(j = 0; j < this.balls_pool.length;j++){ if(this.diffPoint(this.dotContainer.getChildAt(i+1).x,this.dotContainer.getChildAt(i+1).y,this.balls_pool[j].x,this.balls_pool[j].y) < this.playerBall.width / 2 || this.dotContainer.getChildAt(i+1).x < 0 || this.dotContainer.getChildAt(i+1).x > this.ballContainer.width || this.dotContainer.getChildAt(i+1).y < 0 || this.dotContainer.getChildAt(i+1).y > this.ballContainer.height){ this.dotContainer.getChildAt(i+1).visible = false; break; } } if(j != this.balls_pool.length){ break; } /*if( this.dotContainer.getChildAt(i+1).x < 0 ){ sinValue = -this.sinValue; this.dotContainer.getChildAt(i+1).x = -this.dotContainer.getChildAt(i+1).x; }else if(this.dotContainer.getChildAt(i+1).x > this.ballContainer.width ){ sinValue = -this.sinValue; this.dotContainer.getChildAt(i+1).x =this.ballContainer.width * 2 - this.dotContainer.getChildAt(i+1).x; } //边界处理 if(this.dotContainer.getChildAt(i+1).y < 0 ){ cosValue = -this.cosValue; this.dotContainer.getChildAt(i+1).y = -this.dotContainer.getChildAt(i+1).y; }else if(this.dotContainer.getChildAt(i+1).y > this.ballContainer.height){ cosValue = -this.cosValue; this.dotContainer.getChildAt(i+1).y = this.ballContainer.height * 2 - this.dotContainer.getChildAt(i+1).y; }*/ } //如果前面的点与球有碰撞,则后面都不显示 for(i;i < this.dotLength - 1;i++){ this.dotContainer.getChildAt(i+1).visible = false; } }else{ if(this.dotContainer.getChildAt(0).visible == true){ for(let i = 0;i < this.dotLength ;i++){ this.dotContainer.getChildAt(i).visible = false; } } } //if(this.isAttack){ for(let i = 0;i < this.fireBalls.length; i++){ let playerBall = this.fireBalls[i]; this.canPlay = false; playerBall.x += playerBall.speed * playerBall.sinValue; playerBall.y += playerBall.speed * playerBall.cosValue; //this.playerBall.speed = this.playerBall.speed + 0.1; //边界处理 if(playerBall.x < 0 || playerBall.x > this.ballContainer.width){ //this.sinValue = -this.sinValue; this.ballContainer.removeChild(playerBall); this.fireBalls.splice(i,1); this.canPlay = true; this.isAttack = false; break; } //边界处理 if(playerBall.y < 0 || playerBall.y > this.ballContainer.height){ //this.cosValue = -this.cosValue; this.ballContainer.removeChild(playerBall); this.fireBalls.splice(i,1); this.canPlay = true; this.isAttack = false; break; } this.checkAttack(i); } // } } public shakeBg(time = 0.1, rate = 60, distance = 2) { ShakeTool.getInstance().shakeObj(BackGround.Instance, time, rate, distance); } } class BgEffect extends egret.Sprite { private static shapes: Array; private static lines: Array; private shape: egret.Bitmap; public constructor() { super(); this.once(egret.Event.ADDED_TO_STAGE, () => { BgEffect.shapes = new Array(); BgEffect.lines = new Array(); for (let i = 1; i < 7; i++) { BgEffect.shapes.push(RES.getRes('eff' + i + '_png')); BgEffect.lines.push(RES.getRes('eff' + i + '_' + i + '_png')); } let rnd = ~~(Math.random() * BgEffect.shapes.length); this.alpha = 0; this.shape = new egret.Bitmap(BgEffect.shapes[rnd]); this.shape.x = 140; this.shape.y = 140; this.anchorOffsetX = this.shape.anchorOffsetX = 140; this.anchorOffsetY = this.shape.anchorOffsetY = 140; this.addChild(this.shape); for (let i = 0; i < 8; i++) { let angle = Math.random() * Math.PI * 2; let line = new LineSprite(BgEffect.lines[rnd]); line.x = 140; line.y = 140; line.speedx = Math.cos(angle) * 8 * this.scaleX; line.speedy = Math.sin(angle) * 8 * this.scaleX; line.rotation = angle / Math.PI * 180; this.addChild(line); } }, this); } } class LineSprite extends egret.Bitmap { speedx: number; speedy: number; update() { this.x += this.speedx; this.y += this.speedy; this.scaleX -= 0.03; } } /** * 震动工具 * @author chenkai * [url=home.php?mod=space&uid=81950]@since[/url] 2017/5/24 * * Example: * 震动目标obj,1秒内震动10次,震动最大距离10 * ShakeTool.getInstance().shakeObj(obj, 1, 10, 10); */ class ShakeTool { private static instance: ShakeTool; //单例 private initX: number; //初始位置 private initY: number; private target: egret.DisplayObject; //震动目标 private maxDis: number; //震动距离 private count: number = 0; //计时器次数 private rate: number; //一秒震动次数 private timer: egret.Timer = new egret.Timer(200); public static getInstance(): ShakeTool { if (this.instance == null) { this.instance = new ShakeTool(); } return this.instance; } /** * 震动显示对象 * @param target 震动目标对象 * @param time 震动持续时长(秒) * @param rate 震动频率(一秒震动多少次) * @param maxDis 震动最大距离 */ public shakeObj(target: egret.DisplayObject, time: number, rate: number, maxDis: number): void { if (this.target) { return; } this.target = target; this.initX = target.x; this.initY = target.y; this.maxDis = maxDis; this.count = time * rate; this.rate = rate; this.timer.delay = 200 / rate; this.timer.repeatCount = this.count; this.timer.addEventListener(egret.TimerEvent.TIMER, this.shaking, this); this.timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE, this.shakeComplete, this); this.timer.reset(); this.timer.start(); } private shaking(): void { egret.Tween.removeTweens(this.target); this.target.x = this.initX - this.maxDis + Math.random() * this.maxDis * 2; this.target.y = this.initY - this.maxDis + Math.random() * this.maxDis * 2; egret.Tween.get(this.target).to({ x: this.initX, y: this.initY }, 999 / this.rate); } private shakeComplete(): void { if (this.target) { egret.Tween.removeTweens(this.target); this.target.x = this.initX; this.target.y = this.initY; this.target = null; } this.timer.removeEventListener(egret.TimerEvent.TIMER, this.shaking, this); this.timer.removeEventListener(egret.TimerEvent.TIMER_COMPLETE, this.shakeComplete, this); } /**停止震动 */ public stop() { this.shakeComplete(); } }