128 lines
4.5 KiB
C++
128 lines
4.5 KiB
C++
#include <a8/a8.h>
|
|
|
|
#include "tiledmap.h"
|
|
|
|
a8::XValue TiledObject::GetProperty(const std::string& prop_name)
|
|
{
|
|
auto itr = prop_hash.find(prop_name);
|
|
return itr != prop_hash.end() ? itr->second : a8::XValue();
|
|
}
|
|
|
|
bool TiledObject::HasProperty(const std::string& prop_name)
|
|
{
|
|
auto itr = prop_hash.find(prop_name);
|
|
return itr != prop_hash.end();
|
|
}
|
|
|
|
|
|
a8::XValue TiledLayer::GetProperty(const std::string& prop_name)
|
|
{
|
|
auto itr = prop_hash.find(prop_name);
|
|
return itr != prop_hash.end() ? itr->second : a8::XValue();
|
|
}
|
|
|
|
bool TiledLayer::HasProperty(const std::string& prop_name)
|
|
{
|
|
auto itr = prop_hash.find(prop_name);
|
|
return itr != prop_hash.end();
|
|
}
|
|
|
|
bool TiledMap::LoadTmxFile(const std::string& filename)
|
|
{
|
|
a8::XObject xobj;
|
|
if (!xobj.ReadFromXmlFile(filename)) {
|
|
return false;
|
|
}
|
|
|
|
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) {
|
|
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});
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
std::list<TiledObject>* TiledMap::GetObjectGroup(const std::string& object_class_name)
|
|
{
|
|
auto itr = object_group_hash.find(object_class_name);
|
|
return itr != object_group_hash.end() ? &itr->second : nullptr;
|
|
}
|
|
|
|
void TiledMap::Dump()
|
|
{
|
|
a8::XPrintf("map$tile_count:%d\n", {tile_count});
|
|
a8::XPrintf("map$tile_width:%d\n", { tile_width });
|
|
a8::XPrintf("map$tile_height:%d\n", { tile_height });
|
|
a8::XPrintf("layer$layer_hash.size:%d\n", {layer_hash.size()});
|
|
for (auto& pair : layer_hash) {
|
|
a8::XPrintf(" layer$layer_type:%s\n", {pair.first});
|
|
for (auto& layer : pair.second) {
|
|
for (auto& pair2 : layer.prop_hash) {
|
|
a8::XPrintf(" layer$%s:%s\n", {pair2.first, pair2.second});
|
|
}
|
|
}
|
|
}
|
|
a8::XPrintf("object$object_group_hash.size:%d\n", {object_group_hash.size()});
|
|
for (auto& pair : object_group_hash) {
|
|
a8::XPrintf(" object$layer_type:%s\n", {pair.first});
|
|
for (auto& layer : pair.second) {
|
|
for (auto& pair2 : layer.prop_hash) {
|
|
a8::XPrintf(" object$%s:%s\n", {pair2.first, pair2.second});
|
|
}
|
|
}
|
|
}
|
|
}
|