forked from flame-engine/tiled.dart
/
wang_set.dart
56 lines (53 loc) · 1.58 KB
/
wang_set.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
49
50
51
52
53
54
55
56
part of tiled;
/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
///
/// <wangset>
///
/// Defines a list of corner colors and a list of edge colors, and any number
/// of Wang tiles using these colors.
///
/// name: The name of the Wang set.
/// tile: The tile ID of the tile representing this Wang set.
/// Can contain at most one: <properties>
///
/// Can contain up to 255: <wangcolor> (since Tiled 1.5)
///
/// Can contain any number: <wangtile>
class WangSet {
String name;
int tile;
List<WangColor> cornerColors;
List<WangColor> edgeColors;
List<WangTile> wangTiles;
Map<String, Property> properties;
WangSet({
required this.name,
required this.tile,
this.cornerColors = const [],
this.edgeColors = const [],
this.wangTiles = const [],
this.properties = const {},
});
factory WangSet.parse(Parser parser) {
final colors = parser.formatSpecificParsing(
(json) => [
json.getChildrenAs('cornercolors', WangColor.parse),
json.getChildrenAs('edgecolors', WangColor.parse),
],
(xml) {
final isCorner = xml.getString('type') == 'corner';
final colors = xml.getChildrenAs('wangcolor', WangColor.parse);
return isCorner ? [colors, <WangColor>[]] : [<WangColor>[], colors];
},
);
return WangSet(
name: parser.getString('name'),
tile: parser.getInt('tile'),
cornerColors: colors[0],
edgeColors: colors[1],
wangTiles: parser.getChildrenAs('wangtiles', WangTile.parse),
properties: parser.getProperties(),
);
}
}