From cc6e89c003cb4d3235e372072fb0f9a2c1f58b1e Mon Sep 17 00:00:00 2001 From: Michal Pikulski Date: Wed, 15 Oct 2025 23:20:38 +0200 Subject: [PATCH] Fix issue with event triggering multiple times at the end of the minigame --- .../Tiles/TrenchTileSpawner.cs | 167 +----------------- Assets/Settings/DivingMinigameSettings.asset | 6 +- 2 files changed, 9 insertions(+), 164 deletions(-) diff --git a/Assets/Scripts/Minigames/DivingForPictures/Tiles/TrenchTileSpawner.cs b/Assets/Scripts/Minigames/DivingForPictures/Tiles/TrenchTileSpawner.cs index 96a0c99f..9cc88950 100644 --- a/Assets/Scripts/Minigames/DivingForPictures/Tiles/TrenchTileSpawner.cs +++ b/Assets/Scripts/Minigames/DivingForPictures/Tiles/TrenchTileSpawner.cs @@ -545,167 +545,6 @@ namespace Minigames.DivingForPictures _movementCoroutine = null; } - /// - /// Coroutine that checks for tiles to destroy periodically - /// - private IEnumerator TileDestructionRoutine() - { - const float checkInterval = 0.5f; // Check every half second - Logging.Debug($"[TrenchTileSpawner] Started tile destruction coroutine with interval: {checkInterval}s"); - - while (enabled && gameObject.activeInHierarchy && !_isPaused) - { - // Check and handle tile destruction - if (_activeTiles.Count > 0) - { - GameObject topTile = _activeTiles[0]; - if (topTile == null) - { - _activeTiles.RemoveAt(0); - } - else - { - float tileHeight = GetTileHeight(topTile); - - bool shouldDestroy; - if (_isSurfacing) - { - // When surfacing, destroy tiles at the bottom - shouldDestroy = topTile.transform.position.y + tileHeight / 2 < _screenBottom - _settings.TileSpawnBuffer; - } - else - { - // When descending, destroy tiles at the top - shouldDestroy = topTile.transform.position.y - tileHeight / 2 > _screenTop + _settings.TileSpawnBuffer; - } - - if (shouldDestroy) - { - _activeTiles.RemoveAt(0); - onTileDestroyed?.Invoke(topTile); - - if (_devSettings != null && _devSettings.TrenchTileUseObjectPooling && _tilePool != null) - { - // Find the prefab index for this tile - int prefabIndex = GetPrefabIndex(topTile); - if (prefabIndex >= 0) - { - _tilePool.ReturnTile(topTile, prefabIndex); - } - else - { - Destroy(topTile); - } - } - else - { - Destroy(topTile); - } - } - } - } - - // Wait for the next check interval - yield return new WaitForSeconds(checkInterval); - } - - // Clear coroutine reference when stopped - _tileDestructionCoroutine = null; - } - - /// - /// Coroutine that checks if new tiles need to be spawned periodically - /// - private IEnumerator TileSpawningRoutine() - { - const float checkInterval = 0.2f; // Check every fifth of a second - Logging.Debug($"[TrenchTileSpawner] Started tile spawning coroutine with interval: {checkInterval}s"); - - while (enabled && gameObject.activeInHierarchy && !_isPaused && !_stopSpawning) - { - // Check if we need to spawn new tiles - if (_activeTiles.Count == 0) - { - // If we have no active tiles and spawning is stopped, trigger the event - if (_stopSpawning) - { - onLastTileLeft.Invoke(); - } - } - else - { - GameObject bottomTile = _activeTiles[^1]; - if (bottomTile == null) - { - _activeTiles.RemoveAt(_activeTiles.Count - 1); - } - else - { - // Get the tile height once to use in all calculations - float tileHeight = GetTileHeight(bottomTile); - - // If we're in stop spawning mode, check if last tile is leaving - if (_stopSpawning) - { - // Check if this is the last tile, and if it's about to leave the screen - if (_activeTiles.Count == 1) - { - bool isLastTileLeaving; - - if (_isSurfacing) - { - // When surfacing, check if bottom of tile is above top of screen - isLastTileLeaving = bottomTile.transform.position.y - tileHeight / 2 > _screenTop + _settings.TileSpawnBuffer; - } - else - { - // When descending, check if top of tile is below bottom of screen - isLastTileLeaving = bottomTile.transform.position.y + tileHeight / 2 < _screenBottom - _settings.TileSpawnBuffer; - } - - if (isLastTileLeaving) - { - onLastTileLeft.Invoke(); - } - } - } - else - { - // Normal spawning mode - bool shouldSpawn; - float newY; - - if (_isSurfacing) - { - // When surfacing, spawn new tiles at the top - float topEdge = bottomTile.transform.position.y + tileHeight / 2; - shouldSpawn = topEdge < _screenTop + _settings.TileSpawnBuffer; - newY = bottomTile.transform.position.y + tileHeight; - } - else - { - // When descending, spawn new tiles at the bottom - float bottomEdge = bottomTile.transform.position.y - tileHeight / 2; - shouldSpawn = bottomEdge > _screenBottom - _settings.TileSpawnBuffer; - newY = bottomTile.transform.position.y - tileHeight; - } - - if (shouldSpawn) - { - SpawnTileAtY(newY); - } - } - } - } - - // Wait for the next check interval - yield return new WaitForSeconds(checkInterval); - } - - // Clear coroutine reference when stopped - _tileSpawningCoroutine = null; - } - /// /// Coroutine that handles increasing the movement speed over time /// @@ -1260,6 +1099,9 @@ namespace Minigames.DivingForPictures if (_stopSpawning) { onLastTileLeft.Invoke(); + // Stop the coroutine since all tiles are gone and no new ones will spawn + Logging.Debug("[TrenchTileSpawner] No active tiles and spawning stopped - ending tile spawning coroutine"); + yield break; } } else @@ -1296,6 +1138,9 @@ namespace Minigames.DivingForPictures if (isLastTileLeaving) { onLastTileLeft.Invoke(); + // Stop the coroutine since the last tile has left and no new ones will spawn + Logging.Debug("[TrenchTileSpawner] Last tile left screen and spawning stopped - ending tile spawning coroutine"); + yield break; } } } diff --git a/Assets/Settings/DivingMinigameSettings.asset b/Assets/Settings/DivingMinigameSettings.asset index f0360f3c..8fe16611 100644 --- a/Assets/Settings/DivingMinigameSettings.asset +++ b/Assets/Settings/DivingMinigameSettings.asset @@ -50,8 +50,8 @@ MonoBehaviour: smoothMoveSpeed: 8 blockInputDuringBump: 0 viewfinderPrefab: {fileID: 3191012273289593430, guid: 99666bddf27a652479c2a3e0007a94dc, type: 3} - viewfinderShrinkDuration: 2 - viewfinderMoveSpeed: 2 + viewfinderShrinkDuration: 1 + viewfinderMoveSpeed: 3 viewfinderShrinkCurve: serializedVersion: 2 m_Curve: @@ -77,7 +77,7 @@ MonoBehaviour: m_PostInfinity: 2 m_RotationOrder: 4 photoInputMode: 1 - paddingFactor: 2 + paddingFactor: 1 minSizePercent: 0.15 maxSizePercent: 1 viewfinderStartScale: 1