phaser icon indicating copy to clipboard operation
phaser copied to clipboard

Allow to pass custom params to ParticleEmitterConfig#particleClass

Open michalfialadev opened this issue 1 year ago • 0 comments

Currently, the ParticleEmitter's particleClass only accepts a class reference, and (assumption) creates instances of this class without passing any optional constructor arguments, except the required Phaser.GameObjects.Particles.ParticleEmitter argument. This makes such particle classes tied to implementation. It would be preferable to be able to pass array (or an object) of (optional) arguments, so that when for ex the configuration for the particle system comes from server as a game-variation-specific configuration, it can be set dynamically. This would make the custom particle itself more generic, which is a good thing. Similar system is used by tweens eg onComplete and onCompleteParams, and could be used by custom particles eg particleClass and particleClassParams.

To take a live example from 3.55.2: http://127.0.0.1:55572/phaser3-examples/edit.html?src=src/game%20objects/particle%20emitter/custom%20particles.js

It could look like:

class AnimatedParticle extends Phaser.GameObjects.Particles.Particle
{
    t: number;
    i: number;

    constructor (emitter, params: any)
    {
        super(emitter);

        this.t = params.t as number;
        this.i = params.i as number;

        // OR: 
        // this.t = params[0] as number;
        // this.i = params[1] as number;
    }

Maybe its possible to spread the params (something in JS akin to rest?), not sure about the exact JS implementation..

class AnimatedParticle extends Phaser.GameObjects.Particles.Particle
{
    t: number;
    i: number;

    constructor (emitter: Phaser.GameObjects.Particles.ParticleEmitter, myArg1: number, myArg2: number)
    {
        super(emitter);

        this.t = myArg1
        this.i = myArg2;
    }

michalfialadev avatar Apr 17 '23 06:04 michalfialadev