fireMultipleRockets method

void fireMultipleRockets({
  1. List<Color>? fireworksColors,
  2. int minRockets = 6,
  3. int maxRockets = 14,
  4. Duration launchWindow = const Duration(seconds: 2),
})

Implementation

void fireMultipleRockets({
  List<Color>? fireworksColors,
  int minRockets = 6,
  int maxRockets = 14,
  Duration launchWindow = const Duration(seconds: 2),
}) {
  if (_disposed) return;

  final rocketColors = fireworksColors ?? colors;
  final random = Random();
  final rocketsToFire =
      random.nextInt(maxRockets - minRockets + 1) + minRockets;
  final totalDuration = launchWindow.inMilliseconds / 1000.0;
  final interval = totalDuration / rocketsToFire;

  for (var i = 0; i < rocketsToFire; i++) {
    final delayMilliseconds = (interval * 1000 * i).toInt();

    final timer = Timer(Duration(milliseconds: delayMilliseconds), () {
      if (_disposed) return;
      flameGame.launchRocket(
        colors: rocketColors,
        rocketColor: rocketColor,
        minExplosionDuration: minExplosionDuration,
        maxExplosionDuration: maxExplosionDuration,
        minParticleCount: minParticleCount,
        maxParticleCount: maxParticleCount,
        fadeOutDuration: fadeOutDuration,
      );
    });
    _timers.add(timer);
  }
}