Optimization and Documentation

This commit is contained in:
2025-12-18 03:44:51 +08:00
parent 1d134ffc40
commit 668bd03f63
10 changed files with 204 additions and 53 deletions

View File

@@ -20,7 +20,6 @@ namespace Services
private Vector2Int lastSwapTo;
private BombSpawnRequest? pendingBombSpawn;
public BombSpawnRequest? PendingBombSpawn => this.pendingBombSpawn;
public BombService(GameVariables gameVariables, IGameBoard gameBoard) {
this.gameVariables = gameVariables;
@@ -33,8 +32,8 @@ namespace Services
ClearPendingBombs();
}
public UniTask<List<Vector2Int>> GetInitialBombs(List<Vector2Int> protectedPositions, List<Vector2Int> bombCandidates) {
public UniTask<List<Vector2Int>> GetInitialBombs(List<Vector2Int> protectedPositions, HashSet<Vector2Int> bombCandidates) {
HashSet<Vector2Int> initialBombs = new HashSet<Vector2Int>();
foreach (Vector2Int p in bombCandidates) {
if (!GemUtils.IsInBounds(p, this.gameBoard)) continue;
@@ -52,10 +51,10 @@ namespace Services
public List<Vector2Int> ApplyPendingBombSpawns(Action<Vector2Int, GemType, bool> spawnGem) {
List<Vector2Int> positions = new List<Vector2Int>();
BombSpawnRequest? bombSpawnRequest = PendingBombSpawn;
BombSpawnRequest? bombSpawnRequest = this.pendingBombSpawn;
if (bombSpawnRequest != null) {
BombSpawnRequest bombRequest = PendingBombSpawn.GetValueOrDefault();
BombSpawnRequest bombRequest = this.pendingBombSpawn.GetValueOrDefault();
positions.Add(bombRequest.Position);
spawnGem(bombRequest.Position, bombRequest.Color, true);
}
@@ -84,7 +83,7 @@ namespace Services
if (currentMatches == null || !currentMatches.Contains(pivotGem))
return;
// Only create a bomb if pivot is part of a straight 4+ line of the SAME color.
// Only create a bomb if the pivot is part of a straight 4+ line of the SAME color.
int longestLine = GetLongestMatchedLineThroughPivot(pivot, pivotGem.MatchColor);
if (longestLine < 4)
return;
@@ -181,7 +180,7 @@ namespace Services
}
}
// Destroy everything for this wave (non-bombs in range + the detonating bombs themselves)
// Destroy everything for this wave (non-bombs in range and the detonating bombs themselves)
foreach (Vector2Int p in toDestroyNow)
await destroyAtAsync(p);
@@ -216,21 +215,21 @@ namespace Services
private int CountSameColorInDirection(Vector2Int start, Vector2Int direction, GemType color) {
int count = 0;
Vector2Int oivot = start + direction;
Vector2Int pivot = 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);
while (pivot.x >= 0 && pivot.x < this.gameBoard.Width && pivot.y >= 0 && pivot.y < this.gameBoard.Height) {
Gem g = this.gameBoard.GetGemAt(pivot);
if (g == null || g.Type == GemType.Bomb || g.MatchColor != color)
break;
count++;
oivot += direction;
pivot += direction;
}
return count;
}
public void ClearPendingBombs() {
private void ClearPendingBombs() {
this.pendingBombSpawn = null;
}
}