window.utils = {

    //04月25日，单位改为万公里了
    StarDistnaces: [
        //"月球",
        15210,
        //"火星",
        22794,
        //"木星",
        77833,
        //"土星",
        142700,
        //"天王星",
        287099,
        //"海王星",
        450400,
        //"黑洞"
        591352,
        //"飞出太阳系"
        600000,
        //飞出银河系
        24598080000000],

    //判断是手机
    isMobile: function (callback) {
        if (cc.sys.platform == cc.sys.MOBILE_BROWSER) {
            if (callback != null) {
                callback();
            }
        }
    },
    //获得一个 ?aa=aa.jpg 后缀的网络图片地址
    getNewHttpImage(avatarUrl){
        var str = "?aa=aa.jpg";
        var temp = avatarUrl;
        if (avatarUrl.indexOf(str) == -1) {
            temp +=str
            return temp
        }
        return avatarUrl;
    },

    //判断是手机
    isWeb: function (callback) {
        if (cc.sys.platform == cc.sys.DESKTOP_BROWSER) {
            if (callback != null) {
                callback();
            }
        }
    },
    //加载网络图片
    loadHttpSpriteFrame: function (avatarUrl, sprite) {
        cc.loader.load(this.getNewHttpImage(avatarUrl), function (err, texture) {
            if (sprite.node!=null) {
                sprite.spriteFrame = new cc.SpriteFrame(texture);
            }
        });
    },
    //跳转到某个场景名字
    toLoadScene: function (SceneName, callback) {
        cc.director.loadScene(SceneName, callback);
    },
    //准备去某个场景
    redytoLoadScene: function (SceneName) {
        cc.director.preloadScene(SceneName);
    },
    //加载 resources 文件夹下面的 图片
    loadResSpriteFrame: function (src, callback) {
        cc.loader.loadRes(src, cc.SpriteFrame, function (err, texture) {
            if (callback != null) {
                callback(texture);
            }
        }.bind(this));

    },


    dragonBonesPlay: function (node, name, playTimes) {
        let armature = node.getComponent(dragonBones.ArmatureDisplay);
        armature.playAnimation(name, playTimes);
    },
    dragonBonesGet: function (node) {
        let armature = node.getComponent(dragonBones.ArmatureDisplay);
        // console.log("是啥",armature.getAnimationNames(armature.armatureName));
        return armature.getAnimationNames(armature.armatureName)
    },

    getShareData: function (callback) {
        // let data = {
        //     der: der,
        //     pianyi: pianyi,
        //     power: power,
        //     ballSpeed: ballSpeed,
        //     kitTime: kitTime,
        //     ATime: ATime
        // }

        //击打三拳的数据
        let everypowerdata = UserInfo.PlayerGameData.everypowerdata;
        // let everypowerdata = Statistics.player1;

        //打击的力量

        //    力量
        let everypowers = [];
        //    准确度
        let types = [];
        //   拳速
        let speend = [];

        let missTimes = [];
        let hitTimes = [];

        for (var i = 0; i < everypowerdata.length; i++) {
            let tempdata = everypowerdata[i];
            //拳速
            speend.push(tempdata.ballSpeed);

            everypowers.push(tempdata.power);
            // console.log("流量值type", tempdata.type);
            types.push(tempdata.pianyi);

            // if (!tempdata.isOK) {
            //     missTimes.push(tempdata.type);
            // } else {
            //     hitTimes.push(tempdata.type);
            // }
            hitTimes.push(tempdata.type);
        }
        //    流浪值
        let power = parseInt(UserInfo.PlayerGameData.power);


        let types_average = 0;
        for (var i = 0; i < types.length; i++) {
            types_average += types[i];
        }
        console.log("准确率", (types_average / types.length));
        console.log("流量值", power);

        //最大连击
        let maxCombo = Statistics.player1.continuitymax;
        // for (var i = 0; i < types.length; i++) {
        //     if (i == 0) {
        //         maxCombo++;
        //     } else {
        //         if (types[i - 1] == 1 && types[i] == 1) {
        //             maxCombo++;
        //         } else {
        //             maxCombo = 0;
        //         }
        //     }
        // }
        console.log("现在最高练级11111" + maxCombo);

        let speend_average = 0;
        for (var i = 0; i < speend.length; i++) {
            speend_average += speend[i];
        }
        let speends = isNaN(Math.round(speend_average / speend.length))?0:Math.round(speend_average / speend.length)

        console.log("判断 平均速度" , speends);
        //最大拳速
        let speend_max = 0;
        let temp_speend = 0;
        for (var i = 0; i < speend.length; i++) {
            if (speend[i] > temp_speend) {
                temp_speend = speend[i];
            }
        }
        speend_max = temp_speend;

        let powers_max = 0;
        let temp_powers = 0;
        for (var i = 0; i < everypowers.length; i++) {
            if (everypowers[i] > temp_powers) {
                temp_powers = everypowers[i];
            }
        }
        powers_max = temp_powers;


        console.log("速度", (speend_average / speend.length));




        let everypowers_average = 0;
        for (var i = 0; i < everypowers.length; i++) {
            everypowers_average += everypowers[i];
        }
        console.log("力量", (everypowers_average / everypowers.length));
        let calorie = isNaN(Math.round(everypowers_average / everypowers.length))?0:Math.round(everypowers_average / everypowers.length)

        var distance = this.SetDistance(UserInfo.PlayerGameData.currtag, power);


        let types_averages = isNaN(parseInt((types_average / types.length)))?0:parseInt((types_average / types.length))
        console.log("判断 命中率" , types_averages);
        let callbackdata = {
            //平均速度
            speend_average: Math.round(speend_average / speend.length),
            //平均力量
            everypowers_average: calorie,
            //准确率
            types_average: types_averages,
            //流浪值
            power: power,
            //给app 的 数据
            setResult: {
                //总打击拳数
                times: Statistics.dishu.num,
                //给 android 的
                controlTimes : Statistics.dishu.num,
                //    miss拳数
                missTimes: missTimes.length,
                //     命中率
                hitRate: types_averages,
                //    命中拳数
                hitTimes: UserInfo.PlayerGameData.everypowerdataOK.length,
                //     游戏总时长
                duration: (new Date().getTime() - GameData.gameStartTime),
                //    游戏开始时间
                requestTime: GameData.gameStartTime,
                //    分数
                score: Statistics.player1.score,
                //    最大力量
                maxPower: powers_max,
                //     是否赢得游戏
                winGame: 1,
                //    最大球速
                maxSpeed: speend_max,
                //     平均球速
                avgSpeed: speends,
                //     最大连击
                maxCombo: maxCombo,
                //    光年距离(先是死的)
                alternative: distance,
                //      扩展数据(不要给)
                //     extraJson : ""
                // 卡路里    4.24添加
                calorie: calorie * 6,
                // int rankingType, 字段排名 0-score排名  1-alternative排名
                // int type   排名类型 1-好友 2-全国
                //便捷接口
                rankingType : 0,

                type : 2,
            }
        }

        if (callback != null) {
            callback(callbackdata);
        }
    },
    SetDistance: function (currtag, power) {
        var distance = 0;
        var distanceUnit = "万公里";
        var a = 0;
        //普通星球
        if (currtag >= 0 && currtag <= 6) {
            distance = this.StarDistnaces[currtag];
        }
        //太阳系
        else if (currtag == 99) {
            distance = this.StarDistnaces[7];
        }
        //银河系
        else if (currtag == 100) {
            distance = this.StarDistnaces[8];
        }
        a = power / 10000;
        a = Math.floor(a * 100) / 100;//保留两位小数点
        distance = distance + a;
        return distance;
    },
    //开始
    startAnimation(animation,name){
        animation.play(name)
    },
    setAnimationCallBack(animation,type,AnimationName,callback){
        // play : 开始播放时
        // stop : 停止播放时
        // pause : 暂停播放时
        // resume : 恢复播放时
        // lastframe : 假如动画循环次数大于 1，当动画播放到最后一帧时
        // finished : 动画播放完成时

        animation.on(type, function (event,state) {
            if (callback) {
                if (AnimationName == state.name && event ==type ) {
                    callback()

                    animation.off(type);
                }
            }

        },this);
    },


    getAnimations(animation){
        return animation.getClips();
    },
}
window.Log = {
    isShow : true,
    // isShow : false,
    info(){
        if (this.isShow) {
            console.log.apply(console,arguments);
        }

    }
}
window.qiu = {}

