fromString static method

Future<TiledMap> fromString(
  1. String contents,
  2. Future<TsxProvider> tsxProviderFunction(
    1. String key
    )
)

Takes a string contents and converts it to a TiledMap with the help of the TsxProviders returned from the tsxProviderFunction. The tsxProviderFunction is most commonly your static TsxProvider.parse implementation.

Implementation

static Future<TiledMap> fromString(
  String contents,
  Future<TsxProvider> Function(String key) tsxProviderFunction,
) async {
  final tsxSourcePaths = XmlDocument.parse(contents)
      .rootElement
      .children
      .whereType<XmlElement>()
      .where((element) => element.name.local == 'tileset')
      .map((e) => e.getAttribute('source'));

  final tsxProviders = await Future.wait(
    tsxSourcePaths
        .where((key) => key != null)
        .map((key) async => tsxProviderFunction(key!)),
  );

  return TileMapParser.parseTmx(
    contents,
    tsxList: tsxProviders.isEmpty ? null : tsxProviders,
  );
}