/// <summary> /// F=G+H; /// </summary> private void CalcWeight(int cur_x, int cur_y, int next_x, int next_y, bool is_slash, int next_dir) { //检查斜线可达 if (is_slash) { if (this.NoWalk(next_x, cur_y) || this.NoWalk(cur_x, next_y)) { return; } } AStarFindPath.PointInfo next_p = this.m_map[next_x][next_y]; AStarFindPath.PointInfo cur_p = this.m_map[cur_x][cur_y]; //计算G int g = cur_p.g + (is_slash ? 14142 : 10000); if (cur_p.dir != next_dir) { g += 8200; } //计算H if (next_p.g == 0 || next_p.g > g) { next_p.g = g; next_p.parant = cur_p; next_p.dir = next_dir; if (next_p.h == 0) { next_p.h = 10000 * this.CalH(next_x, next_y); } int f = next_p.h + next_p.g; this.m_open_list.Push(new AStarFindPath.OpenItem(next_x, next_y, f)); } }
/// <summary> /// 生成路径 /// </summary> /// <param name="inflex_points">路径数组</param> /// <param name="range">与终点的距离</param> public void GenerateInflexPoint(ref ArrayList inflex_points, float range) { AStarFindPath.PointInfo cur_p = this.m_map[this.m_end_point.x][this.m_end_point.y]; if (cur_p == null) { inflex_points.Add(new Vector2((float)this.m_start_point.x, (float)this.m_start_point.y)); inflex_points.Add(new Vector2((float)this.m_start_point.x, (float)this.m_start_point.y)); } else { if (range > 0.001f) { Vector2 start_pos = new Vector2((float)this.m_start_point.x, (float)this.m_start_point.y); Vector2 end_pos = new Vector2((float)this.m_end_point.x, (float)this.m_end_point.y); Vector2 dir = end_pos - start_pos; if (dir.sqrMagnitude <= range * range) { inflex_points.Add(new Vector2((float)this.m_start_point.x, (float)this.m_start_point.y)); inflex_points.Add(new Vector2((float)this.m_start_point.x, (float)this.m_start_point.y)); return; } if (this.m_is_last_straight) { this.normalise(ref dir); float real_range = range; Vector2 cur_end_pos = default(Vector2); cur_end_pos.x = end_pos.x; cur_end_pos.y = end_pos.y; for (; ;) { dir *= real_range; cur_end_pos = end_pos - dir; cur_end_pos.x = (float)((int)cur_end_pos.x); cur_end_pos.y = (float)((int)cur_end_pos.y); if (Vector2.Distance(cur_end_pos, end_pos) <= range * range) { break; } real_range -= 0.3f; if (real_range < 0f) { cur_end_pos = end_pos; inflex_points.Add(new Vector2(cur_end_pos.x, cur_end_pos.y)); inflex_points.Add(new Vector2((float)this.m_start_point.x, (float)this.m_start_point.y)); return; } } inflex_points.Add(new Vector2(cur_end_pos.x, cur_end_pos.y)); inflex_points.Add(new Vector2((float)this.m_start_point.x, (float)this.m_start_point.y)); return; } AStarFindPath.PointInfo last_p = cur_p; while (cur_p != null) { float tmp_x = (float)cur_p.x + 0.5f - (float)this.m_end_point.x; float tmp_y = (float)cur_p.y + 0.5f - (float)this.m_end_point.y; if (range * range < tmp_x * tmp_x + tmp_y * tmp_y) { break; } last_p = cur_p; cur_p = cur_p.parant; } cur_p = last_p; } int cur_dir = -1; while (cur_p != null) { if (cur_p.dir != cur_dir || (cur_p.x == this.m_start_point.x && cur_p.y == this.m_start_point.y)) { inflex_points.Add(new Vector2((float)cur_p.x + 0.5f, (float)cur_p.y + 0.5f)); cur_dir = cur_p.dir; } cur_p = cur_p.parant; } } }