From e047f7bea1c6544396f45c0392d0faf0b996692b Mon Sep 17 00:00:00 2001 From: Sergey Lapin Date: Sat, 27 Jun 2026 02:44:55 +0300 Subject: [PATCH] Fixed terrain cleanup --- src/features/editScene/TerrainRequirements.md | 7 +++++++ src/features/editScene/systems/TerrainSystem.cpp | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/src/features/editScene/TerrainRequirements.md b/src/features/editScene/TerrainRequirements.md index 72b4cc1..c0d4171 100644 --- a/src/features/editScene/TerrainRequirements.md +++ b/src/features/editScene/TerrainRequirements.md @@ -1219,6 +1219,13 @@ draw off by default with toggle; removing and re-adding terrain is leak-free. ### Milestone 3 — Serialization + heightmap editing + splat painting - Scene serialization for `TerrainComponent` (save/load JSON round-trip). + +**Terrain cleanup on entity/component removal** (added post-M2): +`TerrainSystem::update()` now tracks whether any matching terrain entity exists. +If `m_active` is true but the query returns no entity (because the terrain +entity was deleted or its TerrainComponent was removed), `deactivate()` is +called automatically. Without this, removing the component or deleting the +entity would leave the terrain and colliders orphaned in the scene. - Binary heightmap file save/load alongside scene JSON. - Brush tools (raise/lower/smooth/flatten) with curve-based brush profile. - Splat paint brush (select layer, set opacity, paint blend values via diff --git a/src/features/editScene/systems/TerrainSystem.cpp b/src/features/editScene/systems/TerrainSystem.cpp index f795dda..faadd0e 100644 --- a/src/features/editScene/systems/TerrainSystem.cpp +++ b/src/features/editScene/systems/TerrainSystem.cpp @@ -411,9 +411,11 @@ void TerrainSystem::deactivate() void TerrainSystem::update(float /*deltaTime*/) { + bool found = false; m_terrainQuery.each([&](flecs::entity e, TerrainComponent &tc, TransformComponent &xform) { (void)e; + found = true; if (!tc.enabled && m_active) { deactivate(); return; @@ -422,6 +424,10 @@ void TerrainSystem::update(float /*deltaTime*/) activate(tc, xform); }); + /* If the terrain entity or its component was removed, tear down. */ + if (m_active && !found) + deactivate(); + if (m_active && mTerrainGroup) mTerrainGroup->update(false);