#include #include #include "framework/cpp/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 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(); tile_columns = tileset_node->At("attrs.columns")->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::shared_ptr data_node = layer_node->At("child_node.data")->At(0); layer.data = data_node->At("node_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}); } } std::shared_ptr 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 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 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 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({ object }); } } return true; } std::list* 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::Init() { grid_cell_list.reserve(tile_count); grid_cell_list.assign(tile_count,GridCell{}); for (auto& pair : layer_hash) { for (auto& layer : pair.second) { std::string name = layer.GetProperty("name").GetString(); bool has_speed = layer.HasProperty("speed"); int speed = layer.GetProperty("speed"); bool has_trigger = layer.HasProperty("isTrigger"); bool istrigger = layer.GetProperty("isTrigger");; std::string trigger_point = layer.GetProperty("objName").GetString(); bool has_num = layer.HasProperty("num"); //int num = layer.GetProperty("num"); std::vector grid_cell_path_list; a8::StringList str_list; str_list.SetText(layer.data.GetString().c_str()); for (int i = 2; i < str_list.Count(); ++i) { std::string str_line = str_list.String(i); std::vector split_list; a8::Split(str_line, split_list); int split_count = split_list.size()-1; for(int j = 0; j < split_count; ++j){ int value = a8::XValue(split_list[j]); if(value > 0){ int index = (i-2)*tile_columns + j; if (has_speed) { grid_cell_list[index].x = i-1; grid_cell_list[index].y = j+1; grid_cell_list[index].speed = speed; grid_cell_list[index].value = value; } if (has_trigger) { TriggerPoint tp; { tp.x = i; tp.y = j+1; tp.point = trigger_point; tp.istrigger = istrigger; trigger_list.push_back(tp); } } if (has_num) { grid_cell_path_list.push_back(&grid_cell_list[index]); } } } } if (grid_cell_path_list.size() > 0) { stage_path_hash[name] = grid_cell_path_list; } } } for (auto& pair : object_group_hash) { for (auto& object : pair.second) { StagePoint sp; sp.x = object.GetProperty("tile_x"); sp.y = object.GetProperty("tile_y"); sp.point = object.GetProperty("location").GetString(); std::string str_relation = object.GetProperty("relation").GetString(); std::vector split_list; a8::Split(str_relation, split_list, ','); for (auto& point : split_list) { sp.relation_list.push_back(point); } std::string name = object.GetProperty("name").GetString(); stage_object_hash[name] = sp; } } } void TiledMap::Dump() { Init(); #if 0 for (auto& pair : stage_path_hash) { a8::XPrintf("stage$:%s\n", {pair.first}); for (auto& gc : pair.second) { a8::XPrintf("path$x:%d,y:%d,speed:%d,value:%d\n", {gc->x, gc->y, gc->speed, gc->value}); } } #endif #if 0 for (auto& tp : trigger_list) { a8::XPrintf("x:%d,y:%d,point:%d,istrigger:%d\n", {tp.x, tp.y, tp.point, a8::XValue(tp.istrigger)}); } #endif #if 0 int len = grid_cell_list.size(); a8::XPrintf("grid_cell_size:%s\n", {len}); for (int i= 0; i < len; ++i) { auto gc = grid_cell_list[i]; a8::XPrintf("i:%d, x:%d,y:%d,speed:%d,value:%d\n", {i, gc.x, gc.y, gc.speed, gc.value}); } #endif #if 0 for (auto& pair : stage_object_hash) { a8::XPrintf("object$:%s\n", {pair.first}); a8::XPrintf("value$x:%d,y:%d,p:%s,relation:", {pair.second.x, pair.second.y, pair.second.point}); for (auto& relation : pair.second.relation_list) { a8::XPrintf("%s,", {relation}); } a8::XPrintf("\n",{}); } #endif return; 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) { a8::XPrintf("data$data:%s\n",{layer.data}); 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}); } } } }