Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Possibility to create RenderableTiledMap from TiledMap #1534

Merged
merged 3 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/flame_tiled/lib/src/flame_tsx_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class FlameTsxProvider implements TsxProvider {
}

@override
Parser? getChachedSource() {
Parser? getCachedSource() {
if (data.isEmpty) {
return null;
}
Expand Down
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
4 changes: 2 additions & 2 deletions packages/flame_tiled/test/tiled_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ void main() {
test('correctly loads external tileset', () async {
final tsxProvider = await FlameTsxProvider.parse('external_tileset_1.tsx');

expect(tsxProvider.getChachedSource() != null, true);
expect(tsxProvider.getCachedSource() != null, true);
expect(
tsxProvider
.getChachedSource()!
.getCachedSource()!
.getSingleChild('tileset')
.getString('name') ==
'level1',
Expand Down