From c8007e81ddfae624d84fd3dc42d7248b6d772262 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 1 Jun 2019 17:40:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0waypoing=20dat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cpp/tiledmap.cc | 174 +++++++++++++++++++++++++----------------------- cpp/tiledmap.h | 6 +- 2 files changed, 95 insertions(+), 85 deletions(-) diff --git a/cpp/tiledmap.cc b/cpp/tiledmap.cc index 3539d68..1396c4f 100644 --- a/cpp/tiledmap.cc +++ b/cpp/tiledmap.cc @@ -30,7 +30,7 @@ namespace f8 return itr != prop_hash.end(); } - bool TiledMap::LoadTmxFile(const std::string& filename) + bool TiledMap::LoadTmxFile(const std::string& filename, bool new_tmx_format) { a8::XObject xobj; if (!xobj.ReadFromXmlFile(filename)) { @@ -356,83 +356,87 @@ namespace f8 } } - if (grid_all_list.empty()) { - return false; - } - { - bool has_index = false; - int index = 0; - for (auto& gc : grid_all_list) { - if (gc->x == old_pos_x && gc->y == old_pos_y) { - has_index = true; - break; - } - index++; - } - if (!has_index) { + if (grid_all_list.empty()) { return false; } - - int grid_all_count = grid_all_list.size(); - for (int i = index; i < grid_all_count-1; ++i) { - GridCell* gc_first = grid_all_list[i]; - GridCell* gc_second = grid_all_list[i+1]; - int speed = gc_first->speed; - int time_use = 0; - if (gc_second->x == gc_first->x) { - time_use = a8::XValue((((float)tile_height / speed)*1000)); - } else if (gc_second->y == gc_first->y) { - time_use = a8::XValue((((float)tile_width / speed)*1000)); - } else { - 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)); + { + bool has_index = false; + int index = 0; + for (auto& gc : grid_all_list) { + if (gc->x == old_pos_x && gc->y == old_pos_y) { + has_index = true; + break; + } + index++; } - time_ms -= time_use; - curr_pos_x = gc_second->x; - curr_pos_y = gc_second->y; - if (time_ms < 0) { - return true; + if (!has_index) { + return false; + } + + int grid_all_count = grid_all_list.size(); + for (int i = index; i < grid_all_count-1; ++i) { + GridCell* gc_first = grid_all_list[i]; + GridCell* gc_second = grid_all_list[i+1]; + int speed = gc_first->speed; + int time_use = 0; + if (gc_second->x == gc_first->x) { + time_use = a8::XValue((((float)tile_height / speed)*1000)); + } else if (gc_second->y == gc_first->y) { + time_use = a8::XValue((((float)tile_width / speed)*1000)); + } else { + 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) { + return true; + } } } - } - if (time_ms > 0) { - return true; - } - return false; -} - -std::vector TiledMap::SortGridList(std::vector* grid_list, StagePoint* sp) -{ - std::vector grid_sort_list; - std::map grid_tag_hash; - int grid_list_count = grid_list->size(); - int old_x = 0; - int old_y = 0; - for (auto& gc : *grid_list) { - grid_tag_hash[gc] = false; - } - for (auto& gc : *grid_list) { - if (gc->x == sp->x && gc->y == sp->y) { - grid_sort_list.push_back(gc); - old_x = gc->x; - old_y = gc->y; - grid_tag_hash[gc] = true; + if (time_ms > 0) { + return true; } + return false; } - for (int count = 1; count < grid_list_count; ++count) { - GridCell* gc_next = nullptr; - 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; + std::vector TiledMap::SortGridList(std::vector* grid_list, StagePoint* sp) + { + std::vector grid_sort_list; + std::map grid_tag_hash; + int grid_list_count = grid_list->size(); + int old_x = 0; + int old_y = 0; + for (auto& gc : *grid_list) { + grid_tag_hash[gc] = false; + } + for (auto& gc : *grid_list) { + if (gc->x == sp->x && gc->y == sp->y) { + grid_sort_list.push_back(gc); + old_x = gc->x; + old_y = gc->y; + grid_tag_hash[gc] = true; + } + } + + for (int count = 1; count < grid_list_count; ++count) { + GridCell* gc_next = nullptr; + 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; } } @@ -441,25 +445,27 @@ std::vector TiledMap::SortGridList(std::vector* grid_list, } } if (gc_next != nullptr) { - break; + grid_sort_list.push_back(gc_next); } } - if (gc_next != nullptr) { - grid_sort_list.push_back(gc_next); - } + return grid_sort_list; } - return grid_sort_list; -} -void TiledMap::SetGridCellSpeedByAreaId(std::map& area_speed_hash) -{ - for (auto& pair : stage_path_hash) { - for (auto& grid_cell : pair.second) { - auto itr = area_speed_hash.find(grid_cell->speed); - if (itr != area_speed_hash.end()) { - grid_cell->speed = itr->second; + void TiledMap::SetGridCellSpeedByAreaId(std::map& area_speed_hash) + { + for (auto& pair : stage_path_hash) { + for (auto& grid_cell : pair.second) { + auto itr = area_speed_hash.find(grid_cell->speed); + if (itr != area_speed_hash.end()) { + grid_cell->speed = itr->second; + } } } } -} + + void TiledMap::LoadWayPointsData() + { + + } + } diff --git a/cpp/tiledmap.h b/cpp/tiledmap.h index c2273cc..8185507 100644 --- a/cpp/tiledmap.h +++ b/cpp/tiledmap.h @@ -60,7 +60,7 @@ namespace f8 int tile_rows = 0; //行 int tile_columns = 0; //列 - bool LoadTmxFile(const std::string& filename); + bool LoadTmxFile(const std::string& filename, bool new_tmx_format = false); std::list* GetObjectGroup(const std::string& object_class_name); void Dump(); void Init(); @@ -76,6 +76,9 @@ namespace f8 //速度设置 void SetGridCellSpeedByAreaId(std::map& area_speed_hash);//key:area_id value:speed + private: + void LoadWayPointsData(); + private: std::map> layer_hash; std::map> object_group_hash; @@ -86,5 +89,6 @@ namespace f8 std::vector grid_cell_list; //所有的格子数组 size = x * y std::map> grid_cell_hash; //key 11|2 坐标 + std::map>> waypoint_hash_; //路点数据 }; }