tickRandomSounds method

void tickRandomSounds(
  1. int timeDelta
)

Tick all random sounds.

Implementation

void tickRandomSounds(final int timeDelta) {
  for (final sound in randomSounds) {
    final playNext = getRandomSoundNextPlay(sound);
    if (playNext.runAfter <= 0) {
      final playback = randomSoundPlaybacks[sound];
      SoundChannel? c;
      if (playback != null) {
        playback.sound.destroy();
        c = playback.channel;
      } else {
        c = null;
      }
      final minX = sound.minCoordinates.x;
      final maxX = sound.maxCoordinates.x;
      final minY = sound.minCoordinates.y;
      final maxY = sound.maxCoordinates.y;
      final xDifference = maxX - minX;
      final yDifference = maxY - minY;
      final x = minX + (xDifference * random.nextDouble());
      final y = minY + (yDifference * random.nextDouble());
      final position = SoundPosition3d(x: x, y: y);
      if (c == null) {
        c = game.createSoundChannel(position: position);
      } else {
        c.position = position;
      }
      randomSoundPlaybacks[sound] = SoundPlayback(
        c,
        c.playSound(
          assetReference: sound.sound.copy(
            sound.minGain == sound.maxGain
                ? sound.minGain
                : sound.minGain +
                    ((sound.maxGain - sound.minGain) * random.nextDouble()),
          ),
          keepAlive: true,
        ),
      );
      scheduleRandomSound(sound);
    } else {
      playNext.runAfter -= timeDelta;
    }
  }
}