Skip to content

Commit

Permalink
Don't force-cleanup already cleaned up TileMap cells
Browse files Browse the repository at this point in the history
  • Loading branch information
kleonc committed Aug 23, 2024
1 parent a1acd38 commit 2733968
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions scene/2d/tile_map_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vector2i, Ref<RenderingQuadrant>> &kv : rendering_quadrant_map) {
Expand All @@ -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<Vector2i, CellData> &kv : tile_map_layer_data) {
CellData &cell_data = kv.value;
Expand Down Expand Up @@ -412,9 +413,11 @@ void TileMapLayer::_rendering_update(bool p_force_cleanup) {

// ----------- Occluders processing -----------
if (forced_cleanup) {
// Clean everything.
for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
_rendering_occluders_clear_cell(kv.value);
if (!_rendering_was_cleaned_up) {
// Clean everything.
for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
_rendering_occluders_clear_cell(kv.value);
}
}
} else {
if (_rendering_was_cleaned_up || dirty.flags[DIRTY_FLAGS_TILE_SET]) {
Expand Down Expand Up @@ -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<Vector2i, CellData> &kv : tile_map_layer_data) {
_physics_clear_cell(kv.value);
if (!_physics_was_cleaned_up) {
// Clean everything.
for (KeyValue<Vector2i, CellData> &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]) {
Expand Down Expand Up @@ -967,9 +972,11 @@ void TileMapLayer::_navigation_update(bool p_force_cleanup) {

// ----------- Navigation regions processing -----------
if (forced_cleanup) {
// Clean everything.
for (KeyValue<Vector2i, CellData> &kv : tile_map_layer_data) {
_navigation_clear_cell(kv.value);
if (!_navigation_was_cleaned_up) {
// Clean everything.
for (KeyValue<Vector2i, CellData> &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]) {
Expand Down Expand Up @@ -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<Vector2i, CellData> &kv : tile_map_layer_data) {
_scenes_clear_cell(kv.value);
if (!_scenes_was_cleaned_up) {
// Clean everything.
for (KeyValue<Vector2i, CellData> &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]) {
Expand Down Expand Up @@ -1670,21 +1679,15 @@ void TileMapLayer::_internal_update(bool p_force_cleanup) {
dirty.flags[i] = false;
}

// List the cells to delete definitely.
Vector<Vector2i> to_delete;
// Remove cells that are empty after the cleanup.
for (SelfList<CellData> *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();

Expand Down

0 comments on commit 2733968

Please sign in to comment.