pool.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { _decorator, Component, instantiate, Node, NodePool, Prefab } from 'cc';
  2. import { box } from './box';
  3. import { coin } from './coin';
  4. import { wall } from './wall';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('pool')
  7. export class pool extends Component {
  8. @property(Prefab) box_pf:Prefab = null;
  9. @property(Prefab) coin_pf:Prefab = null;
  10. @property(Prefab) wall_pf:Prefab = null;
  11. private box_pool:NodePool = null;
  12. private coin_pool:NodePool = null;
  13. private wall_pool:NodePool = null;
  14. private pool_num:number = 50;
  15. public init(){
  16. if(this.box_pool==null){
  17. this.box_pool = new NodePool(box);
  18. this.coin_pool = new NodePool(coin);
  19. this.wall_pool = new NodePool(wall);
  20. for (let index = 0; index < this.pool_num; index++) {
  21. this.box_pool.put(instantiate(this.box_pf));
  22. this.coin_pool.put(instantiate(this.coin_pf));
  23. this.wall_pool.put(instantiate(this.wall_pf));
  24. }
  25. this.schedule(()=>{
  26. if(this.box_pool.size()<this.pool_num){
  27. let box = instantiate(this.box_pf);
  28. this.box_pool.put(box);
  29. }
  30. if(this.coin_pool.size()<this.pool_num){
  31. const coin = instantiate(this.coin_pf);
  32. this.coin_pool.put(coin);
  33. }
  34. if(this.wall_pool.size()<this.pool_num){
  35. const wall = instantiate(this.wall_pf);
  36. this.wall_pool.put(wall);
  37. }
  38. },0.5)
  39. }
  40. }
  41. public getBoxPool():NodePool{
  42. return this.box_pool
  43. }
  44. public getCoinPool():NodePool{
  45. return this.coin_pool
  46. }
  47. public getWallPool():NodePool{
  48. return this.wall_pool
  49. }
  50. public putBox(node:Node){
  51. this.box_pool.put(node)
  52. }
  53. public putCoin(node:Node){
  54. this.coin_pool.put(node)
  55. }
  56. public putWall(node:Node){
  57. this.wall_pool.put(node)
  58. }
  59. public getBox(){
  60. if(this.box_pool.size()<=0){
  61. let node = instantiate(this.box_pf)
  62. this.box_pool.put(node)
  63. }
  64. return this.box_pool.get()
  65. }
  66. public getCoin(){
  67. if(this.coin_pool.size()<=0){
  68. let node = instantiate(this.coin_pf)
  69. this.coin_pool.put(node)
  70. }
  71. return this.coin_pool.get()
  72. }
  73. public getWall(){
  74. if(this.wall_pool.size()<=0){
  75. let node = instantiate(this.wall_pf)
  76. this.wall_pool.put(node)
  77. }
  78. return this.wall_pool.get()
  79. }
  80. }