在六边形网格地图中,单位选择攻击目标不符合预期,经过查找是网格间路径消耗计算有误,修复之后功能正常。
改动之前:
int GridHelper::getTwoGridDistance(const double& startPosX, const double& startPosY, const double& endPosX, const double& endPosY)
{
Grid startGrid = GridHelper::getGridByPos(startPosX, startPosY);
Grid endGrid = GridHelper::getGridByPos(endPosX, endPosY);
int dx = std::abs(startGrid.x - endGrid.x);
int dy = std::abs(startGrid.y - endGrid.y);
int dz = std::abs(startGrid.z - endGrid.z);
int radius = std::max(dx, std::max(dy, dz));
return radius;
}
调整之后【路径消耗引入distance变量】:
int GridHelper::getTwoGridDistance(const double& startPosX, const double& startPosY, const double& endPosX, const double& endPosY, double& distance)
{
Grid startGrid = GridHelper::getGridByPos(startPosX, startPosY);
Grid endGrid = GridHelper::getGridByPos(endPosX, endPosY);
int dx = std::abs(startGrid.x - endGrid.x);
int dy = std::abs(startGrid.y - endGrid.y);
int dz = std::abs(startGrid.z - endGrid.z);
int radius = std::max(dx, std::max(dy, dz));
//两个网格间的距离【移动消耗】【无阻挡点有效】
//对角线消耗2,对边消耗1.73,边界消耗1
//通过对角线必然经过边界
distance = 0;
int diagonalStep = std::min(dx, std::min(dy, dz));
distance = diagonalStep * (2/*对角线消耗*/+1/*边界消耗*/) + (radius - diagonalStep*2) * mathSqrt3;
return radius;
}