titled map read ok

This commit is contained in:
root 2018-08-16 09:14:51 -04:00
parent be80b51e87
commit f0907e8457
2 changed files with 40 additions and 3 deletions

View File

@ -5,7 +5,7 @@
a8::XValue TiledObject::GetProperty(const std::string& prop_name) a8::XValue TiledObject::GetProperty(const std::string& prop_name)
{ {
auto itr = prop_hash.find(prop_name); auto itr = prop_hash.find(prop_name);
return itr == prop_hash.end() ? itr->second : a8::XValue(); return itr != prop_hash.end() ? itr->second : a8::XValue();
} }
bool TiledObject::HasProperty(const std::string& prop_name) bool TiledObject::HasProperty(const std::string& prop_name)
@ -33,7 +33,11 @@ bool TiledMap::LoadTmxFile(const std::string& filename)
if (!xobj.ReadFromXmlFile(filename)) { if (!xobj.ReadFromXmlFile(filename)) {
return false; return false;
} }
tile_count = xobj.At("child_node.tileset")->At(0)->At("attrs.tilecount")->AsXValue();
std::shared_ptr<a8::XObject> tileset_node = xobj.At("child_node.tileset")->At(0);
tile_count = tileset_node->At("attrs.tilecount")->AsXValue();
tile_width = tileset_node->At("attrs.tilewidth")->AsXValue();
tile_height = tileset_node->At("attrs.tileheight")->AsXValue();
for (int i = 0; i < xobj.At("child_node.layer")->Size(); ++i) { for (int i = 0; i < xobj.At("child_node.layer")->Size(); ++i) {
std::shared_ptr<a8::XObject> layer_node = xobj.At("child_node.layer")->At(i); std::shared_ptr<a8::XObject> layer_node = xobj.At("child_node.layer")->At(i);
@ -58,6 +62,36 @@ bool TiledMap::LoadTmxFile(const std::string& filename)
layer_hash[layer_name] = std::list<TiledLayer>({layer}); layer_hash[layer_name] = std::list<TiledLayer>({layer});
} }
} }
std::shared_ptr<a8::XObject> objgroup_node = xobj.At("child_node.objectgroup")->At(0);
for (int i = 0; i < objgroup_node->At("child_node.object")->Size(); i++) {
std::shared_ptr<a8::XObject> object_node = objgroup_node->At("child_node.object")->At(i);
TiledObject object;
{
for (int ii = 0; ii < object_node->At("attr_names")->Size(); ii++) {
std::string attr_name = object_node->At("attr_names")->At(ii)->AsXValue();
object.prop_hash[attr_name] = object_node->At("attrs." + attr_name)->AsXValue();
}
std::shared_ptr<a8::XObject> prop_nodes = object_node->At("child_node.properties")->At(0)->At("child_node.property");
for (int j = 0; j < prop_nodes->Size(); j++) {
std::shared_ptr<a8::XObject> prop_node = prop_nodes->At(j);
object.prop_hash[prop_node->At("attrs.name")->AsXValue()] = prop_node->At("attrs.value")->AsXValue();
}
}
std::string object_name = object_node->At("attrs.name")->AsXValue();
auto itr = object_group_hash.find(object_name);
if (itr != object_group_hash.end()) {
itr->second.push_back(object);
}
else
{
object_group_hash[object_name] = std::list<TiledObject>({ object });
}
}
return true; return true;
} }
@ -70,6 +104,8 @@ std::list<TiledObject>* TiledMap::GetObjectGroup(const std::string& object_class
void TiledMap::Dump() void TiledMap::Dump()
{ {
a8::XPrintf("tile_count:%d\n", {tile_count}); a8::XPrintf("tile_count:%d\n", {tile_count});
a8::XPrintf("tile_width:%d\n", { tile_width });
a8::XPrintf("tile_height:%d\n", { tile_height });
a8::XPrintf("layer_hash.size:%d\n", {layer_hash.size()}); a8::XPrintf("layer_hash.size:%d\n", {layer_hash.size()});
for (auto& pair : layer_hash) { for (auto& pair : layer_hash) {
a8::XPrintf(" layer_type:%s\n", {pair.first}); a8::XPrintf(" layer_type:%s\n", {pair.first});

View File

@ -27,7 +27,8 @@ class TiledMap
TiledLayer* ground_layer = nullptr; TiledLayer* ground_layer = nullptr;
std::list<TiledLayer> speed_layers; std::list<TiledLayer> speed_layers;
int tile_count = 0; //瓦片地图的尺寸。(以瓦片数量为单位) int tile_count = 0; //瓦片地图的尺寸。(以瓦片数量为单位)
int tile_size = 0; //瓦片的尺寸。(以像素点为单位) int tile_width = 0; //瓦片的宽。(以像素点为单位)
int tile_height = 0; //瓦片的高。(以像素点为单位)
bool LoadTmxFile(const std::string& filename); bool LoadTmxFile(const std::string& filename);
std::list<TiledObject>* GetObjectGroup(const std::string& object_class_name); std::list<TiledObject>* GetObjectGroup(const std::string& object_class_name);