// 主游戏控制类
const {ccclass, property} = cc._decorator;
@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;
    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();
    }
    //初始化数据
    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(cc.fx.GameConfig.GM_INFO.custom != 0){
            this.level = cc.fx.GameConfig.GM_INFO.custom;
            // this.levelLabel.string = this.level + "";
            cc.fx.GameConfig.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 = [];
        cc.fx.GameConfig.GAME_DATA = [];
        cc.fx.GameConfig.GM_INFO.success = false;
        this.startTime = 0;
        this.overTime = 0;
        this.winTime = 0;
        cc.fx.GameConfig.GM_INFO.total = 0;
        cc.fx.GameConfig.GM_INFO.mean_Time = 0;
        this.schedule(this.updateCountDownTime,1);
        this.startGame();
    }
    //根据是否全面屏，做独立适配方面
    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;
    }
    //返回首页
    backScene(){
        cc.director.loadScene("LoadScene");
    }
    //下一关，或者重新开始，或者返回上一关，根据level决定
    reStart(type){
        // if(cc.fx.GameConfig.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);
        }
    
    }
    //获取时间戳
    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.calculateDistance(this.ball_Array[i].x,this.ball_Array[i].y,
                    this.ball_Array[i-1].x,this.ball_Array[i-1].y,2);
                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 =  cc.fx.GameTool.getTime();
            this.time_Array.push((clickTime - this.timeStart))
            this.timeStart =  cc.fx.GameTool.getTime();
            if(data == this.clickNow){
                cc.fx.AudioManager._instance.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;
                if(this.ball_Array[this.clickNow-1])
                    this.ball_Array[this.clickNow-1].getChildByName("guang").active = false;
                if(this.clickNow == this.now_Array.length){
                    this.pause = true;
                    cc.fx.GameConfig.GM_INFO.total += this.clickNow;
                    this.overTime =  cc.fx.GameTool.getTime();
                    if(this.startTime > 0 && this.overTime > 0){
                        cc.fx.GameConfig.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";
                    let win = this.node.getChildByName("Win");
                    win.active = true; win.setScale(0);
                    cc.fx.AudioManager._instance.playWin();
                    this.round += 1;
                    let sunArray = [];
                    if(this.moveArray.length > 0){
                        for(let m=0 ; m<this.moveArray.length;m++){
                            if(m == this.repeat || m == this.numberRepeat){
                                this.moveArray[m] = 2;
                            }
                            sunArray.push(this.moveArray[m]);
                        }
                    }
                    else{
                        for(let m=0 ; m<this.ball_Array.length;m++){
                            if(m == this.repeat || m == this.numberRepeat){
                                sunArray.push(2);
                            }
                            else
                                sunArray.push(0);
                        }
                    }
                    cc.fx.GameConfig.CLICK_DATA = 
                        {
                            type:1,
                            success:true,
                            round:this.round,
                            totalSunCount:this.now_Array.length,
                            movedSunCount:cc.fx.GameConfig.LEVEL_INFO[this.level-1].moveNumber,
                            sunSpeed:cc.fx.GameConfig.LEVEL_INFO[this.level-1].moveSpeed,
                            overlapSunCount:cc.fx.GameConfig.LEVEL_INFO[this.level-1].repeat,
                            colorList:this.color_Array,
                            duration:finish_time/10,
                            difficultyLevel:this.level,
                            sunList:sunArray,
                            stepTimeList:this.time_Array,
                            remainder:this.countTime
                        }
                    if(cc.fx.GameConfig.CLICK_DATA.duration > 50){
                        cc.fx.GameConfig.CLICK_DATA.duration = 50;
                    }
                    this.setData();
                    cc.tween(win)
                    .delay(0.4)
                    .to(0.01,{scale:2.5})
                    .to(0.35,{scale:1})
                    .delay(0.4)
                    .call(() =>{
                        win.active = false;
                        this.destroyLine();
                        if(this.over == false && this.countTime >= 0){
                            if(this.level >= 15){
                                cc.fx.GameConfig.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();
            }
        }
    }
    //执行失败
    setLoss(){
        cc.fx.AudioManager._instance.playWarning();
        this.begin = false;
        this.overTime =  cc.fx.GameTool.getTime();
        if(this.startTime > 0 && this.overTime > 0){
            cc.fx.GameConfig.GM_INFO.mean_Time += (this.overTime - this.startTime);
        }
        let finish_time = parseInt((this.overTime - this.startTime)/100+"");
        if(finish_time > 58) finish_time = 58;
        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<this.moveArray.length;m++){
                if(m == this.repeat || m == this.numberRepeat){
                    this.moveArray[m] = 2;
                }
                sunArray.push(this.moveArray[m]);
            }
        }
        else{
            for(let m=0 ; m<this.ball_Array.length;m++){
                if(m == this.repeat || m == this.numberRepeat){
                    sunArray.push(2);
                }
                else
                    sunArray.push(0);
            }
        }
        cc.fx.GameConfig.CLICK_DATA = 
        {
            type:1,
            success:false,
            round:this.round,
            totalSunCount:this.now_Array.length,
            movedSunCount:cc.fx.GameConfig.LEVEL_INFO[this.level-1].moveNumber,
            sunSpeed:cc.fx.GameConfig.LEVEL_INFO[this.level-1].moveSpeed,
            overlapSunCount:cc.fx.GameConfig.LEVEL_INFO[this.level-1].repeat,
            colorList:this.color_Array,
            duration:finish_time/10,
            difficultyLevel:this.level,
            sunList:sunArray,
            stepTimeList:this.time_Array,
            remainder:this.countTime
        }
        if(cc.fx.GameConfig.CLICK_DATA.duration > 50){
            cc.fx.GameConfig.CLICK_DATA.duration = 50;
        }
        this.setData();
        if(this.fuhuo == true){
            this.fuhuo = false;
            this.loseLevel(1);
        } 
        else{
            if(this.level > 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();
    }
    //开始游戏
    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();
        }
    }
    //执行太阳运行动画，以及后面移动动画
    runGame(){
        let time = -650;
        for(let i=0; i<this.now_Array.length; i++){
            time = this.now_Array[i].time*1000 + time;
            setTimeout(() => {
                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(cc.fx.GameConfig.LEVEL_INFO[this.level-1].moveNumber<1){
                                    this.pause = false;
                                }
                                else{
                                    this.moveAction();
                                }
    
                            }, 150);
                        }
                    })
                    .start()
                }
            }, time);
        }
    }
    //设置移动重叠数据
    createMove(){
        if(this.over == false){
            let info = cc.fx.GameConfig.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<info.number; z++){
                    this.moveArray[z] = 0;
                }
                for(let p=0; p<info.moveNumber; p++){
                    this.moveArray[info.number-1-p] = 1;
                }
                let timeOut = 0;
                //打乱可移动顺序
                this.moveArray.sort(() => 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;
                        }
                    }
                }
                //将重叠的设置为不可移动  1 1 1 1 0 0 1 0 0 0   / 2 7
                for(let k =0; k<this.moveArray.length;k++){
                    if(this.moveArray[k]==1 && (k == this.repeat || k == this.numberRepeat)){
                        for(let m = this.moveArray.length-1; m >= 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<this.Pos_Array.length;i++){
                    if(this.moveArray [i] == 1){
                        timeOut += 1;
                        setTimeout(() => {
                            pos = this.getPos2(true,i);
                            this.Pos_Array[i] = pos;
                        }, timeOut*300);
                    }
                }

            }
        }
    }
    //移动后数据 重新刷一遍赋值
    updateArray(){
        for(let i=0; i<this.now_Array.length-1;i++){
            this.now_Array[i].x = this.Pos_Array[i].x;
            this.now_Array[i].y = this.Pos_Array[i].y;
        }
    }
    //太阳初始化后移动方法
    moveAction(){
        this.updateArray();
        let moveArray = this.moveArray;
        let info = cc.fx.GameConfig.LEVEL_INFO[this.level-1];
        //改变移动速度
        var speed = 7;
        if(info.moveSpeed >= 2){
            speed = 7 - info.moveSpeed/2;
        }  
        if(info.moveNumber > 0){
            //0 1 2 3 4 5 6 7 8 9
            //可移动区分
            //将重叠的设置为不可移动  1 1 1 1 0 0 1 0 0 0   / 2 7
            for(let k =0; k<this.moveArray.length;k++){
                if(this.moveArray[k]==1 && (k == this.repeat || k == this.numberRepeat)){
                    for(let m = this.moveArray.length-1; m >= 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<moveArray.length;j++){
                if(moveArray[j]==1){
                    lastNumber = j;
                }
            }
            for(let i =0; i<this.ball_Array.length;i++){
                if(moveArray[i] == 1){
                    if(i != lastNumber){    //如果不是最后一个移动的
                        let time = speed*this.calculateDistance(this.ball_Array[i].x,this.ball_Array[i].y,this.Pos_Array[i].x,this.Pos_Array[i].y,3);
                        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();
                    }
                    //如果是最后一个
                    else{
                        let time = speed*this.calculateDistance(this.ball_Array[i].x,this.ball_Array[i].y,this.Pos_Array[i].x,this.Pos_Array[i].y,3);
                        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 =  cc.fx.GameTool.getTime();
        this.timeStart =  cc.fx.GameTool.getTime();
        this.pause = false;
        // this.node.getChildByName("showBtn").active = true;
    }

    //创建太阳所需数据
    createSun(){
        let levelData = cc.fx.GameConfig.LEVEL_INFO[this.level-1];
        let tiemOut = 0;
        for(let l=0; l<levelData.number;l++){
            tiemOut += 1;
            setTimeout(() => {
                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);
        }
    }
    //创建太阳位置，颜色信息等数据
    createPos(){
        let data = {x:this.ball.x,y:this.ball.y,time:0,timeCount:0,color:0,speed:0,repeat:false};
        let levelData = cc.fx.GameConfig.LEVEL_INFO[this.level-1];
        //颜色区分
        let colorArray = [0,1];
        for(let j=0; j<levelData.number-2;j++){
            let colorTemp = parseInt(Math.random()*3+"");
            if(levelData.number < 6 && colorTemp == 2) colorTemp = 0;
            colorArray.push(colorTemp);
            colorArray.sort(() => 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<levelData.number;i++){
            let timeCount = this.timeCount + (0.6-this.calculateDistance(this.ball.x,this.ball.y,this.Pos_Array[i].x,this.Pos_Array[i].y,1));
            let time = this.calculateDistance(this.ball.x,this.ball.y,this.Pos_Array[i].x,this.Pos_Array[i].y,1)+timeCount;
            data = {x:this.Pos_Array[i].x,y:this.Pos_Array[i].y,time:time,timeCount:timeCount,color:colorArray[i],speed:levelData.moveSpeed,repeat:false};
            if(this.repeat == i){
                data.repeat = true;
            }
            else if(this.numberRepeat == i){
                data.repeat = true;
            }
            this.now_Array.push(data);
        }
        this.runGame();
    }
    //获取，N个太阳位置的方法，和POS2分开两个方法，防止递归溢出
    getPos(type,num){
        let posX = Math.random()*620 - 310;
        let posY = Math.random()*(this.fitTop-this.fitBot) + this.fitBot;
        let point = cc.v2(posX,posY);
        if(this.Pos_Array.length > 0){
            for(let i=0; i<this.Pos_Array.length;i++){
                let distance = false;
                let pd = false;
                if(i == num) pd = true;
                distance = this.getDistance(posX,posY,this.Pos_Array[i].x,this.Pos_Array[i].y,pd)
                if(distance == false){
                    i = 10000;
                    point = this.getPos(type,num);
                }
                if(distance == true && i == (this.Pos_Array.length-1)){
                    i = 10000;
                }
           }
        }
        return point;
    }
    //获取，N个太阳可移动位置的方法，和POS分开算防止递归溢出
    getPos2(type,num){
        let posX = Math.random()*620 - 310;
        let posY = Math.random()*(this.fitTop-this.fitBot) + this.fitBot;
        let point = cc.v2(posX,posY);
        if(this.Pos_Array.length > 0){
            for(let i=0; i<this.Pos_Array.length;i++){
                let distance = false;
                let pd = false;
                if(i == num) pd = true;
                distance = this.getDistance2(posX,posY,this.Pos_Array[i].x,this.Pos_Array[i].y,pd)
                if(distance == false){
                    i = 10000;
                    point = this.getPos2(type,num);
                }
                if(distance == true && i == (this.Pos_Array.length-1)){
                    i = 10000;
                }
           }
        }
        return point;
    }
    //获取两点之间距离，返回判断是否在一定范围内 将来提出到工具类
    getDistance(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){
                cc.fx.GameConfig
                if(distance < cc.fx.GameConfig.LEVEL_INFO[this.level-1].maxMove){
                    jg = true;
                } 
                else jg = false;
            }
            else{
                if(distance < cc.fx.GameConfig.LEVEL_INFO[this.level-1].maxMove){
                    jg = true;
                } 
                else jg = false;
            }
        }
        return jg;
    }
    //命中动画
    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();
    }
    //创造太阳
    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, num) {
        const dx = x2 - x1;
        const dy = y2 - y1;
        let time = 0;
        var distance = Math.sqrt(dx*dx + dy*dy);
        if(num == 1){
            if(distance > 800) distance = 800;
            time = distance/800 * 0.6;
        }
        else if(num == 2){
            if(distance > 1200) distance = 1200;
            time = distance/1200 * 1;
        }
        else if(num == 3){
            if(distance > 1200) distance = 1200;
            time = distance/1200 * 1.5;
        }

        return time;
    }
    //获取两点之间距离  返回距离判断
    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 < cc.fx.GameConfig.LEVEL_INFO[this.level-1].maxDistance) jg = true;
                else jg = false;
            }
            else{
                jg = true;
            }
        }
        return jg;
    }
    //背景移动
    bgMove(type){
        let random = Math.random()*300 + 300;
        let posX = Math.random()*600 - 300;
        let y1 = this.bg1.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();
        setTimeout(() => {
            if(this.over == false) this.startGame();
        }, time1*1500);
    }
    //如果是倒计时 调用此方法
    updateCountDownTime () {
        if (this.countTime > 0 && this.pause == false) {
            this.countTime -= 1;
            this.time.string =  cc.fx.GameTool.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;
                if(this.clickCount > 0) time = parseInt(cc.fx.GameConfig.GM_INFO.mean_Time/this.clickCount + "");
                cc.fx.GameConfig.GM_INFO.mean_Time = parseInt(time/100 + "");
                cc.fx.GameConfig.GM_INFO.mean_Time = cc.fx.GameConfig.GM_INFO.mean_Time /10;
                if(time > 10000) time = (Math.random()*10+5)*100;
                this.gameOver(time);
            }
        }
    }
    //上传每次操作数据
    setData(){
         cc.fx.GameTool.setGameData();
    }
    //上传排行榜数据
    gameOver(time){
         cc.fx.GameTool.setRank(time);
        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()
    }
    //作弊，测试用
    showNumber(){
        if(this.show == false) this.show = true;
        else if(this.show == true) this.show = false;
        for(let i=0; i<this.ball_Array.length; i++){
            this.ball_Array[i].getComponent("Sun").showNumber(this.show);
        }
    }
    onEnable () {
        cc.fx.Notifications.on("clickSun", this.clickSun, this);
    }
    onDisable () {
        cc.fx.Notifications.off("clickSun", this.clickSun);
    }
    update (dt) {
    }
}
