cc.Class({
    extends: cc.Component,

    properties: {
        item: cc.Node,
        shooterFrames: [cc.SpriteFrame],
    },

    // 初始化组件
    init (game) {
        this.game = game;
        this.wrap = game.wrap;
        this.modeHit = game.modeHit;
        // this.shooterMng = game.shooterMng;
        this.audioMng = game.audioMng;
        this.vSize = cc.director.getVisibleSize();
        this.posY = this.vSize.height / 2 + 50; 
        // this.weldJoint = this.getComponent(cc.WeldJoint);
        this.shootBody = this.getComponent(cc.RigidBody)

        this.node.x = 0;
        this.node.y = this.vSize.height / 2 - 60;
        this.status = 0;
        
        // 设置箭矢皮肤
        let arrIndex = window.Global.arrowSkinIndex || 0
        this.item.getComponent(cc.Sprite).spriteFrame = this.shooterFrames[arrIndex]
        // 监听触摸事件
        this.scheduleOnce(() => {
            this.wrap.once(cc.Node.EventType.TOUCH_START, this.shotEvent, this)
        }, 0.09)
    },

    shotEvent () {
        if(this.game.gameMode != 'hit') {
            //必中瓶模式结束后1.5秒内暂停发射箭矢
            if(this.modeHit.status == 2) {
                this.wrap.once(cc.Node.EventType.TOUCH_START, this.shotEvent, this)
            } else {
                this.runAnim()
            }
        } else {
            this.runHit()
        }
    },

    runHit () {
        let spawn = cc.spawn(cc.moveTo(0.1, cc.p(this.node.x, this.node.y - this.vSize.height)), cc.fadeOut(0.2));
        this.node.runAction(spawn);
        this.scheduleOnce(this.reset, 0.1)
    },

    runAnim () {
        // this.status = 1;
        this.audioMng.playShoot()
        // let spawn = cc.spawn(cc.moveTo(0.3, cc.p(this.node.x, this.node.y - this.vSize.height)), cc.fadeOut(0.5));
        // this.node.runAction(spawn);
        let vec = cc.v2(this.vSize / 2, 0).sub(this.node.position)
        let distance = vec.mag();
        let velocity = vec.normalize().mulSelf(2500)

        this.shootBody.type = cc.RigidBodyType.Dynamic;
        this.shootBody.linearVelocity = velocity;
    },

    /**
     * 碰撞检测
     */
    // onCollisionEnter: function (other, self) {
    //     //击中必中瓶模式
    //     if(this.game.gameMode == 'hit') {
    //         this.game.inGameUI.addScore(1);
    //         this.reset()
    //         return;
    //     }

    //     if(other.node.group == 'wall') {
    //         if(this.game.gameMode != 'undead') {
    //             this.scheduleOnce(() => {
    //                 this.game.gameOver();
    //             }, 0.5)
    //         }
    //     } else if(other.node.group == 'bottle') {
    //         let score = 1;
    //         this.game.inGameUI.addScore(score);
    //         this.reset()
    //     }
    // },

    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact (contact, selfCollider, otherCollider) {
        if(otherCollider.node.group == 'wall') {
            this.game.delShooter(this.node);
            if(this.game.gameMode != 'hit') {
                this.scheduleOnce(() => {
                    this.game.gameOver();
                }, 0.5)
            }
        } else if(otherCollider.node.group == 'bottle') {
            this.game.delShooter(this.node);
            let score = 1;
            this.game.inGameUI.addScore(score);
            this.game.readyShooter();
        }
    },
    
    /**
     * 物理碰撞回调
     */
    // onPostSolve (contact, selfCollider, otherCollider) {
    //     let impulse = contact.getImpulse();
    //     if (Math.abs(impulse.normalImpulses[0]) < cc.PhysicsManager.PTM_RATIO) {
    //         return;
    //     }

    //     let joint = this.weldJoint;
        
    //     if (joint.enabled) {
    //         joint.enabled = false;
    //         return
    //     }

    //     // let arrowBody = selfCollider.body;
    //     // let targetBody = otherCollider.body;
    //     // let worldCoordsAnchorPoint = arrowBody.getWorldPoint(cc.v2(0.6, 0));

    //     // joint.connectedBody = targetBody;
    //     // joint.anchor = arrowBody.getLocalPoint( worldCoordsAnchorPoint );
    //     // joint.connectedAnchor = targetBody.getLocalPoint( worldCoordsAnchorPoint );
    //     // joint.referenceAngle = targetBody.node.rotation - arrowBody.node.rotation;

    //     // joint.enabled = true;
    // },

    /**
     * 重置箭的位置和状态
     */
    reset () {
        this.game.resetShooter(this.node);
        // this.node.x = 0;
        // this.node.y = this.vSize.height / 2 - 40;
        // this.node.opacity = 255;
        // this.status = 0;
        // this.shootBody.type = cc.RigidBodyType.Static;
    },

    start () {

    },

    update (dt) {
        
    },
});
