251f6f7a-33fe-493d-affd-711c8298138c.js 4.13 KB
"use strict";
cc._RF.push(module, '251f696M/5JPa/9cRyCmBOM', 'NodePoolFactory');
// script/common/utils/NodePoolFactory.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodePoolFactory = void 0;
const ResUtils_1 = require("./ResUtils");
const simba_utils_1 = require("simba-utils");
var NodePoolFactory;
(function (NodePoolFactory) {
    let arrPool = {};
    let arrPrefab = {};
    let creationTasks = [];
    let processingTasks = false;
    async function handleTasks() {
        processingTasks = true;
        while (creationTasks.length) {
            let task = creationTasks.shift();
            let prefab = await ResUtils_1.ResUtils.loadRes(task.path, cc.Prefab);
            if (prefab) {
                let pool = new cc.NodePool(task.poolHanderComp);
                for (let i = 0; i < task.count; i++) {
                    let node = cc.instantiate(prefab);
                    node._path = task.path;
                    pool.put(node);
                    if ((i + 1) % 5 == 0)
                        await simba_utils_1.delay(0.01);
                }
                arrPrefab[task.path] = prefab;
                arrPool[task.path] = pool;
            }
            else {
                cc.error("prefab path not find: ", task.path);
            }
            task.callback();
            await simba_utils_1.delay(0.01);
        }
        processingTasks = false;
    }
    async function createPool(path, count = 1, poolHandlerComp) {
        return new Promise((resolve) => {
            if (arrPool[path]) {
                resolve();
                return;
            }
            creationTasks.push({
                path,
                count,
                poolHanderComp: poolHandlerComp,
                callback: () => resolve()
            });
            if (!processingTasks) {
                handleTasks();
            }
        });
    }
    NodePoolFactory.createPool = createPool;
    /**
     * 获取对象池
     * @param path 对象池名称
     */
    function getPool(path) {
        return arrPool[path];
    }
    NodePoolFactory.getPool = getPool;
    /**
     * 清空对象池
     * @param path 对象池名称
     */
    function clearPool(...path) {
        if (path == null || path.length <= 0)
            return;
        for (let i = 0; i < path.length; i++) {
            let one = path[i];
            if (arrPool[one]) {
                arrPool[one].clear();
                delete arrPool[one];
            }
        }
        ResUtils_1.ResUtils.releaseResArray(path);
        cc.sys.garbageCollect();
    }
    NodePoolFactory.clearPool = clearPool;
    /**
     * 从对象池中取出一个node对象
     * @param path
     */
    function getNode(path) {
        // cc.log("getNode:",path)
        if (arrPool[path]) {
            if (arrPool[path].size() > 0) {
                return arrPool[path].get();
            }
            else {
                let node = cc.instantiate(arrPrefab[path]);
                node._path = path;
                return node;
            }
        }
        else {
            console.error("pool not find:", path);
            return null;
        }
    }
    NodePoolFactory.getNode = getNode;
    /**
     * 把对象放回对象池
     * @param path
     * @param obj
     */
    function putNode(obj) {
        let path = obj._path;
        if (arrPool[path]) {
            arrPool[path].put(obj);
        }
        else {
            obj.removeFromParent(true);
            console.error("pool not find:", path);
        }
    }
    NodePoolFactory.putNode = putNode;
    /**
    * 清空所有对象池
    */
    function clearAll() {
        let pathArr = [];
        for (const key in arrPool) {
            if (arrPool.hasOwnProperty(key)) {
                pathArr[pathArr.length] = key;
                const pool = arrPool[key];
                pool.clear();
            }
        }
        ResUtils_1.ResUtils.releaseResArray(pathArr);
        arrPool = {};
        arrPrefab = {};
        cc.sys.garbageCollect();
    }
    NodePoolFactory.clearAll = clearAll;
})(NodePoolFactory = exports.NodePoolFactory || (exports.NodePoolFactory = {}));

cc._RF.pop();