//以下为js缓动函数
window.Tween = {

    Linear: function (t, b, c, d) {
        return c * t / d + b;
    },
    Quad: {
        easeIn: function (t, b, c, d) {
            return c * (t /= d) * t + b;
        },
        easeOut: function (t, b, c, d) {
            return -c * (t /= d) * (t - 2) + b;
        },
        easeInOut: function (t, b, c, d) {
            if ((t /= d / 2) < 1) return c / 2 * t * t + b;
            return -c / 2 * ((--t) * (t - 2) - 1) + b;
        }
    },
    Cubic: {
        easeIn: function (t, b, c, d) {
            return c * (t /= d) * t * t + b;
        },
        easeOut: function (t, b, c, d) {
            return c * ((t = t / d - 1) * t * t + 1) + b;
        },
        easeInOut: function (t, b, c, d) {
            if ((t /= d / 2) < 1) return c / 2 * t * t * t + b;
            return c / 2 * ((t -= 2) * t * t + 2) + b;
        }
    },
    Quart: {
        easeIn: function (t, b, c, d) {
            return c * (t /= d) * t * t * t + b;
        },
        easeOut: function (t, b, c, d) {
            return -c * ((t = t / d - 1) * t * t * t - 1) + b;
        },
        easeInOut: function (t, b, c, d) {
            if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;
            return -c / 2 * ((t -= 2) * t * t * t - 2) + b;
        }
    },
    Quint: {
        easeIn: function (t, b, c, d) {
            return c * (t /= d) * t * t * t * t + b;
        },
        easeOut: function (t, b, c, d) {
            return c * ((t = t / d - 1) * t * t * t * t + 1) + b;
        },
        easeInOut: function (t, b, c, d) {
            if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;
            return c / 2 * ((t -= 2) * t * t * t * t + 2) + b;
        }
    },
    Sine: {
        easeIn: function (t, b, c, d) {
            return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
        },
        easeOut: function (t, b, c, d) {
            return c * Math.sin(t / d * (Math.PI / 2)) + b;
        },
        easeInOut: function (t, b, c, d) {
            return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
        }
    },
    Expo: {
        easeIn: function (t, b, c, d) {
            return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
        },
        easeOut: function (t, b, c, d) {
            return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
        },
        easeInOut: function (t, b, c, d) {
            if (t == 0) return b;
            if (t == d) return b + c;
            if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
            return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
        }
    },
    Circ: {
        easeIn: function (t, b, c, d) {
            return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;
        },
        easeOut: function (t, b, c, d) {
            return c * Math.sqrt(1 - (t = t / d - 1) * t) + b;
        },
        easeInOut: function (t, b, c, d) {
            if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;
            return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;
        }
    },
    Elastic: {
        easeIn: function (t, b, c, d, a, p) {
            if (t == 0) return b;
            if ((t /= d) == 1) return b + c;
            if (!p) p = d * .3;
            if (!a || a < Math.abs(c)) {
                a = c;
                var s = p / 4;
            } else var s = p / (2 * Math.PI) * Math.asin(c / a);
            return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
        },
        easeOut: function (t, b, c, d, a, p) {
            if (t == 0) return b;
            if ((t /= d) == 1) return b + c;
            if (!p) p = d * .3;
            if (!a || a < Math.abs(c)) {
                a = c;
                var s = p / 4;
            } else var s = p / (2 * Math.PI) * Math.asin(c / a);
            return (a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b);
        },
        easeInOut: function (t, b, c, d, a, p) {
            if (t == 0) return b;
            if ((t /= d / 2) == 2) return b + c;
            if (!p) p = d * (.3 * 1.5);
            if (!a || a < Math.abs(c)) {
                a = c;
                var s = p / 4;
            } else var s = p / (2 * Math.PI) * Math.asin(c / a);
            if (t < 1) return -.5 * (a * Math.pow(2, 10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p)) + b;
            return a * Math.pow(2, -10 * (t -= 1)) * Math.sin((t * d - s) * (2 * Math.PI) / p) * .5 + c + b;
        }
    },
    Back: {
        easeIn: function (t, b, c, d, s) {
            if (s == undefined) s = 1.70158;
            return c * (t /= d) * t * ((s + 1) * t - s) + b;
        },
        easeOut: function (t, b, c, d, s) {
            if (s == undefined) s = 1.70158;
            return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
        },
        easeInOut: function (t, b, c, d, s) {
            if (s == undefined) s = 1.70158;
            if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
            return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
        }
    },
    Bounce: {
        easeIn: function (t, b, c, d) {
            return c - Tween.Bounce.easeOut(d - t, 0, c, d) + b;
        },
        easeOut: function (t, b, c, d) {
            if ((t /= d) < (1 / 2.75)) {
                return c * (7.5625 * t * t) + b;
            } else if (t < (2 / 2.75)) {
                return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;
            } else if (t < (2.5 / 2.75)) {
                return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;
            } else {
                return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;
            }
        },
        easeInOut: function (t, b, c, d) {
            if (t < d / 2) return Tween.Bounce.easeIn(t * 2, 0, c, d) * .5 + b;
            else return Tween.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;
        }
    }
}
