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

@@ -35,7 +35,7 @@ namespace Services
}
public UniTask<List<Vector2Int>> GetInitialBombs(List<Vector2Int> protectedPositions, List<Vector2Int> bombCandidates) {
List<Vector2Int> initialBombs = new List<Vector2Int>();
HashSet<Vector2Int> initialBombs = new HashSet<Vector2Int>();
foreach (Vector2Int p in bombCandidates) {
if (!GemUtils.IsInBounds(p, this.gameBoard)) continue;
@@ -47,7 +47,7 @@ namespace Services
initialBombs.Add(p);
}
return UniTask.FromResult(initialBombs.Distinct().ToList());
return UniTask.FromResult(initialBombs.ToList());
}
public List<Vector2Int> ApplyPendingBombSpawns(Action<Vector2Int, GemType, bool> spawnGem) {
@@ -64,7 +64,7 @@ namespace Services
return positions;
}
public void DetectBombSpawnFromLastSwap(List<Gem> currentMatches) {
public void DetectBombSpawnFromLastSwap(HashSet<Gem> currentMatches) {
Vector2Int from = this.lastSwapFrom;
Vector2Int to = this.lastSwapTo;
@@ -72,7 +72,7 @@ namespace Services
TryCreateBombSpawnAt(to, currentMatches);
}
private void TryCreateBombSpawnAt(Vector2Int pivot, List<Gem> currentMatches) {
private void TryCreateBombSpawnAt(Vector2Int pivot, HashSet<Gem> currentMatches) {
Gem pivotGem = this.gameBoard.GetGemAt(pivot);
if (pivotGem == null)
return;
@@ -81,7 +81,7 @@ namespace Services
if (pivotGem.Type == GemType.Bomb)
return;
if (currentMatches.All(g => g.Position != pivot))
if (currentMatches == null || !currentMatches.Contains(pivotGem))
return;
// Only create a bomb if pivot is part of a straight 4+ line of the SAME color.
@@ -108,14 +108,14 @@ namespace Services
HashSet<Vector2Int> processedBombs = new HashSet<Vector2Int>();
Queue<Vector2Int> waveQueue = new Queue<Vector2Int>(
initialBombs.Where(p =>
{
if (!GemUtils.IsInBounds(p, gameBoard)) return false;
Gem g = gameBoard.GetGemAt(p);
return g is { Type: GemType.Bomb };
})
);
Queue<Vector2Int> waveQueue = new Queue<Vector2Int>();
foreach (Vector2Int position in initialBombs) {
if (GemUtils.IsInBounds(position, gameBoard)) {
Gem gem = gameBoard.GetGemAt(position);
if(gem is { Type: GemType.Bomb })
waveQueue.Enqueue(position);
}
}
while (waveQueue.Count > 0)
{