lq_pool_util.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import Node = cc.Node;
  2. import Animation = cc.Animation;
  3. import ParticleSystem = cc.ParticleSystem;
  4. import instantiate = cc.instantiate;
  5. export class LQPoolUtil {
  6. private static any_pool: { [key: string]: Node[] } = {};
  7. private static reset_ani(node: Node) {
  8. let ani = node.getComponent(Animation);
  9. if (ani) {
  10. let clip = ani.currentClip ? ani.currentClip : ani.defaultClip;
  11. if (!clip) {
  12. return;
  13. }
  14. if (ani.playOnLoad && clip && clip.wrapMode === cc.WrapMode.Normal) {
  15. ani.play(clip.name);
  16. }
  17. } else {
  18. let ani = node.getComponent(sp.Skeleton);
  19. if (ani && !ani.loop) {
  20. ani.setAnimation(0, ani.animation, false);
  21. }
  22. }
  23. for (let i = 0; i < node.childrenCount; i++) {
  24. const child = node.children[i];
  25. this.reset_ani(child);
  26. }
  27. }
  28. public static recursion_stop_particle(node: Node, obj: { has: boolean }) {
  29. const p = node.getComponent(ParticleSystem);
  30. if (p) {
  31. p.stopSystem();
  32. obj.has = true;
  33. p.node.opacity = 0;
  34. }
  35. for (let i = 0; i < node.childrenCount; i++) {
  36. const child = node.children[i];
  37. this.recursion_stop_particle(child, obj);
  38. }
  39. }
  40. public static recursion_reset_particle(node: Node) {
  41. if (!node.isValid) {
  42. return;
  43. }
  44. const p = node.getComponent(ParticleSystem);
  45. if (p) {
  46. p.resetSystem();
  47. p.node.opacity = 255;
  48. }
  49. for (let i = 0; i < node.childrenCount; i++) {
  50. const child = node.children[i];
  51. this.recursion_reset_particle(child);
  52. }
  53. }
  54. public static get_node_from_pool(node_parent: Node, prefab: Node) {
  55. let arr = this.any_pool[prefab.uuid];
  56. if (!arr) {
  57. this.any_pool[prefab.uuid] = [];
  58. arr = [];
  59. }
  60. let node = arr.pop();
  61. if (!node || !node.isValid) {
  62. node = instantiate(prefab);
  63. //@ts-ignore
  64. node.recovery_uuid = prefab.uuid;
  65. //@ts-ignore
  66. node.is_from_pool = false;
  67. node_parent.addChild(node);
  68. } else {
  69. node.active = true;
  70. //@ts-ignore
  71. node.is_from_pool = true;
  72. this.reset_ani(node);
  73. }
  74. return node;
  75. }
  76. private static check_pool_push(arr: Node[], node: Node) {
  77. for (let i = 0; i < arr.length; i++) {
  78. if (arr[i] === node) {
  79. //@ts-ignore
  80. console.warn(`池子不能重复添加节点`, node.name, node.recovery_uuid);
  81. return;
  82. }
  83. }
  84. node.active = false;
  85. arr.push(node);
  86. }
  87. public static push_node_to_pool(node: Node) {
  88. //@ts-ignore
  89. if (!node.recovery_uuid || !this.any_pool[node.recovery_uuid]) {
  90. if (node.isValid) {
  91. node.destroy();
  92. }
  93. return;
  94. }
  95. const obj: { has: boolean } = {has: false};
  96. this.recursion_stop_particle(node, obj);
  97. if (obj.has) {
  98. let old_opacity = node.opacity;
  99. node.opacity = 0;
  100. setTimeout(() => {
  101. this.recursion_reset_particle(node);
  102. node.opacity = old_opacity;
  103. //@ts-ignore
  104. this.check_pool_push(this.any_pool[node.recovery_uuid], node);
  105. }, 500);
  106. } else {
  107. //@ts-ignore
  108. this.check_pool_push(this.any_pool[node.recovery_uuid], node);
  109. }
  110. }
  111. }