cc.Class({
    extends: require("BaseCharactor"),

    properties: {
        PlayerController: cc.Node,
        AiPlayerController: cc.Node,
    },

    onLoad() {
        this._super();
        this.armatureDisplay = this.node.getComponent(dragonBones.ArmatureDisplay);
        this.armature = this.armatureDisplay.armature();

        //添加动画事件监听

        this.armatureDisplay.addEventListener(dragonBones.EventObject.COMPLETE, this.animationEventHandler, this);
        this.armatureDisplay.addEventListener(dragonBones.EventObject.FRAME_EVENT, this.animationEventHandler_Enter, this);
    },

    start() {
        this.init();
    },

    // update (dt) {},
    attack(bLeft) {

        //发射攻击事件  // 普攻
        if (bLeft == 0) {
            //右拳
            this.armatureDisplay.node.scaleX = 1;
            this.armature.animation.play("attack", 1);
            this.node.emit("attack");
        } else if (bLeft == 1) {
            //左拳
            this.armatureDisplay.node.scaleX = -1;
            this.armature.animation.play("attack", 1);
            this.node.emit("attack");
        }
        //暴击右
        else if (bLeft == 2) {
            this.armatureDisplay.node.scaleX = -1;
            this.armature.animation.play("critical_strike", 1);
            this.node.emit("attack_critical_strike");
        }
        //暴击左
        else if (bLeft == 3) {
            this.armatureDisplay.node.scaleX = 1;
            this.armature.animation.play("critical_strike", 1);
            this.node.emit("attack_critical_strike");
        }
        //连击
        else if (bLeft == 4) {
            this.armatureDisplay.node.scaleX = -1;
            this.armature.animation.play("double-hit", 1);
            this.node.emit("attack_double-hit");
        }
        //连击
        else if (bLeft == 5) {
            this.armatureDisplay.node.scaleX = 1;
            this.armature.animation.play("double-hit", 1);
            this.node.emit("attack_double-hit");
        }
    },
    dodge(bLeft) { //躲闪

        console.log("Ai躲闪");
        //发射躲闪事件 
        if (bLeft == 0) {
            //左躲闪
            this.armatureDisplay.node.scaleX = 1;
            this.armature.animation.play("dodge", 1);
        } else if (bLeft == 1) {
            //右躲闪
            this.armatureDisplay.node.scaleX = -1;
            this.armature.animation.play("dodge", 1);
        }
        this.node.emit("dodge");
    },
    hurt(bLeft, type) { //挨打 左右拳，击打类型
        //发射挨打事件 
        if (type == 0) {

            //普通挨打
            if (bLeft == 0) {
                //左挨打
                console.log("Ai挨右打");
                this.armatureDisplay.node.scaleX = -1;
                this.armature.animation.play("be_beaten", 1);
            } else if (bLeft == 1) {
                //右挨打
                console.log("Ai挨左打");
                this.armatureDisplay.node.scaleX = 1;
                this.armature.animation.play("be_beaten", 1);
            }
            this.node.emit("hurt_ord");
        }
        if (type == 1) {
            console.log("Ai挨暴打");
            if (bLeft == 0) {
                //左挨打
                //被暴打
                this.armatureDisplay.node.scaleX = -1;
                this.armature.animation.play("be_critical_strike", 1);

            } else if (bLeft == 1) {
                //被暴打
                //右挨打
                this.armatureDisplay.node.scaleX = 1;
                this.armature.animation.play("be_critical_strike", 1);
            }
            this.node.emit("hurt_critical");
        }
        if (type == 2) {
            console.log("Ai挨连打");
            if (bLeft == 0) {

                //被连击打
                //左挨打
                this.armatureDisplay.node.scaleX = -1;
                this.armature.animation.play("be_double_hit", 1);
            } else if (bLeft == 1) {
                //被连击打
                //右挨打
                this.armatureDisplay.node.scaleX = 1;
                this.armature.animation.play("be_double_hit", 1);
            }
            this.node.emit("hurt_double");
        }
    },
    block() { //防御
        this.armature.animation.play("block", 1);
        this.node.emit("block");
    },
    animationEventHandler(event) {
        if (event.type === dragonBones.EventObject.COMPLETE) {
            //主角
            if (event.animationState.name === "attack") {
                console.log("attack 动作播放完毕！！！");
                //TODO:
                //this.armatureDisplay.node.scaleX = 0.39;
                this.armature.animation.play("idle", 0);

                this.aicontroScp._RightJayShow = false;
                this.aicontroScp._LeftJayShow = false;
                this.aicontroScp._Statepassivity = false;

            }
            if (event.animationState.name === "be_beaten") {
                console.log("critical_strike 动作播放完毕！！！");
                //TODO:
                this.armatureDisplay.node.scaleX = 1;
                this.armature.animation.play("idle", 0);

                this.aicontroScp._Hurt = false;
                this.aicontroScp._Statepassivity = false;

            }
            if (event.animationState.name === "critical_strike") {
                console.log("critical_strike 动作播放完毕！！！");
                //TODO:
                //this.armatureDisplay.node.scaleX = 0.39;
                this.armature.animation.play("idle", 0);

                this.aicontroScp._CriticalStrike = false;
                this.aicontroScp._Statepassivity = false;

            }
            if (event.animationState.name === "double-hit") {
                console.log("double-hit 动作播放完毕！！！");
                //TODO:
                //this.armatureDisplay.node.scaleX = 0.39;
                this.armature.animation.play("idle", 0);
                this.aicontroScp._DoubleHit = false;
                this.aicontroScp._Statepassivity = false;
            }
            if (event.animationState.name === "dodge") {
                console.log("dodge 动作播放完毕！！！");
                //TODO:
                //this.armatureDisplay.node.scaleX = 0.39;
                this.armature.animation.play("idle", 0);

                this.aicontroScp._RightDodge = false;
                this.aicontroScp._LeftDodg = false;
                this.aicontroScp._Statepassivity = false;
            }
            if (event.animationState.name === "block") {
                console.log("block 动作播放完毕！！！");
                //TODO:
                // this.armatureDisplay.node.scaleX = 0.39;

                this.armature.animation.play("idle", 0);

                this.aicontroScp._Defence = false;
                this.aicontroScp._Statepassivity = false;
            }
        }
    },
    animationEventHandler_Enter(event) {

    },
    init() {
        //角色脚本
        this.controScp = this.PlayerController.getComponent('PlayerController');
        //Ai脚本
        this.aicontroScp = this.AiPlayerController.getComponent('AiPlayerController');
    },
});