profectus-docs/docs/guide/recipes/particles.md

78 lines
2.9 KiB
Markdown
Raw Normal View History

2023-04-10 02:40:12 +00:00
# Particles
2023-04-10 13:02:51 +00:00
This is a more comprehensive example based on the Kronos example used in the [nodes](../advanced-concepts/nodes) docs. You will design a particle effect, make it appear on another feature, and ensure it adapts to the game state.
2023-04-10 02:40:12 +00:00
2023-04-10 13:02:51 +00:00
First, design the particle effect. Profectus uses [pixi-particles](https://github.com/pixijs/particle-emitter), and you can use the online particle effect editor [here](http://pixijs.github.io/pixi-particles-editor/). However, the editor returns an older format of the particle effect emitter config, so you'll need to convert it like this:
2023-04-10 02:40:12 +00:00
```ts
import myParticleEffect from "myParticleEffect.json";
import { upgradeConfig } from "@pixi/particle-emitter"
const particleEffect = upgradeConfig(myParticleEffect);
```
2023-04-10 13:02:51 +00:00
Next, create the particles feature and render it. You'll also want to track the bounding rect of the particle effects. Consider the following step:
2023-04-10 02:40:12 +00:00
```ts
const particles = createParticles(() => ({
fullscreen: false,
zIndex: -1,
boundingRect: ref<null | DOMRect>(null),
onContainerResized(boundingRect) {
this.boundingRect.value = boundingRect;
}
}));
```
2023-04-10 13:02:51 +00:00
This code adds a bounding rect for the particles and updates it when the container is created or resizes. Now, create the emitter. Pull in the Kronos example, which displays a particle effect when `actualGain > 0`.
2023-04-10 02:40:12 +00:00
```ts
const particlesEmitter = ref(particles.addEmitter(element.particlesConfig));
const updateParticleEffect = async ([shouldEmit, rect, boundingRect]: [
boolean,
DOMRect | undefined,
DOMRect | undefined
]) => {
const emitter = await particlesEmitter.value;
emitter.emit = isGaining && rect != undefined && boundingRect != undefined;
if (emitter.emit && !emitter.destroyed) {
emitter.cleanup();
emitter.updateOwnerPos(
rect.x + rect.width / 2 - boundingRect.x,
rect.y + rect.height / 2 - boundingRect.y
);
emitter.resetPositionTracking();
}
};
watch(
[
() => Decimal.gt(actualGain.value, 0),
() => layer.nodes.value[name]?.rect,
particles.boundingRect
],
updateParticleEffect
)
```
2023-04-10 13:02:51 +00:00
This code watches for whether the node exists and the `boundingRect` exists, which are required to display the effect. You can additionally watch any other values that would have an impact on the particle effect, like whether or not it should be emitting.
2023-04-10 02:40:12 +00:00
2023-04-10 13:02:51 +00:00
If you're using hot reloading, you might need to reload the particle effect. Here's an example from Kronos:
2023-04-10 02:40:12 +00:00
```ts
const refreshParticleEffect = () => {
particlesEmitter.value
.then(e => e.destroy())
.then(
() =>
(particlesEmitter.value = particles.addEmitter(element.particlesConfig))
)
.then(() =>
updateParticleEffect([
Decimal.gt(actualGain.value, 0),
layer.nodes.value[name]?.rect,
particles.boundingRect.value
])
);
};
```