FastLED 3.9.15
Loading...
Searching...
No Matches

◆ spawnRandomParticle()

void fl::Particles1d::spawnRandomParticle ( )

Spawn a particle with random position, velocity, color, and lifetime.

Spawning behavior:

  • First tries to find an inactive particle slot
  • If all slots are full, replaces the oldest particle by birthTime
Note
Call this from your loop() based on desired spawn rate (e.g., every 2 seconds)
Spawn control is intentionally external for flexibility (music-reactive, triggers, etc.)

Definition at line 42 of file particles.cpp.hpp.

42 {
43 // First, try to find an inactive particle
44 for (size_t i = 0; i < mParticles.size(); i++) {
45 if (!mParticles[i].active) {
46 mParticles[i].spawn(mNumLeds);
47 return;
48 }
49 }
50
51 // All slots full - find and reuse the oldest particle
52 size_t oldestIdx = 0;
53 u32 oldestTime = mParticles[0].birthTime;
54 for (size_t i = 1; i < mParticles.size(); i++) {
55 if (mParticles[i].birthTime < oldestTime) {
56 oldestTime = mParticles[i].birthTime;
57 oldestIdx = i;
58 }
59 }
60 mParticles[oldestIdx].spawn(mNumLeds);
61}
u16 mNumLeds
Definition fx.h:53
fl::vector< Particle > mParticles
Particle pool (oldest particle reused when full)
Definition particles.h:137

References fl::Fx::mNumLeds, and mParticles.

Referenced by ~Particles1d().

+ Here is the caller graph for this function: