@flare-engine/particles 0.4.12 → 0.4.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -7
- package/dist/index.d.mts +31 -9
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +31 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +30 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +30 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -93,13 +93,18 @@ not merely assumed.
|
|
|
93
93
|
The loop allocates nothing per particle, and skips any particle whose interpolated size or alpha has
|
|
94
94
|
reached zero rather than paying for an invisible draw call.
|
|
95
95
|
|
|
96
|
-
**
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
96
|
+
**Per-particle colour is opt-in:** `drawParticles(pool, batch, image, frame, { color: true })`.
|
|
97
|
+
Each particle's interpolated `startR/G/B → endR/G/B` then goes to `drawRaw` as its trailing `color`
|
|
98
|
+
argument, an opaque packed ARGB (`0xAARRGGBB`) that multiplies the frame's texels: a white frame
|
|
99
|
+
takes the colour exactly, and a particle left at the default white draws unchanged. Alpha stays the
|
|
100
|
+
separate `alpha` argument. With a `@flare-engine/render` `SpriteBatch`, sprites drawn with a colour
|
|
101
|
+
share one run per image whatever their colours and alphas, so a fading, colour-shifting emitter is
|
|
102
|
+
one draw call rather than one per distinct alpha — the reason to turn it on even for white
|
|
103
|
+
particles. Without the option, `drawParticles` draws exactly as before: colour is not applied and
|
|
104
|
+
each distinct alpha is its own run. The constant `effect` option is still there for what a
|
|
105
|
+
multiplier cannot express (flash, outline, grayscale).
|
|
106
|
+
|
|
107
|
+
See [API spec](https://github.com/grzott/flare-engine/blob/main/docs/design/api-specs.md#flare-engineparticles).
|
|
103
108
|
|
|
104
109
|
## License
|
|
105
110
|
|
package/dist/index.d.mts
CHANGED
|
@@ -94,10 +94,12 @@ interface ParticleFrameLike {
|
|
|
94
94
|
* Declared structurally so `@flare-engine/particles` keeps its two dependencies
|
|
95
95
|
* (`core`, `math`) and stays usable with no renderer at all. A real
|
|
96
96
|
* `SpriteBatch` from `@flare-engine/render` satisfies it; so does a test double.
|
|
97
|
-
* The parameter list mirrors `SpriteBatch.drawRaw`.
|
|
97
|
+
* The parameter list mirrors `SpriteBatch.drawRaw`. `color` is a packed-ARGB
|
|
98
|
+
* multiplier (`0xAARRGGBB`); a batch that declares fewer parameters simply does
|
|
99
|
+
* not receive it.
|
|
98
100
|
*/
|
|
99
101
|
interface ParticleBatchLike {
|
|
100
|
-
drawRaw(image: unknown, srcX: number, srcY: number, srcW: number, srcH: number, dstX: number, dstY: number, dstW: number, dstH: number, effect?: unknown, anchorX?: number, anchorY?: number, alpha?: number, rotation?: number, stretch?: boolean): void;
|
|
102
|
+
drawRaw(image: unknown, srcX: number, srcY: number, srcW: number, srcH: number, dstX: number, dstY: number, dstW: number, dstH: number, effect?: unknown, anchorX?: number, anchorY?: number, alpha?: number, rotation?: number, stretch?: boolean, blend?: 'normal' | 'additive', flipX?: boolean, flipY?: boolean, color?: number): void;
|
|
101
103
|
}
|
|
102
104
|
/** Options for `drawParticles`. */
|
|
103
105
|
interface DrawParticlesOptions {
|
|
@@ -110,19 +112,35 @@ interface DrawParticlesOptions {
|
|
|
110
112
|
* values. Per-particle effects therefore cost one object per particle per
|
|
111
113
|
* frame, which is exactly what this package exists to avoid — pass a constant
|
|
112
114
|
* here, or build the loop yourself over `pool.particles`.
|
|
115
|
+
*
|
|
116
|
+
* Per-particle colour needs no effect: with `color: true` it is passed as a
|
|
117
|
+
* number. Use this for what a colour multiplier cannot express, such as a
|
|
118
|
+
* flash, an outline or grayscale.
|
|
113
119
|
*/
|
|
114
120
|
effect?: unknown;
|
|
115
121
|
/** Anchor within the destination rect. Defaults to the centre (0.5, 0.5). */
|
|
116
122
|
anchorX?: number;
|
|
117
123
|
anchorY?: number;
|
|
124
|
+
/**
|
|
125
|
+
* Pass each particle's interpolated `startR/G/B → endR/G/B` to `drawRaw` as its
|
|
126
|
+
* trailing `color` argument. Defaults to `false`, which draws exactly as before
|
|
127
|
+
* this option existed: no colour argument, and colour not applied.
|
|
128
|
+
*
|
|
129
|
+
* With `true` and `@flare-engine/render`'s `SpriteBatch`, each particle is
|
|
130
|
+
* tinted by its colour, and coloured sprites share one run per image whatever
|
|
131
|
+
* their colours and alphas — a fading, colour-shifting emitter becomes one draw
|
|
132
|
+
* call instead of one per distinct alpha. The batch's renderer must support
|
|
133
|
+
* per-sprite colours (both of the engine's backends do).
|
|
134
|
+
*/
|
|
135
|
+
color?: boolean;
|
|
118
136
|
}
|
|
119
137
|
/**
|
|
120
138
|
* Queue every live particle into a caller-owned sprite batch.
|
|
121
139
|
*
|
|
122
140
|
* Zero-alloc: the loop reads the pool's active list directly, allocates nothing
|
|
123
141
|
* per particle, and hands each one to `drawRaw` — so a steady-state emitter adds
|
|
124
|
-
* no garbage. Size and
|
|
125
|
-
* rotation from the particle.
|
|
142
|
+
* no garbage. Size, alpha and (with `color: true`) colour come from the pool's
|
|
143
|
+
* own interpolators, and rotation from the particle.
|
|
126
144
|
*
|
|
127
145
|
* The batch is passed in rather than owned so particles share one batch, and one
|
|
128
146
|
* flush, with the rest of the scene — the `drawInto` idiom used elsewhere in the
|
|
@@ -135,11 +153,15 @@ interface DrawParticlesOptions {
|
|
|
135
153
|
* drawParticles(emitter.pool, batch, atlas.image, frame);
|
|
136
154
|
* ```
|
|
137
155
|
*
|
|
138
|
-
* **Colour is
|
|
139
|
-
* `startR/G/B → endR/G/B
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
156
|
+
* **Colour is opt-in** (`{ color: true }`). Then each particle's interpolated
|
|
157
|
+
* `startR/G/B → endR/G/B` goes to `drawRaw` as its trailing `color` argument: an
|
|
158
|
+
* opaque packed ARGB that multiplies the frame's texels, so a white frame takes
|
|
159
|
+
* the colour exactly and a particle left at the default white draws unchanged.
|
|
160
|
+
* Alpha stays the separate `alpha` argument. With `@flare-engine/render`'s
|
|
161
|
+
* `SpriteBatch`, sprites drawn with a colour share one run per image whatever
|
|
162
|
+
* their colours and alphas, so a fading, colour-shifting emitter is one draw
|
|
163
|
+
* call rather than one per distinct alpha. Without the option, colour is not
|
|
164
|
+
* applied and each distinct alpha is its own run, as before.
|
|
143
165
|
*
|
|
144
166
|
* `image` and `frame` are separate parameters because they are separate
|
|
145
167
|
* everywhere else in the engine — a frame is a rectangle within a texture and
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/particle-pool.ts","../src/draw-particles.ts","../src/emitter-shape.ts","../src/particle-emitter.ts"],"mappings":";;UAIiB;;EAEf;EACA;;EAEA;EACA;;EAEA;;EAEA;;EAEA;EACA;;EAEA;EACA;;EAEA;EACA;EACA;EACA;EACA;EACA;;EAEA;;EAEA;;EAEA;;;;;;qBAuDW;UACH;UACA;EAEI,YAAA;;MAMR;;;;;;;MAUA;;MAKA,sBAAsB;;EAK1B,WAAW;;;;;EAWX,cAAc;;EASd,OAAO,YAAY,kBAAkB;;EA+BrC,QAAQ,GAAG;;EAMX,SAAS,GAAG;;UAMJ;;;;;;;EAQR,SAAS,GAAG,WAAW;;EAKvB,aAAa,GAAG,UAAU,KAAK,QAAQ;;EAUvC;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/particle-pool.ts","../src/draw-particles.ts","../src/emitter-shape.ts","../src/particle-emitter.ts"],"mappings":";;UAIiB;;EAEf;EACA;;EAEA;EACA;;EAEA;;EAEA;;EAEA;EACA;;EAEA;EACA;;EAEA;EACA;EACA;EACA;EACA;EACA;;EAEA;;EAEA;;EAEA;;;;;;qBAuDW;UACH;UACA;EAEI,YAAA;;MAMR;;;;;;;MAUA;;MAKA,sBAAsB;;EAK1B,WAAW;;;;;EAWX,cAAc;;EASd,O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@@ interface ParticleFrameLike {
|
|
|
94
94
|
* Declared structurally so `@flare-engine/particles` keeps its two dependencies
|
|
95
95
|
* (`core`, `math`) and stays usable with no renderer at all. A real
|
|
96
96
|
* `SpriteBatch` from `@flare-engine/render` satisfies it; so does a test double.
|
|
97
|
-
* The parameter list mirrors `SpriteBatch.drawRaw`.
|
|
97
|
+
* The parameter list mirrors `SpriteBatch.drawRaw`. `color` is a packed-ARGB
|
|
98
|
+
* multiplier (`0xAARRGGBB`); a batch that declares fewer parameters simply does
|
|
99
|
+
* not receive it.
|
|
98
100
|
*/
|
|
99
101
|
interface ParticleBatchLike {
|
|
100
|
-
drawRaw(image: unknown, srcX: number, srcY: number, srcW: number, srcH: number, dstX: number, dstY: number, dstW: number, dstH: number, effect?: unknown, anchorX?: number, anchorY?: number, alpha?: number, rotation?: number, stretch?: boolean): void;
|
|
102
|
+
drawRaw(image: unknown, srcX: number, srcY: number, srcW: number, srcH: number, dstX: number, dstY: number, dstW: number, dstH: number, effect?: unknown, anchorX?: number, anchorY?: number, alpha?: number, rotation?: number, stretch?: boolean, blend?: 'normal' | 'additive', flipX?: boolean, flipY?: boolean, color?: number): void;
|
|
101
103
|
}
|
|
102
104
|
/** Options for `drawParticles`. */
|
|
103
105
|
interface DrawParticlesOptions {
|
|
@@ -110,19 +112,35 @@ interface DrawParticlesOptions {
|
|
|
110
112
|
* values. Per-particle effects therefore cost one object per particle per
|
|
111
113
|
* frame, which is exactly what this package exists to avoid — pass a constant
|
|
112
114
|
* here, or build the loop yourself over `pool.particles`.
|
|
115
|
+
*
|
|
116
|
+
* Per-particle colour needs no effect: with `color: true` it is passed as a
|
|
117
|
+
* number. Use this for what a colour multiplier cannot express, such as a
|
|
118
|
+
* flash, an outline or grayscale.
|
|
113
119
|
*/
|
|
114
120
|
effect?: unknown;
|
|
115
121
|
/** Anchor within the destination rect. Defaults to the centre (0.5, 0.5). */
|
|
116
122
|
anchorX?: number;
|
|
117
123
|
anchorY?: number;
|
|
124
|
+
/**
|
|
125
|
+
* Pass each particle's interpolated `startR/G/B → endR/G/B` to `drawRaw` as its
|
|
126
|
+
* trailing `color` argument. Defaults to `false`, which draws exactly as before
|
|
127
|
+
* this option existed: no colour argument, and colour not applied.
|
|
128
|
+
*
|
|
129
|
+
* With `true` and `@flare-engine/render`'s `SpriteBatch`, each particle is
|
|
130
|
+
* tinted by its colour, and coloured sprites share one run per image whatever
|
|
131
|
+
* their colours and alphas — a fading, colour-shifting emitter becomes one draw
|
|
132
|
+
* call instead of one per distinct alpha. The batch's renderer must support
|
|
133
|
+
* per-sprite colours (both of the engine's backends do).
|
|
134
|
+
*/
|
|
135
|
+
color?: boolean;
|
|
118
136
|
}
|
|
119
137
|
/**
|
|
120
138
|
* Queue every live particle into a caller-owned sprite batch.
|
|
121
139
|
*
|
|
122
140
|
* Zero-alloc: the loop reads the pool's active list directly, allocates nothing
|
|
123
141
|
* per particle, and hands each one to `drawRaw` — so a steady-state emitter adds
|
|
124
|
-
* no garbage. Size and
|
|
125
|
-
* rotation from the particle.
|
|
142
|
+
* no garbage. Size, alpha and (with `color: true`) colour come from the pool's
|
|
143
|
+
* own interpolators, and rotation from the particle.
|
|
126
144
|
*
|
|
127
145
|
* The batch is passed in rather than owned so particles share one batch, and one
|
|
128
146
|
* flush, with the rest of the scene — the `drawInto` idiom used elsewhere in the
|
|
@@ -135,11 +153,15 @@ interface DrawParticlesOptions {
|
|
|
135
153
|
* drawParticles(emitter.pool, batch, atlas.image, frame);
|
|
136
154
|
* ```
|
|
137
155
|
*
|
|
138
|
-
* **Colour is
|
|
139
|
-
* `startR/G/B → endR/G/B
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
156
|
+
* **Colour is opt-in** (`{ color: true }`). Then each particle's interpolated
|
|
157
|
+
* `startR/G/B → endR/G/B` goes to `drawRaw` as its trailing `color` argument: an
|
|
158
|
+
* opaque packed ARGB that multiplies the frame's texels, so a white frame takes
|
|
159
|
+
* the colour exactly and a particle left at the default white draws unchanged.
|
|
160
|
+
* Alpha stays the separate `alpha` argument. With `@flare-engine/render`'s
|
|
161
|
+
* `SpriteBatch`, sprites drawn with a colour share one run per image whatever
|
|
162
|
+
* their colours and alphas, so a fading, colour-shifting emitter is one draw
|
|
163
|
+
* call rather than one per distinct alpha. Without the option, colour is not
|
|
164
|
+
* applied and each distinct alpha is its own run, as before.
|
|
143
165
|
*
|
|
144
166
|
* `image` and `frame` are separate parameters because they are separate
|
|
145
167
|
* everywhere else in the engine — a frame is a rectangle within a texture and
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/particle-pool.ts","../src/draw-particles.ts","../src/emitter-shape.ts","../src/particle-emitter.ts"],"mappings":";;UAIiB;;EAEf;EACA;;EAEA;EACA;;EAEA;;EAEA;;EAEA;EACA;;EAEA;EACA;;EAEA;EACA;EACA;EACA;EACA;EACA;;EAEA;;EAEA;;EAEA;;;;;;qBAuDW;UACH;UACA;EAEI,YAAA;;MAMR;;;;;;;MAUA;;MAKA,sBAAsB;;EAK1B,WAAW;;;;;EAWX,cAAc;;EASd,OAAO,YAAY,kBAAkB;;EA+BrC,QAAQ,GAAG;;EAMX,SAAS,GAAG;;UAMJ;;;;;;;EAQR,SAAS,GAAG,WAAW;;EAKvB,aAAa,GAAG,UAAU,KAAK,QAAQ;;EAUvC;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/particle-pool.ts","../src/draw-particles.ts","../src/emitter-shape.ts","../src/particle-emitter.ts"],"mappings":";;UAIiB;;EAEf;EACA;;EAEA;EACA;;EAEA;;EAEA;;EAEA;EACA;;EAEA;EACA;;EAEA;EACA;EACA;EACA;EACA;EACA;;EAEA;;EAEA;;EAEA;;;;;;qBAuDW;UACH;UACA;EAEI,YAAA;;MAMR;;;;;;;MAUA;;MAKA,sBAAsB;;EAK1B,WAAW;;;;;EAWX,cAAc;;EASd,OAAO,YAAY,k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|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let _flare_engine_core = require("@flare-engine/core");
|
|
3
3
|
// src/draw-particles.ts
|
|
4
|
+
/** Interpolated colour of the particle being drawn; rewritten for each one. */
|
|
5
|
+
const colorScratch = {
|
|
6
|
+
r: 1,
|
|
7
|
+
g: 1,
|
|
8
|
+
b: 1,
|
|
9
|
+
a: 1
|
|
10
|
+
};
|
|
11
|
+
/** A 0-1 channel as a 0-255 byte, clamped so it cannot spill into a neighbouring channel. */
|
|
12
|
+
function channelByte(value) {
|
|
13
|
+
return value <= 0 ? 0 : value >= 1 ? 255 : Math.round(value * 255);
|
|
14
|
+
}
|
|
4
15
|
/**
|
|
5
16
|
* Queue every live particle into a caller-owned sprite batch.
|
|
6
17
|
*
|
|
7
18
|
* Zero-alloc: the loop reads the pool's active list directly, allocates nothing
|
|
8
19
|
* per particle, and hands each one to `drawRaw` — so a steady-state emitter adds
|
|
9
|
-
* no garbage. Size and
|
|
10
|
-
* rotation from the particle.
|
|
20
|
+
* no garbage. Size, alpha and (with `color: true`) colour come from the pool's
|
|
21
|
+
* own interpolators, and rotation from the particle.
|
|
11
22
|
*
|
|
12
23
|
* The batch is passed in rather than owned so particles share one batch, and one
|
|
13
24
|
* flush, with the rest of the scene — the `drawInto` idiom used elsewhere in the
|
|
@@ -20,11 +31,15 @@ let _flare_engine_core = require("@flare-engine/core");
|
|
|
20
31
|
* drawParticles(emitter.pool, batch, atlas.image, frame);
|
|
21
32
|
* ```
|
|
22
33
|
*
|
|
23
|
-
* **Colour is
|
|
24
|
-
* `startR/G/B → endR/G/B
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
34
|
+
* **Colour is opt-in** (`{ color: true }`). Then each particle's interpolated
|
|
35
|
+
* `startR/G/B → endR/G/B` goes to `drawRaw` as its trailing `color` argument: an
|
|
36
|
+
* opaque packed ARGB that multiplies the frame's texels, so a white frame takes
|
|
37
|
+
* the colour exactly and a particle left at the default white draws unchanged.
|
|
38
|
+
* Alpha stays the separate `alpha` argument. With `@flare-engine/render`'s
|
|
39
|
+
* `SpriteBatch`, sprites drawn with a colour share one run per image whatever
|
|
40
|
+
* their colours and alphas, so a fading, colour-shifting emitter is one draw
|
|
41
|
+
* call rather than one per distinct alpha. Without the option, colour is not
|
|
42
|
+
* applied and each distinct alpha is its own run, as before.
|
|
28
43
|
*
|
|
29
44
|
* `image` and `frame` are separate parameters because they are separate
|
|
30
45
|
* everywhere else in the engine — a frame is a rectangle within a texture and
|
|
@@ -37,6 +52,7 @@ function drawParticles(pool, batch, image, frame, options) {
|
|
|
37
52
|
const effect = options?.effect;
|
|
38
53
|
const anchorX = options?.anchorX ?? .5;
|
|
39
54
|
const anchorY = options?.anchorY ?? .5;
|
|
55
|
+
const colored = options?.color === true;
|
|
40
56
|
const srcX = frame.x;
|
|
41
57
|
const srcY = frame.y;
|
|
42
58
|
const srcW = frame.w;
|
|
@@ -47,7 +63,13 @@ function drawParticles(pool, batch, image, frame, options) {
|
|
|
47
63
|
if (size <= 0) continue;
|
|
48
64
|
const alpha = pool.getAlpha(p);
|
|
49
65
|
if (alpha <= 0) continue;
|
|
50
|
-
|
|
66
|
+
if (!colored) {
|
|
67
|
+
batch.drawRaw(image, srcX, srcY, srcW, srcH, p.x, p.y, size, size, effect, anchorX, anchorY, alpha, p.rotation);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const c = pool.getColorInto(p, colorScratch);
|
|
71
|
+
const color = (4278190080 | channelByte(c.r) << 16 | channelByte(c.g) << 8 | channelByte(c.b)) >>> 0;
|
|
72
|
+
batch.drawRaw(image, srcX, srcY, srcW, srcH, p.x, p.y, size, size, effect, anchorX, anchorY, alpha, p.rotation, void 0, void 0, void 0, void 0, color);
|
|
51
73
|
}
|
|
52
74
|
}
|
|
53
75
|
// src/emitter-shape.ts
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["Pool"],"sources":["../src/draw-particles.ts","../src/emitter-shape.ts","../src/particle-pool.ts","../src/particle-emitter.ts"],"sourcesContent":["import type { ParticlePool } from './particle-pool.js';\n\n/**\n * The frame rectangle within a texture. Structural on purpose: it matches\n * `SpriteFrame` from `@flare-engine/assets` without this package depending on\n * it — extra members like `name` are simply ignored.\n */\nexport interface ParticleFrameLike {\n x: number;\n y: number;\n w: number;\n h: number;\n}\n\n/**\n * The one method this adapter needs from a sprite batch.\n *\n * Declared structurally so `@flare-engine/particles` keeps its two dependencies\n * (`core`, `math`) and stays usable with no renderer at all. A real\n * `SpriteBatch` from `@flare-engine/render` satisfies it; so does a test double.\n * The parameter list mirrors `SpriteBatch.drawRaw`.\n */\nexport interface ParticleBatchLike {\n drawRaw(\n image: unknown,\n srcX: number,\n srcY: number,\n srcW: number,\n srcH: number,\n dstX: number,\n dstY: number,\n dstW: number,\n dstH: number,\n effect?: unknown,\n anchorX?: number,\n anchorY?: number,\n alpha?: number,\n rotation?: number,\n stretch?: boolean,\n ): void;\n}\n\n/** Options for `drawParticles`. */\nexport interface DrawParticlesOptions {\n /**\n * A single effect applied to every particle in this call.\n *\n * Constant for the whole batch, and deliberately not per-particle: a batch\n * retains the effect *reference* until it flushes, so one mutable object\n * reused across the loop would give every particle the last particle's\n * values. Per-particle effects therefore cost one object per particle per\n * frame, which is exactly what this package exists to avoid — pass a constant\n * here, or build the loop yourself over `pool.particles`.\n */\n effect?: unknown;\n /** Anchor within the destination rect. Defaults to the centre (0.5, 0.5). */\n anchorX?: number;\n anchorY?: number;\n}\n\n/**\n * Queue every live particle into a caller-owned sprite batch.\n *\n * Zero-alloc: the loop reads the pool's active list directly, allocates nothing\n * per particle, and hands each one to `drawRaw` — so a steady-state emitter adds\n * no garbage. Size and alpha come from the pool's own interpolators, and\n * rotation from the particle.\n *\n * The batch is passed in rather than owned so particles share one batch, and one\n * flush, with the rest of the scene — the `drawInto` idiom used elsewhere in the\n * engine.\n *\n * ```ts\n * // once, outside the loop\n * const frame = sheet.frames.find((f) => f.name === 'spark')!;\n * // each frame, before the batch flushes\n * drawParticles(emitter.pool, batch, atlas.image, frame);\n * ```\n *\n * **Colour is not applied per particle.** The pool interpolates\n * `startR/G/B → endR/G/B`, but delivering that to a batch needs one effect\n * object per particle per frame (see `DrawParticlesOptions.effect`). Alpha *is*\n * applied, because `drawRaw` takes it as a number. For per-particle colour today,\n * write the loop yourself over `pool.particles` and accept the allocation.\n *\n * `image` and `frame` are separate parameters because they are separate\n * everywhere else in the engine — a frame is a rectangle within a texture and\n * does not carry the texture.\n */\nexport function drawParticles(\n pool: ParticlePool,\n batch: ParticleBatchLike,\n image: unknown,\n frame: ParticleFrameLike,\n options?: DrawParticlesOptions,\n): void {\n const particles = pool.particles;\n const count = particles.length;\n if (count === 0) return;\n\n const effect = options?.effect;\n const anchorX = options?.anchorX ?? 0.5;\n const anchorY = options?.anchorY ?? 0.5;\n const srcX = frame.x;\n const srcY = frame.y;\n const srcW = frame.w;\n const srcH = frame.h;\n\n for (let i = 0; i < count; i++) {\n const p = particles[i]!;\n const size = pool.getSize(p);\n // A fully shrunk or fully faded particle costs a draw call for nothing.\n if (size <= 0) continue;\n const alpha = pool.getAlpha(p);\n if (alpha <= 0) continue;\n\n batch.drawRaw(\n image,\n srcX,\n srcY,\n srcW,\n srcH,\n p.x,\n p.y,\n size,\n size,\n effect,\n anchorX,\n anchorY,\n alpha,\n p.rotation,\n );\n }\n}\n","/**\n * Emitter shapes — where a particle starts, not where it goes.\n *\n * The split matters, because `EmitterConfig` already owns direction: `direction`\n * plus `spread` describe the velocity cone. A shape samples a **position offset**\n * from the emitter origin and reports the outward normal at that point, so the\n * two compose instead of competing. A shape with a meaningful outward direction\n * can opt velocity into it with `radial: true` — that is what turns a `ring`\n * from a circle of drifting particles into an explosion.\n */\n\n/** A filled disc of radius `radius`, sampled uniformly by area. */\nexport interface EmitterCircleShape {\n kind: 'circle';\n radius: number;\n /** Emit outward from the centre instead of along `config.direction`. */\n radial?: boolean;\n}\n\n/** An annulus at `radius`, `thickness` wide (default 0 — a bare ring). */\nexport interface EmitterRingShape {\n kind: 'ring';\n radius: number;\n thickness?: number;\n /** Emit outward from the centre instead of along `config.direction`. */\n radial?: boolean;\n}\n\n/** An axis-aligned rectangle centred on the emitter, sampled uniformly. */\nexport interface EmitterRectShape {\n kind: 'rect';\n width: number;\n height: number;\n}\n\n/**\n * A sector (wedge) of radius `radius` and half-angle `angle`, centred on\n * `config.direction` and sampled uniformly by area.\n *\n * This is a *position* wedge, which is what keeps it distinct from\n * `config.spread`: `spread` fans the velocity out of a single point, `cone`\n * spreads the spawn points themselves across the wedge.\n */\nexport interface EmitterConeShape {\n kind: 'cone';\n radius: number;\n angle: number;\n /** Emit outward from the apex instead of along `config.direction`. */\n radial?: boolean;\n}\n\n/** The emitter origin — every particle starts at the same point. The default. */\nexport interface EmitterPointShape {\n kind: 'point';\n}\n\n/** Where newly emitted particles are placed, relative to the emitter. */\nexport type EmitterShape =\n | EmitterPointShape\n | EmitterCircleShape\n | EmitterRingShape\n | EmitterRectShape\n | EmitterConeShape;\n\n/**\n * A sampled spawn point: an offset from the emitter origin, plus the outward\n * normal at that offset.\n *\n * `nx`/`ny` are unit length for the radial shapes. For `point` and `rect` there\n * is no meaningful outward direction and both are 0 — a caller honouring\n * `radial` must fall back to `config.direction`, which is what `hasRadialNormal`\n * is for.\n */\nexport interface ShapeSample {\n x: number;\n y: number;\n nx: number;\n ny: number;\n}\n\n/** A fresh, zeroed sample. Allocate once outside the loop and reuse. */\nexport function createShapeSample(): ShapeSample {\n return { x: 0, y: 0, nx: 0, ny: 0 };\n}\n\nconst TAU = Math.PI * 2;\n\n/**\n * Does this shape produce a usable outward normal?\n *\n * True only when the shape is radial *and* opted in. Lets a caller decide\n * between the normal and `config.direction` without inspecting the union.\n */\nexport function hasRadialNormal(shape: EmitterShape): boolean {\n switch (shape.kind) {\n case 'circle':\n case 'ring':\n case 'cone':\n return shape.radial === true;\n default:\n return false;\n }\n}\n\n/**\n * Sample a spawn offset into `out`. Zero-alloc: nothing is constructed here, so\n * this is safe to call once per emitted particle in a steady-state loop.\n *\n * `rand` must return a float in [0, 1). Pass the emitter's seeded generator to\n * keep emission deterministic — every shape draws a fixed number of values per\n * call (`point` 0, `ring`/`rect` 2, `circle`/`cone` 2), so a given seed replays\n * exactly.\n *\n * `direction` orients `cone` and is ignored by every other shape.\n */\nexport function sampleEmitterShape(\n shape: EmitterShape,\n rand: () => number,\n out: ShapeSample,\n direction = 0,\n): ShapeSample {\n switch (shape.kind) {\n case 'point': {\n out.x = 0;\n out.y = 0;\n out.nx = 0;\n out.ny = 0;\n return out;\n }\n case 'circle': {\n const angle = rand() * TAU;\n // sqrt keeps the distribution uniform by area; without it particles\n // bunch toward the centre.\n const r = Math.sqrt(rand()) * Math.max(0, shape.radius);\n const cos = Math.cos(angle);\n const sin = Math.sin(angle);\n out.x = cos * r;\n out.y = sin * r;\n out.nx = cos;\n out.ny = sin;\n return out;\n }\n case 'ring': {\n const angle = rand() * TAU;\n const thickness = Math.max(0, shape.thickness ?? 0);\n const base = Math.max(0, shape.radius);\n // Uniform across the band rather than by area: a ring is thin by\n // definition, and area-weighting a 1px band buys nothing.\n const r = base + (rand() - 0.5) * thickness;\n const cos = Math.cos(angle);\n const sin = Math.sin(angle);\n out.x = cos * r;\n out.y = sin * r;\n out.nx = cos;\n out.ny = sin;\n return out;\n }\n case 'rect': {\n const w = Math.max(0, shape.width);\n const h = Math.max(0, shape.height);\n out.x = (rand() - 0.5) * w;\n out.y = (rand() - 0.5) * h;\n out.nx = 0;\n out.ny = 0;\n return out;\n }\n case 'cone': {\n const half = Math.max(0, shape.angle);\n const angle = direction + (rand() * 2 - 1) * half;\n const r = Math.sqrt(rand()) * Math.max(0, shape.radius);\n const cos = Math.cos(angle);\n const sin = Math.sin(angle);\n out.x = cos * r;\n out.y = sin * r;\n out.nx = cos;\n out.ny = sin;\n return out;\n }\n }\n}\n","import { Pool } from '@flare-engine/core';\nimport type { Color } from '@flare-engine/math';\n\n/** A single particle. Managed by ParticlePool — not user-facing. */\nexport interface Particle {\n /** World position. */\n x: number;\n y: number;\n /** Velocity in pixels/second. */\n vx: number;\n vy: number;\n /** Time remaining in seconds. */\n life: number;\n /** Total lifetime for interpolation. */\n maxLife: number;\n /** Start/end size for interpolation. */\n startSize: number;\n endSize: number;\n /** Start/end alpha for interpolation. */\n startAlpha: number;\n endAlpha: number;\n /** Start/end color components for interpolation. */\n startR: number;\n startG: number;\n startB: number;\n endR: number;\n endG: number;\n endB: number;\n /** Rotation in radians. */\n rotation: number;\n /** Angular velocity in radians/second. */\n angularVelocity: number;\n /** Whether this particle is active. */\n active: boolean;\n}\n\n/** Create a default (inactive) particle. */\nfunction createParticle(): Particle {\n return {\n x: 0,\n y: 0,\n vx: 0,\n vy: 0,\n life: 0,\n maxLife: 0,\n startSize: 1,\n endSize: 1,\n startAlpha: 1,\n endAlpha: 0,\n startR: 1,\n startG: 1,\n startB: 1,\n endR: 1,\n endG: 1,\n endB: 1,\n rotation: 0,\n angularVelocity: 0,\n active: false,\n };\n}\n\n/** Reset a particle to default (inactive) state. */\nfunction resetParticle(p: Particle): void {\n p.x = 0;\n p.y = 0;\n p.vx = 0;\n p.vy = 0;\n p.life = 0;\n p.maxLife = 0;\n p.startSize = 1;\n p.endSize = 1;\n p.startAlpha = 1;\n p.endAlpha = 0;\n p.startR = 1;\n p.startG = 1;\n p.startB = 1;\n p.endR = 1;\n p.endG = 1;\n p.endB = 1;\n p.rotation = 0;\n p.angularVelocity = 0;\n p.active = false;\n}\n\n/**\n * ParticlePool — pre-allocated pool of particles.\n * Zero GC pressure during steady-state gameplay.\n */\nexport class ParticlePool {\n private _pool: Pool<Particle>;\n private _active: Particle[] = [];\n\n constructor(capacity = 256) {\n this._pool = new Pool(createParticle, resetParticle, { capacity });\n this._pool.prewarm(capacity);\n }\n\n /** Number of currently active particles. */\n get activeCount(): number {\n return this._active.length;\n }\n\n /**\n * Cumulative factory calls made by the internal pool's acquire()/tryAcquire().\n * prewarm() is excluded: factory calls there pre-fill the free list and are\n * not counted. Intended use: snapshot before and after a frame — a non-zero\n * delta in steady state means unexpected allocation.\n */\n get created(): number {\n return this._pool.created;\n }\n\n /** Read-only access to active particles. */\n get particles(): readonly Particle[] {\n return this._active;\n }\n\n /** Acquire a particle from the pool. */\n acquire(): Particle {\n const p = this._pool.acquire();\n p.active = true;\n this._active.push(p);\n return p;\n }\n\n /**\n * Acquire a particle only if the pool is below capacity.\n * Returns null at cap — no allocation occurs.\n */\n tryAcquire(): Particle | null {\n const p = this._pool.tryAcquire();\n if (!p) return null;\n p.active = true;\n this._active.push(p);\n return p;\n }\n\n /** Update all active particles. Releases dead particles. */\n update(dt: number, gravityX: number, gravityY: number): void {\n let i = 0;\n while (i < this._active.length) {\n const p = this._active[i]!;\n p.life -= dt;\n\n if (p.life <= 0) {\n // Dead — release and swap-remove\n p.active = false;\n this._pool.release(p);\n this._active[i] = this._active[this._active.length - 1]!;\n this._active.pop();\n continue;\n }\n\n // Apply gravity\n p.vx += gravityX * dt;\n p.vy += gravityY * dt;\n\n // Integrate position\n p.x += p.vx * dt;\n p.y += p.vy * dt;\n\n // Rotate\n p.rotation += p.angularVelocity * dt;\n\n i++;\n }\n }\n\n /** Get interpolated size for a particle (0 = start, 1 = end of life). */\n getSize(p: Particle): number {\n const t = 1 - p.life / p.maxLife;\n return p.startSize + (p.endSize - p.startSize) * t;\n }\n\n /** Get interpolated alpha for a particle. */\n getAlpha(p: Particle): number {\n const t = 1 - p.life / p.maxLife;\n return p.startAlpha + (p.endAlpha - p.startAlpha) * t;\n }\n\n /** Shared color buffer — avoids per-call allocation in the render hot path. */\n private _colorBuf: Color = { r: 1, g: 1, b: 1, a: 1 };\n\n /**\n * Get interpolated color for a particle.\n *\n * Returns a shared buffer — read the values immediately, do not retain the\n * reference. For a fresh object call `getColorInto()` with your own out param.\n */\n getColor(p: Particle): Color {\n return this.getColorInto(p, this._colorBuf);\n }\n\n /** Zero-alloc variant — writes the interpolated color into `out`. */\n getColorInto(p: Particle, out: Color): Color {\n const t = 1 - p.life / p.maxLife;\n out.r = p.startR + (p.endR - p.startR) * t;\n out.g = p.startG + (p.endG - p.startG) * t;\n out.b = p.startB + (p.endB - p.startB) * t;\n out.a = p.startAlpha + (p.endAlpha - p.startAlpha) * t;\n return out;\n }\n\n /** Release all active particles back to pool. */\n clear(): void {\n for (const p of this._active) {\n p.active = false;\n this._pool.release(p);\n }\n this._active.length = 0;\n }\n}\n","import type { Color } from '@flare-engine/math';\nimport type { EmitterShape, ShapeSample } from './emitter-shape.js';\nimport { createShapeSample, hasRadialNormal, sampleEmitterShape } from './emitter-shape.js';\nimport type { Particle } from './particle-pool.js';\nimport { ParticlePool } from './particle-pool.js';\n\n/** Configuration for a ParticleEmitter. */\nexport interface EmitterConfig {\n /** Emission rate in particles per second. */\n rate: number;\n /** Particle lifetime in seconds. */\n lifetime: number;\n /** Min lifetime (if set, actual lifetime is random between lifetime and lifetimeMax). */\n lifetimeMax?: number;\n /** Initial speed in pixels/second. */\n speed: number;\n /** Max speed (random range). */\n speedMax?: number;\n /** Emission direction in radians (0 = right). */\n direction: number;\n /** Spread angle in radians (half-angle of the emission cone). */\n spread: number;\n /** Gravity in pixels/second² (applied each frame). */\n gravityX?: number;\n gravityY?: number;\n /** Start size and end size. */\n startSize?: number;\n endSize?: number;\n /** Start alpha and end alpha. */\n startAlpha?: number;\n endAlpha?: number;\n /** Start color. */\n startColor?: Color;\n /** End color. */\n endColor?: Color;\n /** Angular velocity range (radians/second). */\n angularVelocity?: number;\n angularVelocityMax?: number;\n /** Maximum active particles (used as pool capacity default when no opts.maxParticles given). */\n maxParticles?: number;\n /**\n * Where particles spawn, relative to the emitter position. Defaults to\n * `{ kind: 'point' }`, which is the pre-existing behaviour — every particle\n * starts exactly at `x`/`y`.\n *\n * A shape samples position only; `direction` and `spread` still govern\n * velocity, unless the shape sets `radial: true` (see `EmitterShape`).\n */\n shape?: EmitterShape;\n}\n\n/** Construction options for ParticleEmitter. */\nexport interface EmitterOptions {\n /**\n * Hard cap on simultaneously active particles.\n * Overrides config.maxParticles when provided.\n * Defaults to config.maxParticles ?? 256.\n */\n maxParticles?: number;\n /**\n * Initial LCG seed for deterministic particle randomness.\n * Defaults to 1. Call reseed() at any time to restart the sequence.\n */\n seed?: number;\n}\n\n/**\n * ParticleEmitter — emits particles based on a configuration.\n * Uses a ParticlePool for zero-GC operation.\n *\n * Call `update(dt)` each frame. Read `pool.particles` for rendering.\n */\nexport class ParticleEmitter {\n readonly config: EmitterConfig;\n readonly pool: ParticlePool;\n\n /** Emitter world position. */\n x = 0;\n y = 0;\n\n /** Whether the emitter is actively emitting. */\n emitting = true;\n\n private _emitAccumulator = 0;\n private _seed = 0;\n /** Reused spawn-offset buffer — one per emitter, never per particle. */\n private readonly _sample: ShapeSample = createShapeSample();\n /** Bound once at construction; binding per emit would allocate a closure. */\n private readonly _rand = (): number => this._random();\n\n constructor(config: EmitterConfig, opts?: EmitterOptions) {\n this.config = config;\n const cap = opts?.maxParticles ?? config.maxParticles ?? 256;\n this.pool = new ParticlePool(cap);\n this._setSeed(opts?.seed ?? 1);\n }\n\n /** Reset the LCG seed — restarts the particle random sequence from this seed. */\n reseed(seed: number): void {\n this._setSeed(seed);\n }\n\n /** Normalize s into the valid Park-Miller domain [1, 2147483646]. */\n private _setSeed(s: number): void {\n let n = Number.isFinite(s) ? Math.floor(s) % 2147483647 : 0;\n if (n < 0) n += 2147483647;\n if (n === 0) n = 1;\n this._seed = n;\n }\n\n /** Update the emitter: emit new particles and update existing ones. */\n update(dt: number): void {\n if (this.emitting) {\n this._emitAccumulator += dt * this.config.rate;\n while (this._emitAccumulator >= 1) {\n this._emitAccumulator -= 1;\n if (!this._emitOne()) {\n this._emitAccumulator = 0;\n break;\n }\n }\n }\n\n this.pool.update(dt, this.config.gravityX ?? 0, this.config.gravityY ?? 0);\n }\n\n /** Emit a burst of particles. */\n burst(count: number): void {\n for (let i = 0; i < count; i++) {\n if (!this._emitOne()) break;\n }\n }\n\n /** Stop emitting and clear all particles. */\n reset(): void {\n this.emitting = false;\n this._emitAccumulator = 0;\n this.pool.clear();\n }\n\n /** Simple seeded pseudo-random for deterministic particles. */\n private _random(): number {\n this._seed = (this._seed * 16807) % 2147483647;\n return (this._seed - 1) / 2147483646;\n }\n\n /** Random float in [min, max]. */\n private _randomRange(min: number, max: number): number {\n return min + this._random() * (max - min);\n }\n\n /** Emit one particle. Returns false when the pool is at cap — caller should stop. */\n private _emitOne(): boolean {\n const p: Particle | null = this.pool.tryAcquire();\n if (!p) return false;\n\n const c = this.config;\n\n // Shape sampling comes first so it consumes its random draws before the\n // velocity draws below — that ordering is what makes a given seed replay\n // identically, and it is why `point` deliberately draws nothing at all\n // (an emitter with no shape produces the exact pre-shape sequence).\n const shape = c.shape;\n if (shape === undefined || shape.kind === 'point') {\n p.x = this.x;\n p.y = this.y;\n const angle = c.direction + this._randomRange(-c.spread, c.spread);\n const speed = c.speedMax ? this._randomRange(c.speed, c.speedMax) : c.speed;\n p.vx = Math.cos(angle) * speed;\n p.vy = Math.sin(angle) * speed;\n } else {\n const s = sampleEmitterShape(shape, this._rand, this._sample, c.direction);\n p.x = this.x + s.x;\n p.y = this.y + s.y;\n // A radial shape emits along its own outward normal, fanned by `spread`;\n // otherwise `direction` still rules, exactly as for a point emitter.\n const base = hasRadialNormal(shape) ? Math.atan2(s.ny, s.nx) : c.direction;\n const angle = base + this._randomRange(-c.spread, c.spread);\n const speed = c.speedMax ? this._randomRange(c.speed, c.speedMax) : c.speed;\n p.vx = Math.cos(angle) * speed;\n p.vy = Math.sin(angle) * speed;\n }\n\n const lifetime = c.lifetimeMax ? this._randomRange(c.lifetime, c.lifetimeMax) : c.lifetime;\n p.life = lifetime;\n p.maxLife = lifetime;\n\n p.startSize = c.startSize ?? 4;\n p.endSize = c.endSize ?? 0;\n\n p.startAlpha = c.startAlpha ?? 1;\n p.endAlpha = c.endAlpha ?? 0;\n\n if (c.startColor) {\n p.startR = c.startColor.r;\n p.startG = c.startColor.g;\n p.startB = c.startColor.b;\n }\n if (c.endColor) {\n p.endR = c.endColor.r;\n p.endG = c.endColor.g;\n p.endB = c.endColor.b;\n }\n\n if (c.angularVelocity !== undefined) {\n p.angularVelocity = c.angularVelocityMax\n ? this._randomRange(c.angularVelocity, c.angularVelocityMax)\n : c.angularVelocity;\n }\n\n return true;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyFA,SAAgB,cACd,MACA,OACA,OACA,OACA,SACM;CACN,MAAM,YAAY,KAAK;CACvB,MAAM,QAAQ,UAAU;CACxB,IAAI,UAAU,GAAG;CAEjB,MAAM,SAAS,SAAS;CACxB,MAAM,UAAU,SAAS,WAAW;CACpC,MAAM,UAAU,SAAS,WAAW;CACpC,MAAM,OAAO,MAAM;CACnB,MAAM,OAAO,MAAM;CACnB,MAAM,OAAO,MAAM;CACnB,MAAM,OAAO,MAAM;CAEnB,KAAK,IAAI,IAAI,GAAG,IAAI,OAAO,KAAK;EAC9B,MAAM,IAAI,UAAU;EACpB,MAAM,OAAO,KAAK,QAAQ,CAAC;EAE3B,IAAI,QAAQ,GAAG;EACf,MAAM,QAAQ,KAAK,SAAS,CAAC;EAC7B,IAAI,SAAS,GAAG;EAEhB,MAAM,QACJ,OACA,MACA,MACA,MACA,MACA,EAAE,GACF,EAAE,GACF,MACA,MACA,QACA,SACA,SACA,OACA,EAAE,QACJ;CACF;AACF;;;ACpDA,SAAgB,oBAAiC;CAC/C,OAAO;EAAE,GAAG;EAAG,GAAG;EAAG,IAAI;EAAG,IAAI;CAAE;AACpC;AAEA,MAAM,MAAM,KAAK,KAAK;;;;;;;AAQtB,SAAgB,gBAAgB,OAA8B;CAC5D,QAAQ,MAAM,MAAd;EACE,KAAK;EACL,KAAK;EACL,KAAK,QACH,OAAO,MAAM,WAAW;EAC1B,SACE,OAAO;CACX;AACF;;;;;;;;;;;;AAaA,SAAgB,mBACd,OACA,MACA,KACA,YAAY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ACT,EAAE,UAAU;EAEZ,EAAE,YAAY,EAAE,aAAa;EAC7B,EAAE,UAAU,EAAE,WAAW;EAEzB,EAAE,aAAa,EAAE,cAAc;EAC/B,EAAE,WAAW,EAAE,YAAY;EAE3B,IAAI,EAAE,YAAY;GAChB,EAAE,SAAS,EAAE,WAAW;GACxB,EAAE,SAAS,EAAE,WAAW;GACxB,EAAE,SAAS,EAAE,WAAW;EAC1B;EACA,IAAI,EAAE,UAAU;GACd,EAAE,OAAO,EAAE,SAAS;GACpB,EAAE,OAAO,EAAE,SAAS;GACpB,EAAE,OAAO,EAAE,SAAS;EACtB;EAEA,IAAI,EAAE,oBAAoB,KAAA,GACxB,EAAE,kBAAkB,EAAE,qBAClB,KAAK,aAAa,EAAE,iBAAiB,EAAE,kBAAkB,IACzD,EAAE;EAGR,OAAO;CACT;AACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["Pool"],"sources":["../src/draw-particles.ts","../src/emitter-shape.ts","../src/particle-pool.ts","../src/particle-emitter.ts"],"sourcesContent":["import type { Color } from '@flare-engine/math';\nimport type { ParticlePool } from './particle-pool.js';\n\n/**\n * The frame rectangle within a texture. Structural on purpose: it matches\n * `SpriteFrame` from `@flare-engine/assets` without this package depending on\n * it — extra members like `name` are simply ignored.\n */\nexport interface ParticleFrameLike {\n x: number;\n y: number;\n w: number;\n h: number;\n}\n\n/**\n * The one method this adapter needs from a sprite batch.\n *\n * Declared structurally so `@flare-engine/particles` keeps its two dependencies\n * (`core`, `math`) and stays usable with no renderer at all. A real\n * `SpriteBatch` from `@flare-engine/render` satisfies it; so does a test double.\n * The parameter list mirrors `SpriteBatch.drawRaw`. `color` is a packed-ARGB\n * multiplier (`0xAARRGGBB`); a batch that declares fewer parameters simply does\n * not receive it.\n */\nexport interface ParticleBatchLike {\n drawRaw(\n image: unknown,\n srcX: number,\n srcY: number,\n srcW: number,\n srcH: number,\n dstX: number,\n dstY: number,\n dstW: number,\n dstH: number,\n effect?: unknown,\n anchorX?: number,\n anchorY?: number,\n alpha?: number,\n rotation?: number,\n stretch?: boolean,\n blend?: 'normal' | 'additive',\n flipX?: boolean,\n flipY?: boolean,\n color?: number,\n ): void;\n}\n\n/** Options for `drawParticles`. */\nexport interface DrawParticlesOptions {\n /**\n * A single effect applied to every particle in this call.\n *\n * Constant for the whole batch, and deliberately not per-particle: a batch\n * retains the effect *reference* until it flushes, so one mutable object\n * reused across the loop would give every particle the last particle's\n * values. Per-particle effects therefore cost one object per particle per\n * frame, which is exactly what this package exists to avoid — pass a constant\n * here, or build the loop yourself over `pool.particles`.\n *\n * Per-particle colour needs no effect: with `color: true` it is passed as a\n * number. Use this for what a colour multiplier cannot express, such as a\n * flash, an outline or grayscale.\n */\n effect?: unknown;\n /** Anchor within the destination rect. Defaults to the centre (0.5, 0.5). */\n anchorX?: number;\n anchorY?: number;\n /**\n * Pass each particle's interpolated `startR/G/B → endR/G/B` to `drawRaw` as its\n * trailing `color` argument. Defaults to `false`, which draws exactly as before\n * this option existed: no colour argument, and colour not applied.\n *\n * With `true` and `@flare-engine/render`'s `SpriteBatch`, each particle is\n * tinted by its colour, and coloured sprites share one run per image whatever\n * their colours and alphas — a fading, colour-shifting emitter becomes one draw\n * call instead of one per distinct alpha. The batch's renderer must support\n * per-sprite colours (both of the engine's backends do).\n */\n color?: boolean;\n}\n\n/** Interpolated colour of the particle being drawn; rewritten for each one. */\nconst colorScratch: Color = { r: 1, g: 1, b: 1, a: 1 };\n\n/** A 0-1 channel as a 0-255 byte, clamped so it cannot spill into a neighbouring channel. */\nfunction channelByte(value: number): number {\n return value <= 0 ? 0 : value >= 1 ? 255 : Math.round(value * 255);\n}\n\n/**\n * Queue every live particle into a caller-owned sprite batch.\n *\n * Zero-alloc: the loop reads the pool's active list directly, allocates nothing\n * per particle, and hands each one to `drawRaw` — so a steady-state emitter adds\n * no garbage. Size, alpha and (with `color: true`) colour come from the pool's\n * own interpolators, and rotation from the particle.\n *\n * The batch is passed in rather than owned so particles share one batch, and one\n * flush, with the rest of the scene — the `drawInto` idiom used elsewhere in the\n * engine.\n *\n * ```ts\n * // once, outside the loop\n * const frame = sheet.frames.find((f) => f.name === 'spark')!;\n * // each frame, before the batch flushes\n * drawParticles(emitter.pool, batch, atlas.image, frame);\n * ```\n *\n * **Colour is opt-in** (`{ color: true }`). Then each particle's interpolated\n * `startR/G/B → endR/G/B` goes to `drawRaw` as its trailing `color` argument: an\n * opaque packed ARGB that multiplies the frame's texels, so a white frame takes\n * the colour exactly and a particle left at the default white draws unchanged.\n * Alpha stays the separate `alpha` argument. With `@flare-engine/render`'s\n * `SpriteBatch`, sprites drawn with a colour share one run per image whatever\n * their colours and alphas, so a fading, colour-shifting emitter is one draw\n * call rather than one per distinct alpha. Without the option, colour is not\n * applied and each distinct alpha is its own run, as before.\n *\n * `image` and `frame` are separate parameters because they are separate\n * everywhere else in the engine — a frame is a rectangle within a texture and\n * does not carry the texture.\n */\nexport function drawParticles(\n pool: ParticlePool,\n batch: ParticleBatchLike,\n image: unknown,\n frame: ParticleFrameLike,\n options?: DrawParticlesOptions,\n): void {\n const particles = pool.particles;\n const count = particles.length;\n if (count === 0) return;\n\n const effect = options?.effect;\n const anchorX = options?.anchorX ?? 0.5;\n const anchorY = options?.anchorY ?? 0.5;\n const colored = options?.color === true;\n const srcX = frame.x;\n const srcY = frame.y;\n const srcW = frame.w;\n const srcH = frame.h;\n\n for (let i = 0; i < count; i++) {\n const p = particles[i]!;\n const size = pool.getSize(p);\n // A fully shrunk or fully faded particle costs a draw call for nothing.\n if (size <= 0) continue;\n const alpha = pool.getAlpha(p);\n if (alpha <= 0) continue;\n\n if (!colored) {\n batch.drawRaw(\n image,\n srcX,\n srcY,\n srcW,\n srcH,\n p.x,\n p.y,\n size,\n size,\n effect,\n anchorX,\n anchorY,\n alpha,\n p.rotation,\n );\n continue;\n }\n\n const c = pool.getColorInto(p, colorScratch);\n // `>>> 0`: the opaque alpha byte would otherwise make the packed value negative.\n const color =\n (0xff000000 | (channelByte(c.r) << 16) | (channelByte(c.g) << 8) | channelByte(c.b)) >>> 0;\n\n batch.drawRaw(\n image,\n srcX,\n srcY,\n srcW,\n srcH,\n p.x,\n p.y,\n size,\n size,\n effect,\n anchorX,\n anchorY,\n alpha,\n p.rotation,\n undefined,\n undefined,\n undefined,\n undefined,\n color,\n );\n }\n}\n","/**\n * Emitter shapes — where a particle starts, not where it goes.\n *\n * The split matters, because `EmitterConfig` already owns direction: `direction`\n * plus `spread` describe the velocity cone. A shape samples a **position offset**\n * from the emitter origin and reports the outward normal at that point, so the\n * two compose instead of competing. A shape with a meaningful outward direction\n * can opt velocity into it with `radial: true` — that is what turns a `ring`\n * from a circle of drifting particles into an explosion.\n */\n\n/** A filled disc of radius `radius`, sampled uniformly by area. */\nexport interface EmitterCircleShape {\n kind: 'circle';\n radius: number;\n /** Emit outward from the centre instead of along `config.direction`. */\n radial?: boolean;\n}\n\n/** An annulus at `radius`, `thickness` wide (default 0 — a bare ring). */\nexport interface EmitterRingShape {\n kind: 'ring';\n radius: number;\n thickness?: number;\n /** Emit outward from the centre instead of along `config.direction`. */\n radial?: boolean;\n}\n\n/** An axis-aligned rectangle centred on the emitter, sampled uniformly. */\nexport interface EmitterRectShape {\n kind: 'rect';\n width: number;\n height: number;\n}\n\n/**\n * A sector (wedge) of radius `radius` and half-angle `angle`, centred on\n * `config.direction` and sampled uniformly by area.\n *\n * This is a *position* wedge, which is what keeps it distinct from\n * `config.spread`: `spread` fans the velocity out of a single point, `cone`\n * spreads the spawn points themselves across the wedge.\n */\nexport interface EmitterConeShape {\n kind: 'cone';\n radius: number;\n angle: number;\n /** Emit outward from the apex instead of along `config.direction`. */\n radial?: boolean;\n}\n\n/** The emitter origin — every particle starts at the same point. The default. */\nexport interface EmitterPointShape {\n kind: 'point';\n}\n\n/** Where newly emitted particles are placed, relative to the emitter. */\nexport type EmitterShape =\n | EmitterPointShape\n | EmitterCircleShape\n | EmitterRingShape\n | EmitterRectShape\n | EmitterConeShape;\n\n/**\n * A sampled spawn point: an offset from the emitter origin, plus the outward\n * normal at that offset.\n *\n * `nx`/`ny` are unit length for the radial shapes. For `point` and `rect` there\n * is no meaningful outward direction and both are 0 — a caller honouring\n * `radial` must fall back to `config.direction`, which is what `hasRadialNormal`\n * is for.\n */\nexport interface ShapeSample {\n x: number;\n y: number;\n nx: number;\n ny: number;\n}\n\n/** A fresh, zeroed sample. Allocate once outside the loop and reuse. */\nexport function createShapeSample(): ShapeSample {\n return { x: 0, y: 0, nx: 0, ny: 0 };\n}\n\nconst TAU = Math.PI * 2;\n\n/**\n * Does this shape produce a usable outward normal?\n *\n * True only when the shape is radial *and* opted in. Lets a caller decide\n * between the normal and `config.direction` without inspecting the union.\n */\nexport function hasRadialNormal(shape: EmitterShape): boolean {\n switch (shape.kind) {\n case 'circle':\n case 'ring':\n case 'cone':\n return shape.radial === true;\n default:\n return false;\n }\n}\n\n/**\n * Sample a spawn offset into `out`. Zero-alloc: nothing is constructed here, so\n * this is safe to call once per emitted particle in a steady-state loop.\n *\n * `rand` must return a float in [0, 1). Pass the emitter's seeded generator to\n * keep emission deterministic — every shape draws a fixed number of values per\n * call (`point` 0, `ring`/`rect` 2, `circle`/`cone` 2), so a given seed replays\n * exactly.\n *\n * `direction` orients `cone` and is ignored by every other shape.\n */\nexport function sampleEmitterShape(\n shape: EmitterShape,\n rand: () => number,\n out: ShapeSample,\n direction = 0,\n): ShapeSample {\n switch (shape.kind) {\n case 'point': {\n out.x = 0;\n out.y = 0;\n out.nx = 0;\n out.ny = 0;\n return out;\n }\n case 'circle': {\n const angle = rand() * TAU;\n // sqrt keeps the distribution uniform by area; without it particles\n // bunch toward the centre.\n const r = Math.sqrt(rand()) * Math.max(0, shape.radius);\n const cos = Math.cos(angle);\n const sin = Math.sin(angle);\n out.x = cos * r;\n out.y = sin * r;\n out.nx = cos;\n out.ny = sin;\n return out;\n }\n case 'ring': {\n const angle = rand() * TAU;\n const thickness = Math.max(0, shape.thickness ?? 0);\n const base = Math.max(0, shape.radius);\n // Uniform across the band rather than by area: a ring is thin by\n // definition, and area-weighting a 1px band buys nothing.\n const r = base + (rand() - 0.5) * thickness;\n const cos = Math.cos(angle);\n const sin = Math.sin(angle);\n out.x = cos * r;\n out.y = sin * r;\n out.nx = cos;\n out.ny = sin;\n return out;\n }\n case 'rect': {\n const w = Math.max(0, shape.width);\n const h = Math.max(0, shape.height);\n out.x = (rand() - 0.5) * w;\n out.y = (rand() - 0.5) * h;\n out.nx = 0;\n out.ny = 0;\n return out;\n }\n case 'cone': {\n const half = Math.max(0, shape.angle);\n const angle = direction + (rand() * 2 - 1) * half;\n const r = Math.sqrt(rand()) * Math.max(0, shape.radius);\n const cos = Math.cos(angle);\n const sin = Math.sin(angle);\n out.x = cos * r;\n out.y = sin * r;\n out.nx = cos;\n out.ny = sin;\n return out;\n }\n }\n}\n","import { Pool } from '@flare-engine/core';\nimport type { Color } from '@flare-engine/math';\n\n/** A single particle. Managed by ParticlePool — not user-facing. */\nexport interface Particle {\n /** World position. */\n x: number;\n y: number;\n /** Velocity in pixels/second. */\n vx: number;\n vy: number;\n /** Time remaining in seconds. */\n life: number;\n /** Total lifetime for interpolation. */\n maxLife: number;\n /** Start/end size for interpolation. */\n startSize: number;\n endSize: number;\n /** Start/end alpha for interpolation. */\n startAlpha: number;\n endAlpha: number;\n /** Start/end color components for interpolation. */\n startR: number;\n startG: number;\n startB: number;\n endR: number;\n endG: number;\n endB: number;\n /** Rotation in radians. */\n rotation: number;\n /** Angular velocity in radians/second. */\n angularVelocity: number;\n /** Whether this particle is active. */\n active: boolean;\n}\n\n/** Create a default (inactive) particle. */\nfunction createParticle(): Particle {\n return {\n x: 0,\n y: 0,\n vx: 0,\n vy: 0,\n life: 0,\n maxLife: 0,\n startSize: 1,\n endSize: 1,\n startAlpha: 1,\n endAlpha: 0,\n startR: 1,\n startG: 1,\n startB: 1,\n endR: 1,\n endG: 1,\n endB: 1,\n rotation: 0,\n angularVelocity: 0,\n active: false,\n };\n}\n\n/** Reset a particle to default (inactive) state. */\nfunction resetParticle(p: Particle): void {\n p.x = 0;\n p.y = 0;\n p.vx = 0;\n p.vy = 0;\n p.life = 0;\n p.maxLife = 0;\n p.startSize = 1;\n p.endSize = 1;\n p.startAlpha = 1;\n p.endAlpha = 0;\n p.startR = 1;\n p.startG = 1;\n p.startB = 1;\n p.endR = 1;\n p.endG = 1;\n p.endB = 1;\n p.rotation = 0;\n p.angularVelocity = 0;\n p.active = false;\n}\n\n/**\n * ParticlePool — pre-allocated pool of particles.\n * Zero GC pressure during steady-state gameplay.\n */\nexport class ParticlePool {\n private _pool: Pool<Particle>;\n private _active: Particle[] = [];\n\n constructor(capacity = 256) {\n this._pool = new Pool(createParticle, resetParticle, { capacity });\n this._pool.prewarm(capacity);\n }\n\n /** Number of currently active particles. */\n get activeCount(): number {\n return this._active.length;\n }\n\n /**\n * Cumulative factory calls made by the internal pool's acquire()/tryAcquire().\n * prewarm() is excluded: factory calls there pre-fill the free list and are\n * not counted. Intended use: snapshot before and after a frame — a non-zero\n * delta in steady state means unexpected allocation.\n */\n get created(): number {\n return this._pool.created;\n }\n\n /** Read-only access to active particles. */\n get particles(): readonly Particle[] {\n return this._active;\n }\n\n /** Acquire a particle from the pool. */\n acquire(): Particle {\n const p = this._pool.acquire();\n p.active = true;\n this._active.push(p);\n return p;\n }\n\n /**\n * Acquire a particle only if the pool is below capacity.\n * Returns null at cap — no allocation occurs.\n */\n tryAcquire(): Particle | null {\n const p = this._pool.tryAcquire();\n if (!p) return null;\n p.active = true;\n this._active.push(p);\n return p;\n }\n\n /** Update all active particles. Releases dead particles. */\n update(dt: number, gravityX: number, gravityY: number): void {\n let i = 0;\n while (i < this._active.length) {\n const p = this._active[i]!;\n p.life -= dt;\n\n if (p.life <= 0) {\n // Dead — release and swap-remove\n p.active = false;\n this._pool.release(p);\n this._active[i] = this._active[this._active.length - 1]!;\n this._active.pop();\n continue;\n }\n\n // Apply gravity\n p.vx += gravityX * dt;\n p.vy += gravityY * dt;\n\n // Integrate position\n p.x += p.vx * dt;\n p.y += p.vy * dt;\n\n // Rotate\n p.rotation += p.angularVelocity * dt;\n\n i++;\n }\n }\n\n /** Get interpolated size for a particle (0 = start, 1 = end of life). */\n getSize(p: Particle): number {\n const t = 1 - p.life / p.maxLife;\n return p.startSize + (p.endSize - p.startSize) * t;\n }\n\n /** Get interpolated alpha for a particle. */\n getAlpha(p: Particle): number {\n const t = 1 - p.life / p.maxLife;\n return p.startAlpha + (p.endAlpha - p.startAlpha) * t;\n }\n\n /** Shared color buffer — avoids per-call allocation in the render hot path. */\n private _colorBuf: Color = { r: 1, g: 1, b: 1, a: 1 };\n\n /**\n * Get interpolated color for a particle.\n *\n * Returns a shared buffer — read the values immediately, do not retain the\n * reference. For a fresh object call `getColorInto()` with your own out param.\n */\n getColor(p: Particle): Color {\n return this.getColorInto(p, this._colorBuf);\n }\n\n /** Zero-alloc variant — writes the interpolated color into `out`. */\n getColorInto(p: Particle, out: Color): Color {\n const t = 1 - p.life / p.maxLife;\n out.r = p.startR + (p.endR - p.startR) * t;\n out.g = p.startG + (p.endG - p.startG) * t;\n out.b = p.startB + (p.endB - p.startB) * t;\n out.a = p.startAlpha + (p.endAlpha - p.startAlpha) * t;\n return out;\n }\n\n /** Release all active particles back to pool. */\n clear(): void {\n for (const p of this._active) {\n p.active = false;\n this._pool.release(p);\n }\n this._active.length = 0;\n }\n}\n","import type { Color } from '@flare-engine/math';\nimport type { EmitterShape, ShapeSample } from './emitter-shape.js';\nimport { createShapeSample, hasRadialNormal, sampleEmitterShape } from './emitter-shape.js';\nimport type { Particle } from './particle-pool.js';\nimport { ParticlePool } from './particle-pool.js';\n\n/** Configuration for a ParticleEmitter. */\nexport interface EmitterConfig {\n /** Emission rate in particles per second. */\n rate: number;\n /** Particle lifetime in seconds. */\n lifetime: number;\n /** Min lifetime (if set, actual lifetime is random between lifetime and lifetimeMax). */\n lifetimeMax?: number;\n /** Initial speed in pixels/second. */\n speed: number;\n /** Max speed (random range). */\n speedMax?: number;\n /** Emission direction in radians (0 = right). */\n direction: number;\n /** Spread angle in radians (half-angle of the emission cone). */\n spread: number;\n /** Gravity in pixels/second² (applied each frame). */\n gravityX?: number;\n gravityY?: number;\n /** Start size and end size. */\n startSize?: number;\n endSize?: number;\n /** Start alpha and end alpha. */\n startAlpha?: number;\n endAlpha?: number;\n /** Start color. */\n startColor?: Color;\n /** End color. */\n endColor?: Color;\n /** Angular velocity range (radians/second). */\n angularVelocity?: number;\n angularVelocityMax?: number;\n /** Maximum active particles (used as pool capacity default when no opts.maxParticles given). */\n maxParticles?: number;\n /**\n * Where particles spawn, relative to the emitter position. Defaults to\n * `{ kind: 'point' }`, which is the pre-existing behaviour — every particle\n * starts exactly at `x`/`y`.\n *\n * A shape samples position only; `direction` and `spread` still govern\n * velocity, unless the shape sets `radial: true` (see `EmitterShape`).\n */\n shape?: EmitterShape;\n}\n\n/** Construction options for ParticleEmitter. */\nexport interface EmitterOptions {\n /**\n * Hard cap on simultaneously active particles.\n * Overrides config.maxParticles when provided.\n * Defaults to config.maxParticles ?? 256.\n */\n maxParticles?: number;\n /**\n * Initial LCG seed for deterministic particle randomness.\n * Defaults to 1. Call reseed() at any time to restart the sequence.\n */\n seed?: number;\n}\n\n/**\n * ParticleEmitter — emits particles based on a configuration.\n * Uses a ParticlePool for zero-GC operation.\n *\n * Call `update(dt)` each frame. Read `pool.particles` for rendering.\n */\nexport class ParticleEmitter {\n readonly config: EmitterConfig;\n readonly pool: ParticlePool;\n\n /** Emitter world position. */\n x = 0;\n y = 0;\n\n /** Whether the emitter is actively emitting. */\n emitting = true;\n\n private _emitAccumulator = 0;\n private _seed = 0;\n /** Reused spawn-offset buffer — one per emitter, never per particle. */\n private readonly _sample: ShapeSample = createShapeSample();\n /** Bound once at construction; binding per emit would allocate a closure. */\n private readonly _rand = (): number => this._random();\n\n constructor(config: EmitterConfig, opts?: EmitterOptions) {\n this.config = config;\n const cap = opts?.maxParticles ?? config.maxParticles ?? 256;\n this.pool = new ParticlePool(cap);\n this._setSeed(opts?.seed ?? 1);\n }\n\n /** Reset the LCG seed — restarts the particle random sequence from this seed. */\n reseed(seed: number): void {\n this._setSeed(seed);\n }\n\n /** Normalize s into the valid Park-Miller domain [1, 2147483646]. */\n private _setSeed(s: number): void {\n let n = Number.isFinite(s) ? Math.floor(s) % 2147483647 : 0;\n if (n < 0) n += 2147483647;\n if (n === 0) n = 1;\n this._seed = n;\n }\n\n /** Update the emitter: emit new particles and update existing ones. */\n update(dt: number): void {\n if (this.emitting) {\n this._emitAccumulator += dt * this.config.rate;\n while (this._emitAccumulator >= 1) {\n this._emitAccumulator -= 1;\n if (!this._emitOne()) {\n this._emitAccumulator = 0;\n break;\n }\n }\n }\n\n this.pool.update(dt, this.config.gravityX ?? 0, this.config.gravityY ?? 0);\n }\n\n /** Emit a burst of particles. */\n burst(count: number): void {\n for (let i = 0; i < count; i++) {\n if (!this._emitOne()) break;\n }\n }\n\n /** Stop emitting and clear all particles. */\n reset(): void {\n this.emitting = false;\n this._emitAccumulator = 0;\n this.pool.clear();\n }\n\n /** Simple seeded pseudo-random for deterministic particles. */\n private _random(): number {\n this._seed = (this._seed * 16807) % 2147483647;\n return (this._seed - 1) / 2147483646;\n }\n\n /** Random float in [min, max]. */\n private _randomRange(min: number, max: number): number {\n return min + this._random() * (max - min);\n }\n\n /** Emit one particle. Returns false when the pool is at cap — caller should stop. */\n private _emitOne(): boolean {\n const p: Particle | null = this.pool.tryAcquire();\n if (!p) return false;\n\n const c = this.config;\n\n // Shape sampling comes first so it consumes its random draws before the\n // velocity draws below — that ordering is what makes a given seed replay\n // identically, and it is why `point` deliberately draws nothing at all\n // (an emitter with no shape produces the exact pre-shape sequence).\n const shape = c.shape;\n if (shape === undefined || shape.kind === 'point') {\n p.x = this.x;\n p.y = this.y;\n const angle = c.direction + this._randomRange(-c.spread, c.spread);\n const speed = c.speedMax ? this._randomRange(c.speed, c.speedMax) : c.speed;\n p.vx = Math.cos(angle) * speed;\n p.vy = Math.sin(angle) * speed;\n } else {\n const s = sampleEmitterShape(shape, this._rand, this._sample, c.direction);\n p.x = this.x + s.x;\n p.y = this.y + s.y;\n // A radial shape emits along its own outward normal, fanned by `spread`;\n // otherwise `direction` still rules, exactly as for a point emitter.\n const base = hasRadialNormal(shape) ? Math.atan2(s.ny, s.nx) : c.direction;\n const angle = base + this._randomRange(-c.spread, c.spread);\n const speed = c.speedMax ? this._randomRange(c.speed, c.speedMax) : c.speed;\n p.vx = Math.cos(angle) * speed;\n p.vy = Math.sin(angle) * speed;\n }\n\n const lifetime = c.lifetimeMax ? this._randomRange(c.lifetime, c.lifetimeMax) : c.lifetime;\n p.life = lifetime;\n p.maxLife = lifetime;\n\n p.startSize = c.startSize ?? 4;\n p.endSize = c.endSize ?? 0;\n\n p.startAlpha = c.startAlpha ?? 1;\n p.endAlpha = c.endAlpha ?? 0;\n\n if (c.startColor) {\n p.startR = c.startColor.r;\n p.startG = c.startColor.g;\n p.startB = c.startColor.b;\n }\n if (c.endColor) {\n p.endR = c.endColor.r;\n p.endG = c.endColor.g;\n p.endB = c.endColor.b;\n }\n\n if (c.angularVelocity !== undefined) {\n p.angularVelocity = c.angularVelocityMax\n ? this._randomRange(c.angularVelocity, c.angularVelocityMax)\n : c.angularVelocity;\n }\n\n return true;\n }\n}\n"],"mappings":";;;;AAoFA,MAAM,eAAsB;CAAE,GAAG;CAAG,GAAG;CAAG,GAAG;CAAG,GAAG;AAAE;;AAGrD,SAAS,YAAY,OAAuB;CAC1C,OAAO,SAAS,IAAI,IAAI,SAAS,IAAI,MAAM,KAAK,MAAM,QAAQ,GAAG;AACnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,SAAgB,cACd,MACA,OACA,OACA,OACA,SACM;CACN,MAAM,YAAY,KAAK;CACvB,MAAM,QAAQ,UAAU;CACxB,IAAI,UAAU,GAAG;CAEjB,MAAM,SAAS,SAAS;CACxB,MAAM,UAAU,SAAS,WAAW;CACpC,MAAM,UAAU,SAAS,WAAW;CACpC,MAAM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AY;EAE3B,IAAI,EAAE,YAAY;GAChB,EAAE,SAAS,EAAE,WAAW;GACxB,EAAE,SAAS,EAAE,WAAW;GACxB,EAAE,SAAS,EAAE,WAAW;EAC1B;EACA,IAAI,EAAE,UAAU;GACd,EAAE,OAAO,EAAE,SAAS;GACpB,EAAE,OAAO,EAAE,SAAS;GACpB,EAAE,OAAO,EAAE,SAAS;EACtB;EAEA,IAAI,EAAE,oBAAoB,KAAA,GACxB,EAAE,kBAAkB,EAAE,qBAClB,KAAK,aAAa,EAAE,iBAAiB,EAAE,kBAAkB,IACzD,EAAE;EAGR,OAAO;CACT;AACF"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
import { Pool } from "@flare-engine/core";
|
|
2
2
|
// src/draw-particles.ts
|
|
3
|
+
/** Interpolated colour of the particle being drawn; rewritten for each one. */
|
|
4
|
+
const colorScratch = {
|
|
5
|
+
r: 1,
|
|
6
|
+
g: 1,
|
|
7
|
+
b: 1,
|
|
8
|
+
a: 1
|
|
9
|
+
};
|
|
10
|
+
/** A 0-1 channel as a 0-255 byte, clamped so it cannot spill into a neighbouring channel. */
|
|
11
|
+
function channelByte(value) {
|
|
12
|
+
return value <= 0 ? 0 : value >= 1 ? 255 : Math.round(value * 255);
|
|
13
|
+
}
|
|
3
14
|
/**
|
|
4
15
|
* Queue every live particle into a caller-owned sprite batch.
|
|
5
16
|
*
|
|
6
17
|
* Zero-alloc: the loop reads the pool's active list directly, allocates nothing
|
|
7
18
|
* per particle, and hands each one to `drawRaw` — so a steady-state emitter adds
|
|
8
|
-
* no garbage. Size and
|
|
9
|
-
* rotation from the particle.
|
|
19
|
+
* no garbage. Size, alpha and (with `color: true`) colour come from the pool's
|
|
20
|
+
* own interpolators, and rotation from the particle.
|
|
10
21
|
*
|
|
11
22
|
* The batch is passed in rather than owned so particles share one batch, and one
|
|
12
23
|
* flush, with the rest of the scene — the `drawInto` idiom used elsewhere in the
|
|
@@ -19,11 +30,15 @@ import { Pool } from "@flare-engine/core";
|
|
|
19
30
|
* drawParticles(emitter.pool, batch, atlas.image, frame);
|
|
20
31
|
* ```
|
|
21
32
|
*
|
|
22
|
-
* **Colour is
|
|
23
|
-
* `startR/G/B → endR/G/B
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
33
|
+
* **Colour is opt-in** (`{ color: true }`). Then each particle's interpolated
|
|
34
|
+
* `startR/G/B → endR/G/B` goes to `drawRaw` as its trailing `color` argument: an
|
|
35
|
+
* opaque packed ARGB that multiplies the frame's texels, so a white frame takes
|
|
36
|
+
* the colour exactly and a particle left at the default white draws unchanged.
|
|
37
|
+
* Alpha stays the separate `alpha` argument. With `@flare-engine/render`'s
|
|
38
|
+
* `SpriteBatch`, sprites drawn with a colour share one run per image whatever
|
|
39
|
+
* their colours and alphas, so a fading, colour-shifting emitter is one draw
|
|
40
|
+
* call rather than one per distinct alpha. Without the option, colour is not
|
|
41
|
+
* applied and each distinct alpha is its own run, as before.
|
|
27
42
|
*
|
|
28
43
|
* `image` and `frame` are separate parameters because they are separate
|
|
29
44
|
* everywhere else in the engine — a frame is a rectangle within a texture and
|
|
@@ -36,6 +51,7 @@ function drawParticles(pool, batch, image, frame, options) {
|
|
|
36
51
|
const effect = options?.effect;
|
|
37
52
|
const anchorX = options?.anchorX ?? .5;
|
|
38
53
|
const anchorY = options?.anchorY ?? .5;
|
|
54
|
+
const colored = options?.color === true;
|
|
39
55
|
const srcX = frame.x;
|
|
40
56
|
const srcY = frame.y;
|
|
41
57
|
const srcW = frame.w;
|
|
@@ -46,7 +62,13 @@ function drawParticles(pool, batch, image, frame, options) {
|
|
|
46
62
|
if (size <= 0) continue;
|
|
47
63
|
const alpha = pool.getAlpha(p);
|
|
48
64
|
if (alpha <= 0) continue;
|
|
49
|
-
|
|
65
|
+
if (!colored) {
|
|
66
|
+
batch.drawRaw(image, srcX, srcY, srcW, srcH, p.x, p.y, size, size, effect, anchorX, anchorY, alpha, p.rotation);
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const c = pool.getColorInto(p, colorScratch);
|
|
70
|
+
const color = (4278190080 | channelByte(c.r) << 16 | channelByte(c.g) << 8 | channelByte(c.b)) >>> 0;
|
|
71
|
+
batch.drawRaw(image, srcX, srcY, srcW, srcH, p.x, p.y, size, size, effect, anchorX, anchorY, alpha, p.rotation, void 0, void 0, void 0, void 0, color);
|
|
50
72
|
}
|
|
51
73
|
}
|
|
52
74
|
// src/emitter-shape.ts
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/draw-particles.ts","../src/emitter-shape.ts","../src/particle-pool.ts","../src/particle-emitter.ts"],"sourcesContent":["import type { ParticlePool } from './particle-pool.js';\n\n/**\n * The frame rectangle within a texture. Structural on purpose: it matches\n * `SpriteFrame` from `@flare-engine/assets` without this package depending on\n * it — extra members like `name` are simply ignored.\n */\nexport interface ParticleFrameLike {\n x: number;\n y: number;\n w: number;\n h: number;\n}\n\n/**\n * The one method this adapter needs from a sprite batch.\n *\n * Declared structurally so `@flare-engine/particles` keeps its two dependencies\n * (`core`, `math`) and stays usable with no renderer at all. A real\n * `SpriteBatch` from `@flare-engine/render` satisfies it; so does a test double.\n * The parameter list mirrors `SpriteBatch.drawRaw`.\n */\nexport interface ParticleBatchLike {\n drawRaw(\n image: unknown,\n srcX: number,\n srcY: number,\n srcW: number,\n srcH: number,\n dstX: number,\n dstY: number,\n dstW: number,\n dstH: number,\n effect?: unknown,\n anchorX?: number,\n anchorY?: number,\n alpha?: number,\n rotation?: number,\n stretch?: boolean,\n ): void;\n}\n\n/** Options for `drawParticles`. */\nexport interface DrawParticlesOptions {\n /**\n * A single effect applied to every particle in this call.\n *\n * Constant for the whole batch, and deliberately not per-particle: a batch\n * retains the effect *reference* until it flushes, so one mutable object\n * reused across the loop would give every particle the last particle's\n * values. Per-particle effects therefore cost one object per particle per\n * frame, which is exactly what this package exists to avoid — pass a constant\n * here, or build the loop yourself over `pool.particles`.\n */\n effect?: unknown;\n /** Anchor within the destination rect. Defaults to the centre (0.5, 0.5). */\n anchorX?: number;\n anchorY?: number;\n}\n\n/**\n * Queue every live particle into a caller-owned sprite batch.\n *\n * Zero-alloc: the loop reads the pool's active list directly, allocates nothing\n * per particle, and hands each one to `drawRaw` — so a steady-state emitter adds\n * no garbage. Size and alpha come from the pool's own interpolators, and\n * rotation from the particle.\n *\n * The batch is passed in rather than owned so particles share one batch, and one\n * flush, with the rest of the scene — the `drawInto` idiom used elsewhere in the\n * engine.\n *\n * ```ts\n * // once, outside the loop\n * const frame = sheet.frames.find((f) => f.name === 'spark')!;\n * // each frame, before the batch flushes\n * drawParticles(emitter.pool, batch, atlas.image, frame);\n * ```\n *\n * **Colour is not applied per particle.** The pool interpolates\n * `startR/G/B → endR/G/B`, but delivering that to a batch needs one effect\n * object per particle per frame (see `DrawParticlesOptions.effect`). Alpha *is*\n * applied, because `drawRaw` takes it as a number. For per-particle colour today,\n * write the loop yourself over `pool.particles` and accept the allocation.\n *\n * `image` and `frame` are separate parameters because they are separate\n * everywhere else in the engine — a frame is a rectangle within a texture and\n * does not carry the texture.\n */\nexport function drawParticles(\n pool: ParticlePool,\n batch: ParticleBatchLike,\n image: unknown,\n frame: ParticleFrameLike,\n options?: DrawParticlesOptions,\n): void {\n const particles = pool.particles;\n const count = particles.length;\n if (count === 0) return;\n\n const effect = options?.effect;\n const anchorX = options?.anchorX ?? 0.5;\n const anchorY = options?.anchorY ?? 0.5;\n const srcX = frame.x;\n const srcY = frame.y;\n const srcW = frame.w;\n const srcH = frame.h;\n\n for (let i = 0; i < count; i++) {\n const p = particles[i]!;\n const size = pool.getSize(p);\n // A fully shrunk or fully faded particle costs a draw call for nothing.\n if (size <= 0) continue;\n const alpha = pool.getAlpha(p);\n if (alpha <= 0) continue;\n\n batch.drawRaw(\n image,\n srcX,\n srcY,\n srcW,\n srcH,\n p.x,\n p.y,\n size,\n size,\n effect,\n anchorX,\n anchorY,\n alpha,\n p.rotation,\n );\n }\n}\n","/**\n * Emitter shapes — where a particle starts, not where it goes.\n *\n * The split matters, because `EmitterConfig` already owns direction: `direction`\n * plus `spread` describe the velocity cone. A shape samples a **position offset**\n * from the emitter origin and reports the outward normal at that point, so the\n * two compose instead of competing. A shape with a meaningful outward direction\n * can opt velocity into it with `radial: true` — that is what turns a `ring`\n * from a circle of drifting particles into an explosion.\n */\n\n/** A filled disc of radius `radius`, sampled uniformly by area. */\nexport interface EmitterCircleShape {\n kind: 'circle';\n radius: number;\n /** Emit outward from the centre instead of along `config.direction`. */\n radial?: boolean;\n}\n\n/** An annulus at `radius`, `thickness` wide (default 0 — a bare ring). */\nexport interface EmitterRingShape {\n kind: 'ring';\n radius: number;\n thickness?: number;\n /** Emit outward from the centre instead of along `config.direction`. */\n radial?: boolean;\n}\n\n/** An axis-aligned rectangle centred on the emitter, sampled uniformly. */\nexport interface EmitterRectShape {\n kind: 'rect';\n width: number;\n height: number;\n}\n\n/**\n * A sector (wedge) of radius `radius` and half-angle `angle`, centred on\n * `config.direction` and sampled uniformly by area.\n *\n * This is a *position* wedge, which is what keeps it distinct from\n * `config.spread`: `spread` fans the velocity out of a single point, `cone`\n * spreads the spawn points themselves across the wedge.\n */\nexport interface EmitterConeShape {\n kind: 'cone';\n radius: number;\n angle: number;\n /** Emit outward from the apex instead of along `config.direction`. */\n radial?: boolean;\n}\n\n/** The emitter origin — every particle starts at the same point. The default. */\nexport interface EmitterPointShape {\n kind: 'point';\n}\n\n/** Where newly emitted particles are placed, relative to the emitter. */\nexport type EmitterShape =\n | EmitterPointShape\n | EmitterCircleShape\n | EmitterRingShape\n | EmitterRectShape\n | EmitterConeShape;\n\n/**\n * A sampled spawn point: an offset from the emitter origin, plus the outward\n * normal at that offset.\n *\n * `nx`/`ny` are unit length for the radial shapes. For `point` and `rect` there\n * is no meaningful outward direction and both are 0 — a caller honouring\n * `radial` must fall back to `config.direction`, which is what `hasRadialNormal`\n * is for.\n */\nexport interface ShapeSample {\n x: number;\n y: number;\n nx: number;\n ny: number;\n}\n\n/** A fresh, zeroed sample. Allocate once outside the loop and reuse. */\nexport function createShapeSample(): ShapeSample {\n return { x: 0, y: 0, nx: 0, ny: 0 };\n}\n\nconst TAU = Math.PI * 2;\n\n/**\n * Does this shape produce a usable outward normal?\n *\n * True only when the shape is radial *and* opted in. Lets a caller decide\n * between the normal and `config.direction` without inspecting the union.\n */\nexport function hasRadialNormal(shape: EmitterShape): boolean {\n switch (shape.kind) {\n case 'circle':\n case 'ring':\n case 'cone':\n return shape.radial === true;\n default:\n return false;\n }\n}\n\n/**\n * Sample a spawn offset into `out`. Zero-alloc: nothing is constructed here, so\n * this is safe to call once per emitted particle in a steady-state loop.\n *\n * `rand` must return a float in [0, 1). Pass the emitter's seeded generator to\n * keep emission deterministic — every shape draws a fixed number of values per\n * call (`point` 0, `ring`/`rect` 2, `circle`/`cone` 2), so a given seed replays\n * exactly.\n *\n * `direction` orients `cone` and is ignored by every other shape.\n */\nexport function sampleEmitterShape(\n shape: EmitterShape,\n rand: () => number,\n out: ShapeSample,\n direction = 0,\n): ShapeSample {\n switch (shape.kind) {\n case 'point': {\n out.x = 0;\n out.y = 0;\n out.nx = 0;\n out.ny = 0;\n return out;\n }\n case 'circle': {\n const angle = rand() * TAU;\n // sqrt keeps the distribution uniform by area; without it particles\n // bunch toward the centre.\n const r = Math.sqrt(rand()) * Math.max(0, shape.radius);\n const cos = Math.cos(angle);\n const sin = Math.sin(angle);\n out.x = cos * r;\n out.y = sin * r;\n out.nx = cos;\n out.ny = sin;\n return out;\n }\n case 'ring': {\n const angle = rand() * TAU;\n const thickness = Math.max(0, shape.thickness ?? 0);\n const base = Math.max(0, shape.radius);\n // Uniform across the band rather than by area: a ring is thin by\n // definition, and area-weighting a 1px band buys nothing.\n const r = base + (rand() - 0.5) * thickness;\n const cos = Math.cos(angle);\n const sin = Math.sin(angle);\n out.x = cos * r;\n out.y = sin * r;\n out.nx = cos;\n out.ny = sin;\n return out;\n }\n case 'rect': {\n const w = Math.max(0, shape.width);\n const h = Math.max(0, shape.height);\n out.x = (rand() - 0.5) * w;\n out.y = (rand() - 0.5) * h;\n out.nx = 0;\n out.ny = 0;\n return out;\n }\n case 'cone': {\n const half = Math.max(0, shape.angle);\n const angle = direction + (rand() * 2 - 1) * half;\n const r = Math.sqrt(rand()) * Math.max(0, shape.radius);\n const cos = Math.cos(angle);\n const sin = Math.sin(angle);\n out.x = cos * r;\n out.y = sin * r;\n out.nx = cos;\n out.ny = sin;\n return out;\n }\n }\n}\n","import { Pool } from '@flare-engine/core';\nimport type { Color } from '@flare-engine/math';\n\n/** A single particle. Managed by ParticlePool — not user-facing. */\nexport interface Particle {\n /** World position. */\n x: number;\n y: number;\n /** Velocity in pixels/second. */\n vx: number;\n vy: number;\n /** Time remaining in seconds. */\n life: number;\n /** Total lifetime for interpolation. */\n maxLife: number;\n /** Start/end size for interpolation. */\n startSize: number;\n endSize: number;\n /** Start/end alpha for interpolation. */\n startAlpha: number;\n endAlpha: number;\n /** Start/end color components for interpolation. */\n startR: number;\n startG: number;\n startB: number;\n endR: number;\n endG: number;\n endB: number;\n /** Rotation in radians. */\n rotation: number;\n /** Angular velocity in radians/second. */\n angularVelocity: number;\n /** Whether this particle is active. */\n active: boolean;\n}\n\n/** Create a default (inactive) particle. */\nfunction createParticle(): Particle {\n return {\n x: 0,\n y: 0,\n vx: 0,\n vy: 0,\n life: 0,\n maxLife: 0,\n startSize: 1,\n endSize: 1,\n startAlpha: 1,\n endAlpha: 0,\n startR: 1,\n startG: 1,\n startB: 1,\n endR: 1,\n endG: 1,\n endB: 1,\n rotation: 0,\n angularVelocity: 0,\n active: false,\n };\n}\n\n/** Reset a particle to default (inactive) state. */\nfunction resetParticle(p: Particle): void {\n p.x = 0;\n p.y = 0;\n p.vx = 0;\n p.vy = 0;\n p.life = 0;\n p.maxLife = 0;\n p.startSize = 1;\n p.endSize = 1;\n p.startAlpha = 1;\n p.endAlpha = 0;\n p.startR = 1;\n p.startG = 1;\n p.startB = 1;\n p.endR = 1;\n p.endG = 1;\n p.endB = 1;\n p.rotation = 0;\n p.angularVelocity = 0;\n p.active = false;\n}\n\n/**\n * ParticlePool — pre-allocated pool of particles.\n * Zero GC pressure during steady-state gameplay.\n */\nexport class ParticlePool {\n private _pool: Pool<Particle>;\n private _active: Particle[] = [];\n\n constructor(capacity = 256) {\n this._pool = new Pool(createParticle, resetParticle, { capacity });\n this._pool.prewarm(capacity);\n }\n\n /** Number of currently active particles. */\n get activeCount(): number {\n return this._active.length;\n }\n\n /**\n * Cumulative factory calls made by the internal pool's acquire()/tryAcquire().\n * prewarm() is excluded: factory calls there pre-fill the free list and are\n * not counted. Intended use: snapshot before and after a frame — a non-zero\n * delta in steady state means unexpected allocation.\n */\n get created(): number {\n return this._pool.created;\n }\n\n /** Read-only access to active particles. */\n get particles(): readonly Particle[] {\n return this._active;\n }\n\n /** Acquire a particle from the pool. */\n acquire(): Particle {\n const p = this._pool.acquire();\n p.active = true;\n this._active.push(p);\n return p;\n }\n\n /**\n * Acquire a particle only if the pool is below capacity.\n * Returns null at cap — no allocation occurs.\n */\n tryAcquire(): Particle | null {\n const p = this._pool.tryAcquire();\n if (!p) return null;\n p.active = true;\n this._active.push(p);\n return p;\n }\n\n /** Update all active particles. Releases dead particles. */\n update(dt: number, gravityX: number, gravityY: number): void {\n let i = 0;\n while (i < this._active.length) {\n const p = this._active[i]!;\n p.life -= dt;\n\n if (p.life <= 0) {\n // Dead — release and swap-remove\n p.active = false;\n this._pool.release(p);\n this._active[i] = this._active[this._active.length - 1]!;\n this._active.pop();\n continue;\n }\n\n // Apply gravity\n p.vx += gravityX * dt;\n p.vy += gravityY * dt;\n\n // Integrate position\n p.x += p.vx * dt;\n p.y += p.vy * dt;\n\n // Rotate\n p.rotation += p.angularVelocity * dt;\n\n i++;\n }\n }\n\n /** Get interpolated size for a particle (0 = start, 1 = end of life). */\n getSize(p: Particle): number {\n const t = 1 - p.life / p.maxLife;\n return p.startSize + (p.endSize - p.startSize) * t;\n }\n\n /** Get interpolated alpha for a particle. */\n getAlpha(p: Particle): number {\n const t = 1 - p.life / p.maxLife;\n return p.startAlpha + (p.endAlpha - p.startAlpha) * t;\n }\n\n /** Shared color buffer — avoids per-call allocation in the render hot path. */\n private _colorBuf: Color = { r: 1, g: 1, b: 1, a: 1 };\n\n /**\n * Get interpolated color for a particle.\n *\n * Returns a shared buffer — read the values immediately, do not retain the\n * reference. For a fresh object call `getColorInto()` with your own out param.\n */\n getColor(p: Particle): Color {\n return this.getColorInto(p, this._colorBuf);\n }\n\n /** Zero-alloc variant — writes the interpolated color into `out`. */\n getColorInto(p: Particle, out: Color): Color {\n const t = 1 - p.life / p.maxLife;\n out.r = p.startR + (p.endR - p.startR) * t;\n out.g = p.startG + (p.endG - p.startG) * t;\n out.b = p.startB + (p.endB - p.startB) * t;\n out.a = p.startAlpha + (p.endAlpha - p.startAlpha) * t;\n return out;\n }\n\n /** Release all active particles back to pool. */\n clear(): void {\n for (const p of this._active) {\n p.active = false;\n this._pool.release(p);\n }\n this._active.length = 0;\n }\n}\n","import type { Color } from '@flare-engine/math';\nimport type { EmitterShape, ShapeSample } from './emitter-shape.js';\nimport { createShapeSample, hasRadialNormal, sampleEmitterShape } from './emitter-shape.js';\nimport type { Particle } from './particle-pool.js';\nimport { ParticlePool } from './particle-pool.js';\n\n/** Configuration for a ParticleEmitter. */\nexport interface EmitterConfig {\n /** Emission rate in particles per second. */\n rate: number;\n /** Particle lifetime in seconds. */\n lifetime: number;\n /** Min lifetime (if set, actual lifetime is random between lifetime and lifetimeMax). */\n lifetimeMax?: number;\n /** Initial speed in pixels/second. */\n speed: number;\n /** Max speed (random range). */\n speedMax?: number;\n /** Emission direction in radians (0 = right). */\n direction: number;\n /** Spread angle in radians (half-angle of the emission cone). */\n spread: number;\n /** Gravity in pixels/second² (applied each frame). */\n gravityX?: number;\n gravityY?: number;\n /** Start size and end size. */\n startSize?: number;\n endSize?: number;\n /** Start alpha and end alpha. */\n startAlpha?: number;\n endAlpha?: number;\n /** Start color. */\n startColor?: Color;\n /** End color. */\n endColor?: Color;\n /** Angular velocity range (radians/second). */\n angularVelocity?: number;\n angularVelocityMax?: number;\n /** Maximum active particles (used as pool capacity default when no opts.maxParticles given). */\n maxParticles?: number;\n /**\n * Where particles spawn, relative to the emitter position. Defaults to\n * `{ kind: 'point' }`, which is the pre-existing behaviour — every particle\n * starts exactly at `x`/`y`.\n *\n * A shape samples position only; `direction` and `spread` still govern\n * velocity, unless the shape sets `radial: true` (see `EmitterShape`).\n */\n shape?: EmitterShape;\n}\n\n/** Construction options for ParticleEmitter. */\nexport interface EmitterOptions {\n /**\n * Hard cap on simultaneously active particles.\n * Overrides config.maxParticles when provided.\n * Defaults to config.maxParticles ?? 256.\n */\n maxParticles?: number;\n /**\n * Initial LCG seed for deterministic particle randomness.\n * Defaults to 1. Call reseed() at any time to restart the sequence.\n */\n seed?: number;\n}\n\n/**\n * ParticleEmitter — emits particles based on a configuration.\n * Uses a ParticlePool for zero-GC operation.\n *\n * Call `update(dt)` each frame. Read `pool.particles` for rendering.\n */\nexport class ParticleEmitter {\n readonly config: EmitterConfig;\n readonly pool: ParticlePool;\n\n /** Emitter world position. */\n x = 0;\n y = 0;\n\n /** Whether the emitter is actively emitting. */\n emitting = true;\n\n private _emitAccumulator = 0;\n private _seed = 0;\n /** Reused spawn-offset buffer — one per emitter, never per particle. */\n private readonly _sample: ShapeSample = createShapeSample();\n /** Bound once at construction; binding per emit would allocate a closure. */\n private readonly _rand = (): number => this._random();\n\n constructor(config: EmitterConfig, opts?: EmitterOptions) {\n this.config = config;\n const cap = opts?.maxParticles ?? config.maxParticles ?? 256;\n this.pool = new ParticlePool(cap);\n this._setSeed(opts?.seed ?? 1);\n }\n\n /** Reset the LCG seed — restarts the particle random sequence from this seed. */\n reseed(seed: number): void {\n this._setSeed(seed);\n }\n\n /** Normalize s into the valid Park-Miller domain [1, 2147483646]. */\n private _setSeed(s: number): void {\n let n = Number.isFinite(s) ? Math.floor(s) % 2147483647 : 0;\n if (n < 0) n += 2147483647;\n if (n === 0) n = 1;\n this._seed = n;\n }\n\n /** Update the emitter: emit new particles and update existing ones. */\n update(dt: number): void {\n if (this.emitting) {\n this._emitAccumulator += dt * this.config.rate;\n while (this._emitAccumulator >= 1) {\n this._emitAccumulator -= 1;\n if (!this._emitOne()) {\n this._emitAccumulator = 0;\n break;\n }\n }\n }\n\n this.pool.update(dt, this.config.gravityX ?? 0, this.config.gravityY ?? 0);\n }\n\n /** Emit a burst of particles. */\n burst(count: number): void {\n for (let i = 0; i < count; i++) {\n if (!this._emitOne()) break;\n }\n }\n\n /** Stop emitting and clear all particles. */\n reset(): void {\n this.emitting = false;\n this._emitAccumulator = 0;\n this.pool.clear();\n }\n\n /** Simple seeded pseudo-random for deterministic particles. */\n private _random(): number {\n this._seed = (this._seed * 16807) % 2147483647;\n return (this._seed - 1) / 2147483646;\n }\n\n /** Random float in [min, max]. */\n private _randomRange(min: number, max: number): number {\n return min + this._random() * (max - min);\n }\n\n /** Emit one particle. Returns false when the pool is at cap — caller should stop. */\n private _emitOne(): boolean {\n const p: Particle | null = this.pool.tryAcquire();\n if (!p) return false;\n\n const c = this.config;\n\n // Shape sampling comes first so it consumes its random draws before the\n // velocity draws below — that ordering is what makes a given seed replay\n // identically, and it is why `point` deliberately draws nothing at all\n // (an emitter with no shape produces the exact pre-shape sequence).\n const shape = c.shape;\n if (shape === undefined || shape.kind === 'point') {\n p.x = this.x;\n p.y = this.y;\n const angle = c.direction + this._randomRange(-c.spread, c.spread);\n const speed = c.speedMax ? this._randomRange(c.speed, c.speedMax) : c.speed;\n p.vx = Math.cos(angle) * speed;\n p.vy = Math.sin(angle) * speed;\n } else {\n const s = sampleEmitterShape(shape, this._rand, this._sample, c.direction);\n p.x = this.x + s.x;\n p.y = this.y + s.y;\n // A radial shape emits along its own outward normal, fanned by `spread`;\n // otherwise `direction` still rules, exactly as for a point emitter.\n const base = hasRadialNormal(shape) ? Math.atan2(s.ny, s.nx) : c.direction;\n const angle = base + this._randomRange(-c.spread, c.spread);\n const speed = c.speedMax ? this._randomRange(c.speed, c.speedMax) : c.speed;\n p.vx = Math.cos(angle) * speed;\n p.vy = Math.sin(angle) * speed;\n }\n\n const lifetime = c.lifetimeMax ? this._randomRange(c.lifetime, c.lifetimeMax) : c.lifetime;\n p.life = lifetime;\n p.maxLife = lifetime;\n\n p.startSize = c.startSize ?? 4;\n p.endSize = c.endSize ?? 0;\n\n p.startAlpha = c.startAlpha ?? 1;\n p.endAlpha = c.endAlpha ?? 0;\n\n if (c.startColor) {\n p.startR = c.startColor.r;\n p.startG = c.startColor.g;\n p.startB = c.startColor.b;\n }\n if (c.endColor) {\n p.endR = c.endColor.r;\n p.endG = c.endColor.g;\n p.endB = c.endColor.b;\n }\n\n if (c.angularVelocity !== undefined) {\n p.angularVelocity = c.angularVelocityMax\n ? this._randomRange(c.angularVelocity, c.angularVelocityMax)\n : c.angularVelocity;\n }\n\n return true;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyFA,SAAgB,cACd,MACA,OACA,OACA,OACA,SACM;CACN,MAAM,YAAY,KAAK;CACvB,MAAM,QAAQ,UAAU;CACxB,IAAI,UAAU,GAAG;CAEjB,MAAM,SAAS,SAAS;CACxB,MAAM,UAAU,SAAS,WAAW;CACpC,MAAM,UAAU,SAAS,WAAW;CACpC,MAAM,OAAO,MAAM;CACnB,MAAM,OAAO,MAAM;CACnB,MAAM,OAAO,MAAM;CACnB,MAAM,OAAO,MAAM;CAEnB,KAAK,IAAI,IAAI,GAAG,IAAI,OAAO,KAAK;EAC9B,MAAM,IAAI,UAAU;EACpB,MAAM,OAAO,KAAK,QAAQ,CAAC;EAE3B,IAAI,QAAQ,GAAG;EACf,MAAM,QAAQ,KAAK,SAAS,CAAC;EAC7B,IAAI,SAAS,GAAG;EAEhB,MAAM,QACJ,OACA,MACA,MACA,MACA,MACA,EAAE,GACF,EAAE,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AI,KAAK,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,aAC5C,KAAK,aAAa,CAAC,EAAE,QAAQ,EAAE,MAAM;GAC1D,MAAM,QAAQ,EAAE,WAAW,KAAK,aAAa,EAAE,OAAO,EAAE,QAAQ,IAAI,EAAE;GACtE,EAAE,KAAK,KAAK,IAAI,KAAK,IAAI;GACzB,EAAE,KAAK,KAAK,IAAI,KAAK,IAAI;EAC3B;EAEA,MAAM,WAAW,EAAE,cAAc,KAAK,aAAa,EAAE,UAAU,EAAE,WAAW,IAAI,EAAE;EAClF,EAAE,OAAO;EACT,EAAE,UAAU;EAEZ,EAAE,YAAY,EAAE,aAAa;EAC7B,EAAE,UAAU,EAAE,WAAW;EAEzB,EAAE,aAAa,EAAE,cAAc;EAC/B,EAAE,WAAW,EAAE,YAAY;EAE3B,IAAI,EAAE,YAAY;GAChB,EAAE,SAAS,EAAE,WAAW;GACxB,EAAE,SAAS,EAAE,WAAW;GACxB,EAAE,SAAS,EAAE,WAAW;EAC1B;EACA,IAAI,EAAE,UAAU;GACd,EAAE,OAAO,EAAE,SAAS;GACpB,EAAE,OAAO,EAAE,SAAS;GACpB,EAAE,OAAO,EAAE,SAAS;EACtB;EAEA,IAAI,EAAE,oBAAoB,KAAA,GACxB,EAAE,kBAAkB,EAAE,qBAClB,KAAK,aAAa,EAAE,iBAAiB,EAAE,kBAAkB,IACzD,EAAE;EAGR,OAAO;CACT;AACF"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/draw-particles.ts","../src/emitter-shape.ts","../src/particle-pool.ts","../src/particle-emitter.ts"],"sourcesContent":["import type { Color } from '@flare-engine/math';\nimport type { ParticlePool } from './particle-pool.js';\n\n/**\n * The frame rectangle within a texture. Structural on purpose: it matches\n * `SpriteFrame` from `@flare-engine/assets` without this package depending on\n * it — extra members like `name` are simply ignored.\n */\nexport interface ParticleFrameLike {\n x: number;\n y: number;\n w: number;\n h: number;\n}\n\n/**\n * The one method this adapter needs from a sprite batch.\n *\n * Declared structurally so `@flare-engine/particles` keeps its two dependencies\n * (`core`, `math`) and stays usable with no renderer at all. A real\n * `SpriteBatch` from `@flare-engine/render` satisfies it; so does a test double.\n * The parameter list mirrors `SpriteBatch.drawRaw`. `color` is a packed-ARGB\n * multiplier (`0xAARRGGBB`); a batch that declares fewer parameters simply does\n * not receive it.\n */\nexport interface ParticleBatchLike {\n drawRaw(\n image: unknown,\n srcX: number,\n srcY: number,\n srcW: number,\n srcH: number,\n dstX: number,\n dstY: number,\n dstW: number,\n dstH: number,\n effect?: unknown,\n anchorX?: number,\n anchorY?: number,\n alpha?: number,\n rotation?: number,\n stretch?: boolean,\n blend?: 'normal' | 'additive',\n flipX?: boolean,\n flipY?: boolean,\n color?: number,\n ): void;\n}\n\n/** Options for `drawParticles`. */\nexport interface DrawParticlesOptions {\n /**\n * A single effect applied to every particle in this call.\n *\n * Constant for the whole batch, and deliberately not per-particle: a batch\n * retains the effect *reference* until it flushes, so one mutable object\n * reused across the loop would give every particle the last particle's\n * values. Per-particle effects therefore cost one object per particle per\n * frame, which is exactly what this package exists to avoid — pass a constant\n * here, or build the loop yourself over `pool.particles`.\n *\n * Per-particle colour needs no effect: with `color: true` it is passed as a\n * number. Use this for what a colour multiplier cannot express, such as a\n * flash, an outline or grayscale.\n */\n effect?: unknown;\n /** Anchor within the destination rect. Defaults to the centre (0.5, 0.5). */\n anchorX?: number;\n anchorY?: number;\n /**\n * Pass each particle's interpolated `startR/G/B → endR/G/B` to `drawRaw` as its\n * trailing `color` argument. Defaults to `false`, which draws exactly as before\n * this option existed: no colour argument, and colour not applied.\n *\n * With `true` and `@flare-engine/render`'s `SpriteBatch`, each particle is\n * tinted by its colour, and coloured sprites share one run per image whatever\n * their colours and alphas — a fading, colour-shifting emitter becomes one draw\n * call instead of one per distinct alpha. The batch's renderer must support\n * per-sprite colours (both of the engine's backends do).\n */\n color?: boolean;\n}\n\n/** Interpolated colour of the particle being drawn; rewritten for each one. */\nconst colorScratch: Color = { r: 1, g: 1, b: 1, a: 1 };\n\n/** A 0-1 channel as a 0-255 byte, clamped so it cannot spill into a neighbouring channel. */\nfunction channelByte(value: number): number {\n return value <= 0 ? 0 : value >= 1 ? 255 : Math.round(value * 255);\n}\n\n/**\n * Queue every live particle into a caller-owned sprite batch.\n *\n * Zero-alloc: the loop reads the pool's active list directly, allocates nothing\n * per particle, and hands each one to `drawRaw` — so a steady-state emitter adds\n * no garbage. Size, alpha and (with `color: true`) colour come from the pool's\n * own interpolators, and rotation from the particle.\n *\n * The batch is passed in rather than owned so particles share one batch, and one\n * flush, with the rest of the scene — the `drawInto` idiom used elsewhere in the\n * engine.\n *\n * ```ts\n * // once, outside the loop\n * const frame = sheet.frames.find((f) => f.name === 'spark')!;\n * // each frame, before the batch flushes\n * drawParticles(emitter.pool, batch, atlas.image, frame);\n * ```\n *\n * **Colour is opt-in** (`{ color: true }`). Then each particle's interpolated\n * `startR/G/B → endR/G/B` goes to `drawRaw` as its trailing `color` argument: an\n * opaque packed ARGB that multiplies the frame's texels, so a white frame takes\n * the colour exactly and a particle left at the default white draws unchanged.\n * Alpha stays the separate `alpha` argument. With `@flare-engine/render`'s\n * `SpriteBatch`, sprites drawn with a colour share one run per image whatever\n * their colours and alphas, so a fading, colour-shifting emitter is one draw\n * call rather than one per distinct alpha. Without the option, colour is not\n * applied and each distinct alpha is its own run, as before.\n *\n * `image` and `frame` are separate parameters because they are separate\n * everywhere else in the engine — a frame is a rectangle within a texture and\n * does not carry the texture.\n */\nexport function drawParticles(\n pool: ParticlePool,\n batch: ParticleBatchLike,\n image: unknown,\n frame: ParticleFrameLike,\n options?: DrawParticlesOptions,\n): void {\n const particles = pool.particles;\n const count = particles.length;\n if (count === 0) return;\n\n const effect = options?.effect;\n const anchorX = options?.anchorX ?? 0.5;\n const anchorY = options?.anchorY ?? 0.5;\n const colored = options?.color === true;\n const srcX = frame.x;\n const srcY = frame.y;\n const srcW = frame.w;\n const srcH = frame.h;\n\n for (let i = 0; i < count; i++) {\n const p = particles[i]!;\n const size = pool.getSize(p);\n // A fully shrunk or fully faded particle costs a draw call for nothing.\n if (size <= 0) continue;\n const alpha = pool.getAlpha(p);\n if (alpha <= 0) continue;\n\n if (!colored) {\n batch.drawRaw(\n image,\n srcX,\n srcY,\n srcW,\n srcH,\n p.x,\n p.y,\n size,\n size,\n effect,\n anchorX,\n anchorY,\n alpha,\n p.rotation,\n );\n continue;\n }\n\n const c = pool.getColorInto(p, colorScratch);\n // `>>> 0`: the opaque alpha byte would otherwise make the packed value negative.\n const color =\n (0xff000000 | (channelByte(c.r) << 16) | (channelByte(c.g) << 8) | channelByte(c.b)) >>> 0;\n\n batch.drawRaw(\n image,\n srcX,\n srcY,\n srcW,\n srcH,\n p.x,\n p.y,\n size,\n size,\n effect,\n anchorX,\n anchorY,\n alpha,\n p.rotation,\n undefined,\n undefined,\n undefined,\n undefined,\n color,\n );\n }\n}\n","/**\n * Emitter shapes — where a particle starts, not where it goes.\n *\n * The split matters, because `EmitterConfig` already owns direction: `direction`\n * plus `spread` describe the velocity cone. A shape samples a **position offset**\n * from the emitter origin and reports the outward normal at that point, so the\n * two compose instead of competing. A shape with a meaningful outward direction\n * can opt velocity into it with `radial: true` — that is what turns a `ring`\n * from a circle of drifting particles into an explosion.\n */\n\n/** A filled disc of radius `radius`, sampled uniformly by area. */\nexport interface EmitterCircleShape {\n kind: 'circle';\n radius: number;\n /** Emit outward from the centre instead of along `config.direction`. */\n radial?: boolean;\n}\n\n/** An annulus at `radius`, `thickness` wide (default 0 — a bare ring). */\nexport interface EmitterRingShape {\n kind: 'ring';\n radius: number;\n thickness?: number;\n /** Emit outward from the centre instead of along `config.direction`. */\n radial?: boolean;\n}\n\n/** An axis-aligned rectangle centred on the emitter, sampled uniformly. */\nexport interface EmitterRectShape {\n kind: 'rect';\n width: number;\n height: number;\n}\n\n/**\n * A sector (wedge) of radius `radius` and half-angle `angle`, centred on\n * `config.direction` and sampled uniformly by area.\n *\n * This is a *position* wedge, which is what keeps it distinct from\n * `config.spread`: `spread` fans the velocity out of a single point, `cone`\n * spreads the spawn points themselves across the wedge.\n */\nexport interface EmitterConeShape {\n kind: 'cone';\n radius: number;\n angle: number;\n /** Emit outward from the apex instead of along `config.direction`. */\n radial?: boolean;\n}\n\n/** The emitter origin — every particle starts at the same point. The default. */\nexport interface EmitterPointShape {\n kind: 'point';\n}\n\n/** Where newly emitted particles are placed, relative to the emitter. */\nexport type EmitterShape =\n | EmitterPointShape\n | EmitterCircleShape\n | EmitterRingShape\n | EmitterRectShape\n | EmitterConeShape;\n\n/**\n * A sampled spawn point: an offset from the emitter origin, plus the outward\n * normal at that offset.\n *\n * `nx`/`ny` are unit length for the radial shapes. For `point` and `rect` there\n * is no meaningful outward direction and both are 0 — a caller honouring\n * `radial` must fall back to `config.direction`, which is what `hasRadialNormal`\n * is for.\n */\nexport interface ShapeSample {\n x: number;\n y: number;\n nx: number;\n ny: number;\n}\n\n/** A fresh, zeroed sample. Allocate once outside the loop and reuse. */\nexport function createShapeSample(): ShapeSample {\n return { x: 0, y: 0, nx: 0, ny: 0 };\n}\n\nconst TAU = Math.PI * 2;\n\n/**\n * Does this shape produce a usable outward normal?\n *\n * True only when the shape is radial *and* opted in. Lets a caller decide\n * between the normal and `config.direction` without inspecting the union.\n */\nexport function hasRadialNormal(shape: EmitterShape): boolean {\n switch (shape.kind) {\n case 'circle':\n case 'ring':\n case 'cone':\n return shape.radial === true;\n default:\n return false;\n }\n}\n\n/**\n * Sample a spawn offset into `out`. Zero-alloc: nothing is constructed here, so\n * this is safe to call once per emitted particle in a steady-state loop.\n *\n * `rand` must return a float in [0, 1). Pass the emitter's seeded generator to\n * keep emission deterministic — every shape draws a fixed number of values per\n * call (`point` 0, `ring`/`rect` 2, `circle`/`cone` 2), so a given seed replays\n * exactly.\n *\n * `direction` orients `cone` and is ignored by every other shape.\n */\nexport function sampleEmitterShape(\n shape: EmitterShape,\n rand: () => number,\n out: ShapeSample,\n direction = 0,\n): ShapeSample {\n switch (shape.kind) {\n case 'point': {\n out.x = 0;\n out.y = 0;\n out.nx = 0;\n out.ny = 0;\n return out;\n }\n case 'circle': {\n const angle = rand() * TAU;\n // sqrt keeps the distribution uniform by area; without it particles\n // bunch toward the centre.\n const r = Math.sqrt(rand()) * Math.max(0, shape.radius);\n const cos = Math.cos(angle);\n const sin = Math.sin(angle);\n out.x = cos * r;\n out.y = sin * r;\n out.nx = cos;\n out.ny = sin;\n return out;\n }\n case 'ring': {\n const angle = rand() * TAU;\n const thickness = Math.max(0, shape.thickness ?? 0);\n const base = Math.max(0, shape.radius);\n // Uniform across the band rather than by area: a ring is thin by\n // definition, and area-weighting a 1px band buys nothing.\n const r = base + (rand() - 0.5) * thickness;\n const cos = Math.cos(angle);\n const sin = Math.sin(angle);\n out.x = cos * r;\n out.y = sin * r;\n out.nx = cos;\n out.ny = sin;\n return out;\n }\n case 'rect': {\n const w = Math.max(0, shape.width);\n const h = Math.max(0, shape.height);\n out.x = (rand() - 0.5) * w;\n out.y = (rand() - 0.5) * h;\n out.nx = 0;\n out.ny = 0;\n return out;\n }\n case 'cone': {\n const half = Math.max(0, shape.angle);\n const angle = direction + (rand() * 2 - 1) * half;\n const r = Math.sqrt(rand()) * Math.max(0, shape.radius);\n const cos = Math.cos(angle);\n const sin = Math.sin(angle);\n out.x = cos * r;\n out.y = sin * r;\n out.nx = cos;\n out.ny = sin;\n return out;\n }\n }\n}\n","import { Pool } from '@flare-engine/core';\nimport type { Color } from '@flare-engine/math';\n\n/** A single particle. Managed by ParticlePool — not user-facing. */\nexport interface Particle {\n /** World position. */\n x: number;\n y: number;\n /** Velocity in pixels/second. */\n vx: number;\n vy: number;\n /** Time remaining in seconds. */\n life: number;\n /** Total lifetime for interpolation. */\n maxLife: number;\n /** Start/end size for interpolation. */\n startSize: number;\n endSize: number;\n /** Start/end alpha for interpolation. */\n startAlpha: number;\n endAlpha: number;\n /** Start/end color components for interpolation. */\n startR: number;\n startG: number;\n startB: number;\n endR: number;\n endG: number;\n endB: number;\n /** Rotation in radians. */\n rotation: number;\n /** Angular velocity in radians/second. */\n angularVelocity: number;\n /** Whether this particle is active. */\n active: boolean;\n}\n\n/** Create a default (inactive) particle. */\nfunction createParticle(): Particle {\n return {\n x: 0,\n y: 0,\n vx: 0,\n vy: 0,\n life: 0,\n maxLife: 0,\n startSize: 1,\n endSize: 1,\n startAlpha: 1,\n endAlpha: 0,\n startR: 1,\n startG: 1,\n startB: 1,\n endR: 1,\n endG: 1,\n endB: 1,\n rotation: 0,\n angularVelocity: 0,\n active: false,\n };\n}\n\n/** Reset a particle to default (inactive) state. */\nfunction resetParticle(p: Particle): void {\n p.x = 0;\n p.y = 0;\n p.vx = 0;\n p.vy = 0;\n p.life = 0;\n p.maxLife = 0;\n p.startSize = 1;\n p.endSize = 1;\n p.startAlpha = 1;\n p.endAlpha = 0;\n p.startR = 1;\n p.startG = 1;\n p.startB = 1;\n p.endR = 1;\n p.endG = 1;\n p.endB = 1;\n p.rotation = 0;\n p.angularVelocity = 0;\n p.active = false;\n}\n\n/**\n * ParticlePool — pre-allocated pool of particles.\n * Zero GC pressure during steady-state gameplay.\n */\nexport class ParticlePool {\n private _pool: Pool<Particle>;\n private _active: Particle[] = [];\n\n constructor(capacity = 256) {\n this._pool = new Pool(createParticle, resetParticle, { capacity });\n this._pool.prewarm(capacity);\n }\n\n /** Number of currently active particles. */\n get activeCount(): number {\n return this._active.length;\n }\n\n /**\n * Cumulative factory calls made by the internal pool's acquire()/tryAcquire().\n * prewarm() is excluded: factory calls there pre-fill the free list and are\n * not counted. Intended use: snapshot before and after a frame — a non-zero\n * delta in steady state means unexpected allocation.\n */\n get created(): number {\n return this._pool.created;\n }\n\n /** Read-only access to active particles. */\n get particles(): readonly Particle[] {\n return this._active;\n }\n\n /** Acquire a particle from the pool. */\n acquire(): Particle {\n const p = this._pool.acquire();\n p.active = true;\n this._active.push(p);\n return p;\n }\n\n /**\n * Acquire a particle only if the pool is below capacity.\n * Returns null at cap — no allocation occurs.\n */\n tryAcquire(): Particle | null {\n const p = this._pool.tryAcquire();\n if (!p) return null;\n p.active = true;\n this._active.push(p);\n return p;\n }\n\n /** Update all active particles. Releases dead particles. */\n update(dt: number, gravityX: number, gravityY: number): void {\n let i = 0;\n while (i < this._active.length) {\n const p = this._active[i]!;\n p.life -= dt;\n\n if (p.life <= 0) {\n // Dead — release and swap-remove\n p.active = false;\n this._pool.release(p);\n this._active[i] = this._active[this._active.length - 1]!;\n this._active.pop();\n continue;\n }\n\n // Apply gravity\n p.vx += gravityX * dt;\n p.vy += gravityY * dt;\n\n // Integrate position\n p.x += p.vx * dt;\n p.y += p.vy * dt;\n\n // Rotate\n p.rotation += p.angularVelocity * dt;\n\n i++;\n }\n }\n\n /** Get interpolated size for a particle (0 = start, 1 = end of life). */\n getSize(p: Particle): number {\n const t = 1 - p.life / p.maxLife;\n return p.startSize + (p.endSize - p.startSize) * t;\n }\n\n /** Get interpolated alpha for a particle. */\n getAlpha(p: Particle): number {\n const t = 1 - p.life / p.maxLife;\n return p.startAlpha + (p.endAlpha - p.startAlpha) * t;\n }\n\n /** Shared color buffer — avoids per-call allocation in the render hot path. */\n private _colorBuf: Color = { r: 1, g: 1, b: 1, a: 1 };\n\n /**\n * Get interpolated color for a particle.\n *\n * Returns a shared buffer — read the values immediately, do not retain the\n * reference. For a fresh object call `getColorInto()` with your own out param.\n */\n getColor(p: Particle): Color {\n return this.getColorInto(p, this._colorBuf);\n }\n\n /** Zero-alloc variant — writes the interpolated color into `out`. */\n getColorInto(p: Particle, out: Color): Color {\n const t = 1 - p.life / p.maxLife;\n out.r = p.startR + (p.endR - p.startR) * t;\n out.g = p.startG + (p.endG - p.startG) * t;\n out.b = p.startB + (p.endB - p.startB) * t;\n out.a = p.startAlpha + (p.endAlpha - p.startAlpha) * t;\n return out;\n }\n\n /** Release all active particles back to pool. */\n clear(): void {\n for (const p of this._active) {\n p.active = false;\n this._pool.release(p);\n }\n this._active.length = 0;\n }\n}\n","import type { Color } from '@flare-engine/math';\nimport type { EmitterShape, ShapeSample } from './emitter-shape.js';\nimport { createShapeSample, hasRadialNormal, sampleEmitterShape } from './emitter-shape.js';\nimport type { Particle } from './particle-pool.js';\nimport { ParticlePool } from './particle-pool.js';\n\n/** Configuration for a ParticleEmitter. */\nexport interface EmitterConfig {\n /** Emission rate in particles per second. */\n rate: number;\n /** Particle lifetime in seconds. */\n lifetime: number;\n /** Min lifetime (if set, actual lifetime is random between lifetime and lifetimeMax). */\n lifetimeMax?: number;\n /** Initial speed in pixels/second. */\n speed: number;\n /** Max speed (random range). */\n speedMax?: number;\n /** Emission direction in radians (0 = right). */\n direction: number;\n /** Spread angle in radians (half-angle of the emission cone). */\n spread: number;\n /** Gravity in pixels/second² (applied each frame). */\n gravityX?: number;\n gravityY?: number;\n /** Start size and end size. */\n startSize?: number;\n endSize?: number;\n /** Start alpha and end alpha. */\n startAlpha?: number;\n endAlpha?: number;\n /** Start color. */\n startColor?: Color;\n /** End color. */\n endColor?: Color;\n /** Angular velocity range (radians/second). */\n angularVelocity?: number;\n angularVelocityMax?: number;\n /** Maximum active particles (used as pool capacity default when no opts.maxParticles given). */\n maxParticles?: number;\n /**\n * Where particles spawn, relative to the emitter position. Defaults to\n * `{ kind: 'point' }`, which is the pre-existing behaviour — every particle\n * starts exactly at `x`/`y`.\n *\n * A shape samples position only; `direction` and `spread` still govern\n * velocity, unless the shape sets `radial: true` (see `EmitterShape`).\n */\n shape?: EmitterShape;\n}\n\n/** Construction options for ParticleEmitter. */\nexport interface EmitterOptions {\n /**\n * Hard cap on simultaneously active particles.\n * Overrides config.maxParticles when provided.\n * Defaults to config.maxParticles ?? 256.\n */\n maxParticles?: number;\n /**\n * Initial LCG seed for deterministic particle randomness.\n * Defaults to 1. Call reseed() at any time to restart the sequence.\n */\n seed?: number;\n}\n\n/**\n * ParticleEmitter — emits particles based on a configuration.\n * Uses a ParticlePool for zero-GC operation.\n *\n * Call `update(dt)` each frame. Read `pool.particles` for rendering.\n */\nexport class ParticleEmitter {\n readonly config: EmitterConfig;\n readonly pool: ParticlePool;\n\n /** Emitter world position. */\n x = 0;\n y = 0;\n\n /** Whether the emitter is actively emitting. */\n emitting = true;\n\n private _emitAccumulator = 0;\n private _seed = 0;\n /** Reused spawn-offset buffer — one per emitter, never per particle. */\n private readonly _sample: ShapeSample = createShapeSample();\n /** Bound once at construction; binding per emit would allocate a closure. */\n private readonly _rand = (): number => this._random();\n\n constructor(config: EmitterConfig, opts?: EmitterOptions) {\n this.config = config;\n const cap = opts?.maxParticles ?? config.maxParticles ?? 256;\n this.pool = new ParticlePool(cap);\n this._setSeed(opts?.seed ?? 1);\n }\n\n /** Reset the LCG seed — restarts the particle random sequence from this seed. */\n reseed(seed: number): void {\n this._setSeed(seed);\n }\n\n /** Normalize s into the valid Park-Miller domain [1, 2147483646]. */\n private _setSeed(s: number): void {\n let n = Number.isFinite(s) ? Math.floor(s) % 2147483647 : 0;\n if (n < 0) n += 2147483647;\n if (n === 0) n = 1;\n this._seed = n;\n }\n\n /** Update the emitter: emit new particles and update existing ones. */\n update(dt: number): void {\n if (this.emitting) {\n this._emitAccumulator += dt * this.config.rate;\n while (this._emitAccumulator >= 1) {\n this._emitAccumulator -= 1;\n if (!this._emitOne()) {\n this._emitAccumulator = 0;\n break;\n }\n }\n }\n\n this.pool.update(dt, this.config.gravityX ?? 0, this.config.gravityY ?? 0);\n }\n\n /** Emit a burst of particles. */\n burst(count: number): void {\n for (let i = 0; i < count; i++) {\n if (!this._emitOne()) break;\n }\n }\n\n /** Stop emitting and clear all particles. */\n reset(): void {\n this.emitting = false;\n this._emitAccumulator = 0;\n this.pool.clear();\n }\n\n /** Simple seeded pseudo-random for deterministic particles. */\n private _random(): number {\n this._seed = (this._seed * 16807) % 2147483647;\n return (this._seed - 1) / 2147483646;\n }\n\n /** Random float in [min, max]. */\n private _randomRange(min: number, max: number): number {\n return min + this._random() * (max - min);\n }\n\n /** Emit one particle. Returns false when the pool is at cap — caller should stop. */\n private _emitOne(): boolean {\n const p: Particle | null = this.pool.tryAcquire();\n if (!p) return false;\n\n const c = this.config;\n\n // Shape sampling comes first so it consumes its random draws before the\n // velocity draws below — that ordering is what makes a given seed replay\n // identically, and it is why `point` deliberately draws nothing at all\n // (an emitter with no shape produces the exact pre-shape sequence).\n const shape = c.shape;\n if (shape === undefined || shape.kind === 'point') {\n p.x = this.x;\n p.y = this.y;\n const angle = c.direction + this._randomRange(-c.spread, c.spread);\n const speed = c.speedMax ? this._randomRange(c.speed, c.speedMax) : c.speed;\n p.vx = Math.cos(angle) * speed;\n p.vy = Math.sin(angle) * speed;\n } else {\n const s = sampleEmitterShape(shape, this._rand, this._sample, c.direction);\n p.x = this.x + s.x;\n p.y = this.y + s.y;\n // A radial shape emits along its own outward normal, fanned by `spread`;\n // otherwise `direction` still rules, exactly as for a point emitter.\n const base = hasRadialNormal(shape) ? Math.atan2(s.ny, s.nx) : c.direction;\n const angle = base + this._randomRange(-c.spread, c.spread);\n const speed = c.speedMax ? this._randomRange(c.speed, c.speedMax) : c.speed;\n p.vx = Math.cos(angle) * speed;\n p.vy = Math.sin(angle) * speed;\n }\n\n const lifetime = c.lifetimeMax ? this._randomRange(c.lifetime, c.lifetimeMax) : c.lifetime;\n p.life = lifetime;\n p.maxLife = lifetime;\n\n p.startSize = c.startSize ?? 4;\n p.endSize = c.endSize ?? 0;\n\n p.startAlpha = c.startAlpha ?? 1;\n p.endAlpha = c.endAlpha ?? 0;\n\n if (c.startColor) {\n p.startR = c.startColor.r;\n p.startG = c.startColor.g;\n p.startB = c.startColor.b;\n }\n if (c.endColor) {\n p.endR = c.endColor.r;\n p.endG = c.endColor.g;\n p.endB = c.endColor.b;\n }\n\n if (c.angularVelocity !== undefined) {\n p.angularVelocity = c.angularVelocityMax\n ? this._randomRange(c.angularVelocity, c.angularVelocityMax)\n : c.angularVelocity;\n }\n\n return true;\n }\n}\n"],"mappings":";;;AAoFA,MAAM,eAAsB;CAAE,GAAG;CAAG,GAAG;CAAG,GAAG;CAAG,GA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E,OAAO,EAAE,QAAQ,IAAI,EAAE;GACtE,EAAE,KAAK,KAAK,IAAI,KAAK,IAAI;GACzB,EAAE,KAAK,KAAK,IAAI,KAAK,IAAI;EAC3B;EAEA,MAAM,WAAW,EAAE,cAAc,KAAK,aAAa,EAAE,UAAU,EAAE,WAAW,IAAI,EAAE;EAClF,EAAE,OAAO;EACT,EAAE,UAAU;EAEZ,EAAE,YAAY,EAAE,aAAa;EAC7B,EAAE,UAAU,EAAE,WAAW;EAEzB,EAAE,aAAa,EAAE,cAAc;EAC/B,EAAE,WAAW,EAAE,YAAY;EAE3B,IAAI,EAAE,YAAY;GAChB,EAAE,SAAS,EAAE,WAAW;GACxB,EAAE,SAAS,EAAE,WAAW;GACxB,EAAE,SAAS,EAAE,WAAW;EAC1B;EACA,IAAI,EAAE,UAAU;GACd,EAAE,OAAO,EAAE,SAAS;GACpB,EAAE,OAAO,EAAE,SAAS;GACpB,EAAE,OAAO,EAAE,SAAS;EACtB;EAEA,IAAI,EAAE,oBAAoB,KAAA,GACxB,EAAE,kBAAkB,EAAE,qBAClB,KAAK,aAAa,EAAE,iBAAiB,EAAE,kBAAkB,IACzD,EAAE;EAGR,OAAO;CACT;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flare-engine/particles",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.14",
|
|
4
4
|
"description": "Particle emitter, pool, immediate-mode Skia rendering",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"flare-engine",
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
"typecheck": "tsc --noEmit"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@flare-engine/core": "^0.4.
|
|
50
|
-
"@flare-engine/math": "^0.4.
|
|
49
|
+
"@flare-engine/core": "^0.4.14",
|
|
50
|
+
"@flare-engine/math": "^0.4.14"
|
|
51
51
|
},
|
|
52
52
|
"engines": {
|
|
53
53
|
"node": ">=20"
|