From 15bc086a4d6bde6b9c184b8e20aee697bb22bb89 Mon Sep 17 00:00:00 2001 From: songhao Date: Wed, 22 Aug 2018 06:50:27 -0400 Subject: [PATCH] =?UTF-8?q?tiledmap=20=E5=88=86=E7=B1=BB=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cpp/tiledmap.cc | 118 +++++++++++++++++++++++++++++++++++++++++++++++- cpp/tiledmap.h | 33 ++++++++++++++ 2 files changed, 149 insertions(+), 2 deletions(-) diff --git a/cpp/tiledmap.cc b/cpp/tiledmap.cc index 41e9257..3bd4d56 100644 --- a/cpp/tiledmap.cc +++ b/cpp/tiledmap.cc @@ -1,5 +1,5 @@ #include - +#include #include "framework/cpp/tiledmap.h" a8::XValue TiledObject::GetProperty(const std::string& prop_name) @@ -38,6 +38,7 @@ bool TiledMap::LoadTmxFile(const std::string& filename) 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); @@ -93,7 +94,7 @@ bool TiledMap::LoadTmxFile(const std::string& filename) } } - + return true; } @@ -104,8 +105,121 @@ std::list* TiledMap::GetObjectGroup(const std::string& object_class 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 }); diff --git a/cpp/tiledmap.h b/cpp/tiledmap.h index 2bb8ced..964b8b7 100644 --- a/cpp/tiledmap.h +++ b/cpp/tiledmap.h @@ -22,6 +22,31 @@ class TiledLayer a8::XValue data; }; +//格子对象 +struct GridCell +{ + int x = 0; + int y = 0; + int speed = 0; + int value = 0; +}; + +struct StagePoint +{ + int x = 0; + int y = 0; + std::string point; + std::vector relation_list; +}; + +struct TriggerPoint +{ + int x = 0; + int y = 0; + std::string point; + bool istrigger = false; +}; + class TiledMap { public: @@ -30,13 +55,21 @@ class TiledMap int tile_count = 0; //瓦片地图的尺寸。(以瓦片数量为单位) int tile_width = 0; //瓦片的宽。(以像素点为单位) int tile_height = 0; //瓦片的高。(以像素点为单位) + int tile_columns = 0; //列 bool LoadTmxFile(const std::string& filename); std::list* GetObjectGroup(const std::string& object_class_name); void Dump(); + void Init(); private: std::map> layer_hash; std::map> object_group_hash; + std::map stage_object_hash; //key point + std::vector trigger_list; + std::map> stage_path_hash; //key A->B AB + std::vector grid_cell_list; //所有的格子数组 size = x * y + + std::map> grid_cell_hash; //key 11|2 坐标 };