mwg API
    Preparing search index...

    Class ParticleEmitter

    A pooled particle emitter: sparks off a sword hit, dust under a footstep, rain over a map.

    Pooled rather than allocating per particle, because this is the one primitive in the framework that can be asked to create and discard thousands of short-lived things per second, and a garbage collector pause is exactly the frame-time cost this project's own performance priority cares about. The pool is allocated once at max and reused forever after; asking for more particles than that while max are already alive drops the request rather than growing, so a runaway emitter degrades visibly instead of eating memory.

    Every random draw goes through mwg/core's seeded Random, so a replayed or seeded run produces the same spray, rather than particles being the one thing on screen that a deterministic replay cannot reproduce.

    import { ParticleEmitter, type Texture2D } from '@datamoc/mw_games/two-d/render';

    declare const sparkTexture: Texture2D;

    const sparks = new ParticleEmitter({
    texture: sparkTexture,
    max: 64,
    life: [0.2, 0.4],
    speed: [80, 160],
    gravity: { x: 0, y: 300 },
    scale: [1, 0.2],
    alpha: [1, 0],
    });
    sparks.x = 100;
    sparks.y = 60;

    sparks.burst(12); // a sword hit: 12 particles at once, fewer if the pool is nearly full
    sparks.update(1 / 60); // steps physics and, since a texture was given, the sprites too
    console.log(sparks.activeCount); // 12 - still alive right after the burst

    Hierarchy

    • Container
      • ParticleEmitter
    Index
    • Emits count particles at once - a hit spark, an explosion.

      Parameters

      • count: number

      Returns number

      how many were actually emitted, which is fewer than asked when the pool is full