3

I am currently making a Tower Defense game in Phaser 3 and I need some help refactoring my code to implement a generic pool. Now I have several diferent pools that I fill manually.

for (let i = 0; i < 10; i++) {
      let basicEnem = new Enemy(this, 'enemySprite', elements.FIRE,400, 400, 150, 20);
      this.EnemyPool.add(basicEnem);
      this.EnemyPool.killAndHide(basicEnem);
    }
for (let i = 0; i < 200; i++) {
      let bull = new Bullet(this, 50, 400, 90, 10, 100, elements.FIRE, 'bulletSprite');
      this.BulletPool.add(bull);
      this.BUlletPool.killAndHide(bull);
    }

I'd like to create a Pool class which creates a number of copies with an instance. I'd thought about deep cloning but I've been told it doesnt with class methods.

export default class Pool extends Phaser.GameObjects.Group {
    constructor(scene, isPhysical,poolItem,baseSize) {
        super();
        this._scene = scene;
        this._group;
        if (isPhysical)
            this._group = this._scene.physics.add.group();
        else
            this._group = this._scene.add.group();
        for(let i = 0;i<baseSize;i++){
            //a copy of any instance of poolItem class
        }
    }

What should I replace that comment with to make a copy of any given instance and make it equivalent to the first code snippet?

AverZeta
  • 41
  • 1

0 Answers0