// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        jumpCurrentData: 0,//当前跳远的值
        jumpCurrentTime: 0,//当前跳远的时间

        tagDistance_Hero: false,
        // tagDistance_Rivel: false,

        isCanUpdate: false,//是否跳出更新

        //0 跳远 1 标枪
        type: 0,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.GameUI = this.getComponent("GameUI");

        // this.GameUI.setDistanceNum(22.5);
        // this.updateNum_Rivel = 0;
        // this.updateNum_Hero = 0;
        // this.layoutcount.active = false;

        this.offsets = [
            240, 72
        ];
    },

    // start: function () {
      

    // },
    startDistance_Hero: function (type, Hero, b) {
        this.type = type;
        this.Hero = Hero;
        // this.tagDistance_Hero = b;
        var p = Hero.getPosition();
        this.isCanUpdate = b;
        var duration = 1000;

        if (b)
        {
            
            this.setUIActiveShowUIDistance();

            if (type === 0) {//0是跳远
                // console.log("位置", Hero.getComponent("PlayerState").EndDistance);
                this.defaultX_Hero = Hero.getComponent("PlayerState").EndDistance;
                this.jumpCurrentTime = Hero.getComponent("PlayerState").JumpTimer;

                duration = this.jumpCurrentTime * 1000 + 1500;
            }
            if (type === 1) {
                this.javeTime = Hero.javeTime;
                this.defaultX_Hero = p.x;
                duration = this.javeTime * 1000 + 1500;
            }
            setTimeout(function () {
                if (this.callBack_Hero != null) {

                    this.callBack_Hero(this.Label_Hero);
                }
                if (this.defaultX_Hero)
                    this.defaultX_Hero = null;

                this.isCanUpdate = false;
                this.GameUI.setUIActiveTimer_destroy(cc.find('UIDistance'), 0.1);
            }.bind(this), duration);
            // setTimeout(function () {
            //     if (this.defaultX_Hero)
            //         this.defaultX_Hero = null;

            //     this.isCanUpdate = false;
            // }.bind(this), duration);
        }
    },

    setUIActiveShowUIDistance: function () {
        // this.GameUI.setUIActiveShow(cc.find('UIDistance'));
        // console.log("setUIActiveShowUIDistance");
        this.GameUI.displayUIDistance();
    },

    callBack_Rivel: function () {

    },
    /**
     * 谁想知道结果 就谁调用这个方法 获取距离的方法
     * @param callBack
     */
    setCallBack_Rivel: function (callBack) {
        this.callBack_Rivel = callBack;
    },

    callBack_Hero: function () {

    },
    /**
     * 谁想知道结果 就谁调用这个方法 获取距离的方法
     * @param callBack
     */
    setCallBack_Hero: function (callBack) {
        this.callBack_Hero = callBack;
    },
    update: function (dt) {
        if (!this.isCanUpdate)
            return;

        // if (this.tagDistance_Hero) {
        var p = this.Hero.getPosition();

        if (this.type === 1 && this.defaultX_Hero != null) {
            this.calculationX_Hero = p.x - this.defaultX_Hero;
            var showX = this.calculationX_Hero / this.offsets[this.type];
            // var Label = this.count.getComponent("cc.Label");
            var Label = this.Label_Hero = showX.toFixed(2);
            this.GameUI.setDistanceNum(Label);
            // CustomLog("记录位置",this.calculationX_Hero);
            CustomLog("标枪移动 X", Label);
        }
        // this.updateNum_Hero++;
        //跳远时候
        if (this.type === 0 && this.defaultX_Hero != null) {
            // var p = this.Hero.getPosition();
            this.jumpCurrentData += dt * 4;
            this.jumpCurrentData = this.jumpCurrentData > this.defaultX_Hero ? this.defaultX_Hero : this.jumpCurrentData;
            var Label = this.Label_Hero = this.jumpCurrentData.toFixed(2);
            this.GameUI.setDistanceNum(Label);
            // console.log(this.Label_Hero);
        }
        // }
        // if (this.tagDistance_Rivel) {
        //     var p = this.Rivel.getPosition();
        //     // CustomLog("记录位置",p.x);
        //     if (this.defaultX_Rivel != null) {
        //         this.calculationX_Rivel = p.x - this.defaultX_Rivel;
        //         var showX = this.calculationX_Rivel / this.offsets[this.type];
        //         // var Label = this.count.getComponent("cc.Label");
        //         var Label = this.Label_Rivel = showX.toFixed(2);
        //         // this.GameUI.setDistanceNum(Label);
        //         // CustomLog("偏移量 X",showX);
        //     }
        //     if (this.updateNum_Rivel == 0) {
        //         // CustomLog("记录位置 开始 X",p.x);
        //     }
        //     this.updateNum_Rivel++;
        //     // console.log(333);
        // }
    },

    setImage_Player1: function (src) {
        this.GameUI.setImage_Player1(src);
    },
    setImage_Player2: function (src) {
        this.GameUI.setImage_Player2(src);
    },
    setName_Player1: function (name) {
        this.GameUI.setName_Player1(name);
    },
    setName_Player2: function (name) {
        this.GameUI.setName_Player2(name);
    },
    setName_Gender1: function (name) {
        this.GameUI.setName_Gender1(name);
    },
    setName_Gender2: function (name) {
        this.GameUI.setName_Gender2(name);
    },


    // update (dt) {},
});
