diff --git a/doc/classes/TileMap.xml b/doc/classes/TileMap.xml index 2dd6a1a54e0..ba9fc9a6f9b 100644 --- a/doc/classes/TileMap.xml +++ b/doc/classes/TileMap.xml @@ -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. + + + + + 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. + + diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp index dc8953b32c4..35f57607ecf 100644 --- a/scene/2d/tile_map.cpp +++ b/scene/2d/tile_map.cpp @@ -1637,6 +1637,7 @@ void TileMap::_physics_update_dirty_quadrants(SelfList::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); @@ -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(); @@ -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."); @@ -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); diff --git a/scene/2d/tile_map.h b/scene/2d/tile_map.h index 1f3c672f17d..3c135d1317a 100644 --- a/scene/2d/tile_map.h +++ b/scene/2d/tile_map.h @@ -219,6 +219,8 @@ private: // Mapping for RID to coords. HashMap bodies_coords; + // Mapping for RID to tile layer. + HashMap bodies_layers; // Quadrants and internals management. Vector2i _coords_to_quadrant_coords(int p_layer, const Vector2i &p_coords) const; @@ -396,6 +398,8 @@ public: // 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();