Interlude.fromJson constructor

Interlude.fromJson(
  1. Map<String, dynamic> json
)

Implementation

factory Interlude.fromJson(Map<String, dynamic> json) {
  Map<String, Character> characters = {};
  Map<String, String> backgrounds = {};
  Map<String, String> audios = {};
  Map<String, SceneInterface> scenes = {};
  Map<String, dynamic> variables = {};

  // read variables
  if (json.containsKey('variables')) {
    variables = Map<String, dynamic>.from(json['variables']);
  }

  //read character define from json
  const characterDefineKey = [
    "characters",
    "char",
    "char_define",
    "c",
    "char_list"
  ];

  for (var key in characterDefineKey) {
    if (json.containsKey(key)) {
      for (var char in json[key]) {
        if (char['type'] == 'narrator') {
          characters[char['id']] = Narrator(id: char['id']);
        } else if (char['type'] == 'default') {
          characters[char['id']] = DefaultCharacter.fromJson(char);
        } else if (char['type'] == 'spine') {
          characters[char['id']] = SpineCharacter.fromJson(char);
        }
      }
    }
  }

  //read background define from json
  const backgroundDefineKey = [
    "backgrounds",
    "bg",
    "bg_define",
    "b",
    "bg_list"
  ];

  for (var key in backgroundDefineKey) {
    if (json.containsKey(key)) {
      for (var bg in json[key]) {
        // if path is remote url
        if (bg['path'].startsWith('http')) {
          backgrounds[bg['id']] = bg['path'];
        } else {
          backgrounds[bg['id']] = bg['path'];
        }
      }
    }
  }

  // read audio define from json
  const audioDefineKey = [
    "audios",
    "audio",
    "audio_define",
    "a",
    "audio_list"
  ];

  for (var key in audioDefineKey) {
    if (json.containsKey(key)) {
      for (var audio in json[key]) {
        audios[audio['id']] = audio['path'];
      }
    }
  }

  // read scenes from json
  const scenesDefine = [
    "scenes",
    "s",
    "scene",
    "scene_define",
  ];

  for (var key in scenesDefine) {
    if (json.containsKey(key)) {
      for (var scene in json[key]) {
        if (scene['behaviors'] == null || scene['text'] != null) {
          final List<String> text = scene['text'].cast<String>();
          final cutscene = CutScene(
              image: backgrounds[scene['image']]!,
              text: text,
              next: scene["next"],
              id: scene['id'],
              bgm: scene['bgm'] != null ? audios[scene['bgm']] : null);
          scenes[scene['id']] = cutscene;
        } else {
          List<Behavior> behaviors = [];
          for (var behavior in scene['behaviors']) {
            Branch? branch;
            if (behavior['branch'] != null) {
              var b = behavior['branch'];
              branch = Branch(
                id: b['id'] ??
                    'branch_${DateTime.now().millisecondsSinceEpoch}',
                description: b['description'] ?? '',
                targetSceneId: b['target'],
                valueEffects: b['effects'] != null
                    ? Map<String, String>.from(b['effects'])
                    : null,
                condition: b['condition'],
              );
            }

            behaviors.add(Behavior(
              type: behavior['type'] == 'speak'
                  ? BehaviorType.speak
                  : BehaviorType.listen,
              character: characters[behavior['character']]!,
              dialog: behavior['dialog'],
              position: behavior['position'] == null
                  ? null
                  : Position.values.byName(behavior['position']),
              state: behavior['state'],
              branch: branch,
              sound: behavior['sound'] != null
                  ? audios[behavior['sound']]
                  : null,
            ));
          }
          scenes[scene['id']] = Scene(
            id: scene['id'],
            image: backgrounds[scene['background']]!,
            behaviors: behaviors,
            next: scene['next'],
            bgm: scene['bgm'] != null ? audios[scene['bgm']] : null,
          );
        }
      }
    }
  }

  return Interlude(
    characters: characters,
    backgrounds: backgrounds,
    audios: audios,
    scenes: scenes,
    variables: variables,
  );
}