From e2409106d4992d2bece0036f93da0c307fd86b61 Mon Sep 17 00:00:00 2001 From: Jesus Castro Date: Fri, 19 Dec 2025 17:18:18 +0800 Subject: [PATCH] Fixed Bomb Spawning --- Assets/Scripts/Services/BombService.cs | 37 ++++++------------- Assets/Scripts/Services/GameBoardService.cs | 3 +- .../Services/Interfaces/IBombService.cs | 11 ++---- .../Services/Interfaces/IMatchService.cs | 4 +- 4 files changed, 20 insertions(+), 35 deletions(-) diff --git a/Assets/Scripts/Services/BombService.cs b/Assets/Scripts/Services/BombService.cs index 6b0d32d..aa7c771 100644 --- a/Assets/Scripts/Services/BombService.cs +++ b/Assets/Scripts/Services/BombService.cs @@ -19,7 +19,7 @@ namespace Services private Vector2Int lastSwapFrom; private Vector2Int lastSwapTo; - private BombSpawnRequest? pendingBombSpawn; + private List pendingBombSpawns = new List(); public BombService(GameVariables gameVariables, IGameBoard gameBoard) { this.gameVariables = gameVariables; @@ -51,10 +51,8 @@ namespace Services public List ApplyPendingBombSpawns(Action spawnGem) { List positions = new List(); - BombSpawnRequest? bombSpawnRequest = this.pendingBombSpawn; - if (bombSpawnRequest != null) { - BombSpawnRequest bombRequest = this.pendingBombSpawn.GetValueOrDefault(); + foreach (BombSpawnRequest bombRequest in this.pendingBombSpawns) { positions.Add(bombRequest.Position); spawnGem(bombRequest.Position, bombRequest.Color, true); } @@ -89,10 +87,10 @@ namespace Services return; // Prevent duplicates for the same cell. - if (this.pendingBombSpawn.GetValueOrDefault().Position == pivot) + if (this.pendingBombSpawns.Any(b => b.Position == pivot)) return; - this.pendingBombSpawn = new BombSpawnRequest(pivot, pivotGem.MatchColor); + this.pendingBombSpawns.Add(new BombSpawnRequest(pivot, pivotGem.MatchColor)); } private bool IsEligibleForBomb(Vector2Int pivot, GemType color) { @@ -115,8 +113,7 @@ namespace Services public async UniTask DetonateChainAsync( IReadOnlyList initialBombs, - Func destroyAtAsync, - IGameBoard gameBoard) + Func destroyAtAsync) { if (initialBombs == null || initialBombs.Count == 0) return; @@ -127,8 +124,8 @@ namespace Services Queue waveQueue = new Queue(); foreach (Vector2Int position in initialBombs) { - if (GemUtils.IsInBounds(position, gameBoard)) { - Gem gem = gameBoard.GetGemAt(position); + if (GemUtils.IsInBounds(position, this.gameBoard)) { + Gem gem = this.gameBoard.GetGemAt(position); if(gem is { Type: GemType.Bomb }) waveQueue.Enqueue(position); } @@ -141,10 +138,10 @@ namespace Services if (processedBombs.Contains(bombPos)) continue; - if (!GemUtils.IsInBounds(bombPos, gameBoard)) + if (!GemUtils.IsInBounds(bombPos, this.gameBoard)) continue; - Gem g = gameBoard.GetGemAt(bombPos); + Gem g = this.gameBoard.GetGemAt(bombPos); if (g is not { Type: GemType.Bomb }) continue; @@ -159,13 +156,13 @@ namespace Services foreach (Vector2Int position in DiamondAreaInclusive(bombPos, this.gameVariables.bombRadius)) { - if (!GemUtils.IsInBounds(position, gameBoard)) + if (!GemUtils.IsInBounds(position, this.gameBoard)) continue; if (position == bombPos) continue; - Gem cellGem = gameBoard.GetGemAt(position); + Gem cellGem = this.gameBoard.GetGemAt(position); if (cellGem == null) continue; @@ -201,16 +198,6 @@ namespace Services } } - private int GetLongestMatchedLineThroughPivot(Vector2Int pivot, GemType color) { - int horizontal = 1 + CountSameColorInDirection(pivot, Vector2Int.left, color) - + CountSameColorInDirection(pivot, Vector2Int.right, color); - - int vertical = 1 + CountSameColorInDirection(pivot, Vector2Int.up, color) - + CountSameColorInDirection(pivot, Vector2Int.down, color); - - return Mathf.Max(horizontal, vertical); - } - private int CountSameColorInDirection(Vector2Int start, Vector2Int direction, GemType color) { int count = 0; Vector2Int pivot = start + direction; @@ -228,7 +215,7 @@ namespace Services } private void ClearPendingBombs() { - this.pendingBombSpawn = null; + this.pendingBombSpawns.Clear(); } } } \ No newline at end of file diff --git a/Assets/Scripts/Services/GameBoardService.cs b/Assets/Scripts/Services/GameBoardService.cs index 9490257..fe4182f 100644 --- a/Assets/Scripts/Services/GameBoardService.cs +++ b/Assets/Scripts/Services/GameBoardService.cs @@ -296,8 +296,7 @@ namespace Services { if (initialBombs.Count > 0) { await this.bombService.DetonateChainAsync( initialBombs, - DestroyAtAsync, - this.gameBoard); + DestroyAtAsync); foreach (Vector2Int p in matchPositions) await DestroyAtAsync(p); diff --git a/Assets/Scripts/Services/Interfaces/IBombService.cs b/Assets/Scripts/Services/Interfaces/IBombService.cs index 542081d..87d48bb 100644 --- a/Assets/Scripts/Services/Interfaces/IBombService.cs +++ b/Assets/Scripts/Services/Interfaces/IBombService.cs @@ -38,7 +38,9 @@ namespace Services.Interfaces /// /// Possible bombs. /// - /// + /// + /// List of Bombs. + /// UniTask> GetInitialBombs(List protectedPositions, HashSet bombCandidates); /// @@ -50,13 +52,8 @@ namespace Services.Interfaces /// /// Destroy function reference. /// - /// - /// Gameboard reference. - /// - /// UniTask DetonateChainAsync( IReadOnlyList initialBombs, - Func destroyAtAsync, - IGameBoard gameBoard); + Func destroyAtAsync); } } \ No newline at end of file diff --git a/Assets/Scripts/Services/Interfaces/IMatchService.cs b/Assets/Scripts/Services/Interfaces/IMatchService.cs index 70f1a05..eec4032 100644 --- a/Assets/Scripts/Services/Interfaces/IMatchService.cs +++ b/Assets/Scripts/Services/Interfaces/IMatchService.cs @@ -12,7 +12,9 @@ namespace Services.Interfaces { /// /// Protected positions, bombs that we don't want to destroy. /// - /// + /// + /// HashSet of unprotected matches. + /// UniTask> GetMatchPositionsAsync(List protectedPositions); ///