cc.Class({
    extends: cc.Component,

    properties: {
        touchFlag: false,
        _isEnemy: false,
    },



    init(parents, randomIndex) {
        this.父级 = parents;
        this._isEnemy = false;
        if (this.父级.name == "敌方父级") {
            this._isEnemy = true;
        }
        this.添加事件();
        this._name = randomIndex;
        if (randomIndex == 0)
            this._name = "";
        this.PlayDragonBones(this.animationDisplay, "jian" + this._name, this.默认状态, 1);
        this.surplusCount = this.击打次数(randomIndex);
        this.clickCount = 0;
        this.reduceBloodCount = 1;
        this.目标父级 = this.父级;
        this.node.position = new cc.Vec2(0, -190);
        this.canClick = true;
        this.伸头();
    },
    添加事件() {
        this.animationDisplay = this.getComponent(dragonBones.ArmatureDisplay);
        if (this._isEnemy) {

        } else {
            this.node.on(cc.Node.EventType.TOUCH_START, function () {
                if (!this.canClick)
                    return;
                ObjectPools.playSimpleAudioEngine(0);
                var pos = this.node.parent.parent.position;
                Games.拳套.position = pos;
                this.PlayDragonBones(Games.glovesDragon, "attack2", this.隐藏拳套, 1);
                this.击中();
            }, this);
            Games.node.on(cc.Node.EventType.TOUCH_MOVE, function (event) {
                if (!this.canClick)
                    return;
                if (Games.checkTouch(this.node, event.getLocation())) {
                    if (this.touchFlag == false) {
                        this.lastPos = event.getLocation();
                        this.touchFlag = true;
                    }
                } else {
                    if (this.touchFlag == true) {
                        this.currentPos = event.getLocation();
                        this.方向判断();
                    }
                }
            }, this);
            ObjectPools.playSimpleAudioEngine(3);
        }
    },

    方向判断() {
        if (!this.canClick)
            return;
        var valuex = this.currentPos.x - this.lastPos.x;
        if (Math.abs(valuex) < 5)
            return;
        this.touchFlag = false;
        // 左右滑动
        if (valuex > 0) {
            ObjectPools.playSimpleAudioEngine(0);
            var pos = this.node.parent.parent.position;
            Games.拳套.position = pos;
            Games.拳套.scale = new cc.Vec2(1, 1);
            this.PlayDragonBones(Games.glovesDragon, "attack1", this.隐藏拳套, 1);
            this.右击();
        }
        else {
            ObjectPools.playSimpleAudioEngine(0);
            var pos = this.node.parent.parent.position;
            Games.拳套.position = pos;
            Games.拳套.scale = new cc.Vec2(-1, 1);
            this.PlayDragonBones(Games.glovesDragon, "attack1", this.隐藏拳套, 1);
            this.左击();
        }
    },

    默认状态(event) {
        var scripts = this.node.getComponent("dishu");
        scripts.PlayDragonBones(scripts.animationDisplay, "idle" + scripts._name, scripts.默认状态, 1);
    },
    // 伸头后倒计时 2秒后自动缩头
    伸头() {
        var self = this;
        this.scheduleOnce(function () {
            if (this.canClick) {
                console.log("ceshi");
                this.canClick = false;
                this.unscheduleAllCallbacks();
                if (!this._isEnemy) {
                    ObjectPools.playSimpleAudioEngine(5);
                }
                var finished = cc.callFunc(function () {
                    self.node.stopAllActions();
                    ObjectPools.地鼠回收(self.node);
                    Games.随机生成(self.父级);
                });
                this.缩头动作 = cc.sequence(cc.moveBy(0.2, cc.p(0, -150)), finished);
                this.node.runAction(this.缩头动作);
            }
        }, 2);
    },


    击中() {
        this.PlayDragonBones(this.animationDisplay, "yun" + this._name, this.默认状态, 1);
        this.连击();
    },

    左击() {
        var names;
        if (this._name == "") {
            names = "11";
        } else {
            names = 10 + parseInt(this._name) + 1;
        }
        //console.log("左击" + "attack" + names);
        this.PlayDragonBones(this.animationDisplay, "attack" + names, this.默认状态, 1, -1);
        this.连击();
    },

    右击() {
        var names;
        if (this._name == "") {
            names = "11";
        } else {
            names = 10 + parseInt(this._name) + 1;
        }
        //  console.log("左击" + "attack" + names);
        this.PlayDragonBones(this.animationDisplay, "attack" + names, this.默认状态, 1);
        this.连击();
    },

    连击() {
        this.surplusCount--;
        this.clickCount++;
        if (this.node.parent != null) {
            if (this.clickCount >= 3) {
                ObjectPools.playSimpleAudioEngine(4);
                Games.连击播放(this.node.parent.parent.position, Games.连击动画, this.clickCount);
            }
        }
        if (this.surplusCount == 0) {
            this.挂掉();
        }
    },

    隐藏拳套(event) {
        Games.拳套.position = cc.Vec2.ZERO;
    },

    挂掉() {
        if (!this.canClick)
            return;
        ObjectPools.playSimpleAudioEngine(2);
        this.canClick = false;
        this.node.stopAllActions();
        this.unscheduleAllCallbacks();
        var randomIndex = parseInt(Math.random() * 2, 10);
        if (randomIndex == 0) {
            var names;
            if (this._name == "") {
                names = "21";
            } else {
                names = 20 + parseInt(this._name) + 1;
            }
            this.PlayDragonBones(this.animationDisplay, "attack" + names, this.回收, 1);
        } else {
            this.PlayDragonBones(this.animationDisplay, "cry" + this._name, this.回收, 1);
        }
    },

    回收(event) {
        var scripts = this.node.getComponent("dishu");
        Games.掉血(scripts.目标父级, scripts.reduceBloodCount);
        ObjectPools.地鼠回收(this.node);
        Games.随机生成(scripts.父级);
    },

    PlayDragonBones(animationDisplays, newAnimation, completeCallback, playTimes, scaleX = 1) {  //动态加载龙骨
        animationDisplays.playAnimation(newAnimation, playTimes);
        this.node.scale = new cc.Vec2(scaleX, 1);
        animationDisplays.addEventListener(dragonBones.EventObject.COMPLETE, completeCallback, animationDisplays);//.apply(this));
    },

    击打次数(index) {
        var count;
        // idle:普通 idle1:安全帽 idel2:海盗帽 idle:南瓜帽
        switch (index + 1) {
            case 1: count = 1; break;
            case 2: count = 6; break;
            case 3: count = 3; break;
            case 4: count = 9; break;
        }
        return count;
    },
});
