Performance improvements

This commit is contained in:
2025-12-18 03:01:20 +08:00
parent c6ebe96a12
commit 1d134ffc40
8 changed files with 144 additions and 81 deletions

View File

@@ -11,9 +11,8 @@ 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 HashSet<Gem> currentMatches = new HashSet<Gem>();
public HashSet<Gem> CurrentMatches => this.currentMatches;
public MatchService(IGameBoard gameBoard) {
this.gameBoard = gameBoard;
@@ -52,9 +51,10 @@ namespace Services {
}
public UniTask<List<Vector2Int>> GetMatchPositionsAsync(List<Vector2Int> protectedPositions) {
List<Vector2Int> matchPositions = new List<Vector2Int>(CurrentMatches.Count);
for (int i = 0; i < CurrentMatches.Count; i++) {
Gem match = CurrentMatches[i];
List<Vector2Int> matchPositions = new List<Vector2Int>(this.currentMatches.Count);
List<Gem> matches = this.currentMatches.ToList();
for (int i = 0; i < matches.Count; i++) {
Gem match = matches[i];
if (match == null) continue;
Vector2Int pos = match.Position;
@@ -70,39 +70,59 @@ namespace Services {
public void FindAllMatches() {
this.currentMatches.Clear();
for (int x = 0; x < this.gameBoard.Width; x++)
for (int y = 0; y < this.gameBoard.Height; y++) {
Gem currentGem = this.gameBoard.GemsGrid[x, y];
if (currentGem == null)
continue;
Gem[,] grid = this.gameBoard.GemsGrid;
int boardWidth = this.gameBoard.Width;
int boardHeight = this.gameBoard.Height;
if (x > 0 && x < this.gameBoard.Width - 1) {
Gem leftGem = this.gameBoard.GemsGrid[x - 1, y];
Gem rightGem = this.gameBoard.GemsGrid[x + 1, y];
if (leftGem != null && rightGem != null) {
if (leftGem.MatchColor == currentGem.MatchColor && rightGem.MatchColor == currentGem.MatchColor) {
this.currentMatches.Add(currentGem);
this.currentMatches.Add(leftGem);
this.currentMatches.Add(rightGem);
}
}
// Horizontal runs
for (int y = 0; y < boardHeight; y++) {
int x = 0;
while (x < boardWidth) {
Gem start = grid[x, y];
if (start == null) { x++; continue; }
GemType color = start.MatchColor;
int runLen = 1;
while (x + runLen < boardWidth) {
Gem next = grid[x + runLen, y];
if (next == null || next.MatchColor != color) break;
runLen++;
}
if (y > 0 && y < this.gameBoard.Height - 1) {
Gem aboveGem = this.gameBoard.GemsGrid[x, y - 1];
Gem bellowGem = this.gameBoard.GemsGrid[x, y + 1];
if (aboveGem != null && bellowGem != null) {
if (aboveGem.MatchColor == currentGem.MatchColor && bellowGem.MatchColor == currentGem.MatchColor) {
this.currentMatches.Add(currentGem);
this.currentMatches.Add(aboveGem);
this.currentMatches.Add(bellowGem);
}
}
if (runLen >= 3) {
for (int i = 0; i < runLen; i++)
this.currentMatches.Add(grid[x + i, y]);
}
x += runLen;
}
}
if (this.currentMatches.Count > 0)
this.currentMatches = this.currentMatches.Distinct().ToList();
// Vertical runs
for (int x = 0; x < boardWidth; x++) {
int y = 0;
while (y < boardHeight) {
Gem start = grid[x, y];
if (start == null) { y++; continue; }
GemType color = start.MatchColor;
int runLen = 1;
while (y + runLen < boardHeight) {
Gem next = grid[x, y + runLen];
if (next == null || next.MatchColor != color) break;
runLen++;
}
if (runLen >= 3) {
for (int i = 0; i < runLen; i++)
this.currentMatches.Add(grid[x, y + i]);
}
y += runLen;
}
}
}
}
}