Skip to content

Commit

Permalink
Add a method to retrieve all points within a region to AStarGrid2D
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaosus committed May 12, 2024
1 parent bdc0316 commit c222cf9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
28 changes: 28 additions & 0 deletions core/math/a_star_grid_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,33 @@ Vector2 AStarGrid2D::get_point_position(const Vector2i &p_id) const {
return _get_point_unchecked(p_id)->pos;
}

TypedArray<Dictionary> AStarGrid2D::get_points_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) {
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));
Expand Down Expand Up @@ -694,6 +721,7 @@ void AStarGrid2D::_bind_methods() {
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_points_data_in_region", "region"), &AStarGrid2D::get_points_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_id_path", "from_id", "to_id", "allow_partial_path"), &AStarGrid2D::get_id_path, DEFVAL(false));

Expand Down
1 change: 1 addition & 0 deletions core/math/a_star_grid_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class AStarGrid2D : public RefCounted {
void clear();

Vector2 get_point_position(const Vector2i &p_id) const;
TypedArray<Dictionary> get_points_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);
TypedArray<Vector2i> get_id_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false);
};
Expand Down
7 changes: 7 additions & 0 deletions doc/classes/AStarGrid2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@
Returns the weight scale of the point associated with the given [param id].
</description>
</method>
<method name="get_points_data_in_region" qualifiers="const">
<return type="Dictionary[]" />
<param index="0" name="region" type="Rect2i" />
<description>
Returns an array of dictionaries with point data ([Vector2i] id, [Vector2] position, [bool] solid, [float] weight_scale) within a [param region].
</description>
</method>
<method name="is_dirty" qualifiers="const">
<return type="bool" />
<description>
Expand Down

0 comments on commit c222cf9

Please sign in to comment.