75 lines
1.5 KiB
JavaScript
75 lines
1.5 KiB
JavaScript
|
|
|
||
|
|
/******************************************************************
|
||
|
|
*
|
||
|
|
* 桥接节点池
|
||
|
|
*
|
||
|
|
******************************************************************/
|
||
|
|
|
||
|
|
const NxObject = require( "nx.object" );
|
||
|
|
|
||
|
|
const BridgeNodePool = cc.Class( {
|
||
|
|
|
||
|
|
extends: NxObject,
|
||
|
|
|
||
|
|
name: "BridgeNodePool",
|
||
|
|
|
||
|
|
// 初始化
|
||
|
|
initialize: function( _args ) {
|
||
|
|
|
||
|
|
if( !this._super( _args ) ) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 创建节点池
|
||
|
|
this.np = new cc.NodePool();
|
||
|
|
|
||
|
|
let count = _args.count || 10;
|
||
|
|
this.prefab = _args.prefab;
|
||
|
|
this.key = _args.key || "pool";
|
||
|
|
|
||
|
|
// 存量初始化
|
||
|
|
for( let i = 0; i < count; ++i ) {
|
||
|
|
let node = cc.instantiate( this.prefab );
|
||
|
|
node.poolKey = this.key;
|
||
|
|
this.np.put( node );
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
},
|
||
|
|
|
||
|
|
// 销毁
|
||
|
|
uninitialize: function() {
|
||
|
|
|
||
|
|
this.np.clear();
|
||
|
|
|
||
|
|
// USPER
|
||
|
|
return this._super();
|
||
|
|
},
|
||
|
|
|
||
|
|
// 复用
|
||
|
|
get: function() {
|
||
|
|
|
||
|
|
// nx.debug( `>$>>${this.key}取:${this.np.size()}` );
|
||
|
|
|
||
|
|
let node = this.np.get();
|
||
|
|
if( node ) {
|
||
|
|
return node;
|
||
|
|
}
|
||
|
|
|
||
|
|
// 追加
|
||
|
|
// nx.debug( `${this.key}触发复用,数量超限新建` );
|
||
|
|
node = cc.instantiate( this.prefab );
|
||
|
|
node.poolKey = this.key;
|
||
|
|
return node;
|
||
|
|
},
|
||
|
|
|
||
|
|
// 回收
|
||
|
|
put: function( _node ) {
|
||
|
|
this.np.put( _node );
|
||
|
|
// nx.debug( `>$>>${this.key}存:${this.np.size()}` );
|
||
|
|
},
|
||
|
|
|
||
|
|
} );
|
||
|
|
|
||
|
|
|
||
|
|
module.exports = BridgeNodePool;
|