From 0bcbf8e00d2cc9febe1977aca560897553b141de Mon Sep 17 00:00:00 2001 From: Yuri Rubinsky Date: Tue, 20 Dec 2022 23:21:38 +0300 Subject: [PATCH] Add `get_point_position` method to `AStarGrid2D` --- core/math/a_star_grid_2d.cpp | 7 +++++++ core/math/a_star_grid_2d.h | 1 + doc/classes/AStarGrid2D.xml | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/core/math/a_star_grid_2d.cpp b/core/math/a_star_grid_2d.cpp index 8befda28e49..968102e3238 100644 --- a/core/math/a_star_grid_2d.cpp +++ b/core/math/a_star_grid_2d.cpp @@ -463,6 +463,12 @@ void AStarGrid2D::clear() { size = Vector2i(); } +Vector2 AStarGrid2D::get_point_position(const Vector2i &p_id) const { + ERR_FAIL_COND_V_MSG(dirty, Vector2(), "Grid is not initialized. Call the update method."); + ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_id), Vector2(), vformat("Can't get point's position. Point out of bounds (%s/%s, %s/%s).", p_id.x, size.width, p_id.y, size.height)); + return points[p_id.y][p_id.x].pos; +} + Vector AStarGrid2D::get_point_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)); @@ -580,6 +586,7 @@ void AStarGrid2D::_bind_methods() { ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStarGrid2D::get_point_weight_scale); ClassDB::bind_method(D_METHOD("clear"), &AStarGrid2D::clear); + ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStarGrid2D::get_point_position); ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStarGrid2D::get_point_path); ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStarGrid2D::get_id_path); diff --git a/core/math/a_star_grid_2d.h b/core/math/a_star_grid_2d.h index 2b81f47e123..3b39d46a200 100644 --- a/core/math/a_star_grid_2d.h +++ b/core/math/a_star_grid_2d.h @@ -172,6 +172,7 @@ public: void clear(); + Vector2 get_point_position(const Vector2i &p_id) const; Vector get_point_path(const Vector2i &p_from, const Vector2i &p_to); TypedArray get_id_path(const Vector2i &p_from, const Vector2i &p_to); }; diff --git a/doc/classes/AStarGrid2D.xml b/doc/classes/AStarGrid2D.xml index 916946775bd..cba783246f5 100644 --- a/doc/classes/AStarGrid2D.xml +++ b/doc/classes/AStarGrid2D.xml @@ -69,6 +69,13 @@ [b]Note:[/b] This method is not thread-safe. If called from a [Thread], it will return an empty [PackedVector3Array] and will print an error message. + + + + + Returns the position of the point associated with the given [param id]. + +