cc.Class({
    extends: cc.Component,

    properties: {
        player1: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.init();
        this.registerEvent();
    },
    init()
    {
        this.firstX = null;
        this.firsty = null;
         //playerControllerScript
        this.pConSt1 = this.player1.getComponent('BasePlayerController');
        this.pStatesSt = this.player1.getChildByName('PlayerStates').getComponent('BasePlayerStates');
    },
    registerEvent() {
        //touchstart 可以换成cc.Node.EventType.TOUCH_START
        this.node.on('touchstart', this.onEventStart, this);
        //touchmove 可以换成cc.Node.EventType.TOUCH_MOVE
        this.node.on('touchmove', this.onEventMove, this);
        //touchcancel 可以换成cc.Node.EventType.TOUCH_CANCEL
        this.node.on('touchcancel', this.onEventCancel, this);
        //touchend 可以换成cc.Node.EventType.TOUCH_END
        this.node.on('touchend', this.onEventEnd, this);
    },
    // start () {},
    /**
     * 触摸开始
     * @param {*} event
     */
    onEventStart(event) {
        if( this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump)return;
        //世界坐标
        let worldPoint = event.getLocation();
        // console.log('start Event \n worldPoint=', worldPoint);
        // console.log('touch start');

        this.firstX = worldPoint.x;
        this.firstY = worldPoint.y;
    },

    /**
     * 触摸移动
     * @param {*} event
     */
    onEventMove(event) {
        if( this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump)return;
        //世界坐标
        let worldPoint = event.getLocation();
        // console.log('move Move \n worldPoint=', worldPoint);
    },

    /**
     * 触摸
     * 当手指在目标节点区域外离开屏幕时
     * 比如说，触摸node的size是200x200。
     * 当超过这个区域时，就是触发这个事件
     * @param {*} event
     */
    onEventCancel(event) {
        if( this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump)return;
        //世界坐标
        let worldPoint = event.getLocation();
        // console.log('cancel Event \n worldPoint=', worldPoint);
    },

    /**
     * 当手指在目标节点区域内离开屏幕时
     * @param {*} event
     */
    onEventEnd(event) {
        if( this.pStatesSt.currentState == this.pStatesSt.playerAnimState.jump)return;
        //世界坐标
        let worldPoint = event.getLocation();
        // console.log('end Event \n worldPoint=', worldPoint);

        let endX = this.firstX - worldPoint.x;
        let endY = this.firstY - worldPoint.y;
        // var tempPlayer = node.parent.convertToNodeSpaceAR(touchPoint);
        // node.setPosition(tempPlayer);

        if (Math.abs(endX) > Math.abs(endY)){
            //手势向左右
            //判断向左还是向右
            if (endX  > 0){
                //向左函数
                // console.log('left');
            } else {
                //向右函数
                // console.log('right');
                this.pConSt1.speedUp();
            }
        } else {
            //手势向上下
            //判断手势向上还是向下
            if (endY  > 0){
                //向下函数
                // console.log('down');
            } else {
                //向上函数
                // console.log('up');
                this.pConSt1.jump();
            }
        }
    },
    // update (dt) {},
});
