PoolM.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { instantiate } from "cc";
  2. import { Prefab } from "cc";
  3. import AB from "../base/AB";
  4. export class PoolType {
  5. public static readonly POG = 1;
  6. public static readonly GEM = 2;
  7. }
  8. export default class PoolM {
  9. private static _ins: PoolM;
  10. public static get ins(): PoolM {
  11. if (!PoolM._ins) {
  12. PoolM._ins = new PoolM();
  13. }
  14. return PoolM._ins;
  15. }
  16. private pool: Map<number, any> = new Map();
  17. private prefab: Map<number, Prefab> = new Map();
  18. public async get(name: number, prefab: Prefab): Promise<Node> {
  19. // if (!this.pool.has(name)) {
  20. // this.pool.set(name, instantiate(prefab));
  21. // }
  22. // return this.pool.get(name);
  23. //@ts-ignore
  24. let node: Node = instantiate(prefab);
  25. return node;
  26. }
  27. public put(name: number, node: Node) {
  28. if (this.pool.has(name)) {
  29. this.pool.get(name).destroy();
  30. }
  31. this.pool.set(name, node);
  32. }
  33. public async getGem(): Promise<Node> {
  34. return this.getByPath(PoolType.GEM, "prefab/fly/gem");
  35. }
  36. public async getPog() {
  37. return this.getByPath(PoolType.POG, "prefab/fly/pog");
  38. }
  39. public async getByPath(key: number, path: string): Promise<Node> {
  40. if (!this.prefab.has(key)) {
  41. let p = await AB.ins.loadPrefab(path);
  42. this.prefab.set(key, p);
  43. }
  44. return this.get(key, this.prefab.get(key));
  45. }
  46. }