diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2023-02-13 12:58:39 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2023-02-13 12:58:39 +0100 |
commit | 848c910227fa7be2da7285ea5162597bf13adc15 (patch) | |
tree | 5443e2ddd9ad5166d306a72c043f7e7de2ad8924 /scene/2d | |
parent | c144919a7415e6935f48b521f70d2a727d5a9606 (diff) | |
parent | f2c31965c04f17f05d534646ffb5056237a5cdde (diff) |
Merge pull request #73018 from smix8/fix_multilayered_tilemap_navigation_4.x
Fix navigation support for multilayered TileMaps
Diffstat (limited to 'scene/2d')
-rw-r--r-- | scene/2d/tile_map.cpp | 58 | ||||
-rw-r--r-- | scene/2d/tile_map.h | 6 |
2 files changed, 63 insertions, 1 deletions
diff --git a/scene/2d/tile_map.cpp b/scene/2d/tile_map.cpp index a4bcac2e3b..2e0b16388f 100644 --- a/scene/2d/tile_map.cpp +++ b/scene/2d/tile_map.cpp @@ -752,6 +752,20 @@ TileMap::VisibilityMode TileMap::get_navigation_visibility_mode() { return navigation_visibility_mode; } +void TileMap::set_navigation_map(int p_layer, RID p_map) { + ERR_FAIL_INDEX(p_layer, (int)layers.size()); + + layers[p_layer].navigation_map = p_map; +} + +RID TileMap::get_navigation_map(int p_layer) const { + ERR_FAIL_INDEX_V(p_layer, (int)layers.size(), RID()); + if (layers[p_layer].navigation_map.is_valid()) { + return layers[p_layer].navigation_map; + } + return RID(); +} + void TileMap::set_y_sort_enabled(bool p_enable) { Node2D::set_y_sort_enabled(p_enable); _clear_internals(); @@ -897,6 +911,9 @@ void TileMap::_recreate_layer_internals(int p_layer) { // Update the layer internals. _rendering_update_layer(p_layer); + // Update the layer internal navigation maps. + _navigation_update_layer(p_layer); + // Recreate the quadrants. const HashMap<Vector2i, TileMapCell> &tile_map = layers[p_layer].tile_map; for (const KeyValue<Vector2i, TileMapCell> &E : tile_map) { @@ -959,6 +976,9 @@ void TileMap::_clear_layer_internals(int p_layer) { // Clear the layers internals. _rendering_cleanup_layer(p_layer); + // Clear the layers internal navigation maps. + _navigation_cleanup_layer(p_layer); + // Clear the dirty quadrants list. while (layers[p_layer].dirty_quadrant_list.first()) { layers[p_layer].dirty_quadrant_list.remove(layers[p_layer].dirty_quadrant_list.first()); @@ -1083,6 +1103,36 @@ void TileMap::_rendering_notification(int p_what) { } } +void TileMap::_navigation_update_layer(int p_layer) { + ERR_FAIL_INDEX(p_layer, (int)layers.size()); + ERR_FAIL_NULL(NavigationServer2D::get_singleton()); + + if (!layers[p_layer].navigation_map.is_valid()) { + if (p_layer == 0 && is_inside_tree()) { + // Use the default World2D navigation map for the first layer when empty. + layers[p_layer].navigation_map = get_world_2d()->get_navigation_map(); + } else { + RID new_layer_map = NavigationServer2D::get_singleton()->map_create(); + NavigationServer2D::get_singleton()->map_set_active(new_layer_map, true); + layers[p_layer].navigation_map = new_layer_map; + } + } +} + +void TileMap::_navigation_cleanup_layer(int p_layer) { + ERR_FAIL_INDEX(p_layer, (int)layers.size()); + ERR_FAIL_NULL(NavigationServer2D::get_singleton()); + + if (layers[p_layer].navigation_map.is_valid()) { + if (is_inside_tree() && layers[p_layer].navigation_map == get_world_2d()->get_navigation_map()) { + // Do not delete the World2D default navigation map. + return; + } + NavigationServer2D::get_singleton()->free(layers[p_layer].navigation_map); + layers[p_layer].navigation_map = RID(); + } +} + void TileMap::_rendering_update_layer(int p_layer) { ERR_FAIL_INDEX(p_layer, (int)layers.size()); @@ -1732,6 +1782,9 @@ void TileMap::_navigation_update_dirty_quadrants(SelfList<TileMapQuadrant>::List q.navigation_regions[E_cell].resize(tile_set->get_navigation_layers_count()); for (int layer_index = 0; layer_index < tile_set->get_navigation_layers_count(); layer_index++) { + if (layer_index >= (int)layers.size() || !layers[layer_index].navigation_map.is_valid()) { + continue; + } Ref<NavigationPolygon> navigation_polygon; navigation_polygon = tile_data->get_navigation_polygon(layer_index); @@ -1741,7 +1794,7 @@ void TileMap::_navigation_update_dirty_quadrants(SelfList<TileMapQuadrant>::List RID region = NavigationServer2D::get_singleton()->region_create(); NavigationServer2D::get_singleton()->region_set_owner_id(region, get_instance_id()); - NavigationServer2D::get_singleton()->region_set_map(region, get_world_2d()->get_navigation_map()); + NavigationServer2D::get_singleton()->region_set_map(region, layers[layer_index].navigation_map); NavigationServer2D::get_singleton()->region_set_transform(region, tilemap_xform * tile_transform); NavigationServer2D::get_singleton()->region_set_navigation_layers(region, tile_set->get_navigation_layer_layers(layer_index)); NavigationServer2D::get_singleton()->region_set_navigation_polygon(region, navigation_polygon); @@ -4051,6 +4104,9 @@ void TileMap::_bind_methods() { ClassDB::bind_method(D_METHOD("set_navigation_visibility_mode", "navigation_visibility_mode"), &TileMap::set_navigation_visibility_mode); ClassDB::bind_method(D_METHOD("get_navigation_visibility_mode"), &TileMap::get_navigation_visibility_mode); + ClassDB::bind_method(D_METHOD("set_navigation_map", "layer", "map"), &TileMap::set_navigation_map); + ClassDB::bind_method(D_METHOD("get_navigation_map", "layer"), &TileMap::get_navigation_map); + ClassDB::bind_method(D_METHOD("set_cell", "layer", "coords", "source_id", "atlas_coords", "alternative_tile"), &TileMap::set_cell, DEFVAL(TileSet::INVALID_SOURCE), DEFVAL(TileSetSource::INVALID_ATLAS_COORDS), DEFVAL(0)); ClassDB::bind_method(D_METHOD("erase_cell", "layer", "coords"), &TileMap::erase_cell); ClassDB::bind_method(D_METHOD("get_cell_source_id", "layer", "coords", "use_proxies"), &TileMap::get_cell_source_id, DEFVAL(false)); diff --git a/scene/2d/tile_map.h b/scene/2d/tile_map.h index 7cf2a2eded..62d7612be2 100644 --- a/scene/2d/tile_map.h +++ b/scene/2d/tile_map.h @@ -211,6 +211,7 @@ private: HashMap<Vector2i, TileMapCell> tile_map; HashMap<Vector2i, TileMapQuadrant> quadrant_map; SelfList<TileMapQuadrant>::List dirty_quadrant_list; + RID navigation_map; }; LocalVector<TileMapLayer> layers; int selected_layer = -1; @@ -259,6 +260,8 @@ private: void _physics_draw_quadrant_debug(TileMapQuadrant *p_quadrant); void _navigation_notification(int p_what); + void _navigation_update_layer(int p_layer); + void _navigation_cleanup_layer(int p_layer); void _navigation_update_dirty_quadrants(SelfList<TileMapQuadrant>::List &r_dirty_quadrant_list); void _navigation_cleanup_quadrant(TileMapQuadrant *p_quadrant); void _navigation_draw_quadrant_debug(TileMapQuadrant *p_quadrant); @@ -339,6 +342,9 @@ public: void set_navigation_visibility_mode(VisibilityMode p_show_navigation); VisibilityMode get_navigation_visibility_mode(); + void set_navigation_map(int p_layer, RID p_map); + RID get_navigation_map(int p_layer) const; + // Cells accessors. void set_cell(int p_layer, const Vector2i &p_coords, int p_source_id = TileSet::INVALID_SOURCE, const Vector2i p_atlas_coords = TileSetSource::INVALID_ATLAS_COORDS, int p_alternative_tile = 0); void erase_cell(int p_layer, const Vector2i &p_coords); |