var ObjectPool = require("ObjectPool");
cc.Class({
    extends: cc.Component,

    properties: {
        我方: cc.Node,
        敌方: cc.Node,
        对象池: {
            default: null,
            type: ObjectPool
        },
        敌方血条: cc.Node,
        我方血条: cc.Node,
        倒计时: cc.Label,
        敌方分数: cc.Label,
        我方分数: cc.Label,
        结束界面: cc.Node,
        连击动画: cc.Node,
        拳套: cc.Node,
        计时标志: false,
    },

    onLoad() {
        Games = this;
        this.glovesDragon = this.拳套.getComponent(dragonBones.ArmatureDisplay);
        console.log(this.时分(60));
        this.初始化();
    },
    初始化() {
        this.随机生成(this.我方);
        this.随机生成(this.敌方);
        this.敌方分数.string = "10";
        this.我方分数.string = "10";
        this.敌方血条.getComponent(cc.ProgressBar).progress = 1;
        this.我方血条.getComponent(cc.ProgressBar).progress = 1;
        this.倒计 = 60;
        this.计时标志 = true;
    },
    start() {

    },
    掉血(父级, 血量) {
        if (父级.name == this.我方.name) {
            var 分数 = parseInt(this.敌方分数.string);
            分数 -= 血量;
            this.敌方分数.string = 分数.toString();
            this.敌方血条.getComponent(cc.ProgressBar).progress -= 0.1;
            if (分数 <= 0) {
                this.敌方分数.string = "0";
                this.敌方血条.getComponent(cc.ProgressBar).progress = 0;
                console.log("游戏结束,胜利");
                this.游戏成功();
            }
        } else {
            var 分数 = parseInt(this.我方分数.string);
            分数 -= 血量;
            this.我方分数.string = 分数.toString();
            this.我方血条.getComponent(cc.ProgressBar).progress -= 0.1 * 血量;
            if (分数 <= 0) {
                this.我方分数.string = 分数.toString();
                this.我方血条.getComponent(cc.ProgressBar).progress = 0;
                console.log("游戏结束,失败");
                this.游戏失败();
            }
        }
    },

    游戏成功() {
        this.游戏结束();
    },

    游戏失败() {
        this.游戏结束();
    },

    倒计时结束() {
        this.游戏结束();
        this.unscheduleAllCallbacks();
        if (parseInt(this.我方分数.string) > parseInt(this.敌方分数.string)) {
            console.log("胜利");
        } else if (parseInt(this.我方分数.string) == parseInt(this.敌方分数.string)) {
            console.log("平局");
        } else {
            console.log("失败");
        }
    },

    游戏结束() {
        cc.director.pause();
        this.结束界面.active = true;
        this.glovesDragon.armature().animation.stop();
        for (var i = 0; i < this.连击动画.children.length; i++) {
            this.连击动画.children[i].active = false;
        }
        this.拳套.active = false;
    },

    重新开始() {
        cc.director.resume();
        Games.拳套.active = true;
        this.glovesDragon.playAnimation("<None>", 1);
        for (var i = 0; i < this.我方.children.length; i++) {
            var childs = this.我方.children[i].children[0];
            if (childs.children.length == 1) {
                ObjectPools.地鼠回收(childs.children[0]);
            }
        }
        for (var i = 0; i < this.敌方.children.length; i++) {
            var childs = this.敌方.children[i].children[0];
            if (childs.children.length == 1) {
                ObjectPools.地鼠回收(childs.children[0]);
            }
        }
        this.初始化();
        this.结束界面.active = false;
    },

    随机生成(父级) {
        var flag = false;
        for (var i = 0; i < 父级.children.length; i++) {
            var childs = 父级.children[i].children[0];
            if (childs.children.length == 0) {
                flag = true;
            }
        }
        if (flag == false)
            return;
        var randomIndex = parseInt(Math.random() * 3, 10);
        var parents = 父级.children[randomIndex].getChildByName("Mask");
        var randomShapeIndex = parseInt(Math.random() * 5, 10);
        // 炸弹
        if (randomShapeIndex == 4) {
            var item = this.对象池.生成炸弹();
            if (parents.children.length == 0) {
                item.parent = parents;
            } else {
                this.随机生成(父级);
                return;
            }
            item.getComponent("zhadan").init(父级, randomShapeIndex);
        } else {
            var item = this.对象池.生成地鼠();
            if (parents.children.length == 0) {
                item.parent = parents;
            } else {
                this.随机生成(父级);
                return;
            }
            item.getComponent("dishu").init(父级, randomShapeIndex);
        }
    },

    checkTouch: function (target, location) {
        let node = target;
        let pointInNode = node.convertToNodeSpaceAR(location);
        let i, j, c = false;

        if (!target.getComponent(cc.PolygonCollider))
            return;
        let nvert = target.getComponent(cc.PolygonCollider).points.length;
        let testx = pointInNode.x;
        let testy = pointInNode.y;
        let vert = target.getComponent(cc.PolygonCollider).points;

        for (i = 0, j = nvert - 1; i < nvert; j = i++) {
            if (((vert[i].y > testy) != (vert[j].y > testy)) &&
                (testx < (vert[j].x - vert[i].x) * (testy - vert[i].y) / (vert[j].y - vert[i].y) + vert[i].x))
                c = !c;
        }
        return c;
    },

    // 输出03:05:59  时分秒
    时分(result) {
        var h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
        var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
        var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
        //return result = h + ":" + m + ":" + s;
        return result = m + ":" + s;
    },

    连击播放(p, obj, str) {
        for (var i = 0; i < obj.children.length; i++) {
            var exp = obj.children[i];
            var labelstr = exp.getChildByName("New Label").getComponent(cc.Label);
            labelstr.string = str;
            if (exp.active == false) {
                exp.position = p;
                exp.active = true;
                break;
            }
        }
    },
    update(dt) {
        if (this.计时标志) {
            if (this.倒计 === 0) {
                this.倒计时结束();
                this.计时标志 = false;
            }
            this.倒计 -= dt;
            var 时间 = this.时分(this.倒计);
            this.倒计时.string = 时间.toString();
        }
    },
});
