PoolManager.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { IObjectPoolManager } from "./IObjectPoolManager";
  2. /**
  3. * 全局对象池管理
  4. */
  5. export class PoolManager implements IObjectPoolManager {
  6. public _poolMap: Map<string, cc.NodePool> = new Map<string, cc.NodePool>();
  7. GetCount(): number {
  8. throw new Error("Method not implemented.");
  9. }
  10. CreatePool(poolName: string): cc.NodePool {
  11. if (this._poolMap[poolName]) {
  12. console.warn('对象池已经存在,无需重复创建!');
  13. return null;
  14. }
  15. let pool = new cc.NodePool();
  16. this._poolMap[poolName] = pool;
  17. return pool;
  18. }
  19. CreatePrePool(poolName: string, prefab: cc.Prefab, maxNum: number): cc.NodePool {
  20. if (this._poolMap[poolName]) {
  21. console.warn('对象池已经存在,无需重复创建!');
  22. return null;
  23. }
  24. let pool = new cc.NodePool();
  25. this._poolMap[poolName] = pool;
  26. for (let i = 0; i < maxNum; i++) {
  27. let node = cc.instantiate(prefab);
  28. pool.put(node);
  29. }
  30. }
  31. GetPool(poolName: string): cc.NodePool {
  32. if (this._poolMap[poolName]) {
  33. return this._poolMap[poolName];
  34. } else {
  35. return this.CreatePool(poolName);
  36. }
  37. }
  38. ReleasePool(poolName: string) {
  39. if (this._poolMap[poolName]) {
  40. this._poolMap[poolName].clear();
  41. }
  42. // else {
  43. // console.error(`对象池${poolName}不存在`);
  44. // }
  45. }
  46. ReleaseAllPool() {
  47. for (let key in this._poolMap) {
  48. this._poolMap[key].clear();
  49. }
  50. }
  51. }