From bf19a1d3b3244dec21faf7d5a138f53980be73fa Mon Sep 17 00:00:00 2001 From: MisterMX Date: Fri, 30 Sep 2022 11:46:55 +0200 Subject: refactor(AStarGrid2D): Return Vector2i in get_id_path Signed-off-by: MisterMX --- core/math/a_star_grid_2d.cpp | 24 ++++++++++++------------ core/math/a_star_grid_2d.h | 2 +- 2 files changed, 13 insertions(+), 13 deletions(-) (limited to 'core/math') diff --git a/core/math/a_star_grid_2d.cpp b/core/math/a_star_grid_2d.cpp index ad67cfa852..c30acf32bb 100644 --- a/core/math/a_star_grid_2d.cpp +++ b/core/math/a_star_grid_2d.cpp @@ -30,6 +30,8 @@ #include "a_star_grid_2d.h" +#include "core/variant/typed_array.h" + static real_t heuristic_euclidian(const Vector2i &p_from, const Vector2i &p_to) { real_t dx = (real_t)ABS(p_to.x - p_from.x); real_t dy = (real_t)ABS(p_to.y - p_from.y); @@ -492,17 +494,17 @@ Vector AStarGrid2D::get_point_path(const Vector2i &p_from_id, const Vec return path; } -Vector AStarGrid2D::get_id_path(const Vector2i &p_from_id, const Vector2i &p_to_id) { - ERR_FAIL_COND_V_MSG(dirty, Vector(), "Grid is not initialized. Call the update method."); - ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_from_id), Vector(), vformat("Can't get id path. Point out of bounds (%s/%s, %s/%s)", p_from_id.x, size.width, p_from_id.y, size.height)); - ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_to_id), Vector(), vformat("Can't get id path. Point out of bounds (%s/%s, %s/%s)", p_to_id.x, size.width, p_to_id.y, size.height)); +TypedArray AStarGrid2D::get_id_path(const Vector2i &p_from_id, const Vector2i &p_to_id) { + ERR_FAIL_COND_V_MSG(dirty, TypedArray(), "Grid is not initialized. Call the update method."); + ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_from_id), TypedArray(), vformat("Can't get id path. Point out of bounds (%s/%s, %s/%s)", p_from_id.x, size.width, p_from_id.y, size.height)); + ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_to_id), TypedArray(), vformat("Can't get id path. Point out of bounds (%s/%s, %s/%s)", p_to_id.x, size.width, p_to_id.y, size.height)); Point *a = _get_point(p_from_id.x, p_from_id.y); Point *b = _get_point(p_to_id.x, p_to_id.y); if (a == b) { - Vector ret; - ret.push_back(Vector2((float)a->id.x, (float)a->id.y)); + TypedArray ret; + ret.push_back(a); return ret; } @@ -511,7 +513,7 @@ Vector AStarGrid2D::get_id_path(const Vector2i &p_from_id, const Vector bool found_route = _solve(begin_point, end_point); if (!found_route) { - return Vector(); + return TypedArray(); } Point *p = end_point; @@ -521,20 +523,18 @@ Vector AStarGrid2D::get_id_path(const Vector2i &p_from_id, const Vector p = p->prev_point; } - Vector path; + TypedArray path; path.resize(pc); { - Vector2 *w = path.ptrw(); - p = end_point; int64_t idx = pc - 1; while (p != begin_point) { - w[idx--] = Vector2((float)p->id.x, (float)p->id.y); + path[idx--] = p->id; p = p->prev_point; } - w[0] = p->id; + path[0] = p->id; } return path; diff --git a/core/math/a_star_grid_2d.h b/core/math/a_star_grid_2d.h index bf6363aa01..1002f18738 100644 --- a/core/math/a_star_grid_2d.h +++ b/core/math/a_star_grid_2d.h @@ -169,7 +169,7 @@ public: void clear(); Vector get_point_path(const Vector2i &p_from, const Vector2i &p_to); - Vector get_id_path(const Vector2i &p_from, const Vector2i &p_to); + TypedArray get_id_path(const Vector2i &p_from, const Vector2i &p_to); }; VARIANT_ENUM_CAST(AStarGrid2D::DiagonalMode); -- cgit v1.2.3