Skip to content

Commit

Permalink
fix: Possibility to create RenderableTiledMap from TiledMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Hwanseok committed Apr 12, 2022
1 parent cae8c0c commit 7c190fc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
34 changes: 12 additions & 22 deletions packages/flame_tiled/lib/src/renderable_tile_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:flame/extensions.dart';
import 'package:flame/flame.dart';
import 'package:flame/sprite.dart';
import 'package:tiled/tiled.dart';
import 'package:xml/xml.dart';

import 'flame_tsx_provider.dart';
import 'simple_flips.dart';
Expand Down Expand Up @@ -103,7 +102,18 @@ class RenderableTiledMap {
String contents,
Vector2 destTileSize,
) async {
final map = await _loadMap(contents);
final map = await TiledMap.fromString(
contents,
FlameTsxProvider.parse,
);
return fromTiledMap(map, destTileSize);
}

/// Parses a [TiledMap] returning a [RenderableTiledMap].
static Future<RenderableTiledMap> fromTiledMap(
TiledMap map,
Vector2 destTileSize,
) async {
final batchesByLayer = await Future.wait(
_renderableTileLayers(map).map((e) => _loadImages(map)),
);
Expand All @@ -119,26 +129,6 @@ class RenderableTiledMap {
return map.layers.where((layer) => layer.visible).whereType<TileLayer>();
}

static Future<TiledMap> _loadMap(String contents) 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 => FlameTsxProvider.parse(key!)),
);

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

static Future<Map<String, SpriteBatch>> _loadImages(TiledMap map) async {
final result = <String, SpriteBatch>{};

Expand Down
2 changes: 1 addition & 1 deletion packages/flame_tiled/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:

dependencies:
flame: ^1.1.0
tiled: ^0.8.0
tiled: ^0.8.1
xml: ^5.3.0
meta: ^1.7.0
collection: ^1.15.0
Expand Down

0 comments on commit 7c190fc

Please sign in to comment.