-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_loader.cpp
More file actions
111 lines (93 loc) · 3.88 KB
/
xml_loader.cpp
File metadata and controls
111 lines (93 loc) · 3.88 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "xml_loader.hpp"
void XmlLoader::loadUnits(UnitManager* unit_manager, std::string filename) {
boost::property_tree::ptree tree;
boost::property_tree::read_xml(filename, tree);
boost::property_tree::ptree units = tree.get_child("units");
BOOST_FOREACH( boost::property_tree::ptree::value_type v, units ) {
boost::property_tree::ptree thisTree = v.second;
std::string id = thisTree.get<std::string>("id");
std::string filename = thisTree.get<std::string>("filename");
Unit testUnit = Unit(id,filename);
unit_manager->addNewEntType(id,testUnit);
}
}
// Putting this into its own function because it's really non-intuitive
bool XmlLoader::hasTag(std::string text, std::string tag) {
return text.find(tag) != std::string::npos;
}
int XmlLoader::parseTags(std::string tags) {
int retVal = 0;
if (hasTag(tags,"IS_WALKABLE")) {
retVal |= IS_WALKABLE;
}
if (hasTag(tags,"IS_MINABLE")) {
retVal |= IS_MINABLE;
}
if (hasTag(tags,"PATHING_BLOCK")) {
retVal |= PATHING_BLOCK;
}
return retVal;
}
Doodad* XmlLoader::loadWall(boost::property_tree::ptree wall_tree, DoodadManager* doodad_manager, Material* mat_made_of) {
std::string filename = wall_tree.get<std::string>("filename");
std::string tags = wall_tree.get<std::string>("tags");
std::ostringstream stream;
stream << mat_made_of->name << "_" << mat_made_of->wallIDs.size();
Doodad* thisWall = new Doodad(stream.str(), filename, 0, 0, parseTags(tags) | DRAW_SPECIAL);
if (doodad_manager != NULL) {
doodad_manager->addNewEntType(stream.str(), *thisWall);
}
if (mat_made_of != NULL) {
thisWall->madeOf = mat_made_of;
}
return thisWall;
}
Tile* XmlLoader::loadTile(boost::property_tree::ptree tile_tree, Map* map, Material* mat_made_of) {
std::string filename = tile_tree.get<std::string>("filename");
std::string tags = tile_tree.get<std::string>("tags");
Tile* thisTile = new Tile(filename, parseTags(tags));
if (map != NULL) {
map->addTile(thisTile);
}
if (mat_made_of != NULL) {
thisTile->madeOf = mat_made_of;
}
return thisTile;
}
void XmlLoader::loadMaterials(DoodadManager* doodad_manager, Map* map, std::string filename) {
boost::property_tree::ptree tree;
boost::property_tree::read_xml(filename, tree);
boost::property_tree::ptree mats = tree.get_child("materials");
BOOST_FOREACH( boost::property_tree::ptree::value_type &v, mats ) {
boost::property_tree::ptree mat_tree = v.second;
Material* mat = new Material();
map->addMaterial(mat);
mat->name = mat_tree.get<std::string>("name");
// Floor
try {
boost::property_tree::ptree floors = mat_tree.get_child("floor");
/*for ( boost::property_tree::ptree::value_type &v2 : mat_tree.get_child("floor") ) {
boost::property_tree::ptree floor_tree = v2.second;
Tile* floorTile = loadTile(floor_tree, map, mat);
mat->tileIDs.push_back(map->getTileID(floorTile));
}*/
Tile* floorTile = loadTile(floors, map, mat);
mat->tileIDs.push_back(map->getTileID(floorTile));
} catch (boost::property_tree::ptree_bad_path e) {
// Do nothing
}
// Wall
try {
boost::property_tree::ptree walls = mat_tree.get_child("wall");
/*BOOST_FOREACH( boost::property_tree::ptree::value_type v2, walls ) {
boost::property_tree::ptree wall_tree = v2.second;
//Doodad* wall = loadWall(wall_tree, doodad_manager, mat);
//mat->wallIDs.push_back(wall->uid);
}*/
Doodad* wall = loadWall(walls, doodad_manager, mat);
mat->wallIDs.push_back(wall->uid);
} catch (boost::property_tree::ptree_bad_path e) {
// Do nothing
}
}
}