// 对手削球，从右侧到右侧

cc.Class({
    extends: cc.Component,

    properties: {
        
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.initValues();
        this.initNodes();
    },

    start () {
        this.rivalPeelLtoL();
    },

    // 初始化
    initValues:function () {
        // 配置绘制的数据
        // 从配置中心获取数据
        this.config = require ("gameconfig")
        // 配置绘制组件
        this.g = this.getComponent(cc.Graphics);

        // 记录下节点
        this.poses = new Array();
        // 记录是否需要撞击的效果
        this.showBump = false;
    },

    // 初始化子节点
    initNodes:function() {
        this.head = new cc.Node("");
        this.head.setPosition(cc.v2(0, 0));
        this.head.parent = this.node;
    },

    // 外部调用
    // 球的运动停止
    ballStop:function() {

    },

    // 我方接球，从左到左
    minePatLToL:function() {
        var beginPos = cc.v2(-103, -110);
        var dropPos = cc.v2(-64, 144);
        var endPos = cc.v2(-53, 313);

        // 将节点移动到指定位置
        this.head.setPosition(beginPos);

        var bezier_01 = [beginPos, cc.v2(-82, 20), dropPos];
        var bezier_02 = [dropPos, cc.v2(-68, 220), endPos];

        var move_01 = cc.bezierTo(0.5, bezier_01);
        var self = this;
        var move_01_end = cc.callFunc(function() {
            self.bump(dropPos);
        });
        var move_02 = cc.bezierTo(0.5, bezier_02);

        this.head.runAction(cc.sequence(move_01, move_01_end, move_02));
    },

    // 我方接球，从右到右
    minePatRToR:function() {
        var beginPos = cc.v2(52, -109);
        var dropPos = cc.v2(71, 141);
        var endPos = cc.v2(131, 309);

        // 将节点移动到指定位置
        this.head.setPosition(beginPos);

        var bezier_01 = [beginPos, cc.v2(61, 16), dropPos];
        var bezier_02 = [dropPos, cc.v2(40, 289), endPos];

        var move_01 = cc.bezierTo(0.5, bezier_01);
        var self = this;
        var move_01_end = cc.callFunc(function() {
            self.bump(dropPos);
        });
        var move_02 = cc.bezierTo(0.5, bezier_02);

        this.head.runAction(cc.sequence(move_01, move_01_end, move_02));
    },

    // 我方接球，从左到右
    minePatLToR:function() {
        var beginPos = cc.v2(-103, -76);
        var dropPos = cc.v2(47, 140);
        var endPos = cc.v2(221, 346);

        // 将节点移动到指定位置
        this.head.setPosition(beginPos);

        var bezier_01 = [beginPos, cc.v2(-52, 20), dropPos];
        var bezier_02 = [dropPos, cc.v2(22, 340), endPos];

        var move_01 = cc.bezierTo(0.5, bezier_01);
        var self = this;
        var move_01_end = cc.callFunc(function() {
            self.bump(dropPos);
        });
        var move_02 = cc.bezierTo(0.5, bezier_02);

        this.head.runAction(cc.sequence(move_01, move_01_end, move_02));
    },

    // 我方削球，从右到右
    minePeelRToR:function() {
        var beginPos = cc.v2(148, -176);
        var dropPos = cc.v2(79, 99);
        var endPos = cc.v2(104, 319);

        // 将节点移动到指定位置
        this.head.setPosition(beginPos);

        var bezier_01 = [beginPos, cc.v2(180, -20), dropPos];
        var bezier_02 = [dropPos, cc.v2(92, 210), endPos];

        var move_01 = cc.bezierTo(0.5, bezier_01);
        var self = this;
        var move_01_end = cc.callFunc(function() {
            self.bump(dropPos);
        });
        var move_02 = cc.bezierTo(0.5, bezier_02);

        this.head.runAction(cc.sequence(move_01, move_01_end, move_02));
    },

    // 对手发球
    rivalServer:function() {
        var serverBegin = cc.v2(166, 290);
        var beatPos = cc.v2(96, 274);
        var dropPos = cc.v2(28, 144);
        var jumpPos = cc.v2(-14, 21);
        var endPos = cc.v2(-147, -161);

        // 将节点移动到指定位置
        this.head.setPosition(serverBegin);

        var bezier_01 = [serverBegin, cc.v2(125, 400), beatPos];
        var bezier_02 = [beatPos, cc.v2(45, 250), dropPos];
        var bezier_03 = [dropPos, cc.v2(0, 56), jumpPos];
        var bezier_04 = [jumpPos, cc.v2(-80, 10), endPos];
        
        var move_01 = cc.bezierTo(0.5, bezier_01);
        var move_02 = cc.bezierTo(0.5, bezier_02);
        var move_03 = cc.bezierTo(0.5, bezier_03);
        var self = this;
        var move_03_end = cc.callFunc(function() {
            self.bump(jumpPos);
        });
        var move_04 = cc.bezierTo(0.5, bezier_04);
        this.head.runAction(cc.sequence(move_01, move_02, move_03, move_03_end, move_04));
    },

    // 对手接球，从右到左
    rivalPatRToL:function() {
        var beginPos = cc.v2(112, 328);
        var dropPos = cc.v2(-22, 16);
        var endPos = cc.v2(-122, -182);

        // 将节点移动到指定位置
        this.head.setPosition(beginPos);

        var bezier_01 = [beginPos, cc.v2(45, 172), dropPos];
        var bezier_02 = [dropPos, cc.v2(-120, -10), endPos];

        var move_01 = cc.bezierTo(0.5, bezier_01);
        var self = this;
        var move_01_end = cc.callFunc(function() {
            self.bump(dropPos);
        });
        var move_02 = cc.bezierTo(0.5, bezier_02);

        this.head.runAction(cc.sequence(move_01, move_01_end, move_02));
    },

    // 对手削球，从左到左
    rivalPeelLtoL:function() {
        var beginPos = cc.v2(-95, 263);
        var centerPos = cc.v2(-75, 58);
        var endPos = cc.v2(-66, -163);

        // 将节点移动到指定位置
        this.head.setPosition(beginPos);

        var bezier_01 = [beginPos, cc.v2(-230, 410), centerPos];
        var move_01 = cc.bezierTo(1, bezier_01);
        var move_01_end = cc.callFunc(function() {
            cc.log ("动作 1 结束 ... ");
            this.bump(centerPos);
        }, this);
        var bezier_02 = [centerPos, cc.v2(-125, 40), endPos];
        var move_02 = cc.bezierTo(1, bezier_02);
        var move_02_end = cc.callFunc(function() {
            cc.log ("动作 2 结束 ... ");
        }, this);

        this.head.runAction(cc.sequence(move_01, move_01_end, move_02, move_02_end));
    },

    // 球磕碰
    bump:function(loc) {
        this.bumpLoc = loc;
        this.bumpTime = 0;

        this.showBump = true;
    },

    // 绘图
    draw:function(delay) {
        this.g.clear();

        // 画脑袋和身体
        this.g.lineWidth = this.config.CIRCLE_WIDTH

        // 画个头部
        this.g.fillColor.fromHEX('#ff00000');
        this.g.circle(this.head.x, this.head.y, this.config.HEAD_SIZE);
        this.g.close();
        this.g.stroke();
        this.g.fill();
        
        var pos = this.poses[0];
        this.g.lineWidth = 3;
        this.g.moveTo(pos.x, pos.y)
        for (var i = 0; i < this.poses.length; i++) {
            var getPos = this.poses[i];
            this.g.lineTo(getPos.x, getPos.y);
        }

        this.g.stroke();

        // 当需要绘制撞击点时
        if (this.showBump) {

            this.bumpTime += delay;

            var timeScale = 15;
            var space = 1;
            var low = 2.5;
            var high = 10;

            // 右上
            this.g.lineWidth = 3;
            this.g.moveTo(this.bumpLoc.x + space + this.bumpTime * timeScale * low, this.bumpLoc.y + space + this.bumpTime * timeScale * low);
            this.g.lineTo(this.bumpLoc.x + space + this.bumpTime * timeScale * high, this.bumpLoc.y + space + this.bumpTime * timeScale * high)
            this.g.stroke();

            // 右下
            this.g.lineWidth = 3;
            this.g.moveTo(this.bumpLoc.x + space + this.bumpTime * timeScale * low, this.bumpLoc.y - space - this.bumpTime * timeScale * low);
            this.g.lineTo(this.bumpLoc.x + space + this.bumpTime * timeScale * high, this.bumpLoc.y - space - this.bumpTime * timeScale * high)
            this.g.stroke();

            // 左上
            this.g.lineWidth = 3;
            this.g.moveTo(this.bumpLoc.x - space - this.bumpTime * timeScale * low, this.bumpLoc.y + space + this.bumpTime * timeScale * low);
            this.g.lineTo(this.bumpLoc.x - space - this.bumpTime * timeScale * high, this.bumpLoc.y + space + this.bumpTime * timeScale * high)
            this.g.stroke();

            // 右下
            this.g.lineWidth = 3;
            this.g.moveTo(this.bumpLoc.x - space - this.bumpTime * timeScale * low, this.bumpLoc.y - space - this.bumpTime * timeScale * low);
            this.g.lineTo(this.bumpLoc.x - space - this.bumpTime * timeScale * high, this.bumpLoc.y - space - this.bumpTime * timeScale * high)
            this.g.stroke();

            if (this.bumpTime > 0.1) {
                this.showBump = false;
            }
        }
    },

    update (dt) {
        // 记录当前的位置
        var news = [this.head.position];
        // 在结合当前的数组
        this.poses = news.concat(this.poses);
        // 如果数组长度大于20，则将最后一个删除
        if (this.poses.length > 20) {
            this.poses.pop();
        }

        this.draw(dt);

        var beginPos = cc.v2(112, 328);
        var dropPos = cc.v2(-22, 16);
        var endPos = cc.v2(-122, -182);

        // 画手臂
        this.g.lineWidth = this.config.LINE_WIDTH;
        this.g.moveTo(beginPos.x, beginPos.y);
        this.g.bezierCurveTo(beginPos.x, beginPos.y, 45, 172, dropPos.x, dropPos.y);
        this.g.bezierCurveTo(dropPos.x, dropPos.y, -120, -10, endPos.x, endPos.y);
        this.g.stroke();
    },
});
