// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class TouchLogic extends cc.Component {

    _cantouch: boolean;
    touchVec: cc.Vec2;
    private _checkAngle: number;
    _endCall: Function;

    protected onLoad(): void {
        this._cantouch = false;
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
    }
    touchStart(event: cc.Touch) {
        if (!this._cantouch) return;
        this.touchVec = cc.v2(0, 0)
    }
    touchMove(event: cc.Touch) {
        if (!this._cantouch) return;
        let delta = event.getDelta();
        this.touchVec.addSelf(delta);

        if (this.touchVec.mag() > 200) {
            let angel = this.touchVec.signAngle(cc.v2(1, 0));
            cc.log("angle = " + angel * 180 / 3.14);
            angel = angel * 180 / 3.14
            if (Math.abs(angel - this._checkAngle) < 15) {
                this._endCall && this._endCall();
                this._endCall = null;
                this._cantouch = false;
            }
        }

    }
    checkAnge(angle: number, endCall: Function) {
        this._cantouch = true;
        this._checkAngle = angle;
        this._endCall = endCall;
    }

    close() {
        this._cantouch = false;
    }

    // update (dt) {}
}
