Fixed Bomb Spawning

This commit is contained in:
2025-12-19 17:18:18 +08:00
parent 73db75d40a
commit e2409106d4
4 changed files with 20 additions and 35 deletions

View File

@@ -19,7 +19,7 @@ namespace Services
private Vector2Int lastSwapFrom;
private Vector2Int lastSwapTo;
private BombSpawnRequest? pendingBombSpawn;
private List<BombSpawnRequest> pendingBombSpawns = new List<BombSpawnRequest>();
public BombService(GameVariables gameVariables, IGameBoard gameBoard) {
this.gameVariables = gameVariables;
@@ -51,10 +51,8 @@ namespace Services
public List<Vector2Int> ApplyPendingBombSpawns(Action<Vector2Int, GemType, bool> spawnGem) {
List<Vector2Int> positions = new List<Vector2Int>();
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<Vector2Int> initialBombs,
Func<Vector2Int, UniTask> destroyAtAsync,
IGameBoard gameBoard)
Func<Vector2Int, UniTask> destroyAtAsync)
{
if (initialBombs == null || initialBombs.Count == 0)
return;
@@ -127,8 +124,8 @@ namespace Services
Queue<Vector2Int> waveQueue = new Queue<Vector2Int>();
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();
}
}
}