"use strict";
cc._RF.push(module, 'b0432BA295DjIOcuitdGKO1', 'GameManager');
// Script/GameManager.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
// 主游戏控制类
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var GameManager = /** @class */ (function (_super) {
    __extends(GameManager, _super);
    function GameManager() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.goal_Prefab = null;
        _this.middle_Prefab = null;
        return _this;
    }
    GameManager.prototype.onLoad = function () { };
    GameManager.prototype.start = function () {
        var _this = this;
        this.fit();
        this.init();
        setTimeout(function () {
            cc.fx.Notifications.on("moveTube", _this.moveTube, _this);
        }, 1000);
    };
    //初始化数据
    GameManager.prototype.init = function () {
        this.tube1 = this.node.getChildByName("GameArea").getChildByName("tube1");
        this.tube2 = this.node.getChildByName("GameArea").getChildByName("tube2");
        this.tube3 = this.node.getChildByName("GameArea").getChildByName("tube3");
        this.ball1 = this.node.getChildByName("GameArea").getChildByName("ball1");
        this.ball2 = this.node.getChildByName("GameArea").getChildByName("ball2");
        this.ball3 = this.node.getChildByName("GameArea").getChildByName("ball3");
        this.goal = this.node.getChildByName("Goal");
        this.middle = this.node.getChildByName("Middle");
        // this.node.getChildByName("GameArea").getChildByName("tube1").zIndex = 1;
        // this.node.getChildByName("GameArea").getChildByName("tube2").zIndex = 1;
        // this.node.getChildByName("GameArea").getChildByName("tube3").zIndex = 1;
        this.initMap();
        this.initBall();
    };
    //初始化 管子和球的关卡数据
    GameManager.prototype.initMap = function () {
        this.tube_Array = this.changeMap(cc.fx.GameConfig.LEVEL_INFO[0].start, "start");
        this.middle_Array = this.changeMap(cc.fx.GameConfig.LEVEL_INFO[0].middle, "middle");
        this.goal_Array = this.changeMap(cc.fx.GameConfig.LEVEL_INFO[0].goal, "goal");
        this.steps = cc.fx.GameConfig.LEVEL_INFO[0].steps;
    };
    /**
     * 转变地图参数到程序可用
     * @param tagArr tagArr为目标数组，可能为二维数组
     * @param name 关卡，球和管子的类型，有start,middle,goal,其中middle是二维数组，最多为3个
     */
    GameManager.prototype.changeMap = function (tagArr, name) {
        //中间态可能有多组目标  故单独处理
        if (name == "middle") {
            for (var k = 0; k < tagArr.length; k++) {
                var tempArr = tagArr[k];
                for (var i = 0; i < tempArr.length; i++) {
                    if (tempArr[i].length > 0) {
                        for (var j = 0; j < tagArr[i].length; j++) {
                            if (this.conversion_Char(tempArr[i][j]))
                                tempArr[i][j] = this.conversion_Char(tempArr[i][j]);
                        }
                    }
                }
            }
        }
        else {
            for (var i = 0; i < tagArr.length; i++) {
                if (tagArr[i].length > 0) {
                    for (var j = 0; j < tagArr[i].length; j++) {
                        if (this.conversion_Char(tagArr[i][j]))
                            tagArr[i][j] = this.conversion_Char(tagArr[i][j]);
                    }
                }
            }
        }
        return tagArr;
    };
    //转换符号,"rgb"配置改为123
    GameManager.prototype.conversion_Char = function (str) {
        if (str == "r")
            return 1;
        else if (str == "g")
            return 2;
        else if (str == "b")
            return 3;
    };
    //初始化球球 管子状态所有关都一样-
    GameManager.prototype.initBall = function () {
        //初始状态，球的初始化
        for (var i = 0; i < this.tube_Array.length; i++) {
            if (this.tube_Array[i].length > 0) {
                for (var j = 0; j < this.tube_Array[i].length; j++) {
                    var name = "ball" + this.tube_Array[i][j];
                    this[name].active = true;
                    this[name].setPosition((i - 1) * 210, -100 + j * 100);
                    this[name].getComponent("Ball").init(j == (this.tube_Array[i].length - 1) ? true : false, this.tube_Array[i][j]);
                }
            }
        }
        //最终状态，球的初始化
        for (var i = 0; i < this.goal_Array.length; i++) {
            if (this.goal_Array[i].length > 0) {
                for (var j = 0; j < this.goal_Array[i].length; j++) {
                    var name = this.goal_Array[i][j] + 4;
                    this.goal.children[name].active = true;
                    this.goal.children[name].setPosition((i - 1) * 210, -100 + j * 100);
                    this.goal.children[name].getComponent("Ball").init(false, this.goal_Array[i][j]);
                }
            }
        }
        //中间状态，球的初始化
        for (var i = 0; i < this.middle_Array.length; i++) {
            if (this.middle_Array.length > 0) {
                if (this.middle_Array.length == 1) {
                    this.createMiddle(0);
                }
                else if (this.middle_Array.length == 3) {
                    this.createMiddle(1);
                    this.createMiddle(2);
                    this.createMiddle(3);
                    // this.middle.getChildByName("part").active = false;
                }
            }
        }
    };
    GameManager.prototype.createMiddle = function (num) {
        var nodeName = "part";
        if (num != 0)
            nodeName = nodeName + num;
        var mid = this.middle.getChildByName(nodeName);
        mid.active = true;
        var mid_Arr = this.middle_Array[num - 1];
        for (var i = 0; i < mid_Arr.length; i++) {
            if (mid_Arr[i].length > 0) {
                for (var j = 0; j < mid_Arr[i].length; j++) {
                    var name = mid_Arr[i][j] + 4;
                    mid.children[name].active = true;
                    mid.children[name].setPosition((i - 1) * 210, -100 + j * 100);
                    mid.children[name].getComponent("Ball").init(false, mid_Arr[i][j]);
                }
            }
        }
    };
    /**
     * 当管子内球发生移动时，改变管子数组，并且重置所有球是否可移动状态
     * @param data data格式为对象
     *  id:number   移动球的自己的id
     *  tube:number 移动到目标个管子，0,1,2
        start_Pos:cc.v2(), 移动之前的位置
        end_Pos:cc.v2()    移动之后的终点位置
     */
    GameManager.prototype.moveTube = function (data) {
        this.tube_Array[data.tube].push(data.id);
        var tube_Arr = this.tube_Array[1];
        if (data.start_Pos.x == -210) {
            tube_Arr = this.tube_Array[0];
        }
        else if (data.start_Pos.x == 210) {
            tube_Arr = this.tube_Array[2];
        }
        if (tube_Arr.length > 0)
            tube_Arr.pop();
        if (data.start_Pos.y != 50 && tube_Arr.length > 0) {
            var name = "ball" + tube_Arr[tube_Arr.length - 1];
            this[name].getComponent("Ball").set_Touch(true);
        }
        if (data.end_Pos.y != 50) {
            tube_Arr = this.tube_Array[1];
            if (data.end_Pos.x == -210) {
                tube_Arr = this.tube_Array[0];
            }
            else if (data.end_Pos.x == 210) {
                tube_Arr = this.tube_Array[2];
            }
            if (tube_Arr.length > 1) {
                var name = "ball" + tube_Arr[tube_Arr.length - 2];
                console.log(name);
                this[name].getComponent("Ball").set_Touch(false);
            }
        }
        this.gameWin();
    };
    GameManager.prototype.gameWin = function () {
        var result = false;
        //如果没有中间状态，或者中间状态都已达到，则达到最终目标则算胜利
        if (this.middle_Array.length == 0) {
            result = this.isArrEqual(this.goal_Array);
            if (result) {
                alert("成功");
            }
        }
        else {
            for (var i = 0; i < this.middle_Array.length; i++) {
                if (this.isArrEqual(this.middle_Array[i])) {
                    alert("达到中间态");
                    this.middle_Array.splice(i, 1);
                }
            }
        }
    };
    //判断数组是否相同，如果join情况下都不相同就不用往下比较
    GameManager.prototype.isArrEqual = function (tagArr) {
        var result = false;
        if (this.tube_Array.join() == tagArr.join()) {
            result = true;
            for (var i = 0; i < this.tube_Array.length; i++) {
                if (this.tube_Array[i].length != tagArr[i].length) {
                    result = false;
                }
            }
        }
        return result;
    };
    //根据是否全面屏，做独立适配方面
    GameManager.prototype.fit = function () {
        var jg = this.setFit();
        if (!jg) {
        }
    };
    //判断全面屏
    GameManager.prototype.getSetScreenResolutionFlag = function () {
        var size = cc.winSize;
        var width = size.width;
        var height = size.height;
        if ((height / width) > (16.2 / 9))
            return false;
        return true;
    };
    //判断全面屏适配
    GameManager.prototype.setFit = function () {
        var flag = this.getSetScreenResolutionFlag();
        if (flag) {
        }
        else {
        }
        return flag;
    };
    //返回首页
    GameManager.prototype.backScene = function () {
        cc.director.loadScene("LoadScene");
    };
    //下一关，或者重新开始，或者返回上一关，根据level决定
    GameManager.prototype.reStart = function (type) {
    };
    //获取时间戳
    GameManager.prototype.getTime = function () {
        var timestamp = new Date().getTime();
        return timestamp;
    };
    //获胜
    GameManager.prototype.passLevel = function () {
    };
    //失败
    GameManager.prototype.loseLevel = function (type) {
    };
    //开始游戏
    GameManager.prototype.startGame = function () {
    };
    //如果是倒计时 调用此方法
    GameManager.prototype.updateCountDownTime = function () {
        if (this.countTime > 0) {
            this.countTime -= 1;
            // this.time.string =  cc.fx.GameTool.getTimeMargin(this.countTime);
            if (this.countTime < 5) {
                // cc.tween(this.time.node)
                // .to(0.25,{scale:1.5,color:cc.color(255,0,0)})
                // .to(0.25,{scale:1,color:cc.color(255,255,255)})
                // .start()
                var over = this.node.getChildByName("Over");
                cc.tween(over)
                    .to(0.2, { opacity: 255 })
                    .delay(0.1)
                    .to(0.2, { opacity: 0 })
                    .start();
            }
            if (this.countTime <= 0) {
                this.unschedule(this.updateCountDownTime);
                var time = 0;
                this.gameOver(time);
            }
        }
    };
    //上传每次操作数据
    GameManager.prototype.setData = function () {
        cc.fx.GameTool.setGameData();
    };
    //上传排行榜数据
    GameManager.prototype.gameOver = function (time) {
        cc.fx.GameTool.setRank(time);
        this.node.getChildByName("GameOver").active = true;
        this.node.getChildByName("GameOver").opacity = 0;
        cc.tween(this.node.getChildByName("GameOver"))
            .to(0.4, { opacity: 255 })
            .delay(2)
            .to(0.4, { opacity: 50 })
            .call(function () {
            cc.director.loadScene("OverScene");
        })
            .start();
    };
    GameManager.prototype.onEnable = function () {
        // setTimeout(() => {
        //     cc.fx.Notifications.on("moveTube", this.moveTube, this);
        // }, 100);
    };
    GameManager.prototype.onDisable = function () {
        cc.fx.Notifications.off("moveTube", this.moveTube);
    };
    GameManager.prototype.update = function (dt) {
    };
    __decorate([
        property(cc.Prefab)
    ], GameManager.prototype, "goal_Prefab", void 0);
    __decorate([
        property(cc.Prefab)
    ], GameManager.prototype, "middle_Prefab", void 0);
    GameManager = __decorate([
        ccclass
    ], GameManager);
    return GameManager;
}(cc.Component));
exports.default = GameManager;

cc._RF.pop();