NodePool.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. var s =
  5. (this && this.__decorate) ||
  6. function (t, e, i, s) {
  7. var o,
  8. a = arguments.length,
  9. n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s;
  10. if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s);
  11. else
  12. for (var r = t.length - 1; 0 <= r; r--)
  13. (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n);
  14. return 3 < a && n && Object.defineProperty(e, i, n), n;
  15. };
  16. Object.defineProperty(i, "__esModule", {value: !0});
  17. const {ccclass: o, property: a} = cc._decorator;
  18. e = class {
  19. constructor() {
  20. (this._pool = []), (this.createCnt = 0), (this.prefab = null);
  21. }
  22. push(t) {
  23. t && -1 === this._pool.indexOf(t) && ((t.active = !1), t.parent && t.removeFromParent(!1), this._pool.push(t));
  24. }
  25. push2(t, e) {
  26. this._pool.length >= e
  27. ? (t.removeFromParent(!0), t.destroy())
  28. : t &&
  29. -1 === this._pool.indexOf(t) &&
  30. ((t.active = !1), t.parent && t.removeFromParent(!1), this._pool.push(t));
  31. }
  32. pop() {
  33. if (0 === this._pool.length) {
  34. this.createCnt++;
  35. let t = cc.instantiate(this.prefab);
  36. return (t.pool = this), t;
  37. }
  38. {
  39. let t = this._pool.pop();
  40. return (t.active = !0), t;
  41. }
  42. }
  43. clear() {
  44. for (const t of this._pool) t.isValid && t.destroy();
  45. this._pool = null;
  46. }
  47. init(e) {
  48. this.createCnt += e;
  49. for (let t = 0; t < e; ++t) this._pool.push(cc.instantiate(this.prefab));
  50. }
  51. };
  52. s([a(cc.Prefab)], e.prototype, "prefab", void 0), (e = s([o("NodePool")], e)), (i.default = e);