diff --git a/cpp/tiledmap.cc b/cpp/tiledmap.cc index bc90b77..d820195 100644 --- a/cpp/tiledmap.cc +++ b/cpp/tiledmap.cc @@ -30,7 +30,34 @@ bool TiledLayer::HasProperty(const std::string& prop_name) bool TiledMap::LoadTmxFile(const std::string& filename) { a8::XObject xobj; - xobj.ReadFromXmlFile(filename); + if (!xobj.ReadFromXmlFile(filename)) { + return false; + } + tile_count = xobj.At("child_node.tileset")->At(0)->At("attrs.tilecount")->AsXValue(); + for (int i = 0; i < xobj.At("child_node.layer")->Size(); ++i) { + std::shared_ptr layer_node = xobj.At("child_node.layer")->At(i); + + TiledLayer layer; + { + for (int ii = 0; ii < layer_node->At("attr_names")->Size(); ++ii) { + std::string attr_name = layer_node->At("attr_names")->At(ii)->AsXValue(); + layer.prop_hash[attr_name] = layer_node->At("attrs." + attr_name)->AsXValue(); + } + std::shared_ptr prop_nodes = layer_node->At("child_node.properties")->At(0)->At("child_node.property"); + for (int j = 0; j < prop_nodes->Size(); ++j) { + std::shared_ptr prop_node = prop_nodes->At(j); + layer.prop_hash[prop_node->At("attrs.name")->AsXValue()] = prop_node->At("attrs.value")->AsXValue(); + } + } + + std::string layer_name = layer_node->At("attrs.name")->AsXValue(); + auto itr = layer_hash.find(layer_name); + if (itr != layer_hash.end()) { + itr->second.push_back(layer); + } else { + layer_hash[layer_name] = std::list({layer}); + } + } return true; } @@ -39,3 +66,26 @@ std::list* TiledMap::GetObjectGroup(const std::string& object_class auto itr = object_group_hash.find(object_class_name); return itr != object_group_hash.end() ? &itr->second : nullptr; } + +void TiledMap::Dump() +{ + a8::XPrintf("tile_count:%d\n", {tile_count}); + a8::XPrintf("layer_hash.size:%d\n", {layer_hash.size()}); + for (auto& pair : layer_hash) { + a8::XPrintf(" layer_type:%s\n", {pair.first}); + for (auto& layer : pair.second) { + for (auto& pair2 : layer.prop_hash) { + a8::XPrintf(" %s:%s\n", {pair2.first, pair2.second}); + } + } + } + a8::XPrintf("object_group_hash.size:%d\n", {object_group_hash.size()}); + for (auto& pair : object_group_hash) { + a8::XPrintf(" layer_type:%s\n", {pair.first}); + for (auto& layer : pair.second) { + for (auto& pair2 : layer.prop_hash) { + a8::XPrintf(" %s:%s\n", {pair2.first, pair2.second}); + } + } + } +} diff --git a/cpp/tiledmap.h b/cpp/tiledmap.h index a70f0f1..7e200d0 100644 --- a/cpp/tiledmap.h +++ b/cpp/tiledmap.h @@ -6,7 +6,7 @@ class TiledObject a8::XValue GetProperty(const std::string& prop_name); bool HasProperty(const std::string& prop_name); - private: + public: std::map prop_hash; }; @@ -17,7 +17,7 @@ class TiledLayer a8::XValue GetProperty(const std::string& prop_name); bool HasProperty(const std::string& prop_name); - private: + public: std::map prop_hash; }; @@ -26,13 +26,15 @@ class TiledMap public: TiledLayer* ground_layer = nullptr; std::list speed_layers; - int map_size = 0; //瓦片地图的尺寸。(以瓦片数量为单位) + int tile_count = 0; //瓦片地图的尺寸。(以瓦片数量为单位) int tile_size = 0; //瓦片的尺寸。(以像素点为单位) bool LoadTmxFile(const std::string& filename); std::list* GetObjectGroup(const std::string& object_class_name); + void Dump(); private: + std::map> layer_hash; std::map> object_group_hash; };