Skip to content

Commit

Permalink
Merge pull request #76246 from Proggle/tilemap-rid-layer-report
Browse files Browse the repository at this point in the history
TileMap: Add method to fetch the layer for a given body
  • Loading branch information
akien-mga committed Apr 25, 2023
2 parents ae7872f + 093cb90 commit 0093660
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/classes/TileMap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@
Returns the coordinates of the tile for given physics body RID. Such RID can be retrieved from [method KinematicCollision2D.get_collider_rid], when colliding with a tile.
</description>
</method>
<method name="get_layer_for_body_rid">
<return type="int" />
<param index="0" name="body" type="RID" />
<description>
Returns the tilemap layer of the tile for given physics body RID. Such RID can be retrieved from [method KinematicCollision2D.get_collider_rid], when colliding with a tile.
</description>
</method>
<method name="get_layer_modulate" qualifiers="const">
<return type="Color" />
<param index="0" name="layer" type="int" />
Expand Down
8 changes: 8 additions & 0 deletions scene/2d/tile_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,7 @@ void TileMap::_physics_update_dirty_quadrants(SelfList<TileMapQuadrant>::List &r
// Create the body.
RID body = ps->body_create();
bodies_coords[body] = E_cell;
bodies_layers[body] = q.layer;
ps->body_set_mode(body, collision_animatable ? PhysicsServer2D::BODY_MODE_KINEMATIC : PhysicsServer2D::BODY_MODE_STATIC);
ps->body_set_space(body, space);

Expand Down Expand Up @@ -1692,6 +1693,7 @@ void TileMap::_physics_cleanup_quadrant(TileMapQuadrant *p_quadrant) {
ERR_FAIL_NULL(PhysicsServer2D::get_singleton());
for (RID body : p_quadrant->bodies) {
bodies_coords.erase(body);
bodies_layers.erase(body);
PhysicsServer2D::get_singleton()->free(body);
}
p_quadrant->bodies.clear();
Expand Down Expand Up @@ -2895,6 +2897,11 @@ Vector2i TileMap::get_coords_for_body_rid(RID p_physics_body) {
return bodies_coords[p_physics_body];
}

int TileMap::get_layer_for_body_rid(RID p_physics_body) {
ERR_FAIL_COND_V_MSG(!bodies_layers.has(p_physics_body), int(), vformat("No tiles for the given body RID %d.", p_physics_body));
return bodies_layers[p_physics_body];
}

void TileMap::fix_invalid_tiles() {
ERR_FAIL_COND_MSG(tile_set.is_null(), "Cannot fix invalid tiles if Tileset is not open.");

Expand Down Expand Up @@ -4154,6 +4161,7 @@ void TileMap::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_cell_tile_data", "layer", "coords", "use_proxies"), &TileMap::get_cell_tile_data, DEFVAL(false));

ClassDB::bind_method(D_METHOD("get_coords_for_body_rid", "body"), &TileMap::get_coords_for_body_rid);
ClassDB::bind_method(D_METHOD("get_layer_for_body_rid", "body"), &TileMap::get_layer_for_body_rid);

ClassDB::bind_method(D_METHOD("get_pattern", "layer", "coords_array"), &TileMap::get_pattern);
ClassDB::bind_method(D_METHOD("map_pattern", "position_in_tilemap", "coords_in_pattern", "pattern"), &TileMap::map_pattern);
Expand Down
4 changes: 4 additions & 0 deletions scene/2d/tile_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ class TileMap : public Node2D {

// Mapping for RID to coords.
HashMap<RID, Vector2i> bodies_coords;
// Mapping for RID to tile layer.
HashMap<RID, int> bodies_layers;

// Quadrants and internals management.
Vector2i _coords_to_quadrant_coords(int p_layer, const Vector2i &p_coords) const;
Expand Down Expand Up @@ -396,6 +398,8 @@ class TileMap : public Node2D {

// For finding tiles from collision.
Vector2i get_coords_for_body_rid(RID p_physics_body);
// For getting their layers as well.
int get_layer_for_body_rid(RID p_physics_body);

// Fixing and clearing methods.
void fix_invalid_tiles();
Expand Down

0 comments on commit 0093660

Please sign in to comment.