Move Bomb Responsibilities to BombService
This commit is contained in:
@@ -8,29 +8,15 @@ using UnityEngine;
|
||||
|
||||
namespace Services {
|
||||
public class MatchService : IMatchService {
|
||||
private readonly IGameBoard gameBoard;
|
||||
|
||||
|
||||
private List<Gem> currentMatches = new List<Gem>();
|
||||
public List<Gem> CurrentMatches => this.currentMatches;
|
||||
|
||||
private readonly List<BombSpawnRequest> pendingBombSpawns = new List<BombSpawnRequest>();
|
||||
public IReadOnlyList<BombSpawnRequest> PendingBombSpawns => this.pendingBombSpawns;
|
||||
|
||||
private Vector2Int lastSwapFrom;
|
||||
private Vector2Int lastSwapTo;
|
||||
|
||||
private readonly IGameBoard gameBoard;
|
||||
|
||||
public MatchService(IGameBoard gameBoard) {
|
||||
this.gameBoard = gameBoard;
|
||||
}
|
||||
|
||||
public void SetLastSwap(Vector2Int from, Vector2Int to) {
|
||||
this.lastSwapFrom = from;
|
||||
this.lastSwapTo = to;
|
||||
}
|
||||
|
||||
public void ClearPendingBombs() {
|
||||
this.pendingBombSpawns.Clear();
|
||||
}
|
||||
|
||||
public bool MatchesAt(Vector2Int positionToCheck, GemType gemTypeToCheck) {
|
||||
Gem[,] gems = this.gameBoard.GemsGrid;
|
||||
@@ -66,7 +52,6 @@ namespace Services {
|
||||
|
||||
public void FindAllMatches() {
|
||||
this.currentMatches.Clear();
|
||||
this.pendingBombSpawns.Clear();
|
||||
|
||||
for (int x = 0; x < this.gameBoard.Width; x++)
|
||||
for (int y = 0; y < this.gameBoard.Height; y++) {
|
||||
@@ -101,66 +86,6 @@ namespace Services {
|
||||
|
||||
if (this.currentMatches.Count > 0)
|
||||
this.currentMatches = this.currentMatches.Distinct().ToList();
|
||||
|
||||
DetectBombSpawnFromLastSwap();
|
||||
}
|
||||
|
||||
private void DetectBombSpawnFromLastSwap() {
|
||||
Vector2Int from = this.lastSwapFrom;
|
||||
Vector2Int to = this.lastSwapTo;
|
||||
|
||||
TryCreateBombSpawnAt(from);
|
||||
TryCreateBombSpawnAt(to);
|
||||
}
|
||||
|
||||
private void TryCreateBombSpawnAt(Vector2Int pivot) {
|
||||
Gem pivotGem = this.gameBoard.GetGemAt(pivot);
|
||||
if (pivotGem == null)
|
||||
return;
|
||||
|
||||
// If it's already a bomb, don't create another.
|
||||
if (pivotGem.Type == GemType.Bomb)
|
||||
return;
|
||||
|
||||
if (this.currentMatches.All(g => g.Position != pivot))
|
||||
return;
|
||||
|
||||
// Only create a bomb if pivot is part of a straight 4+ line of the SAME color.
|
||||
int longestLine = GetLongestMatchedLineThroughPivot(pivot, pivotGem.MatchColor);
|
||||
if (longestLine < 4)
|
||||
return;
|
||||
|
||||
// Prevent duplicates for the same cell.
|
||||
if (this.pendingBombSpawns.Any(b => b.Position == pivot))
|
||||
return;
|
||||
|
||||
this.pendingBombSpawns.Add(new BombSpawnRequest(pivot, pivotGem.MatchColor));
|
||||
}
|
||||
|
||||
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 oivot = start + direction;
|
||||
|
||||
while (oivot.x >= 0 && oivot.x < this.gameBoard.Width && oivot.y >= 0 && oivot.y < this.gameBoard.Height) {
|
||||
Gem g = this.gameBoard.GetGemAt(oivot);
|
||||
if (g == null || g.Type == GemType.Bomb || g.MatchColor != color)
|
||||
break;
|
||||
|
||||
count++;
|
||||
oivot += direction;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user