123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- System.register(["cc"], function (_export, _context) {
- "use strict";
- var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, Component, instantiate, Prefab, Vec3, _dec, _class, _class2, _crd, ccclass, property, PoolManager;
- return {
- setters: [function (_cc) {
- _cclegacy = _cc.cclegacy;
- __checkObsolete__ = _cc.__checkObsolete__;
- __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
- _decorator = _cc._decorator;
- Component = _cc.Component;
- instantiate = _cc.instantiate;
- Prefab = _cc.Prefab;
- Vec3 = _cc.Vec3;
- }],
- execute: function () {
- _crd = true;
- _cclegacy._RF.push({}, "43f17BYDINONr03pwdby0M9", "PoolManager", undefined);
- __checkObsolete__(['_decorator', 'Component', 'instantiate', 'Node', 'Prefab', 'Vec3']);
- ({
- ccclass,
- property
- } = _decorator);
- /**
- * 对象池管理器
- * 功能:
- * 1. 管理游戏对象的复用
- * 2. 减少频繁创建/销毁对象的性能开销
- * 3. 支持Prefab和Node两种类型的对象池
- */
- _export("PoolManager", PoolManager = (_dec = ccclass('PoolManager'), _dec(_class = (_class2 = class PoolManager extends Component {
- /**
- * 从对象池获取节点(支持Prefab或Node)
- * @param prefab - Prefab资源或节点模板
- * @param parent - 可选父节点
- * @returns 可复用的节点实例
- */
- static getNode(prefab, parent) {
- var poolKey = prefab instanceof Prefab ? prefab.data.name : prefab.name;
- var node = null; //从对象池获取可用节点
- var pool = this._objectPools.get(poolKey);
- if ((pool == null ? void 0 : pool.length) > 0) {
- node = pool.pop();
- } else {
- node = instantiate(prefab);
- } //初始化节点状态
- if (node) {
- node.active = true;
- node.position = Vec3.ZERO;
- if (parent) {
- node.setParent(parent);
- }
- }
- return node;
- }
- /**
- * 通过名称从对象池获取节点
- * @param poolKey - 对象池键名
- * @param parent - 可选父节点
- * @returns 可复用的节点实例(如果没有则返回null)
- */
- static getName(poolKey, parent) {
- var pool = this._objectPools.get(poolKey);
- var node = pool == null ? void 0 : pool.pop();
- if (node) {
- node.active = true;
- if (parent) {
- node.setParent(parent);
- }
- }
- return node != null ? node : null;
- }
- /**
- * 将节点回收到对象池
- * @param node - 要回收的节点
- */
- static putNode(node) {
- if (!(node != null && node.isValid) || !node) return;
- var poolKey = node.name; //清理节点状态
- node.removeFromParent();
- node.active = false; //存入对象池
- if (poolKey) {
- if (!this._objectPools.has(poolKey)) {
- this._objectPools.set(poolKey, []);
- }
- this._objectPools.get(poolKey).push(node);
- } else {
- //没有有效名称的节点直接销毁
- node.destroy();
- }
- }
- /**
- * 获取指定对象池的大小
- * @param poolKey - 对象池键名
- * @returns 对象池中可用节点的数量
- */
- static getPoolSize(poolKey) {
- var _this$_objectPools$ge, _this$_objectPools$ge2;
- return (_this$_objectPools$ge = (_this$_objectPools$ge2 = this._objectPools.get(poolKey)) == null ? void 0 : _this$_objectPools$ge2.length) != null ? _this$_objectPools$ge : 0;
- }
- /**
- * 清空指定对象池
- * @param poolKey - 要清空的对象池键名
- */
- static clearPool(poolKey) {
- var pool = this._objectPools.get(poolKey);
- if (pool) {
- pool.forEach(node => node.destroy());
- this._objectPools.delete(poolKey);
- }
- }
- /**
- * 清空所有对象池
- */
- static clearAll() {
- this._objectPools.forEach((pool, key) => {
- pool.forEach(node => node.destroy());
- });
- this._objectPools.clear();
- }
- }, _class2._objectPools = new Map(), _class2)) || _class));
- _cclegacy._RF.pop();
- _crd = false;
- }
- };
- });
- //# sourceMappingURL=a61bc3ba3fb43f9bad46eb552a7a4a7650b1f404.js.map
|