PoolMagr.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import { _decorator, Prefab, Node, instantiate, NodePool, Vec3 } from "cc";
  2. const { ccclass, property } = _decorator;
  3. @ccclass("PoolMgr")
  4. export class PoolMgr {
  5. private _dictPool: any = {}
  6. private _dictPrefab: any = {}
  7. static _ins: PoolMgr;
  8. /* class member could be defined like this */
  9. // dummy = '';
  10. static get ins() {
  11. if (this._ins) {
  12. return this._ins;
  13. }
  14. this._ins = new PoolMgr();
  15. window["xxx"] = this._ins;
  16. return this._ins;
  17. }
  18. public copyNode(copynode: Node, parent: Node | null): Node {
  19. let name = copynode.name;
  20. this._dictPrefab[name] = copynode;
  21. let node = null;
  22. if (this._dictPool.hasOwnProperty(name)) {
  23. let pool = this._dictPool[name];
  24. if (pool.size() > 0) {
  25. node = pool.get();
  26. } else {
  27. node = instantiate(copynode);
  28. }
  29. } else {
  30. let pool = new NodePool();
  31. this._dictPool[name] = pool;
  32. node = instantiate(copynode);
  33. }
  34. if (parent) {
  35. node.parent = parent;
  36. node.active = true;
  37. }
  38. return node;
  39. }
  40. /**
  41. * @description: get the node from the pool
  42. * @param {Prefab} prefab
  43. * @param {Node} parent
  44. * @param {Vec3} pos
  45. * @return {*}
  46. */
  47. public getNode(prefab: Prefab | string, parent?: Node, pos?: Vec3): Node {
  48. let tempPre;
  49. let name;
  50. if (typeof prefab === 'string') {
  51. tempPre = this._dictPrefab[prefab];
  52. name = prefab;
  53. if (!tempPre) {
  54. console.log("Pool invalid prefab name = ", name);
  55. return null;
  56. }
  57. }
  58. else {
  59. tempPre = prefab;
  60. name = prefab.data.name;
  61. }
  62. let node = null;
  63. if (this._dictPool.hasOwnProperty(name)) {
  64. //已有对应的对象池
  65. let pool = this._dictPool[name];
  66. if (pool.size() > 0) {
  67. node = pool.get();
  68. } else {
  69. node = instantiate(tempPre);
  70. }
  71. } else {
  72. //没有对应对象池,创建他!
  73. let pool = new NodePool();
  74. this._dictPool[name] = pool;
  75. node = instantiate(tempPre);
  76. }
  77. if (parent) {
  78. node.parent = parent;
  79. node.active = true;
  80. if (pos) node.position = pos;
  81. }
  82. return node;
  83. }
  84. // /**
  85. // * @name:
  86. // * @msg:
  87. // * @param {Prefab} prefab 预制体名
  88. // * @param {true} play 是否播放
  89. // * @param {Vec3} pos 位置
  90. // * @param {*} time 回收时间,不回收不填
  91. // * @return {*}
  92. // */
  93. // public playSkill(prefab: Prefab | string, play: boolean=true, pos?: Vec3, time?,parent?):Node {
  94. // let node = this.getNode(prefab, parent?parent:Global.stage[3], pos)
  95. // if (play) {
  96. // node.getComponent(EffectPlay).play()
  97. // }
  98. // if (time) {
  99. // setTimeout(() => {
  100. // if (play) {
  101. // node.getComponent(EffectPlay).stop()
  102. // }
  103. // this.putNode(node)
  104. // }, time * 1000);
  105. // }
  106. // return node
  107. // }
  108. /**
  109. * @description: put the node into the pool
  110. * @param {Node} node
  111. * @param {*} isActive
  112. * @return {*}
  113. */
  114. public putNode(node: Node | null, isActive = false) {
  115. if (!node) {
  116. return;
  117. }
  118. //console.log("回收信息",node.name,node)
  119. let name = node.name;
  120. let pool = null;
  121. // node.active = isActive
  122. if (this._dictPool.hasOwnProperty(name)) {
  123. //已有对应的对象池
  124. pool = this._dictPool[name];
  125. } else {
  126. //没有对应对象池,创建他!
  127. pool = new NodePool();
  128. this._dictPool[name] = pool;
  129. }
  130. pool.put(node);
  131. }
  132. /**
  133. * @description: clear the pool based on name
  134. * @param {string} name
  135. * @return {*}
  136. */
  137. public clearPool(name: string) {
  138. if (this._dictPool.hasOwnProperty(name)) {
  139. let pool = this._dictPool[name];
  140. pool.clear();
  141. }
  142. }
  143. public setPrefab(name: string, prefab: Prefab): void {
  144. this._dictPrefab[name] = prefab;
  145. }
  146. public getPrefab(name: string): Prefab {
  147. return this._dictPrefab[name];
  148. }
  149. }