/****************************************************************** * * 桥接对象池管理器 * ******************************************************************/ cc.Class( { extends: cc.Component, ctor: function() { this.pools = {}; }, onLoad: function() { nx.pools = this; this.clean(); }, onDestroy: function() { if( window.nx ){ nx.pools = null; this.clean(); } }, // 获取单池(通过Node节点) getPool: function( _key, _node, _initCount = 10 ) { // 存在返回 let pool = this.pools[_key]; if( pool ) { return pool; } // 新建无模板 if( !_node ) { nx.error( `[对象池]不存在${_key},获取失败!` ); return null; } // Prefab实例化 if( _node instanceof cc.Prefab ) { _node = cc.instantiate( _node ); } // 新建 pool = nx.factory.create( "bridge.pool", { key: _key, prefab: _node, count: _initCount, } ); this.pools[_key] = pool; return pool; }, // 对象回收 put: function( _node ) { if( !_node ) { return; } if( nx.dt.strEmpty( _node.poolKey ) ) { nx.error( "回收失败,没有关联池信息!" ); _node.removeFromParent( true ); return; } // 池子不存在 let pool = this.pools[_node.poolKey]; if( !pool ) { nx.warn( "[回收失败,没有关联池:", _node.poolKey ); _node.removeFromParent( true ); return; } // 回收 _node.position = cc.Vec2.ZERO; pool.put( _node ); }, // 全子节点回收 putChildren: function( _node ) { let chds = _node.children; while( chds.length > 0 ) { this.put( chds[0] ); } _node.removeAllChildren( false ); }, // 清理 clean: function() { for( let k in this.pools ) { nx.factory.remove( this.pools[k] ); } this.pools = {}; }, } );