From 841fdde99e078bf00ace01a03b18b3be726c8dcb Mon Sep 17 00:00:00 2001 From: songhao Date: Sat, 25 Aug 2018 03:21:21 -0400 Subject: [PATCH] =?UTF-8?q?tiledmap=20=E8=AE=A1=E7=AE=97=E7=A6=BB=E7=BA=BF?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cpp/tiledmap.cc | 56 +++++++++++++++++++++++++++++++++---------------- cpp/tiledmap.h | 1 + 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/cpp/tiledmap.cc b/cpp/tiledmap.cc index f39c920..88ccd96 100644 --- a/cpp/tiledmap.cc +++ b/cpp/tiledmap.cc @@ -40,6 +40,9 @@ bool TiledMap::LoadTmxFile(const std::string& filename) tile_width = tileset_node->At("attrs.tilewidth")->AsXValue(); tile_height = tileset_node->At("attrs.tileheight")->AsXValue(); tile_columns = tileset_node->At("attrs.columns")->AsXValue(); + tile_rows = tile_count/tile_columns + 1; + tile_columns += 1; + tile_count = tile_rows * tile_columns; for (int i = 0; i < xobj.At("child_node.layer")->Size(); ++i) { std::shared_ptr layer_node = xobj.At("child_node.layer")->At(i); @@ -152,27 +155,28 @@ void TiledMap::Init() 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) { + for (int i = 1; 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; + int split_count = split_list.size(); 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; + int index = (i-1)*tile_columns + j; if (has_speed) { - grid_cell_list[index].x = i-1; - grid_cell_list[index].y = j+1; + grid_cell_list[index].x = j; + grid_cell_list[index].y = i-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.x = j; + tp.y = i-1; tp.point = trigger_point; tp.istrigger = istrigger; trigger_list.push_back(tp); @@ -276,16 +280,17 @@ bool TiledMap::CalcCurrPos(std::vector& path_points, int old_pos_x, int old } std::string path_point; std::vector stage_path_list; - bool path_end = false; + bool path_start = true; std::vector grid_all_list; for (auto& point : path_points) { - if (!path_end) { + if (path_start) { path_point = a8::XValue(point).GetString(); - path_end = !path_end; + path_start = !path_start; } else { - path_point += "-" + a8::XValue(point).GetString(); + std::string point_str = a8::XValue(point).GetString(); + path_point += "-" + point_str; stage_path_list.push_back(path_point); - path_end = !path_end; + path_point = point_str; } } @@ -334,19 +339,25 @@ bool TiledMap::CalcCurrPos(std::vector& path_points, int old_pos_x, int old int speed = gc_first->speed; int time_use = 0; if (gc_second->x == gc_first->x) { - time_use = a8::XValue(((tile_width / speed)*1000)); + time_use = a8::XValue((((float)tile_height / speed)*1000)); } else if (gc_second->y == gc_first->y) { - time_use = a8::XValue(((tile_height / speed)*1000)); + time_use = a8::XValue((((float)tile_width / speed)*1000)); } else { - time_use = a8::XValue((std::sqrt((std::pow(tile_width,2) + std::pow(tile_height,2)) / speed)*1000)); + float width_pow = std::pow(tile_width,2.0); + float height_pow = std::pow(tile_height,2.0); + float value_sqrt = std::sqrt(width_pow+height_pow); + time_use = a8::XValue(((value_sqrt / speed)*1000)); } time_ms -= time_use; + curr_pos_x = gc_second->x; + curr_pos_y = gc_second->y; if (time_ms < 0) { - curr_pos_x = gc_second->x; - curr_pos_y = gc_second->y; return true; } } + if (time_ms > 0) { + return true; + } return false; } @@ -371,15 +382,24 @@ std::vector TiledMap::SortGridList(std::vector* grid_list, for (int count = 1; count < grid_list_count; ++count) { GridCell* gc_next = nullptr; - for (int i = 1; i < grid_list_count; ++i) { + for (int i = 0; i < grid_list_count; ++i) { GridCell* gc = grid_list->at(i); for (int x = old_x-1; x <= old_x+1; ++x) { for(int y = old_y-1; y <= old_y+1; ++y) { if (gc->x == x && gc->y == y && !grid_tag_hash[gc]) { grid_tag_hash[gc] = true; gc_next = gc; + old_x = gc->x; + old_y = gc->y; + break; } } + if (gc_next != nullptr) { + break; + } + } + if (gc_next != nullptr) { + break; } } if (gc_next != nullptr) { diff --git a/cpp/tiledmap.h b/cpp/tiledmap.h index e72b054..7fda64e 100644 --- a/cpp/tiledmap.h +++ b/cpp/tiledmap.h @@ -55,6 +55,7 @@ class TiledMap int tile_count = 0; //瓦片地图的尺寸。(以瓦片数量为单位) int tile_width = 0; //瓦片的宽。(以像素点为单位) int tile_height = 0; //瓦片的高。(以像素点为单位) + int tile_rows = 0; //行 int tile_columns = 0; //列 bool LoadTmxFile(const std::string& filename);