1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import { _decorator, Component, instantiate, Node, NodePool, Pool, Prefab } from 'cc';
- import { resMgr } from '../../../Frames/ResourcesMgr';
- const { ccclass, property } = _decorator;
- @ccclass('BulletPool')
- export class BulletPool extends Component{
- private _bulletPrefab: Prefab = null;
- private _enemyBulletPrefab: Prefab = null;
- private _bulletPool: NodePool = new NodePool();
- private _enemyBulletPool: NodePool = new NodePool();
- init() {
- this._bulletPrefab = resMgr.getPrefab("Bullet");
- this._enemyBulletPrefab = resMgr.getPrefab("BulletEnemy");
- this._initPool(this._bulletPool, this._bulletPrefab, 10);
- this._initPool(this._enemyBulletPool, this._enemyBulletPrefab, 10);
- }
- private _initPool(pool: NodePool, prefab: Prefab, count: number) {
- for (let i = 0; i < count; i++) {
- const node = instantiate(prefab);
- pool.put(node);
- }
- }
- getBullet(isEnemy: boolean): Node {
- const pool = isEnemy ? this._enemyBulletPool : this._bulletPool;
- if(pool.size()>0){
- return pool.get()!;
- }else{
- //动态扩容
- const prefab = isEnemy ? this._enemyBulletPrefab: this._bulletPrefab;
- return instantiate(prefab);
- }
- }
- recycle(bullet: Node, isEnemy: boolean) {
- const pool = isEnemy ? this._enemyBulletPool : this._bulletPool;
- if(!pool) return;
- pool.put(bullet);
- }
- }
|