
cc.Class({
    extends: cc.Component,

    properties: {

        index: 300,

        _graphics: cc.Graphics,

        //是否跟随目标来画路径
        followTarget: false,
        //目标位置
        target: cc.Node,

        _PStart: null,
        _pNext: null,
        _isInit: false,//只设置一次

        //是否使用数组来画路径
        followPath: false,


        //用点来初始化路径
        _initPoint: false,
        //开始第一次绘制
        _pointStart: false,

    },
    onLoad() {
        if (this.node.getComponent(cc.Graphics)) {
            this._graphics = this.node.getComponent(cc.Graphics);
        } else {
            this._graphics = this.node.addComponent(cc.Graphics);
        }

    },

    
    //根据目标画线
    DrawLineFromTarget(){
        if (this.followTarget && this.target) {
            this._graphics.clear();
            this._graphics.lineWidth = 3;
            this._graphics.strokeColor = cc.Color.GREEN;
            this._PStart = this.target.getPosition();
            this._POld = null;
            this._isSet = false;
            this._isInit = false;
            this.unschedule(this.callback);
            this.schedule(this.callback = function () {

                if (this._POld&&this._isSet) {
                    if (this._POld.sub(this.target.position).mag() == 0) {
                        // 取消这个计时器
                        this.unschedule(this.callback);
                        // cc.log('取消这个计时器.')
                    }
                }
                if (this._isInit) {
                    this._graphics.lineTo(this.target.x, this.target.y);
                }
                else {

                    this._isInit = true;
                    this._pNext = this.target.getPosition();
                    this._graphics.moveTo(this._PStart.x, this._PStart.y);
                    this._graphics.lineTo(this._pNext.x, this._pNext.y);
                }
                this._graphics.stroke();
                if(!this._isSet){
                    this._POld = this.target.position;
                }
                this._isSet = !this._isSet;
            }, 0.1, 2000, 0);
        }
    },

    AddDrawNode() {
        this.index += 300;
        this._graphics.lineTo(this.index, this.index + Math.floor(Math.random() * 100));
        this._graphics.stroke();
    },
    InitDrawNode(pStart, pNext) {
        this._graphics.clear();
        this._graphics.lineWidth = 3;
        this._graphics.strokeColor = cc.Color.GREEN;
        this._graphics.moveTo(pStart.x, pStart.y);
        this._graphics.lineTo(pNext.x, pNext.y);
        this._graphics.stroke();
    },
    // UpdateTargetNode(target) {
    //     this._graphics.lineTo(target.x, target.y);
    //     this._graphics.stroke();
    // },

    //用路径来绘制
    //cc.v2
    onDrawFromPath(path) {
        if (!path) {
            cc.error('onDrawFromPath 参数为Null.');
            return;
        }
        var _initPath = false;

        this._graphics.clear();
        this._graphics.lineWidth = 3;
        this._graphics.strokeColor = cc.Color.YELLOW;

        let pathIndex = 0;
        let pStart = path[pathIndex];
        let pNext = null;

        this.schedule(function () {

            if (_initPath) {
                pathIndex++;
                if (pathIndex > path.length - 1) {
                    cc.log('数组绘制完成。');
                    return;
                }
                pNext = path[pathIndex];
                this._graphics.lineTo(pNext.x, pNext.y);
                this._graphics.stroke();

            }
            else {

                _initPath = true;
                pathIndex++;
                pNext = path[pathIndex];
                this._graphics.moveTo(pStart.x, pStart.y);
                this._graphics.lineTo(pNext.x, pNext.y);
                this._graphics.stroke();
            }

        }, 0.3, 2000, 1);
    },

    //用点来绘制
    //cc.v2
    onDrawFromPoint(point) {
        if (!point) {
            cc.error('onDrawFromPoint 参数为Null.');
            return;
        }
        if (!this._initPoint) {
            this._graphics.clear();
            this._graphics.lineWidth = 3;
            this._graphics.strokeColor = cc.Color.RED;
            this._PStart = point;
            this._initPoint = true;
        } else {
            this._pNext = point;
            if (!this._pointStart) {
                this._graphics.moveTo(this._PStart.x, this._PStart.y);
                this._graphics.lineTo(this._pNext.x, this._pNext.y);
                this._pointStart = true;
            } else {
                this._graphics.lineTo(this._pNext.x, this._pNext.y);
            }
            this._graphics.stroke();
        }


    },

    //cc.v2
    onDrawFromTwoPoints(point1, piont2) {
        // this._graphics.clear();
        this._graphics.strokeColor = cc.Color.GREEN;
        // this._graphics.strokeColor = cc.hexToColor('#FFFF00');

        this._graphics.moveTo(point1.x, point1.y);
        this._graphics.lineTo(piont2.x, piont2.y);



    },

    onStroke() {
        this._graphics.stroke();
    }
    // update(dt) {


    // },
});
