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

@@ -36,6 +36,7 @@ namespace Services {
#region Variables
private readonly List<GemPresenter> gemPresenters = new List<GemPresenter>();
private readonly Dictionary<Gem, GemView> gemToView = new Dictionary<Gem, GemView>();
private GameState currentState = GameState.Setup;
#endregion
@@ -81,13 +82,13 @@ namespace Services {
SpawnBackgroundTile(position);
int iterations = 0;
int gemToUse = -1;
GemType gemToUse;
do {
gemToUse = RandomUtils.RandomGemTypeAsInt();
gemToUse = RandomUtils.RandomGemType();
iterations++;
} while (this.matchService.MatchesAt(position.ToVector2Int(), (GemType)gemToUse) && iterations < 100);
} while (this.matchService.MatchesAt(position.ToVector2Int(), gemToUse) && iterations < 100);
gemsToSpawn.Add(SetGemAt(position.ToVector2Int(), (GemType)gemToUse));
gemsToSpawn.Add(SetGemAt(position.ToVector2Int(), gemToUse));
}
SpawnCascade(gemsToSpawn);
@@ -108,6 +109,7 @@ namespace Services {
GemTypeValues gemValue = GemUtils.GetGemValues(gem.MatchColor, this.gameVariables.gemsPrefabs);
gemView.Bind(gem, gemValue, isBomb: isBomb);
this.gemPresenters.Add(new GemPresenter(gem, gemView));
this.gemToView.Add(gem, gemView);
}
private void SetAndSpawnGem(Vector2Int position, GemType gemType, bool isBomb) {
@@ -216,7 +218,10 @@ namespace Services {
private async UniTask DestroyMatchesAsync(List<Vector2Int> protectedPositions) {
List<Vector2Int> matchPositions = await this.matchService.GetMatchPositionsAsync(protectedPositions);
List<Vector2Int> initialBombs = await this.bombService.GetInitialBombs(protectedPositions, matchPositions.Distinct().ToList());
HashSet<Vector2Int> uniqueMatchPositions = new HashSet<Vector2Int>(matchPositions);
List<Vector2Int> bombCandidates = uniqueMatchPositions.ToList();
List<Vector2Int> initialBombs = await this.bombService.GetInitialBombs(protectedPositions, bombCandidates);
// If a bomb is part of the match, do NOT destroy matching pieces immediately.
// Let the bomb's manhattan-distance explosion destroy them in sequence.
@@ -226,7 +231,7 @@ namespace Services {
DestroyAtAsync,
this.gameBoard);
foreach (Vector2Int p in matchPositions.Distinct())
foreach (Vector2Int p in uniqueMatchPositions)
await DestroyAtAsync(p);
await UniTask.Delay(this.gameVariables.fillBoardDelayMs);
@@ -236,12 +241,19 @@ namespace Services {
}
// For audio SFX
bool willBreakAnyNonBombGem = matchPositions.Select(pos => this.gameBoard.GetGemAt(pos)).Where(gem => gem != null).Any(gem => gem.Type != GemType.Bomb);
bool willBreakAnyNonBombGem = false;
foreach (Vector2Int pos in uniqueMatchPositions) {
Gem g = this.gameBoard.GetGemAt(pos);
if (g != null && g.Type != GemType.Bomb) {
willBreakAnyNonBombGem = true;
break;
}
}
if (willBreakAnyNonBombGem)
this.audioPresenter.OnMatch(this.gameVariables.matchSfx);
// For score counting
foreach (Vector2Int pos in matchPositions.Distinct().ToList()) {
foreach (Vector2Int pos in uniqueMatchPositions) {
Gem gem = this.gameBoard.GetGemAt(pos);
if (gem == null) continue;
if (gem.Type == GemType.Bomb) continue;
@@ -267,14 +279,11 @@ namespace Services {
}
private void ReleaseMatchedGems(Vector2Int position) {
List<GemView> gemsViews = this.gemsHolder.GetComponentsInChildren<GemView>().ToList();
Gem currentGem = this.gameBoard.GetGemAt(position);
if (currentGem != null)
{
GemView gemView = gemsViews.FirstOrDefault(gv => gv.Gem == currentGem);
if (gemView is null) {
if (!this.gemToView.TryGetValue(currentGem, out GemView gemView) || gemView == null)
return;
}
this.objectPool.Release(gemView);
RemovePresenterFor(gemView);
@@ -283,11 +292,10 @@ namespace Services {
}
private async UniTask MoveGemsDown() {
while (true)
{
List<FallMove> moves = new List<FallMove>();
while (true) {
moves.Clear();
// Build moves from a snapshot of the current grid state (no mid-wave chaining)
List<(Vector2Int from, Vector2Int to, Gem gem)> moves = new List<(Vector2Int, Vector2Int, Gem)>();
for (int x = 0; x < this.gameBoard.Width; x++)
{
for (int y = 1; y < this.gameBoard.Height; y++)
@@ -299,21 +307,21 @@ namespace Services {
if (gem == null)
continue;
Gem below = this.gameBoard.GetGemAt(to);
if (below != null)
if (this.gameBoard.GetGemAt(to) != null)
continue;
moves.Add((from, to, gem));
moves.Add(new FallMove {
from = from,
to = to,
gem = gem
});
}
}
if (moves.Count == 0)
break;
// Apply all moves simultaneously for this wave
for (int i = 0; i < moves.Count; i++)
{
(Vector2Int from, Vector2Int to, Gem gem) move = moves[i];
foreach (FallMove move in moves) {
this.gameBoard.SetGemAt(move.to, move.gem);
this.gameBoard.SetGemAt(move.from, null);
move.gem.SetPosition(move.to);
@@ -347,12 +355,12 @@ namespace Services {
{
Gem currentGem = this.gameBoard.GetGemAt(new Vector2Int(x,y));
if (currentGem == null) {
int gemToUse = RandomUtils.RandomGemTypeAsInt();
GemType gemToUse = RandomUtils.RandomGemType();
int iterations = 0;
while (this.matchService.MatchesAt(new Vector2Int(x, y), (GemType)gemToUse) && iterations < 100)
{
gemToUse = RandomUtils.RandomGemTypeAsInt();
gemToUse = RandomUtils.RandomGemType();
iterations++;
}
@@ -395,6 +403,8 @@ namespace Services {
public void Dispose() {
this.objectPool.Clear();
this.gemPresenters.Clear();
this.gemToView.Clear();
}
}
}