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) if (processedBombs.Contains(bombPos))
{
Vector2Int b = waveQueue.Dequeue();
if (processedBombs.Contains(b))
continue; continue;
if (!GemUtils.IsInBounds(b, gameBoard)) if (!GemUtils.IsInBounds(bombPos, gameBoard))
continue; continue;
Gem g = gameBoard.GetGemAt(b); Gem g = gameBoard.GetGemAt(bombPos);
if (g is not { Type: GemType.Bomb }) if (g is not { Type: GemType.Bomb })
continue; continue;
processedBombs.Add(b); processedBombs.Add(bombPos);
waveBombs.Add(b);
}
if (waveBombs.Count == 0) // delay once per bomb
continue;
// delay once per wave
if (waveDelayMs > 0)
await UniTask.Delay(waveDelayMs); await UniTask.Delay(waveDelayMs);
HashSet<Vector2Int> nextWaveBombs = new HashSet<Vector2Int>();
HashSet<Vector2Int> toDestroyNow = new HashSet<Vector2Int>(); HashSet<Vector2Int> toDestroyNow = new HashSet<Vector2Int>();
for (int i = 0; i < waveBombs.Count; i++)
{
Vector2Int bombPos = waveBombs[i];
// destroy self when it detonates // destroy self when it detonates
toDestroyNow.Add(bombPos); toDestroyNow.Add(bombPos);
foreach (Vector2Int p in DiamondAreaInclusive(bombPos, this.gameVariables.bombRadius)) foreach (Vector2Int position in DiamondAreaInclusive(bombPos, this.gameVariables.bombRadius))
{ {
if (!GemUtils.IsInBounds(p, gameBoard)) if (!GemUtils.IsInBounds(position, gameBoard))
continue; continue;
if (p == bombPos) if (position == bombPos)
continue; continue;
Gem cellGem = gameBoard.GetGemAt(p); Gem cellGem = gameBoard.GetGemAt(position);
if (cellGem == null) if (cellGem == null)
continue; continue;
if (cellGem.Type == GemType.Bomb) if (cellGem.Type == GemType.Bomb)
{ {
// bombs in range are NOT destroyed now. triggered to explode in a later wave. // bombs in range are NOT destroyed now. triggered to explode in a later "step".
if (!processedBombs.Contains(p)) if (!processedBombs.Contains(position))
nextWaveBombs.Add(p); waveQueue.Enqueue(position);
continue; continue;
} }
// Non-bomb gem gets destroyed by this bomb // Non-bomb gem gets destroyed by this bomb
toDestroyNow.Add(p); 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);
} }
} }