tiledmap 计算离线位置

This commit is contained in:
songhao 2018-08-25 03:21:21 -04:00
parent a6d13c24ff
commit 841fdde99e
2 changed files with 39 additions and 18 deletions

View File

@ -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<a8::XObject> layer_node = xobj.At("child_node.layer")->At(i);
@ -152,27 +155,28 @@ void TiledMap::Init()
std::vector<GridCell*> 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<std::string> 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<int>& path_points, int old_pos_x, int old
}
std::string path_point;
std::vector<std::string> stage_path_list;
bool path_end = false;
bool path_start = true;
std::vector<GridCell*> 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<int>& 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<GridCell*> TiledMap::SortGridList(std::vector<GridCell*>* 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) {

View File

@ -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);