Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

examples(particle-demo): rebuild as interactive particle playground

yhh8510e5ba346c
diff --git a/examples/particle-demo/README.md b/examples/particle-demo/README.md
new file mode 100644
index 000000000..c46e8c953
--- /dev/null
+++ b/examples/particle-demo/README.md
@@ -0,0 +1,62 @@
+# Particle Effects
+
+An interactive playground for the engine's `ParticleEmitter` component and the
+`Particle` runtime API. A single emitter follows your mouse, leaving trails; you
+switch its preset on the fly and fling fireworks with a click.
+
+## Controls
+
+| Input | Action |
+| ------------ | ------------------------------------------------------------- |
+| Move mouse | The follow emitter tracks the cursor (World space, so it trails) |
+| `1`–`6` | Jump to a preset |
+| `Space` | Cycle to the next preset |
+| Left-click | Spawn a one-shot firework burst at the cursor |
+| `P` | Pause / resume the follow stream |
+
+## Presets
+
+`1` Fire · `2` Sparks · `3` Smoke · `4` Magic · `5` Snow · `6` Fountain — each
+remixes the emitter's shape, velocity, colour-over-life, size-over-life, gravity,
+damping and blend mode (additive vs. normal). See `src/config.ts`.
+
+## How it works
+
+Particles are a plain ECS component (`ParticleEmitter`) simulated by the built-in
+`ParticleSystem`. Nothing here runs per-particle code — a system only starts an
+emitter, edits its fields, or calls the `Particle` resource.
+
+- **The follow emitter** lives in the scene (so its `texture` resolves) with the
+ lifecycle fields fixed (`looping`, `playOnStart`, `maxParticles`, World
+ `simulationSpace`). `setupSystem` applies preset 0 at startup so `config.ts` is
+ the single source for how each preset looks.
+- **Switching presets** (`src/systems/control.ts`) mutates the emitter's fields
+ live via `Query(Mut(ParticleEmitter))`. New particles adopt the look next
+ frame; particles already alive finish with the look they were born with — a
+ natural cross-fade, no reset needed.
+- **Pausing** calls `Particle.stop(entity)` / `.play(entity)` — emission halts
+ while live particles play out.
+- **Click bursts** spawn a temporary emitter (`burstCount` > 0, `looping: false`,
+ a short `duration` so it emits exactly once) that reuses the follow emitter's
+ resolved `texture` handle. It has no self-despawn, so `cleanupSystem` removes it
+ once `Particle.getAliveCount(entity)` reaches 0.
+
+Particles draw as additive quads sampling `assets/textures/particle.png` (a soft
+radial dot), so overlapping particles bloom into glows.
+
+## Files
+
+```
+assets/
+ scenes/main.esscene # camera, dark backdrop, the follow emitter
+ textures/particle.png # soft radial particle sprite
+src/
+ main.ts # registers the four systems
+ config.ts # the six presets, the burst, applyPreset()
+ components.ts # Follow tag, Burst component
+ systems/
+ setup.ts # applies preset 0 to the follow emitter
+ follow.ts # emitter tracks the cursor
+ control.ts # presets / pause / click-to-burst input
+ cleanup.ts # despawns fireworks once they finish
+```
diff --git a/examples/particle-demo/assets/scenes/main.esscene b/examples/particle-demo/assets/scenes/main.esscene
index 3e1a9141c..b1e5b1dba 100644
--- a/examples/particle-demo/assets/scenes/main.esscene
+++ b/examples/particle-demo/assets/scenes/main.esscene
@@ -7,163 +7,129 @@
"name": "Camera",
"parent": null,
"children": [],
+ "visible": true,
"components": [
{
"type": "Transform",
"data": {
- "position": { "x": 0, "y": 0, "z": 10 }
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 10
+ }
}
},
{
"type": "Camera",
"data": {
"projectionType": 1,
- "orthoSize": 300,
+ "orthoSize": 360,
"isActive": true,
"priority": 0
}
}
- ],
- "visible": true
+ ]
},
{
"id": 1,
- "name": "Fire",
+ "name": "Background",
"parent": null,
"children": [],
+ "visible": true,
"components": [
- { "type": "Transform", "data": { "position": { "x": -250, "y": -100, "z": 0 } } },
- { "type": "ParticleEmitter", "data": {
- "rate": 40,
- "maxParticles": 200,
- "lifetimeMin": 0.8,
- "lifetimeMax": 1.5,
- "shape": 1,
- "shapeRadius": 20,
- "speedMin": 80,
- "speedMax": 180,
- "angleSpreadMin": 70,
- "angleSpreadMax": 110,
- "startSizeMin": 15,
- "startSizeMax": 25,
- "endSizeMin": 2,
- "endSizeMax": 5,
- "sizeEasing": 2,
- "startColor": { "r": 1, "g": 0.6, "b": 0.1, "a": 1 },
- "endColor": { "r": 1, "g": 0.1, "b": 0, "a": 0 },
- "colorEasing": 1,
- "gravity": { "x": 0, "y": 50 },
- "simulationSpace": 0,
- "looping": true,
- "playOnStart": true,
- "texture": "@uuid:24cb4e0f-7c75-4df4-9a4d-58480ee405c0"
- }}
- ],
- "visible": true
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": {
+ "x": 1400,
+ "y": 820
+ },
+ "color": {
+ "r": 0.04,
+ "g": 0.04,
+ "b": 0.06,
+ "a": 1
+ },
+ "layer": 0
+ }
+ }
+ ]
},
{
"id": 2,
- "name": "Sparkle",
- "parent": null,
- "children": [],
- "components": [
- { "type": "Transform", "data": {} },
- { "type": "ParticleEmitter", "data": {
- "rate": 25,
- "maxParticles": 150,
- "lifetimeMin": 1,
- "lifetimeMax": 2,
- "shape": 0,
- "speedMin": 100,
- "speedMax": 250,
- "angleSpreadMin": 0,
- "angleSpreadMax": 360,
- "startSizeMin": 8,
- "startSizeMax": 14,
- "endSizeMin": 0,
- "endSizeMax": 0,
- "sizeEasing": 2,
- "startColor": { "r": 0.5, "g": 0.8, "b": 1, "a": 1 },
- "endColor": { "r": 0.2, "g": 0.4, "b": 1, "a": 0 },
- "colorEasing": 0,
- "angularVelocityMin": -180,
- "angularVelocityMax": 180,
- "damping": 0.5,
- "looping": true,
- "playOnStart": true,
- "texture": "@uuid:24cb4e0f-7c75-4df4-9a4d-58480ee405c0"
- }}
- ],
- "visible": true
- },
- {
- "id": 3,
- "name": "Smoke",
- "parent": null,
- "children": [],
- "components": [
- { "type": "Transform", "data": { "position": { "x": 250, "y": -100, "z": 0 } } },
- { "type": "ParticleEmitter", "data": {
- "rate": 15,
- "maxParticles": 100,
- "lifetimeMin": 2,
- "lifetimeMax": 4,
- "shape": 1,
- "shapeRadius": 10,
- "speedMin": 30,
- "speedMax": 60,
- "angleSpreadMin": 60,
- "angleSpreadMax": 120,
- "startSizeMin": 20,
- "startSizeMax": 30,
- "endSizeMin": 80,
- "endSizeMax": 120,
- "sizeEasing": 2,
- "startColor": { "r": 0.5, "g": 0.5, "b": 0.5, "a": 0.6 },
- "endColor": { "r": 0.3, "g": 0.3, "b": 0.3, "a": 0 },
- "colorEasing": 1,
- "gravity": { "x": 0, "y": 20 },
- "damping": 0.3,
- "looping": true,
- "playOnStart": true,
- "texture": "@uuid:24cb4e0f-7c75-4df4-9a4d-58480ee405c0"
- }}
- ],
- "visible": true
- },
- {
- "id": 4,
- "name": "Burst",
+ "name": "Follow",
"parent": null,
"children": [],
+ "visible": true,
"components": [
- { "type": "Transform", "data": { "position": { "x": 0, "y": 180, "z": 0 } } },
- { "type": "ParticleEmitter", "data": {
- "rate": 0,
- "burstCount": 30,
- "burstInterval": 2,
- "maxParticles": 200,
- "lifetimeMin": 0.5,
- "lifetimeMax": 1.2,
- "shape": 0,
- "speedMin": 200,
- "speedMax": 400,
- "angleSpreadMin": 0,
- "angleSpreadMax": 360,
- "startSizeMin": 6,
- "startSizeMax": 12,
- "endSizeMin": 0,
- "endSizeMax": 0,
- "sizeEasing": 0,
- "startColor": { "r": 1, "g": 0.9, "b": 0.2, "a": 1 },
- "endColor": { "r": 1, "g": 0.3, "b": 0.1, "a": 0 },
- "gravity": { "x": 0, "y": -200 },
- "looping": true,
- "playOnStart": true,
- "texture": "@uuid:24cb4e0f-7c75-4df4-9a4d-58480ee405c0"
- }}
- ],
- "visible": true
+ {
+ "type": "Transform",
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 0
+ }
+ }
+ },
+ {
+ "type": "ParticleEmitter",
+ "data": {
+ "rate": 90,
+ "maxParticles": 600,
+ "looping": true,
+ "playOnStart": true,
+ "simulationSpace": 0,
+ "lifetimeMin": 0.7,
+ "lifetimeMax": 1.4,
+ "shape": 1,
+ "shapeRadius": 14,
+ "speedMin": 90,
+ "speedMax": 190,
+ "angleSpreadMin": 75,
+ "angleSpreadMax": 105,
+ "startSizeMin": 16,
+ "startSizeMax": 26,
+ "endSizeMin": 3,
+ "endSizeMax": 6,
+ "sizeEasing": 2,
+ "startColor": {
+ "r": 1,
+ "g": 0.65,
+ "b": 0.15,
+ "a": 1
+ },
+ "endColor": {
+ "r": 1,
+ "g": 0.12,
+ "b": 0.02,
+ "a": 0
+ },
+ "colorEasing": 1,
+ "gravity": {
+ "x": 0,
+ "y": 60
+ },
+ "blendMode": 1,
+ "layer": 5,
+ "texture": "@uuid:24cb4e0f-7c75-4df4-9a4d-58480ee405c0"
+ }
+ },
+ {
+ "type": "Follow",
+ "data": {}
+ }
+ ]
}
]
}
diff --git a/examples/particle-demo/project.esproject b/examples/particle-demo/project.esproject
index 86d5eb360..f0a635f8e 100644
--- a/examples/particle-demo/project.esproject
+++ b/examples/particle-demo/project.esproject
@@ -1,13 +1,13 @@
{
"formatVersion": "1",
"name": "Particle Effects",
- "description": "A configurable particle emitter with additive blending.",
+ "description": "An interactive particle playground: a mouse-follow emitter with six switchable presets and click-to-burst fireworks.",
"tag": "Effects",
- "version": "0.1.0",
+ "version": "0.2.0",
"defaultScene": "assets/scenes/main.esscene",
"designResolution": {
- "width": 800,
- "height": 600
+ "width": 1280,
+ "height": 720
},
"spineVersion": "none"
}
diff --git a/examples/particle-demo/src/components.ts b/examples/particle-demo/src/components.ts
new file mode 100644
index 000000000..5229adc5e
--- /dev/null
+++ b/examples/particle-demo/src/components.ts
@@ -0,0 +1,10 @@
+import { defineComponent, defineTag } from 'esengine';
+
+// The one emitter that tracks the mouse and whose preset the player switches.
+export const Follow = defineTag('Follow');
+
+// A one-shot firework emitter spawned on click. `age` gates the getAliveCount
+// despawn check so it isn't removed on the first frame, before it has emitted.
+export const Burst = defineComponent('Burst', {
+ age: 0,
+});
diff --git a/examples/particle-demo/src/config.ts b/examples/particle-demo/src/config.ts
new file mode 100644
index 000000000..86b8a4af1
--- /dev/null
+++ b/examples/particle-demo/src/config.ts
@@ -0,0 +1,138 @@
+import { EmitterShape, BlendMode, ParticleEasing } from 'esengine';
+import type { ParticleEmitterData } from 'esengine';
+
+// A particle "look" — the subset of ParticleEmitter fields the presets vary.
+// Lifecycle (looping/playOnStart/maxParticles), the texture, and the World
+// simulation space stay fixed on the follow emitter; a preset only changes how
+// it looks and moves. Angles are degrees measured from +x, so 90 = up.
+export interface Preset {
+ name: string;
+ rate: number;
+ lifetimeMin: number;
+ lifetimeMax: number;
+ shape: number;
+ shapeRadius: number;
+ shapeAngle: number;
+ speedMin: number;
+ speedMax: number;
+ angleSpreadMin: number;
+ angleSpreadMax: number;
+ startSizeMin: number;
+ startSizeMax: number;
+ endSizeMin: number;
+ endSizeMax: number;
+ sizeEasing: number;
+ startColor: { r: number; g: number; b: number; a: number };
+ endColor: { r: number; g: number; b: number; a: number };
+ colorEasing: number;
+ angularVelocityMin: number;
+ angularVelocityMax: number;
+ gravity: { x: number; y: number };
+ damping: number;
+ blendMode: number;
+}
+
+export const PRESETS: Preset[] = [
+ {
+ name: 'Fire', rate: 90, lifetimeMin: 0.7, lifetimeMax: 1.4,
+ shape: EmitterShape.Circle, shapeRadius: 14, shapeAngle: 0,
+ speedMin: 90, speedMax: 190, angleSpreadMin: 75, angleSpreadMax: 105,
+ startSizeMin: 16, startSizeMax: 26, endSizeMin: 3, endSizeMax: 6, sizeEasing: ParticleEasing.EaseOut,
+ startColor: { r: 1, g: 0.65, b: 0.15, a: 1 }, endColor: { r: 1, g: 0.12, b: 0.02, a: 0 }, colorEasing: ParticleEasing.EaseIn,
+ angularVelocityMin: 0, angularVelocityMax: 0, gravity: { x: 0, y: 60 }, damping: 0, blendMode: BlendMode.Additive,
+ },
+ {
+ name: 'Sparks', rate: 70, lifetimeMin: 0.5, lifetimeMax: 1.0,
+ shape: EmitterShape.Point, shapeRadius: 0, shapeAngle: 0,
+ speedMin: 160, speedMax: 360, angleSpreadMin: 0, angleSpreadMax: 360,
+ startSizeMin: 5, startSizeMax: 9, endSizeMin: 0, endSizeMax: 0, sizeEasing: ParticleEasing.Linear,
+ startColor: { r: 1, g: 0.95, b: 0.6, a: 1 }, endColor: { r: 1, g: 0.4, b: 0.1, a: 0 }, colorEasing: ParticleEasing.Linear,
+ angularVelocityMin: -220, angularVelocityMax: 220, gravity: { x: 0, y: -280 }, damping: 0.6, blendMode: BlendMode.Additive,
+ },
+ {
+ name: 'Smoke', rate: 26, lifetimeMin: 1.8, lifetimeMax: 3.2,
+ shape: EmitterShape.Circle, shapeRadius: 12, shapeAngle: 0,
+ speedMin: 25, speedMax: 55, angleSpreadMin: 70, angleSpreadMax: 110,
+ startSizeMin: 22, startSizeMax: 34, endSizeMin: 70, endSizeMax: 110, sizeEasing: ParticleEasing.EaseOut,
+ startColor: { r: 0.55, g: 0.55, b: 0.58, a: 0.5 }, endColor: { r: 0.28, g: 0.28, b: 0.30, a: 0 }, colorEasing: ParticleEasing.EaseIn,
+ angularVelocityMin: -20, angularVelocityMax: 20, gravity: { x: 0, y: 35 }, damping: 0.4, blendMode: BlendMode.Normal,
+ },
+ {
+ name: 'Magic', rate: 80, lifetimeMin: 1.2, lifetimeMax: 2.2,
+ shape: EmitterShape.Circle, shapeRadius: 30, shapeAngle: 0,
+ speedMin: 30, speedMax: 90, angleSpreadMin: 0, angleSpreadMax: 360,
+ startSizeMin: 8, startSizeMax: 16, endSizeMin: 0, endSizeMax: 0, sizeEasing: ParticleEasing.EaseInOut,
+ startColor: { r: 0.65, g: 0.4, b: 1.0, a: 1 }, endColor: { r: 0.3, g: 0.9, b: 1.0, a: 0 }, colorEasing: ParticleEasing.Linear,
+ angularVelocityMin: -120, angularVelocityMax: 120, gravity: { x: 0, y: 45 }, damping: 0.2, blendMode: BlendMode.Additive,
+ },
+ {
+ name: 'Snow', rate: 40, lifetimeMin: 2.5, lifetimeMax: 4.0,
+ shape: EmitterShape.Circle, shapeRadius: 40, shapeAngle: 0,
+ speedMin: 30, speedMax: 70, angleSpreadMin: 255, angleSpreadMax: 285,
+ startSizeMin: 6, startSizeMax: 12, endSizeMin: 6, endSizeMax: 12, sizeEasing: ParticleEasing.Linear,
+ startColor: { r: 1, g: 1, b: 1, a: 0.9 }, endColor: { r: 0.8, g: 0.9, b: 1.0, a: 0.25 }, colorEasing: ParticleEasing.Linear,
+ angularVelocityMin: -30, angularVelocityMax: 30, gravity: { x: 0, y: -15 }, damping: 0.1, blendMode: BlendMode.Normal,
+ },
+ {
+ name: 'Fountain', rate: 120, lifetimeMin: 1.1, lifetimeMax: 1.9,
+ shape: EmitterShape.Point, shapeRadius: 0, shapeAngle: 0,
+ speedMin: 300, speedMax: 440, angleSpreadMin: 82, angleSpreadMax: 98,
+ startSizeMin: 6, startSizeMax: 12, endSizeMin: 3, endSizeMax: 5, sizeEasing: ParticleEasing.Linear,
+ startColor: { r: 0.5, g: 0.8, b: 1, a: 1 }, endColor: { r: 0.2, g: 0.4, b: 1, a: 0 }, colorEasing: ParticleEasing.Linear,
+ angularVelocityMin: 0, angularVelocityMax: 0, gravity: { x: 0, y: -420 }, damping: 0, blendMode: BlendMode.Additive,
+ },
+];
+
+// The pool size the follow emitter is authored with — big enough for the
+// highest-rate preset (Fountain: 120/s × ~2s life). Kept constant across presets
+// because changing maxParticles mid-run does not resize an existing pool.
+export const FOLLOW_MAX_PARTICLES = 600;
+
+// A one-shot firework spawned at the cursor on left-click. duration must be > 0
+// (the sim only emits while elapsed < duration), and a huge burstInterval keeps
+// it to a single burst; getAliveCount() then drives despawn once it fades out.
+export const BURST: Partial<ParticleEmitterData> = {
+ rate: 0, burstCount: 64, burstInterval: 999, duration: 0.1,
+ looping: false, playOnStart: true, maxParticles: 140,
+ shape: EmitterShape.Point, speedMin: 180, speedMax: 440,
+ lifetimeMin: 0.7, lifetimeMax: 1.5, angleSpreadMin: 0, angleSpreadMax: 360,
+ startSizeMin: 6, startSizeMax: 12, endSizeMin: 0, endSizeMax: 0, sizeEasing: ParticleEasing.EaseOut,
+ startColor: { r: 1, g: 0.9, b: 0.4, a: 1 }, endColor: { r: 1, g: 0.3, b: 0.1, a: 0 },
+ gravity: { x: 0, y: -160 }, damping: 0.4, blendMode: BlendMode.Additive, layer: 5,
+};
+
+// Copy a preset's look onto a live emitter (scalar-by-scalar so the ECS proxy's
+// nested Vec2/Color fields are written in place). New particles pick it up next
+// frame; particles already alive keep the look they were born with.
+export function applyPreset(e: ParticleEmitterData, p: Preset): void {
+ e.rate = p.rate;
+ e.lifetimeMin = p.lifetimeMin;
+ e.lifetimeMax = p.lifetimeMax;
+ e.shape = p.shape;
+ e.shapeRadius = p.shapeRadius;
+ e.shapeAngle = p.shapeAngle;
+ e.speedMin = p.speedMin;
+ e.speedMax = p.speedMax;
+ e.angleSpreadMin = p.angleSpreadMin;
+ e.angleSpreadMax = p.angleSpreadMax;
+ e.startSizeMin = p.startSizeMin;
+ e.startSizeMax = p.startSizeMax;
+ e.endSizeMin = p.endSizeMin;
+ e.endSizeMax = p.endSizeMax;
+ e.sizeEasing = p.sizeEasing;
+ e.startColor.r = p.startColor.r;
+ e.startColor.g = p.startColor.g;
+ e.startColor.b = p.startColor.b;
+ e.startColor.a = p.startColor.a;
+ e.endColor.r = p.endColor.r;
+ e.endColor.g = p.endColor.g;
+ e.endColor.b = p.endColor.b;
+ e.endColor.a = p.endColor.a;
+ e.colorEasing = p.colorEasing;
+ e.angularVelocityMin = p.angularVelocityMin;
+ e.angularVelocityMax = p.angularVelocityMax;
+ e.gravity.x = p.gravity.x;
+ e.gravity.y = p.gravity.y;
+ e.damping = p.damping;
+ e.blendMode = p.blendMode;
+}
diff --git a/examples/particle-demo/src/main.ts b/examples/particle-demo/src/main.ts
index 9f0253242..8254437c3 100644
--- a/examples/particle-demo/src/main.ts
+++ b/examples/particle-demo/src/main.ts
@@ -1 +1,25 @@
-// Scene handles all entities
+// Particle Effects — an interactive tour of the ParticleEmitter component and
+// the `Particle` runtime API.
+//
+// • Move the mouse — a World-space emitter follows the cursor, leaving trails.
+// • 1–6 / Space — switch its preset: Fire, Sparks, Smoke, Magic, Snow,
+// Fountain (each remixes the emitter's shape, velocity, colour, size, gravity
+// and blend mode live).
+// • Left-click — spawn a one-shot firework burst at the cursor.
+// • P — pause / resume the follow stream (Particle.stop / play).
+//
+// Emitters are plain ECS components simulated by the built-in ParticleSystem;
+// systems here only start them, mutate their fields, and use the `Particle`
+// resource for play/stop and getAliveCount-driven cleanup.
+import { addStartupSystem, addSystemToSchedule, Schedule } from 'esengine';
+
+import './components';
+import { setupSystem } from './systems/setup';
+import { followSystem } from './systems/follow';
+import { controlSystem } from './systems/control';
+import { cleanupSystem } from './systems/cleanup';
+
+addStartupSystem(setupSystem);
+addSystemToSchedule(Schedule.Update, followSystem);
+addSystemToSchedule(Schedule.Update, controlSystem);
+addSystemToSchedule(Schedule.Update, cleanupSystem);
diff --git a/examples/particle-demo/src/systems/cleanup.ts b/examples/particle-demo/src/systems/cleanup.ts
new file mode 100644
index 000000000..c3ca273aa
--- /dev/null
+++ b/examples/particle-demo/src/systems/cleanup.ts
@@ -0,0 +1,20 @@
+import {
+ defineSystem, Query, Mut, Res, Time, Commands, Particle, ParticleAPI,
+} from 'esengine';
+import { Burst } from '../components';
+
+// A firework emitter never stops itself, so despawn it once its particles have
+// all died. The small age gate avoids removing it on the very first frame,
+// before the burst has emitted (getAliveCount would still read 0).
+export const cleanupSystem = defineSystem(
+ [Query(Mut(Burst)), Res(Particle), Res(Time), Commands()],
+ (bursts, particles: ParticleAPI, time, cmds) => {
+ for (const [entity, burst] of bursts) {
+ burst.age += time.delta;
+ if (burst.age > 0.2 && particles.getAliveCount(entity) === 0) {
+ cmds.despawn(entity);
+ }
+ }
+ },
+ { name: 'CleanupSystem' },
+);
diff --git a/examples/particle-demo/src/systems/control.ts b/examples/particle-demo/src/systems/control.ts
new file mode 100644
index 000000000..b1b755581
--- /dev/null
+++ b/examples/particle-demo/src/systems/control.ts
@@ -0,0 +1,46 @@
+import {
+ defineSystem, Query, Mut, Res, Commands,
+ Input, UICameraInfo, Transform, Particle, ParticleAPI, ParticleEmitter,
+} from 'esengine';
+import type { UICameraData } from 'esengine';
+import { Follow, Burst } from '../components';
+import { PRESETS, BURST, applyPreset } from '../config';
+
+// Current preset index and whether the stream is paused — module state, since a
+// keypress this frame changes what the follow emitter does next frame.
+let current = 0;
+let paused = false;
+
+// All the input for the demo: 1–6 / Space pick a preset (applied live to the
+// follow emitter), P toggles emission via the Particle resource, and left-click
+// spawns a firework at the cursor that reuses the follow emitter's texture.
+export const controlSystem = defineSystem(
+ [Res(Input), Res(Particle), Res(UICameraInfo), Query(Mut(ParticleEmitter), Follow), Commands()],
+ (input, particles: ParticleAPI, camera: UICameraData, follows, cmds) => {
+ let next = -1;
+ if (input.isKeyPressed('Space')) next = (current + 1) % PRESETS.length;
+ for (let i = 0; i < PRESETS.length; i++) {
+ if (input.isKeyPressed(`Digit${i + 1}`)) next = i;
+ }
+ const togglePause = input.isKeyPressed('KeyP');
+ const click = camera.valid && input.isMouseButtonPressed(0);
+
+ for (const [entity, emitter] of follows) {
+ if (next >= 0) {
+ current = next;
+ applyPreset(emitter, PRESETS[current]);
+ }
+ if (togglePause) {
+ paused = !paused;
+ if (paused) particles.stop(entity); else particles.play(entity);
+ }
+ if (click) {
+ cmds.spawn()
+ .insert(Transform, { position: { x: camera.worldMouseX, y: camera.worldMouseY, z: 0 } })
+ .insert(ParticleEmitter, { ...BURST, texture: emitter.texture })
+ .insert(Burst, { age: 0 });
+ }
+ }
+ },
+ { name: 'ControlSystem' },
+);
diff --git a/examples/particle-demo/src/systems/follow.ts b/examples/particle-demo/src/systems/follow.ts
new file mode 100644
index 000000000..d2888f4a7
--- /dev/null
+++ b/examples/particle-demo/src/systems/follow.ts
@@ -0,0 +1,20 @@
+import {
+ defineSystem, Query, Mut, Res, Transform, UICameraInfo,
+} from 'esengine';
+import type { UICameraData } from 'esengine';
+import { Follow } from '../components';
+
+// Pin the follow emitter to the world-space cursor. Because it simulates in
+// World space, particles stay where they were born and streak out behind the
+// moving cursor.
+export const followSystem = defineSystem(
+ [Query(Mut(Transform), Follow), Res(UICameraInfo)],
+ (follows, camera: UICameraData) => {
+ if (!camera.valid) return;
+ for (const [, transform] of follows) {
+ transform.position.x = camera.worldMouseX;
+ transform.position.y = camera.worldMouseY;
+ }
+ },
+ { name: 'FollowSystem' },
+);
diff --git a/examples/particle-demo/src/systems/setup.ts b/examples/particle-demo/src/systems/setup.ts
new file mode 100644
index 000000000..a5d1f9507
--- /dev/null
+++ b/examples/particle-demo/src/systems/setup.ts
@@ -0,0 +1,15 @@
+import { defineSystem, Query, Mut, ParticleEmitter } from 'esengine';
+import { Follow } from '../components';
+import { PRESETS, applyPreset } from '../config';
+
+// Give the scene's follow emitter its starting look (the first preset) once at
+// startup, so config.ts is the single source of truth for how a preset looks.
+export const setupSystem = defineSystem(
+ [Query(Mut(ParticleEmitter), Follow)],
+ (follows) => {
+ for (const [, emitter] of follows) {
+ applyPreset(emitter, PRESETS[0]);
+ }
+ },
+ { name: 'SetupSystem' },
+);
diff --git a/examples/particle-demo/thumbnail.png b/examples/particle-demo/thumbnail.png
index 4abc36b6f..ff7dac802 100644
Binary files a/examples/particle-demo/thumbnail.png and b/examples/particle-demo/thumbnail.png differ