run method

Future<void> run({
  1. EventCallback<SoundEvent>? onSound,
})

Run this world.

If sdl is not null, then it should call Sdl.init itself.

Implementation

Future<void> run({final EventCallback<SoundEvent>? onSound}) async {
  Synthizer? synthizer;
  Context? context;
  if (triggerMapFile.existsSync() == true) {
    final data = triggerMapFile.readAsStringSync();
    final json = jsonDecode(data) as JsonType;
    final triggerMap = TriggerMap.fromJson(json);
    for (final trigger in triggerMap.triggers) {
      game.triggerMap.triggers.removeWhere(
        (final element) => element.name == trigger.name,
      );
    }
    game.triggerMap.triggers.addAll(triggerMap.triggers);
  }
  final preferences = playerPreferences;
  if (onSound == null) {
    final soundOptions = world.soundOptions;
    String? libsndfilePath;
    if (Platform.isLinux) {
      libsndfilePath = soundOptions.libsndfilePathLinux;
    } else if (Platform.isWindows) {
      libsndfilePath = soundOptions.libsndfilePathWindows;
    } else if (Platform.isMacOS) {
      libsndfilePath = soundOptions.libsndfilePathMac;
    }
    if (libsndfilePath == null ||
        File(libsndfilePath).existsSync() == false) {
      libsndfilePath = null;
    }
    synthizer = Synthizer()
      ..initialize(
        logLevel: soundOptions.synthizerLogLevel,
        loggingBackend: soundOptions.synthizerLoggingBackend,
        libsndfilePath: libsndfilePath,
      );
    context = synthizer.createContext();
    final soundManager = SoundManager(
      game: game,
      context: context,
      bufferCache: BufferCache(
        synthizer: synthizer,
        maxSize: pow(1024, 3).floor(),
        random: game.random,
      ),
    );
    game.sounds.listen(soundManager.handleEvent);
  } else {
    game.sounds.listen(onSound);
  }
  sdl.init();
  try {
    await game.run(
      sdl,
      framesPerSecond: world.globalOptions.framesPerSecond,
      onStart: () {
        game
          ..interfaceSounds.gain = preferences.interfaceSoundsGain
          ..musicSounds.gain = preferences.musicGain
          ..ambianceSounds.gain = preferences.musicGain
          ..setDefaultPannerStrategy(preferences.pannerStrategy)
          ..pushLevel(
            getMainMenu(),
          );
      },
    );
    final json = game.triggerMap.toJson();
    final data = indentedJsonEncoder.convert(json);
    triggerMapFile.writeAsStringSync(data);
  } on Exception {
    rethrow;
  } finally {
    context?.destroy();
    synthizer?.shutdown();
    sdl.quit();
    for (final haptic in hapticDevices) {
      haptic.close();
    }
  }
}