Update BombService.cs

- Matched bombs now explode one at a time
This commit is contained in:
2025-12-18 14:38:57 +08:00
parent 667a39c260
commit 3956a6ffab

View File

@@ -118,75 +118,55 @@ namespace Services
while (waveQueue.Count > 0) while (waveQueue.Count > 0)
{ {
// current wave (per bomb) Vector2Int bombPos = waveQueue.Dequeue();
List<Vector2Int> waveBombs = new List<Vector2Int>();
while (waveQueue.Count > 0)
{
Vector2Int b = waveQueue.Dequeue();
if (processedBombs.Contains(b))
continue;
if (!GemUtils.IsInBounds(b, gameBoard)) if (processedBombs.Contains(bombPos))
continue;
Gem g = gameBoard.GetGemAt(b);
if (g is not { Type: GemType.Bomb })
continue;
processedBombs.Add(b);
waveBombs.Add(b);
}
if (waveBombs.Count == 0)
continue; continue;
// delay once per wave if (!GemUtils.IsInBounds(bombPos, gameBoard))
if (waveDelayMs > 0) continue;
await UniTask.Delay(waveDelayMs);
HashSet<Vector2Int> nextWaveBombs = new HashSet<Vector2Int>(); Gem g = gameBoard.GetGemAt(bombPos);
if (g is not { Type: GemType.Bomb })
continue;
processedBombs.Add(bombPos);
// delay once per bomb
await UniTask.Delay(waveDelayMs);
HashSet<Vector2Int> toDestroyNow = new HashSet<Vector2Int>(); HashSet<Vector2Int> toDestroyNow = new HashSet<Vector2Int>();
for (int i = 0; i < waveBombs.Count; i++) // destroy self when it detonates
toDestroyNow.Add(bombPos);
foreach (Vector2Int position in DiamondAreaInclusive(bombPos, this.gameVariables.bombRadius))
{ {
Vector2Int bombPos = waveBombs[i]; if (!GemUtils.IsInBounds(position, gameBoard))
continue;
// destroy self when it detonates if (position == bombPos)
toDestroyNow.Add(bombPos); continue;
foreach (Vector2Int p in DiamondAreaInclusive(bombPos, this.gameVariables.bombRadius)) Gem cellGem = gameBoard.GetGemAt(position);
if (cellGem == null)
continue;
if (cellGem.Type == GemType.Bomb)
{ {
if (!GemUtils.IsInBounds(p, gameBoard)) // bombs in range are NOT destroyed now. triggered to explode in a later "step".
continue; if (!processedBombs.Contains(position))
waveQueue.Enqueue(position);
if (p == bombPos) continue;
continue;
Gem cellGem = gameBoard.GetGemAt(p);
if (cellGem == null)
continue;
if (cellGem.Type == GemType.Bomb)
{
// bombs in range are NOT destroyed now. triggered to explode in a later wave.
if (!processedBombs.Contains(p))
nextWaveBombs.Add(p);
continue;
}
// Non-bomb gem gets destroyed by this bomb
toDestroyNow.Add(p);
} }
// Non-bomb gem gets destroyed by this bomb
toDestroyNow.Add(position);
} }
// Destroy everything for this wave (non-bombs in range and the detonating bombs themselves) // Destroy everything for this specific bomb detonation
foreach (Vector2Int p in toDestroyNow) foreach (Vector2Int p in toDestroyNow)
await destroyAtAsync(p); await destroyAtAsync(p);
// Schedule the next wave (triggered bombs)
foreach (Vector2Int b in nextWaveBombs)
waveQueue.Enqueue(b);
} }
} }