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

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    private isDragging: boolean = false;
    private touchOffset: cc.Vec2 = cc.v2(0, 0);

    @property(cc.Prefab)
    greenBullet: cc.Prefab = null;

    playerLifes: number = 5;

    onCollisionEnter(otherCollider, selfCollider) {
        if(otherCollider.name === 'Bullet<PolygonCollider>') {
            this.playerLifes--;
            otherCollider.node.destroy();
            if(this.playerLifes <= 0) {
                this.node.destroy();
                cc.director.loadScene('Menu');
            }
        }
    }
    shootBullets() {
        var bullet = cc.instantiate(this.greenBullet);
        bullet.setPosition(this.node.position.x, this.node.position.y + 40);
        this.node.parent.addChild(bullet);
    }

    onLoad() {
        // Enable touch on this node directly
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);

        this.schedule(this.shootBullets, 0.25, cc.macro.REPEAT_FOREVER, 0);
    }

    onTouchStart(event: cc.Event.EventTouch) {
        this.isDragging = true;

        // Get touch location in parent coordinates
        const touchPos = this.node.parent.convertToNodeSpaceAR(event.getLocation());

        // Calculate offset between jet center and touch point
        this.touchOffset = this.node.position.sub(cc.v2(touchPos.x, touchPos.y));
    }
    onTouchMove(event: cc.Event.EventTouch) {
        if (!this.isDragging) return;

        // Convert touch position to parent space
        const touchPos = this.node.parent.convertToNodeSpaceAR(event.getLocation());

        // Keep the offset so dragging feels natural (finger stays on the same point)
        this.node.setPosition(cc.v2(touchPos.x, touchPos.y).add(this.touchOffset));
    }
    onTouchEnd(event: cc.Event.EventTouch) {
        this.isDragging = false;
    }

    onDestroy() {
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }
}
