A burst that lights the room
Open the live demo · Read the source · View on GitHub
Before a burst could carry a light source, an explosion or a muzzle flash cast
no light however bright its particles were painted. ParticleSystem.burst
takes an optional source, and a LightEmitter passed there is fed by the
burst's own particles from the instant they appear.
Step 1: One argument on an ordinary burst #
Nothing about the burst itself is different: the same count, the same
emitter, the same lifetime a plain spark burst would use. The only new thing
is source.
// Nothing above this differs from an ordinary burst. `source` is the
// whole of what makes it a light: a `LightEmitter` passed here has its
// glow fed by exactly these particles, for as long as they live.
_flashEffect = ParticleEffect(
count: 60,
emitter: const SphereEmitter(speed: Range(2.0, 5.0)),
lifetime: const Range(0.2, 0.4),
size: const Range(0.05, 0.09),
color: Vector4(1.0, 0.85, 0.5, 1.0),
);
_particles.burst(_flashEffect, Vector3.zero(), source: _flash);
Step 2: Follow it while it lasts #
The glow fades on its own once the last particle of the burst has died, so the light needs no cleanup: it is measured every step, for exactly as long as there is anything left to measure.
final ParticleGlow glow = _flash.glow;
if (glow.located) {
_glow.setPositionFrom(glow.centre);
_glow.intensity = glow.power * 0.6;
}
Step 3: What "lit" means here #
A burst that never reaches a light emitter never updates a ParticleGlow,
so the check on this page is direct: after one step, the flash should have
counted particles and produced a positive amount of light.
if (_flash.glow.count == 0 || _flash.glow.power <= 0.0) {
throw StateError('the burst should have lit its own flash');
}
Note. This is the same mechanism the standing-emission torch on the particle-lights page uses. The difference is only in how the particles arrive: all at once here, one sub-step's worth at a time there.