From 2733968612880cf6196a5caef3dbcca53fda4cbc Mon Sep 17 00:00:00 2001 From: kleonc <9283098+kleonc@users.noreply.github.com> Date: Fri, 23 Aug 2024 13:09:40 +0200 Subject: [PATCH] Don't force-cleanup already cleaned up TileMap cells --- scene/2d/tile_map_layer.cpp | 47 ++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/scene/2d/tile_map_layer.cpp b/scene/2d/tile_map_layer.cpp index 7b125a689598..19c3b74a3cd4 100644 --- a/scene/2d/tile_map_layer.cpp +++ b/scene/2d/tile_map_layer.cpp @@ -213,6 +213,7 @@ void TileMapLayer::_rendering_update(bool p_force_cleanup) { (is_y_sort_enabled() && (dirty.flags[DIRTY_FLAGS_LAYER_Y_SORT_ORIGIN] || dirty.flags[DIRTY_FLAGS_LAYER_X_DRAW_ORDER_REVERSED] || dirty.flags[DIRTY_FLAGS_LAYER_LOCAL_TRANSFORM])) || (!is_y_sort_enabled() && dirty.flags[DIRTY_FLAGS_LAYER_RENDERING_QUADRANT_SIZE]); + bool quadrants_were_cleaned_up = false; // Free all quadrants. if (forced_cleanup || quadrant_shape_changed) { for (const KeyValue> &kv : rendering_quadrant_map) { @@ -224,12 +225,12 @@ void TileMapLayer::_rendering_update(bool p_force_cleanup) { kv.value->cells.clear(); } rendering_quadrant_map.clear(); - _rendering_was_cleaned_up = true; + quadrants_were_cleaned_up = true; } if (!forced_cleanup) { // List all quadrants to update, recreating them if needed. - if (dirty.flags[DIRTY_FLAGS_TILE_SET] || dirty.flags[DIRTY_FLAGS_LAYER_IN_TREE] || _rendering_was_cleaned_up) { + if (dirty.flags[DIRTY_FLAGS_TILE_SET] || dirty.flags[DIRTY_FLAGS_LAYER_IN_TREE] || _rendering_was_cleaned_up || quadrants_were_cleaned_up) { // Update all cells. for (KeyValue &kv : tile_map_layer_data) { CellData &cell_data = kv.value; @@ -412,9 +413,11 @@ void TileMapLayer::_rendering_update(bool p_force_cleanup) { // ----------- Occluders processing ----------- if (forced_cleanup) { - // Clean everything. - for (KeyValue &kv : tile_map_layer_data) { - _rendering_occluders_clear_cell(kv.value); + if (!_rendering_was_cleaned_up) { + // Clean everything. + for (KeyValue &kv : tile_map_layer_data) { + _rendering_occluders_clear_cell(kv.value); + } } } else { if (_rendering_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_SET]) { @@ -688,9 +691,11 @@ void TileMapLayer::_physics_update(bool p_force_cleanup) { // Check if we should cleanup everything. bool forced_cleanup = p_force_cleanup || !enabled || !collision_enabled || !is_inside_tree() || tile_set.is_null(); if (forced_cleanup) { - // Clean everything. - for (KeyValue &kv : tile_map_layer_data) { - _physics_clear_cell(kv.value); + if (!_physics_was_cleaned_up) { + // Clean everything. + for (KeyValue &kv : tile_map_layer_data) { + _physics_clear_cell(kv.value); + } } } else { if (_physics_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_SET] || dirty.flags[DIRTY_FLAGS_LAYER_USE_KINEMATIC_BODIES] || dirty.flags[DIRTY_FLAGS_LAYER_IN_TREE]) { @@ -967,9 +972,11 @@ void TileMapLayer::_navigation_update(bool p_force_cleanup) { // ----------- Navigation regions processing ----------- if (forced_cleanup) { - // Clean everything. - for (KeyValue &kv : tile_map_layer_data) { - _navigation_clear_cell(kv.value); + if (!_navigation_was_cleaned_up) { + // Clean everything. + for (KeyValue &kv : tile_map_layer_data) { + _navigation_clear_cell(kv.value); + } } } else { if (_navigation_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_SET] || dirty.flags[DIRTY_FLAGS_LAYER_IN_TREE] || dirty.flags[DIRTY_FLAGS_LAYER_NAVIGATION_MAP]) { @@ -1212,9 +1219,11 @@ void TileMapLayer::_scenes_update(bool p_force_cleanup) { bool forced_cleanup = p_force_cleanup || !enabled || !is_inside_tree() || tile_set.is_null(); if (forced_cleanup) { - // Clean everything. - for (KeyValue &kv : tile_map_layer_data) { - _scenes_clear_cell(kv.value); + if (!_scenes_was_cleaned_up) { + // Clean everything. + for (KeyValue &kv : tile_map_layer_data) { + _scenes_clear_cell(kv.value); + } } } else { if (_scenes_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_SET] || dirty.flags[DIRTY_FLAGS_LAYER_IN_TREE]) { @@ -1670,21 +1679,15 @@ void TileMapLayer::_internal_update(bool p_force_cleanup) { dirty.flags[i] = false; } - // List the cells to delete definitely. - Vector to_delete; + // Remove cells that are empty after the cleanup. for (SelfList *cell_data_list_element = dirty.cell_list.first(); cell_data_list_element; cell_data_list_element = cell_data_list_element->next()) { CellData &cell_data = *cell_data_list_element->self(); // Select the cell from tile_map if it is invalid. if (cell_data.cell.source_id == TileSet::INVALID_SOURCE) { - to_delete.push_back(cell_data.coords); + tile_map_layer_data.erase(cell_data.coords); } } - // Remove cells that are empty after the cleanup. - for (const Vector2i &coords : to_delete) { - tile_map_layer_data.erase(coords); - } - // Clear the dirty cells list. dirty.cell_list.clear();