完成tiledmap基本读取
This commit is contained in:
parent
ef2c68bf65
commit
be80b51e87
@ -30,7 +30,34 @@ bool TiledLayer::HasProperty(const std::string& prop_name)
|
|||||||
bool TiledMap::LoadTmxFile(const std::string& filename)
|
bool TiledMap::LoadTmxFile(const std::string& filename)
|
||||||
{
|
{
|
||||||
a8::XObject xobj;
|
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<a8::XObject> 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<a8::XObject> 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<a8::XObject> 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<TiledLayer>({layer});
|
||||||
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,3 +66,26 @@ std::list<TiledObject>* TiledMap::GetObjectGroup(const std::string& object_class
|
|||||||
auto itr = object_group_hash.find(object_class_name);
|
auto itr = object_group_hash.find(object_class_name);
|
||||||
return itr != object_group_hash.end() ? &itr->second : nullptr;
|
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});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -6,7 +6,7 @@ class TiledObject
|
|||||||
a8::XValue GetProperty(const std::string& prop_name);
|
a8::XValue GetProperty(const std::string& prop_name);
|
||||||
bool HasProperty(const std::string& prop_name);
|
bool HasProperty(const std::string& prop_name);
|
||||||
|
|
||||||
private:
|
public:
|
||||||
std::map<std::string, a8::XValue> prop_hash;
|
std::map<std::string, a8::XValue> prop_hash;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ class TiledLayer
|
|||||||
a8::XValue GetProperty(const std::string& prop_name);
|
a8::XValue GetProperty(const std::string& prop_name);
|
||||||
bool HasProperty(const std::string& prop_name);
|
bool HasProperty(const std::string& prop_name);
|
||||||
|
|
||||||
private:
|
public:
|
||||||
std::map<std::string, a8::XValue> prop_hash;
|
std::map<std::string, a8::XValue> prop_hash;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -26,13 +26,15 @@ class TiledMap
|
|||||||
public:
|
public:
|
||||||
TiledLayer* ground_layer = nullptr;
|
TiledLayer* ground_layer = nullptr;
|
||||||
std::list<TiledLayer> speed_layers;
|
std::list<TiledLayer> speed_layers;
|
||||||
int map_size = 0; //瓦片地图的尺寸。(以瓦片数量为单位)
|
int tile_count = 0; //瓦片地图的尺寸。(以瓦片数量为单位)
|
||||||
int tile_size = 0; //瓦片的尺寸。(以像素点为单位)
|
int tile_size = 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);
|
||||||
|
void Dump();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::map<std::string, std::list<TiledLayer>> layer_hash;
|
||||||
std::map<std::string, std::list<TiledObject>> object_group_hash;
|
std::map<std::string, std::list<TiledObject>> object_group_hash;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user