const { ccclass, property } = cc._decorator;
|
|
export enum PoolType {
|
BALL = 'ball',
|
BRICK = 'brick',
|
PROP_SHOOT = 'shoot',
|
PROP_TIMES = 'times',
|
PROP_WALL = 'wall'
|
}
|
|
@ccclass
|
export class PoolManager extends cc.Component {
|
private static instance: PoolManager;
|
|
@property(cc.Prefab)
|
ballPrefab: cc.Prefab = null;
|
@property(cc.Prefab)
|
brickPrefab: cc.Prefab = null;
|
@property(cc.Prefab)
|
propShootPrefab: cc.Prefab = null;
|
@property(cc.Prefab)
|
propTimesPrefab: cc.Prefab = null;
|
@property(cc.Prefab)
|
propWallPrefab: cc.Prefab = null;
|
|
public static get Ins() {
|
return PoolManager.instance;
|
}
|
|
private readonly ballReadyCount = 1000;
|
private readonly brickReadyCount = 1000;
|
private readonly shootReadyCount = 20;
|
private readonly timesReadyCount = 20;
|
private readonly wallReadyCount = 5;
|
private pools = {};
|
private createCounter = {};
|
onLoad() {
|
PoolManager.instance = this;
|
cc.game.addPersistRootNode(this.node);
|
|
this.pools[PoolType.BALL] = new cc.NodePool('Ball');
|
this.pools[PoolType.BRICK] = new cc.NodePool('Brick');
|
this.pools[PoolType.PROP_SHOOT] = new cc.NodePool('Prop');
|
this.pools[PoolType.PROP_TIMES] = new cc.NodePool('Prop');
|
this.pools[PoolType.PROP_WALL] = new cc.NodePool('Prop');
|
}
|
|
start() {
|
this.schedule(() => {
|
this.tryLoad();
|
}, 0.033);
|
}
|
|
private tryLoad() {
|
let counter = this.createCounter[PoolType.BALL] || 0;
|
let fullFlag = 0;
|
if (counter < this.ballReadyCount) {
|
for (let i = 0; i < 10; ++i) {
|
this._create(PoolType.BALL);
|
}
|
} else {
|
fullFlag++;
|
}
|
|
counter = this.createCounter[PoolType.BRICK] || 0;
|
if (counter < this.brickReadyCount) {
|
for (let i = 0; i < 10; ++i) {
|
this._create(PoolType.BRICK);
|
}
|
} else {
|
fullFlag++;
|
}
|
|
counter = this.createCounter[PoolType.PROP_SHOOT] || 0;
|
if (counter < this.shootReadyCount) {
|
this._create(PoolType.PROP_SHOOT);
|
} else {
|
fullFlag++;
|
}
|
|
counter = this.createCounter[PoolType.PROP_TIMES] || 0;
|
if (counter < this.timesReadyCount) {
|
this._create(PoolType.PROP_TIMES);
|
} else {
|
fullFlag++;
|
}
|
|
counter = this.createCounter[PoolType.PROP_WALL] || 0;
|
if (counter < this.wallReadyCount) {
|
this._create(PoolType.PROP_WALL);
|
} else {
|
fullFlag++;
|
}
|
|
if (fullFlag >= 5) {
|
this.unscheduleAllCallbacks();
|
console.log('poolmanager all reloaded.');
|
}
|
}
|
|
private _create(poolType: PoolType) {
|
let counter = this.createCounter[poolType] || 0;
|
let _node = null;
|
if (poolType == PoolType.BALL) {
|
_node = cc.instantiate(this.ballPrefab);
|
} else if (poolType == PoolType.BRICK) {
|
_node = cc.instantiate(this.brickPrefab);
|
} else if (poolType == PoolType.PROP_SHOOT) {
|
_node = cc.instantiate(this.propShootPrefab);
|
} else if (poolType == PoolType.PROP_TIMES) {
|
_node = cc.instantiate(this.propTimesPrefab);
|
} else if (poolType == PoolType.PROP_WALL) {
|
_node = cc.instantiate(this.propWallPrefab);
|
} else {
|
cc.error('PoolManager create null');
|
}
|
|
counter++;
|
this.createCounter[poolType] = counter;
|
|
let pool: cc.NodePool = this.pools[poolType];
|
pool.put(_node);
|
}
|
|
get(poolType: PoolType, manager: any) {
|
let _node = null;
|
let pool: cc.NodePool = this.pools[poolType];
|
if (pool.size() > 0) {
|
_node = pool.get(manager);
|
} else {
|
this._create(poolType);
|
_node = pool.get(manager);
|
}
|
|
return _node;
|
}
|
|
put(poolType: PoolType, node: cc.Node) {
|
let pool: cc.NodePool = this.pools[poolType];
|
pool.put(node);
|
}
|
}
|