explode method

ParticleSystemComponent explode()

Triggers the explosion of the rocket and returns the particle system.

Implementation

ParticleSystemComponent explode() {
  final color = colors[random.nextInt(colors.length)];
  explosionDuration = minExplosionDuration +
      random.nextDouble() * (maxExplosionDuration - minExplosionDuration);

  // Calculate fadeOutDuration as 20% of explosionDuration
  adjustedFadeOutDuration = explosionDuration! * 0.2;

  particleCount = random.nextInt(maxParticleCount - minParticleCount + 1) +
      minParticleCount;

  final particleComponent = ParticleSystemComponent(
    particle: Particle.generate(
      lifespan: explosionDuration!,
      count: particleCount!,
      generator: (i) {
        final theta = random.nextDouble() * 2 * pi;
        final speed = random.nextDouble() * 150 + 50;

        return FadingMovingParticle(
          lifespan: explosionDuration!,
          position: position.clone(),
          speed: Vector2(cos(theta) * speed, sin(theta) * speed),
          acceleration: Vector2(0, 100),
          radius: 3,
          baseColor: color,
          fadeOutDuration: adjustedFadeOutDuration!,
        );
      },
    ),
  );

  gameRef.add(particleComponent);
  return particleComponent;
}