玩家离线计算位置

This commit is contained in:
songhao 2018-08-24 07:02:09 -04:00
parent 15bc086a4d
commit 5bc827bd2f
2 changed files with 163 additions and 10 deletions

View File

@ -1,4 +1,5 @@
#include <a8/a8.h>
#include <cmath>
#include <a8/stringlist.h>
#include "framework/cpp/tiledmap.h"
@ -105,6 +106,30 @@ std::list<TiledObject>* TiledMap::GetObjectGroup(const std::string& object_class
return itr != object_group_hash.end() ? &itr->second : nullptr;
}
StagePoint* TiledMap::GetStageObject(int point)
{
auto itr = stage_object_hash.find(point);
return itr != stage_object_hash.end() ? &itr->second : nullptr;
}
bool TiledMap::HasStageObject(int point)
{
auto itr = stage_object_hash.find(point);
return itr != stage_object_hash.end();
}
std::vector<GridCell*>* TiledMap::GetStagePath(const std::string& path_name)
{
auto itr = stage_path_hash.find(path_name);
return itr != stage_path_hash.end() ? &itr->second : nullptr;
}
bool TiledMap::HasStagePath(const std::string& path_name)
{
auto itr = stage_path_hash.find(path_name);
return itr != stage_path_hash.end();
}
void TiledMap::Init()
{
grid_cell_list.reserve(tile_count);
@ -119,7 +144,7 @@ void TiledMap::Init()
bool has_trigger = layer.HasProperty("isTrigger");
bool istrigger = layer.GetProperty("isTrigger");;
std::string trigger_point = layer.GetProperty("objName").GetString();
int trigger_point = layer.GetProperty("objName");
bool has_num = layer.HasProperty("num");
//int num = layer.GetProperty("num");
@ -171,15 +196,15 @@ void TiledMap::Init()
StagePoint sp;
sp.x = object.GetProperty("tile_x");
sp.y = object.GetProperty("tile_y");
sp.point = object.GetProperty("location").GetString();
sp.point = object.GetProperty("name");
std::string str_relation = object.GetProperty("relation").GetString();
std::vector<std::string> split_list;
a8::Split(str_relation, split_list, ',');
for (auto& point : split_list) {
sp.relation_list.push_back(point);
sp.relation_list.push_back(a8::XValue(point));
}
std::string name = object.GetProperty("name").GetString();
stage_object_hash[name] = sp;
int point = object.GetProperty("name");
stage_object_hash[point] = sp;
}
}
}
@ -243,3 +268,123 @@ void TiledMap::Dump()
}
}
}
bool TiledMap::CalcCurrPos(std::list<int>& path_points, int old_pos_x, int old_pos_y, int time_ms, int& curr_pos_x, int& curr_pos_y)
{
if (path_points.size() < 2) {
return false;
}
std::string path_point;
std::vector<std::string> stage_path_list;
bool path_end = false;
std::vector<GridCell*> grid_all_list;
for (auto& point : path_points) {
if (!path_end) {
path_point = a8::XValue(point).GetString();
path_end = !path_end;
} else {
path_point += "|" + a8::XValue(point).GetString();
stage_path_list.push_back(path_point);
path_end = !path_end;
}
}
bool exc_once = true;
for (auto& stage_path : stage_path_list) {
std::vector<GridCell*>* grid_list = nullptr;
std::string stage_path_reverse;
std::vector<std::string> split_list;
a8::Split(stage_path,split_list,'|');
if (split_list.size() != 2) {
return false;
}
StagePoint* sp = GetStageObject(a8::XValue(split_list.at(0)));
if (sp == nullptr) {
return false;
}
grid_list = GetStagePath(stage_path);
if (grid_list == nullptr) {
stage_path_reverse = split_list[1] + "|" + split_list[0];
grid_list = GetStagePath(stage_path_reverse);
if (grid_list == nullptr) {
return false;
}
}
std::vector<GridCell*> grid_sort_list = SortGridList(grid_list,sp);
if (grid_sort_list.empty()){
return false;
}
if (exc_once) {
grid_all_list.insert(grid_all_list.end(),grid_sort_list.begin(),grid_sort_list.end());
exc_once = false;
} else {
grid_all_list.insert(grid_all_list.end(),grid_sort_list.begin()+1,grid_sort_list.end());
}
}
if (grid_all_list.empty()) {
return false;
}
int grid_all_count = grid_all_list.size();
for (int i = 0; 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(((tile_width / speed)*1000));
} else if (gc_second->y == gc_first->y) {
time_use = a8::XValue(((tile_height / speed)*1000));
} else {
time_use = a8::XValue((std::sqrt((std::pow(tile_width,2) + std::pow(tile_height,2)) / speed)*1000));
}
time_ms -= time_use;
if (time_ms < 0) {
curr_pos_x = gc_second->x;
curr_pos_y = gc_second->y;
return true;
}
}
return false;
}
std::vector<GridCell*> TiledMap::SortGridList(std::vector<GridCell*>* grid_list, StagePoint* sp)
{
std::vector<GridCell*> grid_sort_list;
std::map<GridCell*, bool> 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 = 1; 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;
}
}
}
}
if (gc_next != nullptr) {
grid_sort_list.push_back(gc_next);
}
}
return grid_sort_list;
}

View File

@ -35,15 +35,15 @@ struct StagePoint
{
int x = 0;
int y = 0;
std::string point;
std::vector<std::string> relation_list;
int point = 0;
std::vector<int> relation_list;
};
struct TriggerPoint
{
int x = 0;
int y = 0;
std::string point;
int point = 0;
bool istrigger = false;
};
@ -61,14 +61,22 @@ class TiledMap
std::list<TiledObject>* GetObjectGroup(const std::string& object_class_name);
void Dump();
void Init();
StagePoint* GetStageObject(int point);
bool HasStageObject(int point);
std::vector<GridCell*>* GetStagePath(const std::string& path_name);
bool HasStagePath(const std::string& path_name);
std::vector<GridCell*> SortGridList(std::vector<GridCell*>* grid_list, StagePoint* sp);
bool CalcCurrPos(std::list<int>& path_points, int old_pos_x, int old_pos_y, int time_ms, int& curr_pos_x, int& curr_pos_y);
private:
std::map<std::string, std::list<TiledLayer>> layer_hash;
std::map<std::string, std::list<TiledObject>> object_group_hash;
std::map<std::string, StagePoint> stage_object_hash; //key point
std::map<int, StagePoint> stage_object_hash; //key point
std::vector<TriggerPoint> trigger_list;
std::map<std::string, std::vector<GridCell*>> stage_path_hash; //key A->B AB
std::map<std::string, std::vector<GridCell*>> stage_path_hash; //key 11|2
std::vector<GridCell> grid_cell_list; //所有的格子数组 size = x * y
std::map<std::string, std::vector<GridCell>> grid_cell_hash; //key 11|2 坐标