-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtiledjsonreader.dart
48 lines (40 loc) · 1.35 KB
/
tiledjsonreader.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
library tiledjsonreader;
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:tiledjsonreader/map/tiled_map.dart';
import 'map/tile_set_detail.dart';
export 'map/layer/tile_layer.dart';
class TiledJsonReader {
final String pathFile;
String? _basePathFile;
String? _fileName;
TiledMap? _map;
TiledJsonReader(this.pathFile) {
_fileName = pathFile.split('/').last;
if (!(_fileName?.contains('.json') == true ||
_fileName?.contains('.tmj') == true)) {
throw Exception('only supports json files');
}
_basePathFile = pathFile.replaceAll(_fileName ?? '', '');
}
Future<TiledMap> read() async {
String data = await rootBundle.loadString(pathFile);
Map<String, dynamic> _result = jsonDecode(data);
_map = TiledMap.fromJson(_result);
if (_map?.tileSets != null) {
await Future.forEach(_map!.tileSets!, (TileSetDetail tileSet) async {
if (tileSet.source != null) {
if (!(tileSet.source!.contains('.json') ||
tileSet.source!.contains('.tsj'))) {
throw Exception('Invalid TileSet source: only supports json files');
}
String data = await rootBundle.loadString(
'$_basePathFile${tileSet.source}',
);
tileSet.updateFromMap(jsonDecode(data));
}
});
}
return Future.value(_map);
}
}