"use strict";
cc._RF.push(module, '80998T1AYNNRZdRxsVvXR65', 'SceneManager');
// Script/SceneManager.ts

"use strict";
// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
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 Map_1 = require("./Map");
var MiniGameSdk_1 = require("./Sdk/MiniGameSdk");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var SceneManager = /** @class */ (function (_super) {
    __extends(SceneManager, _super);
    function SceneManager() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.label = null;
        _this.text = 'hello';
        _this.freeze = null;
        _this.hammer = null;
        _this.magic_wand = null;
        _this.pause = null;
        _this.Block_Array = [];
        _this.Wall_Prefab = [];
        // @property({type: [cc.ParticleSystem], tooltip:"粒子数组"})
        // particleEffects : Array<cc.ParticleSystem> = [];
        _this.load1 = false;
        _this.load2 = false;
        _this.load3 = false;
        _this.btnName = '';
        return _this;
    }
    // LIFE-CYCLE CALLBACKS:
    SceneManager.prototype.onLoad = function () {
        cc.game.setFrameRate(63);
        this.changeBg();
        setTimeout(function () {
            cc.director.preloadScene("HomeScene", function (err) {
                if (err) {
                    // console.error('预加载 HomeScene 场景失败:', err);
                    return;
                }
                // console.log('成功预加载 HomeScene 场景');
            });
        }, 1000);
    };
    SceneManager.prototype.changeBg = function () {
        var _this = this;
        var number = Math.floor(Math.random() * 8) + 1;
        var path = 'bg/bg' + number;
        cc.resources.load(path, cc.SpriteFrame, function (err, spriteFrame) {
            if (err) {
                console.error('动态加载背景图失败:', err);
                return;
            }
            _this.node.getChildByName("Game").getChildByName("bg").getComponent(cc.Sprite).spriteFrame = spriteFrame;
        });
    };
    SceneManager.prototype.loadParticleEffects = function () {
        var _this = this;
        var path = 'Particle';
        cc.resources.loadDir(path, cc.ParticleAsset, function (err, assets) {
            if (err) {
                console.error('动态加载粒子特效失败:', err);
                return;
            }
            _this.particleEffects = assets;
            _this.setParticleSort();
            _this.load3 = true;
            //console.log('粒子特效加载成功，共加载了', this.particleEffects.length, '个粒子特效');
        });
    };
    SceneManager.prototype.setWallPrefabSort = function () {
        var order = ['down', 'downLeft', 'downRight', 'left', 'right', 'up', 'upLeft', 'upRight'];
        this.Wall_Prefab.sort(function (a, b) {
            var indexA = order.indexOf(a.name);
            var indexB = order.indexOf(b.name);
            if (indexA === -1)
                return 1;
            if (indexB === -1)
                return -1;
            return indexA - indexB;
        });
    };
    SceneManager.prototype.setParticleSort = function () {
        var order = ['top', 'bot', 'rig', 'lef'];
        this.particleEffects.sort(function (a, b) {
            // console.log(a.name.substr(0,3),b.name.substr(0,3));
            var indexA = order.indexOf(a.name.substr(0, 3));
            var indexB = order.indexOf(b.name.substr(0, 3));
            if (indexA === -1)
                return 1;
            if (indexB === -1)
                return -1;
            return indexA - indexB;
        });
    };
    SceneManager.prototype.setSort = function () {
        this.Block_Array.sort(function (a, b) {
            var _a, _b;
            // 从名称中提取数字部分
            var numberA = parseInt(((_a = a.name.match(/\d+/)) === null || _a === void 0 ? void 0 : _a[0]) || '0', 10);
            var numberB = parseInt(((_b = b.name.match(/\d+/)) === null || _b === void 0 ? void 0 : _b[0]) || '0', 10);
            return numberA - numberB;
        });
    };
    SceneManager.prototype.start = function () {
    };
    SceneManager.prototype.startGame = function () {
        cc.director.loadScene("HomeScene", function (err) {
            if (err) {
                console.error('加载 HomeScene 场景失败:', err);
            }
            else {
                // console.log('成功切换到 HomeScene 场景');
                cc.director.loadScene("HomeScene");
            }
        });
    };
    SceneManager.prototype.returnHome = function () {
        if (this.node.getChildByName("Pause").getChildByName("btn").getComponent("btnControl")._touch) {
            if (Map_1.default._instance.gameStart == true) {
                MiniGameSdk_1.MiniGameSdk.API.showToast("体力值减少");
                cc.fx.GameTool.setUserHealth(-1, function (data) {
                });
                var data = {
                    time: Map_1.default._instance.count_Time,
                    add_Time: Map_1.default._instance.add_Time,
                    result: "give_up"
                };
                cc.fx.GameTool.shushu_Track("finish_stage", data);
            }
            this.node.getChildByName("Pause").getChildByName("btn").getComponent("btnControl").setTouch(false);
            cc.fx.AudioManager._instance.playEffect("zhuan1", null);
            this.node.getChildByName("zhuanchang").active = true;
            this.node.getChildByName("zhuanchang").getComponent(sp.Skeleton).setAnimation(1, "up", false);
            cc.director.preloadScene("HomeScene", function (err, asset) {
                if (err) {
                    console.error('动态加载 Prefab 失败:', err);
                    return;
                }
            });
            setTimeout(function () {
                cc.director.loadScene("HomeScene");
            }, 1200);
        }
    };
    SceneManager.prototype.destroyNodesInFrames = function (nodes, callback) {
        var _this = this;
        var BATCH_SIZE = 10; // 每帧销毁的节点数量
        var index = 0;
        var destroyBatch = function () {
            var count = 0;
            while (index < nodes.length && count < BATCH_SIZE) {
                var node = nodes[index];
                if (node) {
                    node.active = false;
                }
                index++;
                count++;
            }
            if (index < nodes.length) {
                _this.scheduleOnce(destroyBatch, 6);
            }
            else {
                callback();
            }
        };
        destroyBatch();
    };
    // 改进后的切换场景方法
    SceneManager.prototype.switchToEmptyScene = function () {
        var allNodes = cc.director.getScene().children;
        this.destroyNodesInFrames(allNodes, function () {
            cc.director.loadScene("HomeScene");
        });
    };
    SceneManager.prototype.openPause = function () {
        cc.fx.AudioManager._instance.playEffect("anniu_little", null);
        if (this.pause.getComponent("btnControl")._touch) {
            this.pause.getComponent("btnControl").setTouch(false);
            this.node.getChildByName("Pause").active = true;
            Map_1.default._instance.pause = true;
        }
    };
    SceneManager.prototype.closePause = function () {
        cc.fx.AudioManager._instance.playEffect("anniu_little", null);
        this.pause.getComponent("btnControl").setTouch(true);
        this.node.getChildByName("Pause").active = false;
        if (Map_1.default._instance.node.parent.getChildByName("Ice").active == false) {
            Map_1.default._instance.pause = false;
        }
    };
    SceneManager.prototype.openPropBuy = function (name) {
        Map_1.default._instance.pause = true;
        this.btnName = name;
        var propWindow = this.node.getChildByName("Game").getChildByName("propWindow");
        propWindow.active = true;
        propWindow.getChildByName("freeze").active = false;
        propWindow.getChildByName("hammer").active = false;
        propWindow.getChildByName("magic").active = false;
        propWindow.getChildByName("buy_Btn").getComponent("btnControl").setTouch(true);
        propWindow.getChildByName(name).active = true;
    };
    SceneManager.prototype.clickBtn = function () {
        cc.fx.AudioManager._instance.playEffect("anniu_Big", null);
        var propWindow = this.node.getChildByName("Game").getChildByName("propWindow");
        if (propWindow.getChildByName("buy_Btn").getComponent("btnControl")._touch) {
            propWindow.getChildByName("buy_Btn").getComponent("btnControl").setTouch(false);
            if (this.btnName == "freeze")
                Map_1.default._instance.buyFreeze();
            else if (this.btnName == "hammer")
                Map_1.default._instance.buyHammer();
            else if (this.btnName == "magic")
                Map_1.default._instance.buyMagic();
        }
    };
    //打开商店
    SceneManager.prototype.openShop = function () {
        var _this = this;
        // 加载 shop  bundle
        cc.assetManager.loadBundle('shop', function (err, bundle) {
            bundle.load('prefab/shop', cc.Prefab, function (err, prefab) {
                if (err) {
                    cc.error(err.message || err);
                    return;
                }
                if (prefab) {
                    var shopNode = cc.instantiate(prefab);
                    _this.node.addChild(shopNode);
                    console.log("shopNode parent:", shopNode.parent);
                }
            });
        });
    };
    SceneManager.prototype.closePropBuy = function () {
        Map_1.default._instance.pause = false;
        var freezeBtn = Map_1.default._instance.node.parent.getChildByName("Bottom").getChildByName("timeBtn");
        var hammerBtn = Map_1.default._instance.node.parent.getChildByName("Bottom").getChildByName("destroyBtn");
        var magicBtn = Map_1.default._instance.node.parent.getChildByName("Bottom").getChildByName("magicBtn");
        if (this.btnName == "freeze")
            freezeBtn.getComponent("btnControl").setTouch(true);
        else if (this.btnName == "hammer")
            hammerBtn.getComponent("btnControl").setTouch(true);
        else if (this.btnName == "magic")
            magicBtn.getComponent("btnControl").setTouch(true);
        this.node.getChildByName("Game").getChildByName("propWindow").active = false;
    };
    SceneManager.prototype.update = function (dt) {
    };
    __decorate([
        property(cc.Label)
    ], SceneManager.prototype, "label", void 0);
    __decorate([
        property
    ], SceneManager.prototype, "text", void 0);
    __decorate([
        property(cc.Node)
    ], SceneManager.prototype, "freeze", void 0);
    __decorate([
        property(cc.Node)
    ], SceneManager.prototype, "hammer", void 0);
    __decorate([
        property(cc.Node)
    ], SceneManager.prototype, "magic_wand", void 0);
    __decorate([
        property(cc.Node)
    ], SceneManager.prototype, "pause", void 0);
    __decorate([
        property({ type: [cc.Prefab], tooltip: "方块数组" })
    ], SceneManager.prototype, "Block_Array", void 0);
    __decorate([
        property({ type: [cc.Prefab], tooltip: "墙壁数组" })
    ], SceneManager.prototype, "Wall_Prefab", void 0);
    SceneManager = __decorate([
        ccclass
    ], SceneManager);
    return SceneManager;
}(cc.Component));
exports.default = SceneManager;

cc._RF.pop();