2
|
1 module xmlmap;
|
|
2
|
|
3 import tileset;
|
|
4 import tilemap;
|
|
5
|
|
6 import tango.text.xml.Document;
|
|
7 import tango.text.convert.Integer;
|
|
8 import tango.io.Stdout; // TODO
|
|
9 import tools;
|
|
10 import imagecache;
|
3
|
11 import dsfml.system.all;
|
|
12 import Text = tango.text.Util;
|
|
13
|
2
|
14
|
|
15 alias XmlPath!(char).NodeSet NodeSet;
|
|
16 alias Document!(char).Node NodeImpl;
|
|
17 private char[] get_attribute(NodeSet node, char[] name) {
|
|
18 return node.attribute(name).nodes[0].value;
|
|
19 }
|
|
20
|
|
21 Tileset parse_tileset(Cache cache, char[] content) {
|
|
22 auto tileset = new Tileset(cache);
|
|
23
|
|
24 auto doc = new Document!(char);
|
|
25 doc.parse(content);
|
|
26 auto root = doc.query["tileset"];
|
|
27 auto nodes = root.child.nodes;
|
|
28 foreach(NodeImpl tiles_node; nodes) {
|
|
29 tileset.add_tile(Integer.parse(tiles_node.getAttribute("id").value), tiles_node.getAttribute("filename").value);
|
|
30 }
|
|
31 return tileset;
|
|
32 }
|
|
33
|
|
34 Tilemap parse_map(Cache cache, char[] content) {
|
|
35 auto doc = new Document!(char);
|
|
36 doc.parse(content);
|
|
37 auto root = doc.query["map"];
|
|
38 int width = Integer.parse(get_attribute(root, "width"));
|
|
39 int height = Integer.parse(get_attribute(root, "height"));
|
|
40 Stdout.formatln("Width: {}, Height: {}", width, height);
|
|
41 auto tiles_node = root.child.nodes[0];
|
|
42 char[] tileset_file = tiles_node.getAttribute("tileset").value;
|
|
43 auto tilemap = new Tilemap(parse_tileset(cache, read_file_contents(tileset_file)), width, height, 32, 32); // TODO: variable tile size
|
3
|
44 int layer_id=0;
|
2
|
45 foreach(NodeImpl layer_node; tiles_node.query["layer"].nodes) {
|
|
46 int tile_width = Integer.parse(layer_node.getAttribute("tilewidth").value);
|
|
47 int tile_height = Integer.parse(layer_node.getAttribute("tileheight").value);
|
3
|
48 tilemap.layer_tsizes[layer_id] = Vector2i(tile_width, tile_height);
|
|
49 char[][] content_ = Text.delimit(Text.trim(layer_node.value), ",");
|
|
50 int i=0;
|
|
51 for(int x=0; x < width; x++) {
|
|
52 for(int y=0; y < height; y++) {
|
|
53 tilemap.map[layer_id][x][y] = Integer.parse(content_[i]);
|
|
54 i++;
|
|
55 }
|
|
56 }
|
|
57 layer_id++;
|
2
|
58 }
|
|
59 return tilemap;
|
|
60 }
|