Fixed Bomb Spawning
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -38,7 +38,9 @@ namespace Services.Interfaces
|
||||
/// <param name="bombCandidates">
|
||||
/// Possible bombs.
|
||||
/// </param>
|
||||
/// <returns></returns>
|
||||
/// <returns>
|
||||
/// List of Bombs.
|
||||
/// </returns>
|
||||
UniTask<List<Vector2Int>> GetInitialBombs(List<Vector2Int> protectedPositions, HashSet<Vector2Int> bombCandidates);
|
||||
|
||||
/// <summary>
|
||||
@@ -50,13 +52,8 @@ namespace Services.Interfaces
|
||||
/// <param name="destroyAtAsync">
|
||||
/// Destroy function reference.
|
||||
/// </param>
|
||||
/// <param name="gameBoard">
|
||||
/// Gameboard reference.
|
||||
/// </param>
|
||||
/// <returns></returns>
|
||||
UniTask DetonateChainAsync(
|
||||
IReadOnlyList<Vector2Int> initialBombs,
|
||||
Func<Vector2Int, UniTask> destroyAtAsync,
|
||||
IGameBoard gameBoard);
|
||||
Func<Vector2Int, UniTask> destroyAtAsync);
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,9 @@ namespace Services.Interfaces {
|
||||
/// <param name="protectedPositions">
|
||||
/// Protected positions, bombs that we don't want to destroy.
|
||||
/// </param>
|
||||
/// <returns></returns>
|
||||
/// <returns>
|
||||
/// HashSet of unprotected matches.
|
||||
/// </returns>
|
||||
UniTask<HashSet<Vector2Int>> GetMatchPositionsAsync(List<Vector2Int> protectedPositions);
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user