Emitter

View source

Stores the configuration used to emit timed particle entities.

Pair this component with a matching GlobalTransform2D or GlobalTransform3D and a Timer. When it is enabled and the timer starts a cycle, emission samples its configured ranges and creates the resulting particles. Each particle receives a sampled lifetime, a Particle marker, a Timer, and the emitter’s current world transform. Optional prefab and patch callbacks customize the particle’s components before and after it is built.

Use this component on the entity that represents the emitter; the matching transform determines particle placement and the timer determines emission cycles. The member documentation describes the available configuration and its defaults.

Examples

import { World } from '@wimaengine/ecs'
import { Range } from '@wimaengine/datastructures'
import { Emitter } from '@wimaengine/emitter'
import { GlobalTransform2D } from '@wimaengine/transform'
import { Timer, TimerMode } from '@wimaengine/time'

// These represent application-defined particle components.
class SparkVisual {}
class SpawnedByEmitter {}

const world = new World()
world.spawn([
  new GlobalTransform2D(),
  new Timer({ duration: 0.1, mode: TimerMode.Repeat }),
  new Emitter({
    prefab: () => [new SparkVisual()],
    patch: (commands, entity) => {
      commands.entity(entity).insert(new SpawnedByEmitter()).build()
    },
    burstCount: new Range(2, 4),
    lifetime: new Range(0.5, 1.5)
  })
])

Constructor


constructor(options?: EmitterOptions)

View source

Creates an emitter configuration.

Supplied burstCount and lifetime objects are stored as-is rather than copied. The default ranges are created for this emitter when those options are omitted.

Parameters

NameDescriptionDefault
options?—{}

Properties


prefab: object

View source

Factory that returns extra components for each emitted particle.

The emitter calls this once for every particle in a burst. The returned array is passed to EntityCommands#insertPrefab before the particle’s built-in components are added. Return an empty array or leave the field undefined when no extra components are needed.

patch: object

View source

Runs after an emitted particle has been built.

The first argument is an EntityCommands instance and the second is the new particle’s EntityHandle. Select the entity with commands.entity(entity) before queuing inserts or other entity commands, then call EntityCommands#build.

burstCount: Range

View source

Number of particles to create at the next timer cycle.

The emitter samples this range with a random value and rounds the result for the next burst. Its default is new Range(1, 1).

enabled: boolean

View source

Whether the emitter is allowed to create new particles.

Disabling an emitter does not affect its timer or particles that were emitted earlier.

lifetime: Range

View source

Lifetime range, in seconds, for each emitted particle.

The emitter samples this range independently for every particle. Its default is new Range(), whose bounds are 0 and 1.

Methods


staticcopy(source: Emitter, target?: Emitter): Emitter

View source

Copies an emitter configuration.

Function fields are copied by reference. burstCount and lifetime are replaced with new Range objects containing the same bounds, so changing either range on the target does not change the source. When target is supplied, its existing range objects are replaced.

Parameters

NameDescriptionDefault
source

Emitter whose settings to copy.

—
target?

Emitter to populate.

...

Returns

The populated target emitter.

staticclone(target: Emitter): Emitter

View source

Creates an independent emitter copy.

The callback references are shared, while the two Range instances are newly allocated by Emitter.copy.

Parameters

NameDescriptionDefault
target

Emitter to copy.

—

Returns

A new emitter with copied settings.

staticserialize(value: Emitter): EmitterSerial

View source

Converts an emitter to its plain serialized field object.

The range fields become { start, end } objects. prefab and patch are kept as function values rather than converted to names or source text; a JSON serializer will omit those fields. The returned object still includes the fields with their undefined values when callbacks are not configured.

Parameters

NameDescriptionDefault
value

Emitter to serialize.

—

Returns

Serialized emitter fields.

staticdeserialize(value: EmitterSerial, out?: Emitter): Emitter

View source

Restores serialized fields into an emitter.

When out is supplied, its existing Range instances are updated through Range.deserialize and the same emitter is returned. Callback values are assigned as-is. This method does not validate its input; call Emitter.validateSerial for untrusted data first.

Parameters

NameDescriptionDefault
value

Serialized emitter fields.

—
out?

Emitter to populate.

...

Returns

The populated emitter.

staticvalidateSerial(value: unknown): value is EmitterSerial

View source

Checks the shape accepted by Emitter.deserialize.

The candidate must contain all five serialized fields. prefab and patch may be undefined or functions; they are not invoked or checked for a particular return value. Both range values must contain numeric start and end fields, while enabled must be boolean. Range ordering is not checked, and this method does not make the value JSON-safe.

Parameters

NameDescriptionDefault
value

Candidate serialized value.

—

Returns

Whether the value has valid serialized fields.