Merge pull request #91868 from Chaosus/astargrid_get_point_positions_in_region

Add a method to retrieve all points within a region to `AStarGrid2D`
This commit is contained in:
Rémi Verschelde 2024-08-28 00:10:42 +02:00
commit 9c7cf5440c
No known key found for this signature in database
GPG Key ID: C3336907360768E1
3 changed files with 36 additions and 0 deletions

View File

@ -562,6 +562,33 @@ Vector2 AStarGrid2D::get_point_position(const Vector2i &p_id) const {
return _get_point_unchecked(p_id)->pos; return _get_point_unchecked(p_id)->pos;
} }
TypedArray<Dictionary> AStarGrid2D::get_point_data_in_region(const Rect2i &p_region) const {
ERR_FAIL_COND_V_MSG(dirty, TypedArray<Dictionary>(), "Grid is not initialized. Call the update method.");
const Rect2i inter_region = region.intersection(p_region);
const int32_t start_x = inter_region.position.x - region.position.x;
const int32_t start_y = inter_region.position.y - region.position.y;
const int32_t end_x = inter_region.get_end().x - region.position.x;
const int32_t end_y = inter_region.get_end().y - region.position.y;
TypedArray<Dictionary> data;
for (int32_t y = start_y; y < end_y; y++) {
for (int32_t x = start_x; x < end_x; x++) {
const Point &p = points[y][x];
Dictionary dict;
dict["id"] = p.id;
dict["position"] = p.pos;
dict["solid"] = p.solid;
dict["weight_scale"] = p.weight_scale;
data.push_back(dict);
}
}
return data;
}
Vector<Vector2> AStarGrid2D::get_point_path(const Vector2i &p_from_id, const Vector2i &p_to_id, bool p_allow_partial_path) { Vector<Vector2> AStarGrid2D::get_point_path(const Vector2i &p_from_id, const Vector2i &p_to_id, bool p_allow_partial_path) {
ERR_FAIL_COND_V_MSG(dirty, Vector<Vector2>(), "Grid is not initialized. Call the update method."); ERR_FAIL_COND_V_MSG(dirty, Vector<Vector2>(), "Grid is not initialized. Call the update method.");
ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_from_id), Vector<Vector2>(), vformat("Can't get id path. Point %s out of bounds %s.", p_from_id, region)); ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_from_id), Vector<Vector2>(), vformat("Can't get id path. Point %s out of bounds %s.", p_from_id, region));
@ -698,6 +725,7 @@ void AStarGrid2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("clear"), &AStarGrid2D::clear); 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_position", "id"), &AStarGrid2D::get_point_position);
ClassDB::bind_method(D_METHOD("get_point_data_in_region", "region"), &AStarGrid2D::get_point_data_in_region);
ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id", "allow_partial_path"), &AStarGrid2D::get_point_path, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id", "allow_partial_path"), &AStarGrid2D::get_point_path, DEFVAL(false));
ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id", "allow_partial_path"), &AStarGrid2D::get_id_path, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id", "allow_partial_path"), &AStarGrid2D::get_id_path, DEFVAL(false));

View File

@ -209,6 +209,7 @@ public:
void clear(); void clear();
Vector2 get_point_position(const Vector2i &p_id) const; Vector2 get_point_position(const Vector2i &p_id) const;
TypedArray<Dictionary> get_point_data_in_region(const Rect2i &p_region) const;
Vector<Vector2> get_point_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false); Vector<Vector2> get_point_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false);
TypedArray<Vector2i> get_id_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false); TypedArray<Vector2i> get_id_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false);
}; };

View File

@ -81,6 +81,13 @@
If there is no valid path to the target, and [param allow_partial_path] is [code]true[/code], returns a path to the point closest to the target that can be reached. If there is no valid path to the target, and [param allow_partial_path] is [code]true[/code], returns a path to the point closest to the target that can be reached.
</description> </description>
</method> </method>
<method name="get_point_data_in_region" qualifiers="const">
<return type="Dictionary[]" />
<param index="0" name="region" type="Rect2i" />
<description>
Returns an array of dictionaries with point data ([code]id[/code]: [Vector2i], [code]position[/code]: [Vector2], [code]solid[/code]: [bool], [code]weight_scale[/code]: [float]) within a [param region].
</description>
</method>
<method name="get_point_path"> <method name="get_point_path">
<return type="PackedVector2Array" /> <return type="PackedVector2Array" />
<param index="0" name="from_id" type="Vector2i" /> <param index="0" name="from_id" type="Vector2i" />