Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a fill_region method to the AStarGrid2D #79495

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions core/math/a_star_grid_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,38 @@ real_t AStarGrid2D::get_point_weight_scale(const Vector2i &p_id) const {
return GET_POINT_UNCHECKED(p_id).weight_scale;
}

void AStarGrid2D::fill_solid_region(const Rect2i &p_region, bool p_solid) {
Chaosus marked this conversation as resolved.
Show resolved Hide resolved
ERR_FAIL_COND_MSG(dirty, "Grid is not initialized. Call the update method.");

Rect2i safe_region = p_region.intersection(region);
int from_x = safe_region.get_position().x;
int from_y = safe_region.get_position().y;
int end_x = safe_region.get_end().x;
int end_y = safe_region.get_end().y;

for (int x = from_x; x < end_x; x++) {
for (int y = from_y; y < end_y; y++) {
GET_POINT_UNCHECKED(Vector2i(x, y)).solid = p_solid;
}
}
}

void AStarGrid2D::fill_weight_scale_region(const Rect2i &p_region, real_t p_weight_scale) {
Chaosus marked this conversation as resolved.
Show resolved Hide resolved
ERR_FAIL_COND_MSG(dirty, "Grid is not initialized. Call the update method.");
ERR_FAIL_COND_MSG(p_weight_scale < 0.0, vformat("Can't set point's weight scale less than 0.0: %f.", p_weight_scale));

Rect2i safe_region = p_region.intersection(region);
int from_x = safe_region.get_position().x;
int from_y = safe_region.get_position().y;
int end_x = safe_region.get_end().x;
int end_y = safe_region.get_end().y;
for (int x = from_x; x < end_x; x++) {
for (int y = from_y; y < end_y; y++) {
GET_POINT_UNCHECKED(Vector2i(x, y)).weight_scale = p_weight_scale;
}
}
}

AStarGrid2D::Point *AStarGrid2D::_jump(Point *p_from, Point *p_to) {
if (!p_to || p_to->solid) {
return nullptr;
Expand Down Expand Up @@ -606,6 +638,8 @@ void AStarGrid2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_point_solid", "id"), &AStarGrid2D::is_point_solid);
ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStarGrid2D::set_point_weight_scale);
ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStarGrid2D::get_point_weight_scale);
ClassDB::bind_method(D_METHOD("fill_solid_region", "region", "solid"), &AStarGrid2D::fill_solid_region, DEFVAL(true));
ClassDB::bind_method(D_METHOD("fill_weight_scale_region", "region", "weight_scale"), &AStarGrid2D::fill_weight_scale_region);
ClassDB::bind_method(D_METHOD("clear"), &AStarGrid2D::clear);

ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStarGrid2D::get_point_position);
Expand Down
3 changes: 3 additions & 0 deletions core/math/a_star_grid_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ class AStarGrid2D : public RefCounted {
void set_point_weight_scale(const Vector2i &p_id, real_t p_weight_scale);
real_t get_point_weight_scale(const Vector2i &p_id) const;

void fill_solid_region(const Rect2i &p_region, bool p_solid = true);
void fill_weight_scale_region(const Rect2i &p_region, real_t p_weight_scale);

void clear();

Vector2 get_point_position(const Vector2i &p_id) const;
Expand Down
18 changes: 18 additions & 0 deletions doc/classes/AStarGrid2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@
Clears the grid and sets the [member region] to [code]Rect2i(0, 0, 0, 0)[/code].
</description>
</method>
<method name="fill_solid_region">
<return type="void" />
<param index="0" name="region" type="Rect2i" />
<param index="1" name="solid" type="bool" default="true" />
<description>
Fills the given [param region] on the grid with the specified value for the solid flag.
[b]Note:[/b] Calling [method update] is not needed after the call of this function.
</description>
</method>
<method name="fill_weight_scale_region">
<return type="void" />
<param index="0" name="region" type="Rect2i" />
<param index="1" name="weight_scale" type="float" />
<description>
Fills the given [param region] on the grid with the specified value for the weight scale.
[b]Note:[/b] Calling [method update] is not needed after the call of this function.
</description>
</method>
<method name="get_id_path">
<return type="Vector2i[]" />
<param index="0" name="from_id" type="Vector2i" />
Expand Down